Skip to content

Commit

Permalink
A quick refactoring to reduce repeated code (#251)
Browse files Browse the repository at this point in the history
use `_tempered_evalaute!!` function introduced in
#247 to reduce duplicated
code
  • Loading branch information
sunxd3 authored Dec 9, 2024
1 parent bac2171 commit 859e2b9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "JuliaBUGS"
uuid = "ba9fb4c0-828e-4473-b6a1-cd2560fee5bf"
version = "0.7.3"
version = "0.7.4"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
51 changes: 4 additions & 47 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -491,53 +491,10 @@ function AbstractPPL.evaluate!!(model::BUGSModel)
end

function AbstractPPL.evaluate!!(model::BUGSModel, flattened_values::AbstractVector)
var_lengths = if model.transformed
model.transformed_var_lengths
else
model.untransformed_var_lengths
end

evaluation_env = deepcopy(model.evaluation_env)
current_idx = 1
logp = 0.0
for (i, vn) in enumerate(model.flattened_graph_node_data.sorted_nodes)
is_stochastic = model.flattened_graph_node_data.is_stochastic_vals[i]
is_observed = model.flattened_graph_node_data.is_observed_vals[i]
node_function = model.flattened_graph_node_data.node_function_vals[i]
loop_vars = model.flattened_graph_node_data.loop_vars_vals[i]
if !is_stochastic
value = node_function(evaluation_env, loop_vars)
evaluation_env = BangBang.setindex!!(evaluation_env, value, vn)
else
dist = node_function(evaluation_env, loop_vars)
if !is_observed
l = var_lengths[vn]
if model.transformed
b = Bijectors.bijector(dist)
b_inv = Bijectors.inverse(b)
reconstructed_value = reconstruct(
b_inv,
dist,
view(flattened_values, current_idx:(current_idx + l - 1)),
)
value, logjac = Bijectors.with_logabsdet_jacobian(
b_inv, reconstructed_value
)
else
value = reconstruct(
dist, view(flattened_values, current_idx:(current_idx + l - 1))
)
logjac = 0.0
end
current_idx += l
logp += logpdf(dist, value) + logjac
evaluation_env = BangBang.setindex!!(evaluation_env, value, vn)
else
logp += logpdf(dist, AbstractPPL.get(evaluation_env, vn))
end
end
end
return evaluation_env, logp
evaluation_env, (logprior, loglikelihood, tempered_logjoint) = _tempered_evaluate!!(
model, flattened_values; temperature=1.0
)
return evaluation_env, tempered_logjoint
end

"""
Expand Down

2 comments on commit 859e2b9

@sunxd3
Copy link
Member Author

@sunxd3 sunxd3 commented on 859e2b9 Dec 9, 2024

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/121037

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.7.4 -m "<description of version>" 859e2b981a6213cdd046ead6d3ad000c4bdce3fc
git push origin v0.7.4

Please sign in to comment.