|
6 | 6 | import numpy as np |
7 | 7 | import pandas as pd |
8 | 8 | from numpy import pi, sqrt |
| 9 | +from copy import copy |
9 | 10 | from numpy.random import rand, randn |
10 | 11 | from scipy.stats._multivariate import _LOG_2PI, _PSD, _squeeze_output |
11 | 12 | from scipy.stats import Covariance, _covariance |
@@ -880,22 +881,19 @@ def cov(self): |
880 | 881 | """ |
881 | 882 | # Compute the mean of the distribution |
882 | 883 | mean = self.mean() |
883 | | - |
884 | | - # Extract weights for each sample (assuming self.w exists) |
885 | | - w = self.w |
886 | | - |
887 | | - # Normalise weights to sum to 1 |
| 884 | + |
| 885 | + w = copy(self.w) |
888 | 886 | w = w / np.sum(w) |
889 | | - |
890 | | - # Extract individual covariances (self.P should be shape (n_samples, nx, nx)) |
| 887 | + |
| 888 | + # Extract individual covariances |
891 | 889 | covs = self.P |
892 | | - |
893 | | - # Compute the difference between each sample and the mean |
894 | | - diffs = self.m - mean # shape (n_samples, nx) |
895 | | - |
| 890 | + |
| 891 | + # Compute the difference between each mixand mean and the distribution mean |
| 892 | + diffs = self.m - mean # shape (nC, nx) |
| 893 | + |
896 | 894 | # Compute the outer product of diffs for each sample |
897 | | - outer_diffs = diffs[:, :, None] * diffs[:, None, :] # shape (n_samples, nx, nx) |
898 | | - |
| 895 | + outer_diffs = diffs[:, :, None] * diffs[:, None, :] # shape (nC, nx, nx) |
| 896 | + |
899 | 897 | # Weighted sum of covariances and outer products |
900 | 898 | return np.sum(w[:, None, None] * (covs + outer_diffs), axis=0) |
901 | 899 |
|
|
0 commit comments