@@ -431,9 +431,8 @@ function check_var_group(var_group::Vector{<:VarName}, model::BUGSModel)
431
431
end
432
432
433
433
function AbstractPPL. evaluate!! (rng:: Random.AbstractRNG , model:: BUGSModel )
434
- (; evaluation_env, g) = model
435
- vi = deepcopy (evaluation_env)
436
434
logp = 0.0
435
+ evaluation_env = deepcopy (model. evaluation_env)
437
436
for (i, vn) in enumerate (model. flattened_graph_node_data. sorted_nodes)
438
437
is_stochastic = model. flattened_graph_node_data. is_stochastic_vals[i]
439
438
node_function = model. flattened_graph_node_data. node_function_vals[i]
@@ -444,7 +443,16 @@ function AbstractPPL.evaluate!!(rng::Random.AbstractRNG, model::BUGSModel)
444
443
else
445
444
dist = node_function (model. evaluation_env, loop_vars)
446
445
value = rand (rng, dist) # just sample from the prior
447
- logp += logpdf (dist, value)
446
+ if model. transformed
447
+ # see below for why we need to transform the value
448
+ value_transformed = Bijectors. transform (Bijectors. bijector (dist), value)
449
+ logp +=
450
+ Distributions. logpdf (dist, value) + Bijectors. logabsdetjac (
451
+ Bijectors. inverse (Bijectors. bijector (dist)), value_transformed
452
+ )
453
+ else
454
+ logp += Distributions. logpdf (dist, value)
455
+ end
448
456
evaluation_env = setindex!! (evaluation_env, value, vn)
449
457
end
450
458
end
@@ -467,6 +475,8 @@ function AbstractPPL.evaluate!!(model::BUGSModel)
467
475
if model. transformed
468
476
# although the values stored in `evaluation_env` are in their original space,
469
477
# here we behave as accepting a vector of parameters in the transformed space
478
+ # this is so that we have consistent logp values between
479
+ # (1) set values in original space then evaluate (2) directly evaluate with the values in transformed space
470
480
value_transformed = Bijectors. transform (Bijectors. bijector (dist), value)
471
481
logp +=
472
482
Distributions. logpdf (dist, value) + Bijectors. logabsdetjac (
0 commit comments