comparison toolboxes/FullBNT-1.0.7/KPMstats/gaussian_prob.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function p = gaussian_prob(x, m, C, use_log)
2 % GAUSSIAN_PROB Evaluate a multivariate Gaussian density.
3 % p = gaussian_prob(X, m, C)
4 % p(i) = N(X(:,i), m, C) where C = covariance matrix and each COLUMN of x is a datavector
5
6 % p = gaussian_prob(X, m, C, 1) returns log N(X(:,i), m, C) (to prevents underflow).
7 %
8 % If X has size dxN, then p has size Nx1, where N = number of examples
9
10 if nargin < 4, use_log = 0; end
11
12 if length(m)==1 % scalar
13 x = x(:)';
14 end
15 [d N] = size(x);
16 %assert(length(m)==d); % slow
17 m = m(:);
18 M = m*ones(1,N); % replicate the mean across columns
19 denom = (2*pi)^(d/2)*sqrt(abs(det(C)));
20 mahal = sum(((x-M)'*inv(C)).*(x-M)',2); % Chris Bregler's trick
21 if any(mahal<0)
22 warning('mahal < 0 => C is not psd')
23 end
24 if use_log
25 p = -0.5*mahal - log(denom);
26 else
27 p = exp(-0.5*mahal) / (denom+eps);
28 end