Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor graph and model part of the code #80

Closed
wants to merge 23 commits into from
Closed

Conversation

sunxd3
Copy link
Member

@sunxd3 sunxd3 commented Sep 3, 2023

This PR is aim to address the following points:

  • Crude interfaces for NodeInfo, Model, and Context
    • NodeInfo
      • eval function for NodeInfo should hide the field of NodeInfo, this allows storing a Function instead of just Expr in the NodeInfo by implementing a eval function for new NodeInfo subtypes
      • a Module can be passed to eval, which will allow more freedom in the evaluation, this is not yet exposed
    • Model
      • some interface functions like get_graph, transformation, node_iterator are implemented so that future model can share default implementation of evaluate!!
    • Context
      • observe, assume, and logical_evaluate are implemented; given an order defined by node_iterator, one of these functions will be called on the nodes
      • LogDensityContext's evaluate!! is still implemented separately, maybe we can unify the interface further in the future
  • Fix compatibility with trans-dimensional bijectors
    • When initializing the Model, two param_lengths are computed, one for without transformation, the other will consider the size of the transformed distribution
  • Link function in stochastic assignment is (temporarily) disabled
    • This feature was not well tested before
    • This is a feature supported only by nimble, not by the original BUGS
    • There are some implementation complexity mainly from the fact that link function are essentially used specified bijectors, but since we have random variable transformation mechanism already (via Bijectors), this is not essential
  • Model definitions are separated into another file
  • Tests are also partly refactored
    • We do not rely on change behavior of DynamicPPL anymore, instead, now we create a VarInfo whose values are transformed
    • Also use the LogDensityFunction interface at points

@sunxd3 sunxd3 requested a review from yebai September 3, 2023 10:06
@sunxd3
Copy link
Member Author

sunxd3 commented Sep 3, 2023

@yebai apologize for another PR with multiple changes, I hope it's not too confusing.

@codecov
Copy link

codecov bot commented Sep 3, 2023

Codecov Report

Patch coverage: 88.69% and project coverage change: +5.28% 🎉

Comparison is base (ec81b3e) 69.20% compared to head (568c397) 74.49%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #80      +/-   ##
==========================================
+ Coverage   69.20%   74.49%   +5.28%     
==========================================
  Files          17       19       +2     
  Lines        1640     1635       -5     
==========================================
+ Hits         1135     1218      +83     
+ Misses        505      417      -88     
Files Changed Coverage Δ
src/JuliaBUGS.jl 94.87% <ø> (ø)
src/compiler_pass.jl 90.90% <ø> (ø)
src/parser.jl 92.69% <ø> (ø)
src/node_functions.jl 91.90% <66.66%> (-0.50%) ⬇️
src/model.jl 84.97% <84.97%> (ø)
src/utils.jl 95.12% <95.12%> (ø)
ext/JuliaBUGSAdvancedHMCExt.jl 100.00% <100.00%> (+100.00%) ⬆️
src/graphs.jl 88.48% <100.00%> (+17.31%) ⬆️
src/logdensityproblems.jl 71.42% <100.00%> (+71.42%) ⬆️
src/variable_types.jl 78.57% <100.00%> (+2.57%) ⬆️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sunxd3 sunxd3 marked this pull request as draft September 3, 2023 10:38
src/model.jl Outdated Show resolved Hide resolved
src/graphs.jl Outdated Show resolved Hide resolved
src/model.jl Outdated Show resolved Hide resolved
src/model.jl Outdated Show resolved Hide resolved
src/model.jl Outdated Show resolved Hide resolved
test/logp_tests/lkj.jl Outdated Show resolved Hide resolved
test/logp_tests/lkj.jl Outdated Show resolved Hide resolved
test/logp_tests/rats.jl Outdated Show resolved Hide resolved
test/logp_tests/rats.jl Outdated Show resolved Hide resolved
test/logp_tests/rats.jl Outdated Show resolved Hide resolved
test/logp_tests/rats.jl Outdated Show resolved Hide resolved
test/logp_tests/rats.jl Outdated Show resolved Hide resolved
Copy link
Member

