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" 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/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/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/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 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) 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