Evaluation of Multivariate Gaussian with NumPy


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:

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