I'm trying to define the density of the Multivariate Normal distribution, since it will be faster to compute for greater dimensions (vectors of size 50 or more), specially when I have to do those computations many times (10,000).
I've defined the density as :
multinormalDens[x_, mean_, var_] := Module[{},
Det[2*Pi*var]^(-0.5)*Exp[-0.5*(x - mean).Inverse[var].(x - mean)]
];
I use the Wishart to provide with several positive definite covariance matrices.
varmat = RandomVariate[
WishartMatrixDistribution[52, IdentityMatrix[50]], 10000];
meanmat = Table[RandomReal[{0, 5}, 50], {i, 1, 10000}];
They will all be evaluated for the same point:
x0 = RandomReal[{0, 5}, 50];
Now running the comparison for 10,000 tries, I get:
In[43]:= Table[
multinormalDens[x0, meanmat[[i]], varmat[[i]]] ==
PDF[MultinormalDistribution[meanmat[[i]], varmat[[i]]], x0], {i, 1,
Length[varmat]}] // Total
Out[43]= 6643 False + 3357 True
How come I have 6643 different outcomes? Are they from the matrix inversion?
Edit:
data = Table[
multinormalDens[x0, meanmat[[i]], varmat[[i]]] -
PDF[MultinormalDistribution[meanmat[[i]], varmat[[i]]], x0], {i,
1, Length[varmat]}];
ListPlot[data]
ListPlot[data, PlotRange -> All]
Exp[-(...)]
with large arguments. $\endgroup$meanmat
can be simplified tomeanmat = RandomReal[{0, 5}, {10000, 50}];
$\endgroup$