3
$\begingroup$

Problem, the following snippet doesn't substitute vars

normal := MultinormalDistribution[{t1, t2}, cov];
fit[t1_, t2_, x1_, x2_] := PDF[normal, {x1, x2}];
fit[0, 0, 0, 0] (* is not numeric *)

I vaguely recall there was some way to make this give numeric result, something like PDF@@{Evaluate@normal,{x1,x2}] , does anyone know the common pattern to use here?

$\endgroup$
  • 3
    $\begingroup$ You could do normal[t1_, t2_] := ... in the first line and normal[t1,t2] in the second. $\endgroup$ – anderstood Mar 7 '17 at 4:00
2
$\begingroup$
cov := {{2, -1/3}, {-1/3, 2/3}};

normal := MultinormalDistribution[{t1, t2}, cov];

(fit[t1_, t2_, x1_, x2_] := PDF[#, {x1, x2}]) & @ normal

fit[0, 0, 0, 1] // N
0.0635204

If you prefer a more automated approach or you cannot have even normal evaluate at the time of defining fit try blockSet from Expressions containing globally undefined symbols inside a function where they are defined

ClearAll[fit]

blockSet[
  fit[t1_, t2_, x1_, x2_] := PDF[normal, {x1, x2}]
]

fit[0, 0, 0, 1] // N
0.0635204

The definition that was created:

?fit
fit[t1$535_, t2$536_, x1$537_, x2$538_] := 
  Block[{t1 = t1$535, t2 = t2$536, x1 = x1$537, x2 = x2$538},
    PDF[normal, {x1, x2}]]

In most cases I would favor anderstood's recommendation of explicit parameter passing however.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.