| 1 |
import numpy as np |
| 2 |
from scipy import linalg |
| 3 |
|
| 4 |
|
| 5 |
def log_multivariate_normal_density(X, means, covars, covariance_type='diag'): |
| 6 |
""" |
| 7 |
Compute the log probability under a multivariate Gaussian distribution. |
| 8 |
|
| 9 |
Parameters |
| 10 |
---------- |
| 11 |
X : array_like, shape (n_samples, n_features) |
| 12 |
List of n_features-dimensional data points. Each row corresponds to a |
| 13 |
single data point. |
| 14 |
|
| 15 |
means : array_like, shape (n_components, n_features) |
| 16 |
List of n_features-dimensional mean vectors for n_components Gaussians. |
| 17 |
Each row corresponds to a single mean vector. |
| 18 |
|
| 19 |
covars : array_like |
| 20 |
List of n_components covariance parameters for each Gaussian. The shape |
| 21 |
depends on `covariance_type`: |
| 22 |
|
| 23 |
* (n_components, ) if "spherical", |
| 24 |
* (n_components, n_features) if "diag", |
| 25 |
* (n_components, n_features, n_features) if "full", |
| 26 |
* (n_features, n_features) if "tied". |
| 27 |
|
| 28 |
covariance_type : {"spherical", "diag", "full", "tied"}, optional |
| 29 |
The type of the covariance parameters. Defaults to 'diag'. |
| 30 |
|
| 31 |
Returns |
| 32 |
------- |
| 33 |
lpr : array_like, shape (n_samples, n_components) |
| 34 |
Array containing the log probabilities of each data point in |
| 35 |
X under each of the n_components multivariate Gaussian distributions. |
| 36 |
""" |
| 37 |
log_multivariate_normal_density_dict = { |
| 38 |
'spherical': _log_multivariate_normal_density_spherical, |
| 39 |
'tied': _log_multivariate_normal_density_tied, |
| 40 |
'diag': _log_multivariate_normal_density_diag, |
| 41 |
'full': _log_multivariate_normal_density_full} |
| 42 |
return log_multivariate_normal_density_dict[covariance_type]( |
| 43 |
X, means, covars |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def _log_multivariate_normal_density_diag(X, means, covars): |
| 48 |
"""Compute Gaussian log-density at X for a diagonal model.""" |
| 49 |
|
| 50 |
nc, nf = means.shape |
| 51 |
|
| 52 |
covars = np.maximum(covars, np.finfo(float).tiny) |
| 53 |
with np.errstate(over="ignore"): |
| 54 |
return -0.5 * (nf * np.log(2 * np.pi) |
| 55 |
+ np.log(covars).sum(axis=-1) |
| 56 |
+ ((X[:, None, :] - means) ** 2 / covars).sum(axis=-1)) |
| 57 |
|
| 58 |
|
| 59 |
def _log_multivariate_normal_density_spherical(X, means, covars): |
| 60 |
"""Compute Gaussian log-density at X for a spherical model.""" |
| 61 |
nc, nf = means.shape |
| 62 |
if covars.ndim == 1: |
| 63 |
covars = covars[:, np.newaxis] |
| 64 |
covars = np.broadcast_to(covars, (nc, nf)) |
| 65 |
return _log_multivariate_normal_density_diag(X, means, covars) |
| 66 |
|
| 67 |
|
| 68 |
def _log_multivariate_normal_density_tied(X, means, covars): |
| 69 |
"""Compute Gaussian log-density at X for a tied model.""" |
| 70 |
nc, nf = means.shape |
| 71 |
cv = np.broadcast_to(covars, (nc, nf, nf)) |
| 72 |
return _log_multivariate_normal_density_full(X, means, cv) |
| 73 |
|
| 74 |
|
| 75 |
def _log_multivariate_normal_density_full(X, means, covars, min_covar=1.e-7): |
| 76 |
"""Log probability for full covariance matrices.""" |
| 77 |
nc, nf = means.shape |
| 78 |
log_prob = [] |
| 79 |
for c, (mu, cv) in enumerate(zip(means, covars)): |
| 80 |
try: |
| 81 |
cv_chol = linalg.cholesky(cv, lower=True) |
| 82 |
except linalg.LinAlgError: |
| 83 |
|
| 84 |
|
| 85 |
try: |
| 86 |
cv_chol = linalg.cholesky(cv + min_covar * np.eye(nf), |
| 87 |
lower=True) |
| 88 |
except linalg.LinAlgError: |
| 89 |
raise ValueError("'covars' must be symmetric, " |
| 90 |
"positive-definite") |
| 91 |
|
| 92 |
cv_log_det = 2 * np.sum(np.log(np.diagonal(cv_chol))) |
| 93 |
cv_sol = linalg.solve_triangular(cv_chol, (X - mu).T, lower=True).T |
| 94 |
log_prob.append(-.5 * (nf * np.log(2 * np.pi) |
| 95 |
+ (cv_sol ** 2).sum(axis=1) |
| 96 |
+ cv_log_det)) |
| 97 |
|
| 98 |
return np.transpose(log_prob) |