Repository navigation
Fix MillerMadow, ChaoShen and Shrinkage bugs - #464
Merged
Datseris merged 4 commits intoNov 23, 2026
Merged
Conversation
The Miller-Madow correction (m - 1)/2N is in nats, but it was added to the plug-in entropy in the unit of the entropy's base. The default `MillerMadow()` (base 2) therefore under-corrected by a factor log(2), and base 10 over-corrected. Convert the correction to the entropy's base. N was `length(x)`, but the correction needs the number of observations, i.e. the number of encoded points, which is smaller than `length(x)` for outcome spaces that embed `x` (e.g. `OrdinalPatterns`). Use the total of the counts for counting-based outcome spaces, and `encoded_space_cardinality` otherwise.
When every outcome occurs once (f₁ = N), the docstring says f₁ is set to N - 1 so that the coverage C = 1 - f₁/N is positive. The code used `f₁ == N - 1` (a comparison) instead of `f₁ = N - 1`, so C = 0 and the estimate was NaN.
Eq. 5 of Hausser & Strimmer (2009) is
λ = (1 - Σ θ̂ₖ²) / ((n - 1) Σ (tₖ - θ̂ₖ)²),
but the code computed (1 - Σ θ̂ₖ²) / (n - 1) * Σ (tₖ - θ̂ₖ)², multiplying
by the sum of squares instead of dividing by it. For counts (6, 3, 1)
this gave λ = 0.0076 instead of 0.47, so the estimator barely shrank.
When the estimate already equals the target (Σ (tₖ - θ̂ₖ)² = 0, e.g. a
single observed outcome), use λ = 0 instead of dividing by zero.
Datseris
approved these changes
Nov 19, 2026
Datseris
left a comment
Member
There was a problem hiding this comment.
Thanks! We dont add changelog entries for bugfixes, so if you could please remove those then this is good to go!
denizhanpak
force-pushed
the
fix-discrete-estimator-bugs
branch
from
November 21, 2026 04:40
b72cf2f to
196f305
Compare
Contributor
Author
|
Fixed the change log it is ready to be merged whenever. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #464 +/- ##
==========================================
+ Coverage 91.97% 93.35% +1.38%
==========================================
Files 86 88 +2
Lines 2468 2499 +31
==========================================
+ Hits 2270 2333 +63
+ Misses 198 166 -32 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Member
|
thanks a lot i am merging this in. I know you have a bunch of PRs open but these weeks are really busy from everyday work so it will take me a bit to get to them, sorry! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes three bugs in discrete estimators, one commit each, with regression tests that fail on
mainand pass here. Bumps the version to 3.12.2, with a changelog entry.MillerMadow: the correction is in nats, and N waslength(x)The correction
(m - 1)/2Nis in nats, but it was added unconverted to the plug-in entropy, whatever the base. The defaultMillerMadow()is base 2, so it under-corrected by a factorlog(2); base 10 over-corrected.Nwaslength(x), but it should be the number of observations, i.e. encoded points. These differ for outcome spaces that embed the input: 30 points give 28 ordinal patterns withOrdinalPatterns(m = 3).Nis now the total of the counts for counting-based outcome spaces, andencoded_space_cardinality(o, x)otherwise.ChaoShen:NaNwhen every outcome is a singletonThe docstring says that if
f₁ = N,f₁is set toN - 1, so the coverageC = 1 - f₁/Nstays positive. The code hadf₁ == N - 1(a comparison), soC = 0and the result wasNaN:Shrinkage: the optimal shrinkage intensityEq. 5 of Hausser & Strimmer (2009) is
λ = (1 - Σ θ̂ₖ²) / ((n - 1) Σ (tₖ - θ̂ₖ)²). The code computed(1 - Σ θ̂ₖ²) / (n - 1) * Σ (tₖ - θ̂ₖ)², multiplying by the sum of squares instead of dividing by it. For counts (6, 3, 1), λ was 0.0076 instead of 0.47, so the estimator barely shrank.When the estimate already equals the target (
Σ (tₖ - θ̂ₖ)² = 0, e.g. one observed outcome), λ = 0 is used instead of dividing by zero. Without this guard, the corrected formula would give 0/0 for constant data, which works onmain.Note:
Shrinkagewith a non-default targetthas separate problems (#463). This PR does not change them.Tests
The full test suite passes locally.