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

Support for combining models #14

Merged
merged 12 commits into from
Mar 5, 2024
Merged

Support for combining models #14

merged 12 commits into from
Mar 5, 2024

Conversation

richfitz
Copy link
Member

@richfitz richfitz commented Mar 1, 2024

This PR adds support for combining models via adding their log densities. The basic idea is to support the fairly common case that we have a pair of models, one representing the (log) priors and the other representing the (log) likelihood that we want to join together to make a posterior. We can then construct these however we want and combine them with mcstate_model_combine (or + which is syntactic sugar for this).

The way that different model components combine is described in some detail in the docs but briefly:

  • parameters become the union over both models
  • domains take the most restrictive per parameter
  • densities are summed on the log scale
  • gradients are also summed on the log scale, assuming (correctly) zero gradient for variables missing from a component model
  • direct sample can only be included from a single model

Because I was finding it hard to work with, I've changed domain slightly to include parameter names.

Copy link

codecov bot commented Mar 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (06322fa) to head (08c60e3).

❗ Current head 08c60e3 differs from pull request most recent head 5fbb465. Consider uploading reports for the commit 5fbb465 to get more accurate results

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #14   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           39        40    +1     
  Lines         1689      1689           
=========================================
  Hits          1689      1689           

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

@richfitz richfitz marked this pull request as ready for review March 4, 2024 08:33
Copy link
Contributor

@M-Kusumgar M-Kusumgar left a comment

Choose a reason for hiding this comment

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

Nice one, was pretty clear, couple small suggestions!

R/combine.R Outdated
call = call)
}

## Next we need to check that the model with a sample has the full set of
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
## Next we need to check that the model with a sample has the full set of
## Next we need to check that the model with a sample has the full set of parameters

R/combine.R Outdated Show resolved Hide resolved
Comment on lines +185 to +196
paste("Can't create a direct_sample from these models as '{name}' does",
"not contain all parameters in both models"),
Copy link
Contributor

Choose a reason for hiding this comment

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

im not sure that the name "a" or "b" is useful to the user, i found it actively confusing in the test for this, can we do a

`+.mcstate_model` <- function(x, y,
  names = c(deparse(substitute(x)),deparse(substitute(y))))

and pass this down into this function so that we can reference the variable that they tried to sum?

@richfitz richfitz force-pushed the mrc-5122 branch 2 times, most recently from 509e01b to 24e47b5 Compare March 5, 2024 15:16
Copy link
Contributor

@weshinsley weshinsley left a comment

Choose a reason for hiding this comment

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

Looks neat - some tiny doc typos!

R/combine.R Outdated Show resolved Hide resolved
R/combine.R Outdated Show resolved Hide resolved
R/combine.R Outdated Show resolved Hide resolved
R/combine.R Outdated Show resolved Hide resolved
Copy link
Contributor

@M-Kusumgar M-Kusumgar left a comment

Choose a reason for hiding this comment

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

Nice one, i like the "lhs" and "rhs", much clearer!

@richfitz richfitz merged commit 31eb49f into main Mar 5, 2024
7 checks passed
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.

3 participants