Skip to content

Commit

Permalink
Update tests, use link or LogDensityFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxd3 committed Sep 7, 2023
1 parent f20d378 commit 783c063
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/JuliaBUGS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function compile(model_def::Expr, data, inits)
vars, array_sizes, array_bitmap, link_functions, node_args, node_functions, dependencies = program!(
NodeFunctions(vars, array_sizes, array_bitmap), model_def, merged_data
)
g = BUGSGraph(vars, link_functions, node_args, node_functions, dependencies)
g = BUGSGraph(vars, node_args, node_functions, dependencies)
sorted_nodes = map(Base.Fix1(label_for, g), topological_sort(g))
return BUGSModel(g, sorted_nodes, vars, array_sizes, merged_data, inits)
end
Expand Down
8 changes: 4 additions & 4 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function AbstractPPL.evaluate!!(model::BUGSModel, ::DefaultContext)
value_transformed = transform(bijector(dist), value)
logp +=
logpdf(dist, value) +
logabsdetjac(inverse(bijector(dist)), value_transformed)
logabsdetjac(Bijectors.inverse(bijector(dist)), value_transformed)
else
logp += logpdf(dist, value)
end
Expand Down Expand Up @@ -286,7 +286,7 @@ function AbstractPPL.evaluate!!(
current_idx += l
# TODO: this use `DynamicPPL.reconstruct`, which needs attention when decoupling from DynamicPPL
value, logjac = DynamicPPL.with_logabsdet_jacobian_and_reconstruct(
inverse(bijector(dist)), dist, value_transformed
Bijectors.inverse(bijector(dist)), dist, value_transformed
)
logp += logpdf(dist, value) + logjac
vi = setindex!!(vi, value, vn)
Expand Down Expand Up @@ -332,7 +332,7 @@ function AbstractPPL.evaluate!!(model::MarkovBlanketCoveredBUGSModel, ::DefaultC
value_transformed = transform(bijector(dist), value)
logp +=
logpdf(dist, value) +
logabsdetjac(inverse(bijector(dist)), value_transformed)
logabsdetjac(Bijectors.inverse(bijector(dist)), value_transformed)
else
logp += logpdf(dist, value)
end
Expand Down Expand Up @@ -372,7 +372,7 @@ function AbstractPPL.evaluate!!(
current_idx += l
# TODO: this use `DynamicPPL.reconstruct`, which needs attention when decoupling from DynamicPPL
value, logjac = DynamicPPL.with_logabsdet_jacobian_and_reconstruct(
inverse(bijector(dist)), dist, value_transformed
Bijectors.inverse(bijector(dist)), dist, value_transformed
)
logp += logpdf(dist, value) + logjac
vi = setindex!!(vi, value, vn)
Expand Down
6 changes: 3 additions & 3 deletions test/logp_tests/binomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dppl_model = dppl_gamma_model()

bugs_logp =
JuliaBUGS.evaluate!!(
DynamicPPL.settrans!!(bugs_model, false), DefaultBUGSContext()
DynamicPPL.settrans!!(bugs_model, false), DefaultContext()
).logp
params_vi = JuliaBUGS.get_params_varinfo(bugs_model, vi)
# test if JuliaBUGS and DynamicPPL agree on parameters in the model
Expand All @@ -29,7 +29,7 @@ t_p = DynamicPPL.LogDensityFunction(
@test bugs_logp LogDensityProblems.logdensity(p, [10.0]) rtol = 1E-6

bugs_logp =
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultBUGSContext()).logp
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultContext()).logp
@test bugs_logp
LogDensityProblems.logdensity(t_p, [transform(bijector(dbin(0.1, 10)), 10.0)]) rtol =
1E-6
1E-6
8 changes: 4 additions & 4 deletions test/logp_tests/blockers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ data = JuliaBUGS.BUGSExamples.VOLUME_I[:blockers].data
inits = JuliaBUGS.BUGSExamples.VOLUME_I[:blockers].inits[1]

bugs_model = compile(bugs_model_def, data, inits)
vi = JuliaBUGS.get_varinfo(bugs_model)
vi = bugs_model.varinfo

@model function blockers(rc, rt, nc, nt, Num)
d ~ dnorm(0.0, 1.0E-6)
Expand Down Expand Up @@ -36,7 +36,7 @@ dppl_model = blockers(rc, rt, nc, nt, Num)

bugs_logp =
JuliaBUGS.evaluate!!(
DynamicPPL.settrans!!(bugs_model, false), DefaultBUGSContext()
DynamicPPL.settrans!!(bugs_model, false), DefaultContext()
).logp
params_vi = JuliaBUGS.get_params_varinfo(bugs_model, vi)
# test if JuliaBUGS and DynamicPPL agree on parameters in the model
Expand All @@ -50,11 +50,11 @@ dppl_logp =
@test bugs_logp dppl_logp rtol = 1E-6

bugs_logp =
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultBUGSContext()).logp
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultContext()).logp
dppl_logp =
DynamicPPL.evaluate!!(
dppl_model,
DynamicPPL.settrans!!(get_params_varinfo(bugs_model), true),
DynamicPPL.link!!(get_params_varinfo(bugs_model), dppl_model),
DynamicPPL.DefaultContext(),
)[2].logp
@test bugs_logp dppl_logp rtol = 1E-6
8 changes: 4 additions & 4 deletions test/logp_tests/bones.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ data = JuliaBUGS.BUGSExamples.VOLUME_I[:bones].data
inits = JuliaBUGS.BUGSExamples.VOLUME_I[:bones].inits[1]

