1 ## Author: Paul Kienzle <pkienzle@users.sf.net>
2 ## This program is granted to the public domain.
5 ## @deftypefn {Function File} {@var{y} =} mvnpdf (@var{x})
6 ## @deftypefnx{Function File} {@var{y} =} mvnpdf (@var{x}, @var{mu})
7 ## @deftypefnx{Function File} {@var{y} =} mvnpdf (@var{x}, @var{mu}, @var{sigma})
8 ## Compute multivariate normal pdf for @var{x} given mean @var{mu} and covariance matrix
9 ## @var{sigma}. The dimension of @var{x} is @var{d} x @var{p}, @var{mu} is
10 ## @var{1} x @var{p} and @var{sigma} is @var{p} x @var{p}. The normal pdf is
16 ## $$ 1/y^2 = (2 pi)^p |\Sigma| \exp \{ (x-\mu)^T \Sigma^{-1} (x-\mu) \} $$
20 ## 1/@var{y}^2 = (2 pi)^@var{p} |@var{Sigma}| exp @{ (@var{x}-@var{mu})' inv(@var{Sigma})@
21 ## (@var{x}-@var{mu}) @}
25 ## @strong{References}
27 ## NIST Engineering Statistics Handbook 6.5.4.2
28 ## http://www.itl.nist.gov/div898/handbook/pmc/section5/pmc542.htm
32 ## Using Cholesky factorization on the positive definite covariance matrix:
35 ## @var{r} = chol (@var{sigma});
38 ## where @var{r}'*@var{r} = @var{sigma}. Being upper triangular, the determinant
39 ## of @var{r} is trivially the product of the diagonal, and the determinant of
40 ## @var{sigma} is the square of this:
43 ## @var{det} = prod (diag (@var{r}))^2;
46 ## The formula asks for the square root of the determinant, so no need to
49 ## The exponential argument @var{A} = @var{x}' * inv (@var{sigma}) * @var{x}
52 ## @var{A} = @var{x}' * inv (@var{sigma}) * @var{x}
53 ## = @var{x}' * inv (@var{r}' * @var{r}) * @var{x}
54 ## = @var{x}' * inv (@var{r}) * inv(@var{r}') * @var{x}
57 ## Given that inv (@var{r}') == inv(@var{r})', at least in theory if not numerically,
60 ## @var{A} = (@var{x}' / @var{r}) * (@var{x}'/@var{r})' = sumsq (@var{x}'/@var{r})
63 ## The interface takes the parameters to the multivariate normal in columns rather than
64 ## rows, so we are actually dealing with the transpose:
67 ## @var{A} = sumsq (@var{x}/r)
70 ## and the final result is:
73 ## @var{r} = chol (@var{sigma})
74 ## @var{y} = (2*pi)^(-@var{p}/2) * exp (-sumsq ((@var{x}-@var{mu})/@var{r}, 2)/2) / prod (diag (@var{r}))
77 ## @seealso{mvncdf, mvnrnd}
80 function pdf = mvnpdf (x, mu = 0, sigma = 1)
83 error ("mvnpdf: first input must be a matrix");
86 if (!isvector (mu) && !isscalar (mu))
87 error ("mvnpdf: second input must be a real scalar or vector");
90 if (!ismatrix (sigma) || !issquare (sigma))
91 error ("mvnpdf: third input must be a square matrix");
94 [ps, ps] = size (sigma);
97 error ("mvnpdf: dimensions of data and covariance matrix does not match");
100 if (numel (mu) != p && numel (mu) != 1)
101 error ("mvnpdf: dimensions of data does not match dimensions of mean value");
105 if (all (size (mu) == [1, p]))
106 mu = repmat (mu, [d, 1]);
110 pdf = (2*pi)^(-p/2) * exp (-sumsq (x-mu, 2)/2);
113 pdf = (2*pi)^(-p/2) * exp (-sumsq ((x-mu)/r, 2)/2) / prod (diag (r));
119 %! sigma = [1, 0.1; 0.1, 0.5];
120 %! [X, Y] = meshgrid (linspace (-3, 3, 25));
121 %! XY = [X(:), Y(:)];
122 %! Z = mvnpdf (XY, mu, sigma);
123 %! mesh (X, Y, reshape (Z, size (X)));
128 %! sigma = [.9 .4; .4 .3];
129 %! x = [ 0.5 -1.2; -0.5 -1.4; 0 -1.5];
130 %! p = [ 0.41680003660313; 0.10278162359708; 0.27187267524566 ];
131 %! q = mvnpdf (x, mu, sigma);
132 %! assert (p, q, 10*eps);