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

Testvalues #137

Merged
merged 10 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
@@ -1,7 +1,7 @@
name = "MeasureTheory"
uuid = "eadaa1a4-d27c-401d-8699-e962e1bbc33b"
authors = ["Chad Scherrer <[email protected]> and contributors"]
version = "0.10.1"
version = "0.10.2"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
6 changes: 6 additions & 0 deletions src/combinators/chain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,9 @@ function Base.collect(r::Realized)
end
return a
end

function testvalue(mc::Chain)
μ = mc.μ
κ = mc.κ
rand(Chain(Dirac ∘ testvalue ∘ κ, (Dirac ∘ testvalue)(μ)))
end
5 changes: 5 additions & 0 deletions src/parameterized/dirichlet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ end
Base.rand(rng::AbstractRNG, T::Type, μ::Dirichlet) = rand(rng, Dists.Dirichlet(μ.α))

distproxy(d::Dirichlet{(:α,)}) = Dists.Dirichlet(d.α)

function testvalue(d::Dirichlet{(:α,)})
n = length(d.α)
Fill(1/n, n)
end
5 changes: 5 additions & 0 deletions src/parameterized/lkj-cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ function Base.rand(rng::AbstractRNG, ::Type, d::LKJCholesky{(:k, :η,)})
return cholesky(Positive, rand(rng, Dists.LKJ(d.k, d.η)))
end;

function testvalue(d::LKJCholesky)
t = CorrCholesky(d.k)
return transform(t, zeros(dimension(t)))
end

function Base.rand(rng::AbstractRNG, ::Type, d::LKJCholesky{(:k, :logη)})
return cholesky(Positive, rand(rng, Dists.LKJ(d.k, exp(d.logη))))
end;
Expand Down
9 changes: 9 additions & 0 deletions src/parameterized/multinomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ function logmultinomial(k)
end
result
end

function testvalue(d::Multinomial{(:n,:p)})
n = d.n
l = length(d.p)
q,r = divrem(n, l)
x = fill(q, l)
x[1] += r
return x
end
2 changes: 2 additions & 0 deletions src/primitives/dirac.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ function logdensity(μ::Dirac{M}, ν::Dirac{M}, x) where {M}
return -Inf
end
end

testvalue(d::Dirac) = d.x
2 changes: 2 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ end

constructor(::T) where {T} = constructor(T)

constructor(::Type{T}) where {T} = constructorof(T)

macro trysupport(ex)
ex = esc(ex)
quote
Expand Down
47 changes: 47 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,53 @@ function draw2(μ)
return (x,y)
end

measures = [
# Chain(x -> Normal(μ=x), Normal(μ=0.0))
For(3) do j Normal(σ=j) end
For(2,3) do i,j Normal(i,j) end
# Likelihood
# Pointwise
Normal() ^ 3
Normal() ^ (2,3)
# SpikeMixture(Normal(), 2)
# Dirac(0.0) + Normal()
# transforms
3 * Normal()
Bernoulli(0.2)
Beta(2,3)
Binomial(10,0.3)
Cauchy()
Dirichlet(ones(3))
Exponential()
Gumbel()
# InverseGamma(2)
cscherrer marked this conversation as resolved.
Show resolved Hide resolved
Laplace()
LKJCholesky(3,2.0)
Multinomial(n=10,p=[0.2,0.3,0.5])
# MvNormal
NegativeBinomial(5,0.2)
Normal(2,3)
Poisson(3.1)
StudentT(ν=2.1)
Uniform()
# CountingMeasure(Float64)
Dirac(π)
Lebesgue(ℝ)
cscherrer marked this conversation as resolved.
Show resolved Hide resolved
# TrivialMeasure()
]

@testset "testvalue" begin
for μ in measures
@test logdensity(μ, testvalue(μ)) isa Float64
cscherrer marked this conversation as resolved.
Show resolved Hide resolved
end

@testset "testvalue(::Chain)" begin
mc = Chain(x -> Normal(μ=x), Normal(μ=0.0))
r = testvalue(mc)
@test logdensity(mc, Iterators.take(r, 10)) isa Float64
cscherrer marked this conversation as resolved.
Show resolved Hide resolved
end
end

@testset "Parameterized Measures" begin
@testset "Binomial" begin
D = Binomial{(:n, :p)}
Expand Down