bugs_model = compile(bugs_model_def, data, inits)
vi = JuliaBUGS.get_varinfo(bugs_model)
vi = bugs_model.varinfo

@model function bones(grade, nChild, nInd, ncat, gamma, delta)
theta = Vector{Real}(undef, nChild)
Expand Down Expand Up @@ -38,7 +38,7 @@ dppl_model = bones(grade, nChild, nInd, ncat, gamma, delta)

bugs_logp =
JuliaBUGS.evaluate!!(
DynamicPPL.settrans!!(bugs_model, false), DefaultBUGSContext()
DynamicPPL.settrans!!(bugs_model, false), DefaultContext()
).logp
params_vi = JuliaBUGS.get_params_varinfo(bugs_model, vi)
# test if JuliaBUGS and DynamicPPL agree on parameters in the model
Expand All @@ -52,11 +52,11 @@ dppl_logp =
@test bugs_logp dppl_logp rtol = 1E-6

bugs_logp =
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultBUGSContext()).logp
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultContext()).logp
dppl_logp =
DynamicPPL.evaluate!!(
dppl_model,
DynamicPPL.settrans!!(get_params_varinfo(bugs_model), true),
DynamicPPL.link!!(get_params_varinfo(bugs_model), dppl_model),
DynamicPPL.DefaultContext(),
)[2].logp
@test bugs_logp dppl_logp rtol = 1E-6
Empty file removed test/logp_tests/ddirich.jl
Empty file.
8 changes: 4 additions & 4 deletions test/logp_tests/dogs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ data = JuliaBUGS.BUGSExamples.VOLUME_I[:dogs].data
inits = JuliaBUGS.BUGSExamples.VOLUME_I[:dogs].inits[1]

bugs_model = compile(bugs_model_def, data, inits)
vi = JuliaBUGS.get_varinfo(bugs_model)
vi = bugs_model.varinfo

@model function dogs(Dogs, Trials, Y, y)
# Initialize matrices
Expand Down Expand Up @@ -41,7 +41,7 @@ dppl_model = dogs(Dogs, Trials, Y, 1 .- Y)

bugs_logp =
JuliaBUGS.evaluate!!(
DynamicPPL.settrans!!(bugs_model, false), DefaultBUGSContext()
DynamicPPL.settrans!!(bugs_model, false), DefaultContext()
).logp

dppl_logp =
Expand All @@ -52,11 +52,11 @@ dppl_logp =
@test bugs_logp dppl_logp rtol = 1E-6

