Skip to content

Fix MillerMadow, ChaoShen and Shrinkage bugs - #464

Merged
Datseris merged 4 commits into
JuliaDynamics:mainfrom
denizhanpak:fix-discrete-estimator-bugs
Nov 23, 2026
Merged

Datseris merged 4 commits into
JuliaDynamics:mainfrom
denizhanpak:fix-discrete-estimator-bugs

Conversation

@denizhanpak

Copy link
Copy Markdown
Contributor

Fixes three bugs in discrete estimators, one commit each, with regression tests that fail on main and pass here. Bumps the version to 3.12.2, with a changelog entry.

MillerMadow: the correction is in nats, and N was length(x)

The correction (m - 1)/2N is in nats, but it was added unconverted to the plug-in entropy, whatever the base. The default MillerMadow() is base 2, so it under-corrected by a factor log(2); base 10 over-corrected.

x = rand(1:5, 1000)
information(MillerMadow(Shannon(; base = 2)), UniqueElements(), x) -
    information(PlugIn(Shannon(; base = 2)), UniqueElements(), x)
# main: 0.0020    expected (5 - 1)/(2 * 1000)/log(2) = 0.0029

N was length(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 with OrdinalPatterns(m = 3). N is now the total of the counts for counting-based outcome spaces, and encoded_space_cardinality(o, x) otherwise.

ChaoShen: NaN when every outcome is a singleton

The docstring says that if f₁ = N, f₁ is set to N - 1, so the coverage C = 1 - f₁/N stays positive. The code had f₁ == N - 1 (a comparison), so C = 0 and the result was NaN:

information(ChaoShen(), UniqueElements(), 1:10) # main: NaN

Shrinkage: the optimal shrinkage intensity

Eq. 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 on main.

Note: Shrinkage with a non-default target t has separate problems (#463). This PR does not change them.

Tests

The full test suite passes locally.

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 Datseris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! We dont add changelog entries for bugfixes, so if you could please remove those then this is good to go!

@denizhanpak
denizhanpak force-pushed the fix-discrete-estimator-bugs branch from b72cf2f to 196f305 Compare November 21, 2026 04:40
@denizhanpak

Copy link
Copy Markdown
Contributor Author

Fixed the change log it is ready to be merged whenever.

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.35%. Comparing base (9fcf9d7) to head (196f305).
⚠️ Report is 43 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Datseris

Copy link
Copy Markdown
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!

@Datseris
Datseris merged commit b2492f3 into JuliaDynamics:main Nov 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants