#136 · Probability · Medium
⊣ Solve on deep-ml.comCompute the Kullback-Leibler divergence between two multivariate Gaussian distributions. Given the mean vectors and covariance matrices of two distributions P and Q, calculate KL(P || Q).
import numpy as np
def kl_divergence_gaussian(mu_p: np.ndarray, cov_p: np.ndarray, mu_q: np.ndarray, cov_q: np.ndarray) -> float:
k = mu_p.shape[0]
cov_q_inv = np.linalg.inv(cov_q)
# KL(P || Q) = 0.5 * (tr(Sigma_q^{-1} Sigma_p) + (mu_q - mu_p)^T Sigma_q^{-1} (mu_q - mu_p) - k + ln(det(Sigma_q) / det(Sigma_p)))
diff = mu_q - mu_p
trace_term = np.trace(cov_q_inv @ cov_p)
mahalanobis_term = diff.T @ cov_q_inv @ diff
log_det_term = np.log(np.linalg.det(cov_q) / np.linalg.det(cov_p))
kl = 0.5 * (trace_term + mahalanobis_term - k + log_det_term)
return float(kl)tr(Sigma_q^{-1} @ Sigma_p) measures how well Q's covariance covers P's.ln(det(Sigma_q) / det(Sigma_p)) accounts for the volume difference.-k term is a correction for the dimensionality.