bugs_logp =
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultBUGSContext()).logp
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultContext()).logp
dppl_logp =
DynamicPPL.evaluate!!(
dppl_model,
DynamicPPL.settrans!!(get_params_varinfo(bugs_model), true),
DynamicPPL.link!!(get_params_varinfo(bugs_model), dppl_model),
DynamicPPL.DefaultContext(),
)[2].logp
@test bugs_logp dppl_logp rtol = 1E-6
Empty file removed test/logp_tests/dwish.jl
Empty file.
4 changes: 2 additions & 2 deletions test/logp_tests/gamma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dppl_model = dppl_gamma_model()

bugs_logp =
JuliaBUGS.evaluate!!(
DynamicPPL.settrans!!(bugs_model, false), DefaultBUGSContext()
DynamicPPL.settrans!!(bugs_model, false), DefaultContext()
).logp
params_vi = JuliaBUGS.get_params_varinfo(bugs_model, vi)
# test if JuliaBUGS and DynamicPPL agree on parameters in the model
Expand All @@ -30,7 +30,7 @@ t_p = DynamicPPL.LogDensityFunction(
@test bugs_logp LogDensityProblems.logdensity(p, [10.0]) rtol = 1E-6

bugs_logp =
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultBUGSContext()).logp
JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultContext()).logp
@test bugs_logp
LogDensityProblems.logdensity(t_p, [transform(bijector(dgamma(0.001, 0.001)), 10.0)]) rtol =
1E-6
Empty file removed test/logp_tests/lkj.jl
Empty file.
21 changes: 17 additions & 4 deletions test/logp_tests/rats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ model_def = @bugs begin
alpha0 = alpha_c - xbar * beta_c
end
bugs_model = compile(model_def, data, inits);
vi = bugs_model.varinfo

@model function rats(Y, x, xbar, N, T)
tau_c ~ dgamma(0.001, 0.001)
Expand Down Expand Up @@ -53,15 +54,27 @@ bugs_model = compile(model_def, data, inits);
end
dppl_model = rats(Y, x, xbar, N, T)

vi, bugs_logp = get_vi_logp(bugs_model, false)
bugs_model = DynamicPPL.settrans!!(bugs_model, false)
bugs_logp = JuliaBUGS.evaluate!!(bugs_model, DefaultContext()).logp
params_vi = JuliaBUGS.get_params_varinfo(bugs_model, vi)
# test if JuliaBUGS and DynamicPPL agree on parameters in the model
@test params_in_dppl_model(dppl_model) == keys(params_vi)

vi, dppl_logp = get_vi_logp(dppl_model, vi, false)
dppl_logp =
DynamicPPL.evaluate!!(
dppl_model, DynamicPPL.settrans!!(vi, false), DynamicPPL.DefaultContext()
)[2].logp
@test bugs_logp -174029.387 rtol = 1E-6 # reference value from ProbPALA
@test bugs_logp dppl_logp rtol = 1E-6

vi, bugs_logp = get_vi_logp(bugs_model, true)
vi, dppl_logp = get_vi_logp(dppl_model, vi, true)
dppl_logp =
DynamicPPL.evaluate!!(
dppl_model,
link!!(get_params_varinfo(bugs_model), dppl_model),
DynamicPPL.DefaultContext(),
)[2].logp
bugs_logp = JuliaBUGS.evaluate!!(DynamicPPL.settrans!!(bugs_model, true), DefaultContext()).logp
@test bugs_logp dppl_logp rtol = 1E-6

@test bugs_model.param_length ==
LogDensityProblems.dimension(DynamicPPL.LogDensityFunction(dppl_model))
6 changes: 2 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using JuliaBUGS:
Stochastic,
Logical,
evaluate!!,
DefaultBUGSContext,
DefaultContext,
BUGSGraph,
stochastic_neighbors,
stochastic_inneighbors,
Expand All @@ -26,9 +26,7 @@ using JuliaBUGS:
LogDensityContext,
ConcreteNodeInfo,
SimpleVarInfo,
get_params_varinfo,
get_varinfo,
transformation
get_params_varinfo
using JuliaBUGS.BUGSPrimitives
using JuliaBUGS.BUGSPrimitives: mean
using LogDensityProblems, LogDensityProblemsAD
Expand Down

0 comments on commit 783c063

Please sign in to comment.