Skip to content

FIX Clip negative diagonal covariance estimate to 0 in GaussianMixture - #30401

Closed
ogrisel wants to merge 7 commits into
scikit-learn:mainfrom
ogrisel:fix-clip-negative-diagonal-covariance
Closed

ogrisel wants to merge 7 commits into
scikit-learn:mainfrom
ogrisel:fix-clip-negative-diagonal-covariance

Conversation

@ogrisel

@ogrisel ogrisel commented Dec 3, 2024

Copy link
Copy Markdown
Member

Hackish tentative fix for #30382.

I am not sure if this is the correct way to handle this but would like to discuss it (if the tests pass).

@github-actions

github-actions Bot commented Dec 3, 2024 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 23eaebc. Link to the linter CI: here

@ogrisel
ogrisel force-pushed the fix-clip-negative-diagonal-covariance branch from 4efb87c to 32c87de Compare December 4, 2024 10:52
# implementation or the random seed this can either lead to a bad
# estimate of the covariance or a ValueErorr caused by a LinalgError in
# the Cholesky decomposition.
pytest.xfail("The 'tied' covariance type fails to estimates the covariance.")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For information, I naively tried to hack the tied covariance estimation as follows:

diff --git a/sklearn/mixture/_gaussian_mixture.py b/sklearn/mixture/_gaussian_mixture.py
index cf8896ce7c..093aa6ce2a 100644
--- a/sklearn/mixture/_gaussian_mixture.py
+++ b/sklearn/mixture/_gaussian_mixture.py
@@ -202,7 +202,10 @@ def _estimate_gaussian_covariances_tied(resp, X, nk, means, reg_covar):
     avg_means2 = np.dot(nk * means.T, means)
     covariance = avg_X2 - avg_means2
     covariance /= nk.sum()
-    covariance.flat[:: len(covariance) + 1] += reg_covar
+    diag = np.diag(covariance)
+    diag = np.clip(diag, 0, None)
+    diag += reg_covar
+    covariance.flat[:: len(covariance) + 1] = diag
     return covariance

But this does not fix this particular test failure.

Calling the full method and averaging the estimated covariance matrices across the components makes the test pass, but it's not as computationally efficient as the current code (and I am not sure that it's more numerically stable in general).

@ogrisel

ogrisel commented Dec 5, 2024

Copy link
Copy Markdown
Member Author

I am not sure if clipping for the tied/diagonal covariance cases is always a good idea. It does remove some negative rounding errors, but there still are errors in the other direction that are not clipped.

Another alternative would be to accept that fitting GMM on unscaled float32 data is fundamentally problematic and that scaling or converting the input to float64 is the best way forward.

@ogrisel

ogrisel commented Dec 5, 2024 •

Copy link
Copy Markdown
Member Author

We should probably not merge this PR as is: I had not realized that resp and means are float64 allocated even when the input is float32. I discussed in the linked issue.

Let's close this PR and instead open PRs to:

  • implement the removal of the redundantly useless avg_X_means term;
  • suggest passing float64 input data when the estimated covariances are invalid because of numerical errors.

@ogrisel ogrisel closed this Dec 5, 2024
@ogrisel
ogrisel deleted the fix-clip-negative-diagonal-covariance branch December 5, 2024 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant