0

Evaluation of Multivariate Gaussian with NumPy

 2 years ago
source link: http://siongui.github.io/2012/05/25/evaluation-of-multivariate-gaussian-with-numpy/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Evaluation of Multivariate Gaussian with NumPy

May 25, 2012

To implement a continuous HMM, it involves the evaluation of multivariate Gaussian (multivariate normal distribution). This post gives description of how to evaluate multivariate Gaussian with NumPy.

The formula for multivariate Gaussian used for continuous HMM is:

gif.latex?N(o;%5Cmu,%5CSigma)&space;=%5Cfrac%7B1%7D%7B%5Csqrt%7B(2%5Cpi)%5E%7Bn%7D%5Cleft&space;%7C&space;%5CSigma&space;%5Cright&space;%7C%7D%7De%5E%7B-%5Cfrac%7B1%7D%7B2%7D(o-%5Cmu)%27%5CSigma%5E%7B-1%7D(o-%5Cmu)%7D

where o is vector extracted from observation, μ is mean vector, and Σ is covariance matrix.

For the computation and implementation to be easily done, covariance matrix is assume to be diagonal without loss of much performance. The following is the code snippet:

dimension = mean_vector.size
detDiagCovMatrix = numpy.sqrt(numpy.prod(numpy.diag(covariance_matrix)))
frac = (2*numpy.pi)**(-dimension/2.0) * (1/detDiagCovMatrix)
fprime = feature_vector - mean_vector
fprime **= 2
if log:
  logValue = -0.5*numpy.dot(fprime, 1/numpy.diag(covariance_matrix))
  logValue += numpy.log(frac)
  return logValue
else:
  return frac * numpy.exp(-0.5*numpy.dot(fprime, 1/numpy.diag(covariance_matrix)))

Both mean vector and covariance matrix are of type numpy.ndarray with proper dimension. The code is self-explanatory. If more details are needed, please follow the link in [1].


References:

[1](1, 2)

PS: Original link is invalid now. See archive 1 or archive 2 for the source code.

[2]David Cournapeau (author of source code in [1])

[3]Numpy Example List


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK