Skip to content

Commit

Permalink
add regression test against master
Browse files Browse the repository at this point in the history
  • Loading branch information
xukai92 committed Aug 5, 2020
1 parent 07236bf commit 31c6517
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
.history
.DS_Store
Manifest.toml
test/Project.toml
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"

[targets]
test = ["Distributed", "Distributions", "ForwardDiff", "Plots", "MCMCDebugging", "Test", "Turing", "UnicodePlots", "Bijectors", "OrdinaryDiffEq", "Zygote"]
test = ["Distributed", "Distributions", "ForwardDiff", "Plots", "MCMCDebugging", "BSON", "Test", "Turing", "UnicodePlots", "Bijectors", "OrdinaryDiffEq", "Zygote"]
63 changes: 63 additions & 0 deletions test/regression.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Test, Random, BSON, AdvancedHMC, Distributions, ForwardDiff

is_ef6de39 = isdefined(AdvancedHMC, :EndPointTS)

function simulate_and_compare(hmc_variant)
filepath_ef6de39 = "$(@__DIR__)/regression/ef6de39/$hmc_variant.bson"

Random.seed!(1)

dim = 5; θ₀ = rand(dim)

ℓπ(θ) = logpdf(MvNormal(zeros(dim), ones(dim)), θ)

n_samples, n_adapts = 2_000, 1_000

metric = DiagEuclideanMetric(dim)
hamiltonian = Hamiltonian(metric, ℓπ, ForwardDiff)

ϵ₀ = find_good_stepsize(hamiltonian, θ₀)
integrator = Leapfrog(ϵ₀)

if is_ef6de39
κ = if hmc_variant == "hmc_mh"
StaticTrajectory{EndPointTS}(integrator, 10)
elseif hmc_variant == "hmc_multi"
StaticTrajectory{MultinomialTS}(integrator, 10)
elseif hmc_variant == "nuts_slice"
NUTS{SliceTS, GeneralisedNoUTurn}(integrator)
elseif hmc_variant == "nuts_multi"
NUTS{MultinomialTS, GeneralisedNoUTurn}(integrator)
end
else
κ = if hmc_variant == "hmc_mh"
HMCKernel(Trajectory(integrator, FixedNSteps(10)), MetropolisTS)
elseif hmc_variant == "hmc_multi"
HMCKernel(Trajectory(integrator, FixedNSteps(10)), MultinomialTS)
elseif hmc_variant == "nuts_slice"
HMCKernel(Trajectory(integrator, NoUTurn()), SliceTS)
elseif hmc_variant == "nuts_multi"
HMCKernel(Trajectory(integrator, NoUTurn()), MultinomialTS)
end
end

adaptor = StanHMCAdaptor(MassMatrixAdaptor(metric), StepSizeAdaptor(0.8, ϵ₀))

samples, stats = sample(
hamiltonian, κ, θ₀, n_samples, adaptor, n_adapts; verbose=false, progress=false
)

is_ef6de39 && return BSON.bson(filepath_ef6de39, samples=samples, stats=stats)

bson_ef6de39 = BSON.load(filepath_ef6de39)
@test samples == bson_ef6de39[:samples]
@test stats == bson_ef6de39[:stats]
end

if "REGRESSION_TEST" in keys(ENV) && ENV["REGRESSION_TEST"] == "1"
@testset "Regression" begin
for hmc_variant in ["hmc_mh", "hmc_multi", "nuts_slice", "nuts_multi"]
simulate_and_compare(hmc_variant)
end
end
end
Binary file added test/regression/ef6de39/hmc_mh.bson
Binary file not shown.
Binary file added test/regression/ef6de39/hmc_multi.bson
Binary file not shown.
Binary file added test/regression/ef6de39/nuts_multi.bson
Binary file not shown.
Binary file added test/regression/ef6de39/nuts_slice.bson
Binary file not shown.
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ println(ENV)
"integrator",
"trajectory",
"adaptation",
"regression",
"sampler",
"sampler-vec",
"demo",
Expand Down

0 comments on commit 31c6517

Please sign in to comment.