Fix for #2374#2379
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in axis-based moment computations where falsy boolean options (notably unbiased=False) were not forwarded to the underlying torch implementation, causing ht.var(..., axis=..., ddof=0) to behave as if Bessel correction was enabled. It also adjusts the same forwarding logic for Fischer in kurtosis/skew paths and adds a regression test for the reported variance edge case.
Changes:
- Forward
unbiasedandFischerthrough__moment_w_axiswhenever they are provided (includingFalse). - Add a regression test for
ht.var(..., axis=0, ddof=0)on a single-element input (issue #2374). - Fix a minor docstring typo (“implicitely” → “implicitly”).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| heat/core/statistics.py | Fixes boolean kwarg forwarding for axis-based moment helpers (unbiased, Fischer) and corrects a docstring typo. |
| tests/core/test_statistics.py | Adds a regression test covering the ddof=0 + axis edge case from #2374. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| kwargs = {"dim": axis} | ||
| if unbiased: | ||
| if unbiased is not None: | ||
| kwargs["unbiased"] = unbiased | ||
| if Fischer: | ||
| if Fischer is not None: | ||
| kwargs["Fischer"] = Fischer |
| if unbiased is not None: | ||
| kwargs["unbiased"] = unbiased | ||
| if Fischer: | ||
| if Fischer is not None: | ||
| kwargs["Fischer"] = Fischer |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2379 +/- ##
=======================================
Coverage 91.74% 91.74%
=======================================
Files 87 87
Lines 14120 14120
=======================================
Hits 12955 12955
Misses 1165 1165
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
| # helper for calculating a statistical moment with a given axis | ||
| kwargs = {"dim": axis} | ||
| if unbiased: | ||
| if unbiased is not None: |
There was a problem hiding this comment.
unbiased is correction in torch.var since version 2.0 and works with integers. We will have to look what it is for the other functions.
There was a problem hiding this comment.
I think we should revert correction back for now. It is only in var and accepts ints. Maybe, change it to bias and flip the values.
Nonetheless, we should use the same parameters as in the other APIs for our public functions and update the calls to torch.var
There was a problem hiding this comment.
correction is the closest to the torch and numpy APIs, so why would be do something yet completely different?
There was a problem hiding this comment.
Actually, I don't see it close to numpys API.
Torch/NumPy:
- correction is only used in var
- correction takes ints and floats and is a array api compatible name for ddof.
- skew and kurtosis use bias in scipy and are bools
PR:
- correction is used everywhere
- correction takes bools in var and it has a different logic underneath
- skew and kurtosis also have the correction parameter name
Right now in the PR, it is nothing alike but the name in a single place.
|
I don't know if this is the right place for it, but upon merging the fix we can now omit the four heat/tests/naive_bayes/test_gaussiannb.py Lines 79 to 80 in 5d9851b heat/tests/naive_bayes/test_gaussiannb.py Line 91 in 5d9851b heat/tests/naive_bayes/test_gaussiannb.py Line 105 in 5d9851b |
|
@maliesen, thanks for pointing this out! You are solving all of our bugs! :) |
See #2374 for the description of the problem reported by @maliesen.
Short recap: The function
__moment_w_axistakesunbiasedas input and forwards it to the respective torch function (varin this case). However, it only does so ifunbiasedisTrue. We need to forwardunbiased=Falseas well, though.The same happens with
Fisher(see #2375) which may also be fixed by this PR. It remains to be checked, though.Issue/s resolved: #2374