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

feat: add mean(::ClosedFormExpectation, ::Logpdf{MVNormal}, ::MVNormal) #21

Merged
merged 1 commit into from
May 20, 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
2 changes: 2 additions & 0 deletions src/ClosedFormExpectations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ include("Normal/expectation.jl")
include("Normal/williams/normal.jl")
include("Normal/williams/normal_mean_variance.jl")
include("Normal/williams/ef_parametrization.jl")
# mv normal
include("MvNormal/expectation.jl")

# gamma
include("Gamma/Gamma.jl")
Expand Down
7 changes: 7 additions & 0 deletions src/MvNormal/expectation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Distributions: Laplace, Normal, Rayleigh, params, cdf, std
import ExponentialFamily: MultivariateNormalDistributionsFamily
using SpecialFunctions: erf, loggamma

function mean(::ClosedFormExpectation, p::Logpdf{T1}, q::T2) where {T1 <: MultivariateNormalDistributionsFamily, T2 <: MultivariateNormalDistributionsFamily}
-kldivergence(q, p.dist) - entropy(q)
end
2 changes: 1 addition & 1 deletion src/expressions/distributions/LogGamma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end
LogGamma(α::Real, β::Real; check_args::Bool=true) = LogGamma(promote(α, β)...; check_args=check_args)
LogGamma(α::Integer, β::Integer; check_args::Bool=true) = LogGamma(float(α), float(β); check_args=check_args)

@distr_support LogGamma 0.0 Inf
@distr_support LogGamma -Inf Inf

#### Conversions
Base.convert(::Type{LogGamma{T}}, α::S, β::S) where {T <: Real, S <: Real} = LogGamma(T(α), T(β))
Expand Down
11 changes: 11 additions & 0 deletions test/MvNormal/mean_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@testitem "mean(::ClosedFormExpectation, p::Logpdf{MultivariateNormalDistributionsFamily}, q::MultivariateNormalDistributionsFamily)" begin
include("mvnormal_utils.jl")

rng = StableRNG(123)
for _ in 1:10
n = rand(rng, 2:10)
μ1, Σ1 = rand(rng, n), generate_random_posdef_matrix(rng, n)
μ2, Σ2 = rand(rng, n), generate_random_posdef_matrix(rng, n)
central_limit_theorem_test(ClosedFormExpectation(), Logpdf(MvNormal(μ1, Σ1)), MvNormal(μ2, Σ2), 10^6)
end
end
14 changes: 14 additions & 0 deletions test/MvNormal/mvnormal_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include("../test_utils.jl")

using Distributions
using ExponentialFamily

function prepare_samples(::MultivariateNormalDistributionsFamily{T}, samples) where {T}
return eachcol(samples)
end

function generate_random_posdef_matrix(rng, d::Int)
A = randn(rng, d, d)
A = A'*A
return (A + A')/2
end
2 changes: 2 additions & 0 deletions test/distributions/loggamma_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
d = LogGamma(1, 1)
@test convert(LogGamma{Float64}, d) === d
@test isa(convert(LogGamma{Float32}, d), LogGamma{Float32})
@test minimum(d) == -Inf
@test maximum(d) == Inf
end
7 changes: 6 additions & 1 deletion test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ function concentration_rule(expectation, statistical_mean, N, α,sobolev_const,
return statistical_mean - ϵ < expectation < statistical_mean + ϵ
end

function prepare_samples(::Any, samples)
return samples
end

function central_limit_theorem_test(::ClosedFormExpectation, f, q, N = 10^6)
rng = StableRNG(123)
samples = rand(rng, q, N)
transformed_samples = f.(samples)
samples = prepare_samples(q, samples)
transformed_samples = map(f, samples)
expectation = mean(ClosedFormExpectation(), f, q)
result = sigma_rule(expectation, mean(transformed_samples), std(transformed_samples), N)
if !result
Expand Down