Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "ComplexityMeasures"
uuid = "ab4b797d-85ee-42ba-b621-05d793b346a2"
authors = "Kristian Agasøster Haaga <kahaaga@gmail.com>, George Datseries <datseris.george@gmail.com>"
repo = "https://github.com/juliadynamics/ComplexityMeasures.jl.git"
version = "3.12.1"
version = "3.12.2"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand Down
2 changes: 1 addition & 1 deletion src/discrete_info_estimators/chao_shen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 9 additions & 3 deletions src/discrete_info_estimators/miller_madow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/probabilities_estimators/Shrinkage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.λ
Expand Down
9 changes: 9 additions & 0 deletions test/infomeasures/discrete_estimators/chao_shen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions test/infomeasures/discrete_estimators/miller_madow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 15 additions & 0 deletions test/probabilities_estimators/Shrinkage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading