3
$\begingroup$

I'm trying to evaluate the PDF of a truncated multi-normal distribution. My distribution has 8 dimensions and each value must be ≥ 0.

ivalues = {1.03371, 0.617498, 1.26354, 0.855324, 0.408308, 0.158506, 1.70032,0.269946}
covariancematrix = IdentityMatrix[8]  
truncate = Table[{0, \[Infinity]}, 8]
TruncatedDistribution[truncate,MultinormalDistribution[ivalues, covariancematrix]]

So I sampled one set of random values (rvalues) from this distribution using RandomVariate

rvalues= {1.13594, 0.641371, 1.31146, 0.915561, 0.327869, 0.225612, 1.44007, 0.268547}

Now I want to evaluate the PDF at rvalues

PDF[TruncatedDistribution[truncate,MultinormalDistribution[ivalues, covariancematrix]],
      rvalues] // AbsoluteTiming

{0.150935, 0.00037916}

It takes about 0.15 seconds. Since I need to do a lot of this calculations (hundreds of thousands) mi script is runnnig very slow.

Is there any faster way of obtaining this PDF? Thanks

$\endgroup$
4
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Commented Apr 5, 2016 at 18:18
  • 2
    $\begingroup$ Just evaluate the PDF symbolically first, then feed the values to that - I get many thousands per second on a netbook, certainly over 100K/s on a real machine.... $\endgroup$
    – ciao
    Commented Apr 5, 2016 at 22:47
  • $\begingroup$ Hi Ciao, I forgot to mention that the distribution might move during the iterations. Would this method work if I'm constantly changing my ivalues? Could you write the answer (and a code if you like) in the answer section? Thanks $\endgroup$
    – BPinto
    Commented Apr 6, 2016 at 0:01
  • $\begingroup$ @BPinto - I'll add a quick-and-dirty (as in not pretty code) answer as an expample so you get the idea (sorry, busy right now so it will be short and to the point). BTW - I just happened to see your message - without the "@" I was not notified, so sorry for delay. $\endgroup$
    – ciao
    Commented Apr 6, 2016 at 6:25

2 Answers 2

6
$\begingroup$

Per my comment. Assume things not defined here were as in your example:

myDist = TruncatedDistribution[truncate, 
   MultinormalDistribution[{a, b, c, d, e, f, g, h}, 
    covariancematrix]];

myPDF[{a_, b_, c_, d_, e_, f_, g_, h_}, {i_, j_, k_, l_, m_, n_, o_, 
    p_}] = N@PDF[myDist, {i, j, k, l, m, n, o, p}];

Just call myPDF with the lists of current ivalues and rvalues.

E.g, using some random values (here dist was already defined):

rvals = RandomVariate[dist, 2000];
ivalues = RandomReal[{0, 2}, {2000, 8}];

Doing

MapThread[myPDF, {ivalues, rvals}]

is ~2000x faster on an old netbook vs calling your PDF on each set. S/B even faster I'd venture on a real machine.

Hope that's useful, again sorry for brevity of exposition.

Edit: Note use of N@ on the PDF - gives a little extra boost by keeping things machine precision. Remove if you are inputting and expect as output arbitrary precision.

$\endgroup$
3
$\begingroup$

If your real covariance matrix is the identity matrix, then all 8 of the random variables are independent and there's no need for the overhead of dealing with a general structure for a multivariate normal. You can construct the truncated distributions separately, generate a random sample from each, and then multiply the 8 probability densities together.

ivalues = {1.03371, 0.617498, 1.26354, 0.855324, 0.408308, 0.158506, 1.70032, 0.269946};
d = Map[TruncatedDistribution[{0, \[Infinity]}, NormalDistribution[#, 1]] &, ivalues];
rvalues = Map[RandomVariate, d];
(* Using rvalues from original question *)
rvalues = {1.13594, 0.641371, 1.31146, 0.915561, 0.327869, 0.225612, 1.44007, 0.268547}

Timing[Times @@ MapThread[PDF, {d, rvalues}]]
(* {0.00205371, 0.00635803} *)
$\endgroup$
4
  • $\begingroup$ Thank you for your Answer, I've tried that one but the result is not the same. As you can see I've got 0.00037916 and you 0.0008101343010582806. $\endgroup$
    – BPinto
    Commented Apr 5, 2016 at 23:42
  • $\begingroup$ You're welcome. You might attempt to convince @ciao to write up his comment as an answer as that is a much better general solution than mine. $\endgroup$
    – JimB
    Commented Apr 5, 2016 at 23:44
  • $\begingroup$ My result is different because I used a new random value of rvalues. I'll change it to the values you used. $\endgroup$
    – JimB
    Commented Apr 6, 2016 at 13:07
  • $\begingroup$ @BPinto. Not sure where you get the value of 0.00037916. When I run your code and my code with the same values for ivalues and rvalues, I get 0.00635803 for both. $\endgroup$
    – JimB
    Commented Apr 6, 2016 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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