Skip to content
Snippets Groups Projects
Commit 26996aaf authored by Luca Lenz's avatar Luca Lenz
Browse files

fixed tests

parent 1f90090b
No related branches found
No related tags found
No related merge requests found
......@@ -6,10 +6,12 @@ using AbstractMCMC
using Revise
using MultilevelChainSampler
@testset "hastings" begin
# behaves like truncation on cyclic unit interval
t = LogDensity(Normal(0,1))
p = CyclicWalk(-1, 1, .1)
p = StaticProposal(Uniform(-1, 1))
# ignore log probability
mh = MH(p)
......@@ -38,44 +40,44 @@ using MultilevelChainSampler
sample(t, mh, 100)
end
@testset "delayed_acceptance" begin
p = CyclicWalk(-1, 1, .1)
p2 = vcat(p,p)
energy(x::Tuple) = -sum(x.^2)
t = LogDensity(energy)
p = StaticProposal(Normal(0,1))
t = LogDensity(x->-sum(x.^2))
# sample from two level, ignore subchains
da = MLDA(p2, (2,))
da = MLDA([p,p], [2,])
c = sample(t, da, 100)
@test length(c) == 100
@test length(c[1]) == 2
@test typeof(c[1]) == NTuple{2,Float64}
# save save logprobs
da = MLDA(p2, (3,), true, false)
da = MLDA([p,p], [3,]; save_logprob=true)
c = sample(t, da, 100)
@test length(c) == 100
@test length(c[1]) == 2
@test typeof(c[1]) == Tuple{Tuple{Float64, Float64}, Float64}
# save subchains
da = MLDA(p2, (3,); save_subchain=true)
da = MLDA([p,p], [3,]; save_subchain=true)
c = sample(t, da, 100; discard_initial=1)
@test typeof(c) <: Vector{<:Vector}
@test all( length.(c) .== 100 .* size(da) )
# sample from three level
p3 = vcat(p,p,p)
da = MLDA(p3, (2,3), true; save_subchain=true)
da = MLDA([p,p,p], [2,3]; save_subchain=true)
c = sample(t, da, 100, discard_initial=1)
@test typeof(c) <: Vector{<:Vector}
@test all( length.(c) .== 100 .* size(da) )
da = MLDA(p3, (3,2), false, true; save_subchain=true)
da = MLDA([p,p,p], [3,2]; save_reject=true, save_subchain=true)
c = sample(t, da, 100, discard_initial=1)
@test length(c) == 3
@test all( length.(c) .== 100 .* size(da) )
@test all( typeof.(c[1]) .== [ Tuple{Float64, Bool } ] )
......@@ -84,88 +86,45 @@ end
x2 = first.(c[2][1:2:end])
x3 = first.(c[3][1:1:end])
x = [ zip(x1,x2,x3) ... ]
rej_rates = mean.(map(t->last.(t), c))
println("Rejection rates ", rej_rates)
#println("Rejection rates ", rej_rates)
last_state = last.(c);
x = first.(last_state)
@test all( eltype.(c) .== Tuple{Float64,Bool} )
# random subchain length
d = truncated(Poisson(), 1, nothing)
da = MLDA(p2, (d,), false, false; save_subchain=true)
da = MLDA([p,p], [d,]; save_subchain=true)
c = sample(t, da, 100, discard_initial=1)
@test all( typeof.(c) == [ Vector{Tuple{Float64}}, Vector{Tuple{Float64, Int}}] )
@test length(c[2]) == 100
@test all( unique( last.(c[2]) ) .== last.(c[2]) )
da = MLDA(p2, (d,), true, false; save_subchain=true)
d = truncated(Poisson(), 1, nothing)
da = MLDA([p,p], [d,]; save_logprob=true, save_subchain=true)
c = sample(t, da, 100, discard_initial=1)
@test all( typeof.(c) == [ Vector{Tuple{Float64,Float64}}, Vector{Tuple{Float64, Float64, Int}}] )
@test length(c[2]) == 100
@test issorted(last.(c[2]))
@test all( unique( last.(c[2]) ) .== last.(c[2]) )
da = MLDA(p2, (d,), false, true; save_subchain=true)
c = sample(t, da, 100, discard_initial=1)
@test all( typeof.(c) == [ Vector{Tuple{Float64, Bool}}, Vector{Tuple{Float64, Bool, Int}}] )
@test length(c[2]) == 100
@test issorted(last.(c[2]))
@test all( unique( last.(c[2]) ) .== last.(c[2]) )
da = MLDA(p2, (d,), true, true; save_subchain=true)
d = truncated(Poisson(), 1, nothing)
da = MLDA([p,p], [d,]; save_reject=true, save_subchain=true)
c = sample(t, da, 100, discard_initial=1)
@test all( typeof.(c) == [ Vector{Tuple{Float64,Float64,Bool}}, Vector{Tuple{Float64, Float64,Bool, Int}}] )
@test all( typeof.(c) == [ Vector{Tuple{Float64,Bool}}, Vector{Tuple{Float64, Bool, Int}}] )
@test length(c[2]) == 100
@test issorted(last.(c[2]))
@test all( unique( last.(c[2]) ) .== last.(c[2]) )
da = MLDA(p3, (d,d,), false, false; save_subchain=true)
c = sample(t, da, 100, discard_initial=1)
@test all( typeof.(c) == [ Vector{Tuple{Float64}}, Vector{Tuple{Float64, Int}}, Vector{Tuple{Float64, Int}}] )
@test length(c[3]) == 100
@test issorted(last.(c[2])) && issorted(last.(c[3]))
@test all( unique( last.(c[2]) ) .== last.(c[2]) ) && all( unique( last.(c[3]) ) .== last.(c[3]) )
da = MLDA(p3, (d,d,), false, true; save_subchain=true)
d = truncated(Poisson(), 1, nothing)
da = MLDA([p,p,p], [d,d]; save_reject=true, save_subchain=true)
c = sample(t, da, 100, discard_initial=1)
println(typeof.(c))
@test all( typeof.(c) .== [ Vector{Tuple{Float64, Bool}}, Vector{Tuple{Float64, Bool, Int}}, Vector{Tuple{Float64, Bool, Int}}] )
@test all( typeof.(c) == [ Vector{Tuple{Float64,Bool}}, Vector{Tuple{Float64,Bool, Int}}, Vector{Tuple{Float64, Bool, Int}}] )
@test length(c[3]) == 100
@test issorted(last.(c[2])) && issorted(last.(c[3]))
@test all( unique( last.(c[2]) ) .== last.(c[2]) ) && all( unique( last.(c[3]) ) .== last.(c[3]) )
end
@testset "chains" begin
energy(x::Tuple) = -sum(x.^2)
t = LogDensity(energy)
p = CyclicWalk(-1, 1, .1)
p2 = vcat(p,p)
# bundle samples as MultilevelChains
da = MLDA(p2, (3,); save_subchain=true)
c = sample(t, da, 100; chain_type=MultilevelChains, discard_initial=1)
@test typeof(c) <: MultilevelChains
s1 = subchain(c,1)
s2 = subchain(c,2)
@test length(s1) == 300
@test length(s2) == 100
@test eltype(s2) == Tuple{Float64, Float64}
#=
mlc = sample(t, da, 100 + 1; chain_type=MultilevelChains)
@test typeof(mlc) <: MultilevelChains
@test length(subchain(mlc,1)) == 600 + 1
@test length(subchain(mlc,2)) == 300 + 1
@test length(subchain(mlc,3)) == 100 + 1
=#
end
......@@ -7,7 +7,7 @@ using AbstractMCMC
using Revise
using MultilevelChainSampler
@testset "proposal" begin
@testset "proposals" begin
function propose_chain(p, n=100)
x = [propose(p)]
for i in 1:n
......@@ -22,24 +22,29 @@ using MultilevelChainSampler
@test ( mean(c), 0.0 , atol=0.1 )
@test ( std(c), 1.0 , atol=0.1 )
# sample from cyclic unit interval should behave like a uniform
p = CyclicWalk(0,1, .1)
# multi proposal
p = stack(
StaticProposal(Normal(0,1)),
StaticProposal(Normal(0,1))
)
c = propose_chain(p, 10000)
@test ( mean(c) , .5 , atol=0.1 )
@test ( var(c) , var(Uniform()), atol=0.1 )
@test ( mean(c) , [0.0, 0.0] , atol=0.1 )
@test ( std(c) , [1.0, 1.0] , atol=0.1 )
end
@testset "hastings" begin
# behaves like truncation on cyclic unit interval
t = LogDensity(Normal(0,1))
p = CyclicWalk(-1, 1, .1)
t = LogDensity(x->-sum(x.^2 /2))
cycle(a,b) = t -> @. mod(t-a, b-a) + a
CyclicWalk(a,b,s) = TransformedProposal(cycle(a,b), RandomWalk(Uniform(a,b), Uniform(-s,s)))
p = CyclicWalk(-1, 1, .25)
# save log probability
mh = MetropolisHastings(p, false, false)
c = sample(t, mh, 100000)
x = getfield.(c, :x)
mh = MH(p, false, false)
x = sample(t, mh, 100000)
@test length(x) == 100000
@test ( mean(x), 0.0 , atol=0.1 )
@test ( std(x), std(TruncatedNormal(0,1,-1,1)) , atol=0.1 )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment