Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize Contingency #16

Merged
merged 1 commit into from
Jul 9, 2024
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
14 changes: 7 additions & 7 deletions src/densities/contingency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ A `Contingency` distribution over more than two variables requires higher-order
- `renormalize`, optional, supports either `Val(true)` or `Val(false)`, specifies whether matrix `P` must be automatically renormalized. Does not modify the original `P` and allocates a new one for the renormalized version. If set to `false` the contingency matrix `P` **must** be normalized by hand, otherwise the result of related calculations might be wrong

"""
struct Contingency{T,P<:AbstractMatrix{T}}
struct Contingency{T,P<:AbstractArray{T}}
p::P

Contingency{T,P}(A::AbstractMatrix) where {T,P<:AbstractMatrix{T}} = new(A)
Contingency{T,P}(A::AbstractArray) where {T,P<:AbstractArray{T}} = new(A)
end

Contingency(P::AbstractMatrix) = Contingency(P, Val(true))
Contingency(P::AbstractArray) = Contingency(P, Val(true))

function Contingency(P::M, ::Val{true}) where {T,M<:AbstractMatrix{T}}
function Contingency(P::M, ::Val{true}) where {T,M<:AbstractArray{T}}
return Contingency{T,M}(P ./ sum(P))
end

Contingency(P::M, ::Val{false}) where {T,M<:AbstractMatrix{T}} = Contingency{T,M}(P)
Contingency(P::M, ::Val{false}) where {T,M<:AbstractArray{T}} = Contingency{T,M}(P)

BayesBase.components(distribution::Contingency) = distribution.p
BayesBase.component(distribution::Contingency, k) = distribution.p[:, k]

function BayesBase.vague(::Type{<:Contingency}, dims::Int)
return Contingency(ones(dims, dims) ./ abs2(dims))
function BayesBase.vague(::Type{<:Contingency}, dims::Int, nvars::Int=2)
return Contingency(ones([dims for _ in 1:nvars]...))
end

BayesBase.paramfloattype(distribution::Contingency) = deep_eltype(components(distribution))
Expand Down
15 changes: 15 additions & 0 deletions test/densities/contingency_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ end

@test typeof(d2) <: Contingency
@test components(d2) ≈ ones(4, 4) ./ 16

d3 = vague(Contingency, 5, 3)
@test typeof(d3) <: Contingency
@test components(d3) ≈ ones(5, 5, 5) ./ 125
end

@testitem "Contingency: entropy" begin
Expand All @@ -31,6 +35,17 @@ end
@test entropy(Contingency(10.0 * [0.09 0.00; 0.00 0.91])) ≈ 0.30253782309749805
@test !isnan(entropy(Contingency([0.0 1.0; 1.0 0.0])))
@test !isinf(entropy(Contingency([0.0 1.0; 1.0 0.0])))

@test entropy(
Contingency(
stack([
0.3 * [0.2 0.1 0.7; 0.4 0.3 0.3; 0.1 0.6 0.3],
0.7 * [0.2 0.1 0.7; 0.4 0.3 0.3; 0.1 0.6 0.3],
],),
),
) ≈ 2.6390313416381166

@test entropy(Contingency(ones(2, 2, 2))) == 2.0794415416798357
end

@testitem "Contingency: isapprox" begin
Expand Down
Loading