# Fast way to evaluate the PDF of a multivariable distribution

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
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

• 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! – user9660 Apr 5 '16 at 18:18
• 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.... – ciao Apr 5 '16 at 22:47
• 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 – BPinto Apr 6 '16 at 0:01
• @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. – ciao Apr 6 '16 at 6:25

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.

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}


• My result is different because I used a new random value of rvalues. I'll change it to the values you used. – JimB Apr 6 '16 at 13:07