Skip to content

Commit

Permalink
Testvalues (#137)
Browse files Browse the repository at this point in the history
* fix testvalue(::CorrCholesky)

* add tests

* drop the eval

* Fix Dirac

* fix dirichet

* bump versoin

* multinomial

* Add constructor method

* testvalue(::Chain)

* update tests
  • Loading branch information
cscherrer authored Aug 15, 2021
1 parent 32ddc20 commit 12a6c19
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 1 deletion.
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
59 changes: 59 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,65 @@ function draw2(μ)
return (x,y)
end

function test_measure(μ)
logdensity(μ, testvalue(μ)) isa AbstractFloat
end

test_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
Normal() ^ 3
Normal() ^ (2,3)
3 * Normal()
Bernoulli(0.2)
Beta(2,3)
Binomial(10,0.3)
Cauchy()
Dirichlet(ones(3))
Exponential()
Gumbel()
Laplace()
LKJCholesky(3,2.0)
Multinomial(n=10,p=[0.2,0.3,0.5])
NegativeBinomial(5,0.2)
Normal(2,3)
Poisson(3.1)
StudentT=2.1)
Uniform()
Dirac(π)
Lebesgue(ℝ)
Normal() Cauchy()
]

testbroken_measures = [
Pushforward(as𝕀, Normal())
SpikeMixture(Normal(), 2)
# InverseGamma(2) # Not defined yet
# MvNormal(I(3)) # Entirely broken for now
CountingMeasure(Float64)
Likelihood
Dirac(0.0) + Normal()

TrivialMeasure()
]

@testset "testvalue" begin
for μ in test_measures
@test test_measure(μ)
end

for μ in testbroken_measures
@test_broken test_measure(μ)
end

@testset "testvalue(::Chain)" begin
mc = Chain(x -> Normal=x), Normal=0.0))
r = testvalue(mc)
@test logdensity(mc, Iterators.take(r, 10)) isa AbstractFloat
end
end

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

2 comments on commit 12a6c19

@cscherrer
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/42937

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.2 -m "<description of version>" 12a6c19a37a8b8d179b3314b4a9bee757611e4e5
git push origin v0.10.2

Please sign in to comment.