diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toolboxes/FullBNT-1.0.7/KPMstats/gaussian_prob.m	Tue Feb 10 15:05:51 2015 +0000
@@ -0,0 +1,28 @@
+function p = gaussian_prob(x, m, C, use_log)
+% GAUSSIAN_PROB Evaluate a multivariate Gaussian density.
+% p = gaussian_prob(X, m, C)
+% p(i) = N(X(:,i), m, C) where C = covariance matrix and each COLUMN of x is a datavector
+
+% p = gaussian_prob(X, m, C, 1) returns log N(X(:,i), m, C) (to prevents underflow).
+%
+% If X has size dxN, then p has size Nx1, where N = number of examples
+
+if nargin < 4, use_log = 0; end
+
+if length(m)==1 % scalar
+  x = x(:)';
+end
+[d N] = size(x);
+%assert(length(m)==d); % slow
+m = m(:);
+M = m*ones(1,N); % replicate the mean across columns
+denom = (2*pi)^(d/2)*sqrt(abs(det(C)));
+mahal = sum(((x-M)'*inv(C)).*(x-M)',2);   % Chris Bregler's trick
+if any(mahal<0)
+  warning('mahal < 0 => C is not psd')
+end
+if use_log
+  p = -0.5*mahal - log(denom);
+else
+  p = exp(-0.5*mahal) / (denom+eps);
+end