From 35250560fc7f429855d77dec4e6455173ec65ffc Mon Sep 17 00:00:00 2001 From: denizhanpak Date: Fri, 13 Nov 2026 12:33:32 -0500 Subject: [PATCH 1/4] Fix the units and the sample size of the MillerMadow correction 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. --- src/discrete_info_estimators/miller_madow.jl | 12 +++++++++--- .../discrete_estimators/miller_madow.jl | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/discrete_info_estimators/miller_madow.jl b/src/discrete_info_estimators/miller_madow.jl index 40a5f09e7..95d2476a4 100644 --- a/src/discrete_info_estimators/miller_madow.jl +++ b/src/discrete_info_estimators/miller_madow.jl @@ -17,18 +17,24 @@ H_S^{MM} = H_S^{plugin} + \\dfrac{m - 1}{2N}, where ``H_S^{plugin}`` is the Shannon entropy estimated using the [`PlugIn`](@ref) estimator, `m` is the number of bins with nonzero probability (as defined in -[Paninski2003](@citet)), and `N` is the number of observations. +[Paninski2003](@citet)), and `N` is the number of observations (the number of encoded +points, e.g. the number of ordinal patterns for [`OrdinalPatterns`](@ref)). The +correction ``(m - 1)/2N`` is in nats; for an entropy in base ``b`` it is divided by +``\\log(b)``. """ Base.@kwdef struct MillerMadow{I <: InformationMeasure} <: DiscreteInfoEstimatorShannon{I} definition::I = Shannon() end function information(hest::MillerMadow{<:Shannon}, pest::ProbabilitiesEstimator, o::OutcomeSpace, x) - N = length(x) + # The number of observations is the number of encoded points, which is smaller than + # `length(x)` for outcome spaces that embed `x` (e.g. `OrdinalPatterns`). + N = is_counting_based(o) ? sum(counts(o, x)) : encoded_space_cardinality(o, x) probs = allprobabilities(pest, o, x) # Estimate of the number of bins with nonzero pN-probability; here estimated as # in Paninski (2003) m̂ = count(probs .> 0.0) h_naive = information(PlugIn(hest.definition), probs) - return h_naive + (m̂ - 1) / (2 * N) + # The correction (m̂ - 1) / 2N is in nats; convert it to the unit of the entropy. + return h_naive + convert_logunit((m̂ - 1) / (2 * N), ℯ, hest.definition.base) end diff --git a/test/infomeasures/discrete_estimators/miller_madow.jl b/test/infomeasures/discrete_estimators/miller_madow.jl index f182a641e..733598341 100644 --- a/test/infomeasures/discrete_estimators/miller_madow.jl +++ b/test/infomeasures/discrete_estimators/miller_madow.jl @@ -12,3 +12,22 @@ h = information(MillerMadow(Shannon()), pest, x) # https://arxiv.org/pdf/2204.01469.pdf), we should get a larger or equal value with the # Miller-Madow estimator. @test h >= information(PlugIn(Shannon()), pest, x) + +# The correction (m - 1)/2N is in nats, and is converted to the base of the entropy. +cts = counts(pest, x) +m = count(>(0), cts) +for base in (2, ℯ, 10) + hp = information(PlugIn(Shannon(; base)), pest, x) + hm = information(MillerMadow(Shannon(; base)), pest, x) + @test hm - hp ≈ (m - 1) / (2 * length(x)) / log(base) +end + +# N is the number of encoded points: here the number of ordinal patterns, which is +# smaller than `length(y)`. +y = randn(rng, 30) +o = OrdinalPatterns(m = 3) +cts = counts(o, y) +@test sum(cts) == 28 +hp = information(PlugIn(Shannon(; base = ℯ)), o, y) +hm = information(MillerMadow(Shannon(; base = ℯ)), o, y) +@test hm - hp ≈ (count(>(0), cts) - 1) / (2 * 28) From c4d69b0b4bc696941dc1b1ce6a20f0908c02cf8a Mon Sep 17 00:00:00 2001 From: denizhanpak Date: Fri, 13 Nov 2026 12:33:32 -0500 Subject: [PATCH 2/4] Fix ChaoShen when every outcome is a singleton MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/discrete_info_estimators/chao_shen.jl | 2 +- test/infomeasures/discrete_estimators/chao_shen.jl | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/discrete_info_estimators/chao_shen.jl b/src/discrete_info_estimators/chao_shen.jl index b7ed532f8..7ecdd7d6d 100644 --- a/src/discrete_info_estimators/chao_shen.jl +++ b/src/discrete_info_estimators/chao_shen.jl @@ -42,7 +42,7 @@ function information(hest::ChaoShen{<:Shannon}, pest::ProbabilitiesEstimator, o: N = sum(cts) if f₁ == N - f₁ == N - 1 + f₁ = N - 1 end C = 1 - (f₁ / N) diff --git a/test/infomeasures/discrete_estimators/chao_shen.jl b/test/infomeasures/discrete_estimators/chao_shen.jl index 3df1de91c..a35245e21 100644 --- a/test/infomeasures/discrete_estimators/chao_shen.jl +++ b/test/infomeasures/discrete_estimators/chao_shen.jl @@ -12,3 +12,12 @@ h = information(ChaoShen(Shannon()), o, x) # https://arxiv.org/pdf/2204.01469.pdf), we should get a larger or equal value with the # Chao-Shen estimator. @test h >= information(PlugIn(Shannon()), o, x) + +# If every outcome is a singleton (f₁ = N), f₁ is set to N - 1, so the coverage is +# C = 1/N instead of 0, and the estimate is finite and positive. +N = 10 +h = information(ChaoShen(Shannon(; base = ℯ)), o, collect(1:N)) +C = 1 / N +p = 1 / N +@test h ≈ -N * (C * p * log(C * p)) / (1 - (1 - C * p)^N) +@test isfinite(h) && h > 0 From 477b37332308f77ebb6a536e09afc7a73f699458 Mon Sep 17 00:00:00 2001 From: denizhanpak Date: Fri, 13 Nov 2026 12:33:32 -0500 Subject: [PATCH 3/4] Fix the optimal shrinkage intensity of Shrinkage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/probabilities_estimators/Shrinkage.jl | 4 +++- test/probabilities_estimators/Shrinkage.jl | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/probabilities_estimators/Shrinkage.jl b/src/probabilities_estimators/Shrinkage.jl index 5fbaf313b..cce94856d 100644 --- a/src/probabilities_estimators/Shrinkage.jl +++ b/src/probabilities_estimators/Shrinkage.jl @@ -107,7 +107,9 @@ function get_λ(est, n, probs_observed, t, m) tₖ = get_tₖ(t, k, m) densum += (tₖ - probs_observed[k])^2 end - λ = (1 - sum(probs_observed .^ 2)) / (n - 1) * densum + # If the estimate already equals the target (densum = 0), shrinkage changes + # nothing, so use λ = 0 (this avoids 0/0 for a single observed outcome). + λ = iszero(densum) ? 0.0 : (1 - sum(probs_observed .^ 2)) / ((n - 1) * densum) # User-picked shrinkage intensity. else λ = est.λ diff --git a/test/probabilities_estimators/Shrinkage.jl b/test/probabilities_estimators/Shrinkage.jl index 58ee8f5eb..a177a637b 100644 --- a/test/probabilities_estimators/Shrinkage.jl +++ b/test/probabilities_estimators/Shrinkage.jl @@ -55,3 +55,18 @@ end # We don't need to test for non-counting based outcome spaces, because the # Shrinkage constructor prevents us from combining such outcome spaces with # the estimator. + +@testset "Shrinkage: optimal shrinkage intensity" begin + # Counts 6, 3, 1 (n = 10), shrunk towards the uniform target t = 1/3. Optimal + # intensity, eq. 5 in Hausser & Strimmer (2009): + # λ = (1 - Σ θ̂ₖ²) / ((n - 1) Σ (tₖ - θ̂ₖ)²) + x = vcat(fill(1, 6), fill(2, 3), fill(3, 1)) + θ̂ = [0.6, 0.3, 0.1] + t = 1 / 3 + λ = (1 - sum(θ̂ .^ 2)) / ((10 - 1) * sum((t .- θ̂) .^ 2)) + ps = probabilities(Shrinkage(), UniqueElements(), x) + @test collect(ps) ≈ λ .* t .+ (1 - λ) .* θ̂ + + # A single observed outcome equals the target, so nothing is shrunk. + @test collect(probabilities(Shrinkage(), UniqueElements(), fill(1, 10))) == [1.0] +end From 196f305ace96eac0955c854f7bedfc719889de79 Mon Sep 17 00:00:00 2001 From: denizhanpak Date: Fri, 13 Nov 2026 12:33:32 -0500 Subject: [PATCH 4/4] Bump version to 3.12.2 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 4d09deb41..16217941c 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "ComplexityMeasures" uuid = "ab4b797d-85ee-42ba-b621-05d793b346a2" authors = "Kristian Agasøster Haaga , George Datseries " repo = "https://github.com/juliadynamics/ComplexityMeasures.jl.git" -version = "3.12.1" +version = "3.12.2" [deps] Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"