@yebai yebai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very big PR; I reviewed half of it. I'll try to complete it tomorrow.

@@ -1,6 +1,6 @@
name = "JuliaBUGS"
uuid = "ba9fb4c0-828e-4473-b6a1-cd2560fee5bf"
version = "0.1.0"
version = "0.1.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version = "0.1.1"
version = "0.2"

@@ -197,7 +201,7 @@ n_samples, n_adapts = 2000, 1000

D = LogDensityProblems.dimension(model); initial_θ = rand(D)

samples_and_stats = AbstractMCMC.sample(
@run samples_and_stats = AbstractMCMC.sample(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@run samples_and_stats = AbstractMCMC.sample(
samples_and_stats = AbstractMCMC.sample(

@@ -0,0 +1,10 @@
# Evaluation of `BUGSModel`

During model evaluation, `varinfo` serves as the runtime environment, housing the current values of variables.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
During model evaluation, `varinfo` serves as the runtime environment, housing the current values of variables.
During model evaluation, `varinfo` stores the current variables' values.

# Evaluation of `BUGSModel`

During model evaluation, `varinfo` serves as the runtime environment, housing the current values of variables.
These values are always stored in their native (possibly constraint) spaces.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
These values are always stored in their native (possibly constraint) spaces.
These variables are always stored as-is, i.e. without transformation.


During model evaluation, `varinfo` serves as the runtime environment, housing the current values of variables.
These values are always stored in their native (possibly constraint) spaces.
The `varinfo` represents the state of program evaluation at different evaluation points, given a chosen order of node evaluation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `varinfo` represents the state of program evaluation at different evaluation points, given a chosen order of node evaluation.
The `varinfo` represents the state of program evaluation at different evaluation points, given a chosen ordering of node evaluations.

Comment on lines +29 to +38
@enum VariableTypes::Bool begin
Logical
Stochastic
end

@enum StochasticVariableTypes::Bool begin
Observation
Assumption
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe merge these two types:

@enum VariableTypes Logical Observation Assumption

"""
eval([m::Module, ]ni::ConcreteNodeInfo, vi)

Evaluate a node under a specified module `m`. If no module is provided, the default module used is JuliaBUGS.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Evaluating nodes in JuliaBUGS sounds terrible; why not default to Main?


"""
LogDensityContext
Find the Markov blanket of `v` in `g`. `v` can be a single `VarName` or a vector of `VarName`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Find the Markov blanket of `v` in `g`. `v` can be a single `VarName` or a vector of `VarName`.
Find the Markov blanket of variable(s) `v` in graph `g`. `v` can be a single `VarName` or a vector of `VarName`.

else
logp += logpdf(dist, value)
end
function markov_blanket(g, v::VarName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function markov_blanket(g, v::VarName)
function markov_blanket(g::BUGSGraph, v::VarName)

logp += logpdf(dist, vi[vn])
end
end
function markov_blanket(g, v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function markov_blanket(g, v)
function markov_blanket(g::BUGSGraph,, v::Vector{VarName})

@yebai
Copy link
Member

yebai commented Sep 4, 2023

Please add some tests #66 and #71

@sunxd3
Copy link
Member Author

sunxd3 commented Sep 5, 2023

Thanks @yebai for the reviews, given this size of the PR and difficulty of fully reviewing, I will break it into a series of smaller PRs.
The review that are already proposed will be reflected.

@sunxd3 sunxd3 marked this pull request as draft September 5, 2023 06:48
@sunxd3
Copy link
Member Author

sunxd3 commented Sep 17, 2023

This PR has been broken down into #86, #88, #89, #90

@sunxd3 sunxd3 closed this Sep 17, 2023
@yebai yebai deleted the sunxd-refactor-eval branch September 17, 2023 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants