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

Allow use of compiled compare in more places #192

Merged
merged 11 commits into from Jan 24, 2022
Merged

Conversation

richfitz
Copy link
Member

@richfitz richfitz commented Jan 24, 2022

This PR allows use of the compiled compare function in a few more places, using support added in mrc-ide/dust#360

  • deterministic models (was entirely unsupported)
  • multistage models (required partially running the filter)

Merge mrc-ide/dust#360 first and unpin branch
Fixes #177

@codecov
Copy link

codecov bot commented Jan 24, 2022

Codecov Report

Merging #192 (f707030) into master (a925eee) will not change coverage.
The diff coverage is 100.00%.

❗ Current head f707030 differs from pull request most recent head 9fad761. Consider uploading reports for the commit 9fad761 to get more accurate results
Impacted file tree graph

@@            Coverage Diff            @@
##            master      #192   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           28        28           
  Lines         2616      2638   +22     
=========================================
+ Hits          2616      2638   +22     
Impacted Files Coverage Δ
R/deterministic.R 100.00% <100.00%> (ø)
R/deterministic_state.R 100.00% <100.00%> (ø)
R/particle_filter.R 100.00% <100.00%> (ø)
R/particle_filter_state.R 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a925eee...9fad761. Read the comment docs.

DESCRIPTION Outdated Show resolved Hide resolved
@@ -31,28 +31,134 @@ particle_filter_state <- R6::R6Class(
min_log_likelihood = NULL,
support = NULL,

run_compiled = function() {
step_r = function(step_index, partial = FALSE) {
Copy link
Member

Choose a reason for hiding this comment

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

Similar to the comment on the dust PR, what's the reason for separating deterministic and stochastic, rather than having a single step function?

Copy link
Member Author

Choose a reason for hiding this comment

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

there's always a design trade-off here - one function with many if/elses or two functions with similar logic. Here the compiled and non-compiled versions are so different that the two functions approach feels much easier to follow the logic through. In particular the R step function is very complicated and has quite a few performance-critical tweaks


step_compiled = function(step_index, partial = FALSE) {
if (partial) {
stop("'partial' not supported with compiled compare")
Copy link
Member

Choose a reason for hiding this comment

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

Maybe I'm confused, but I thought support was just added for this? Or am I misunderstanding what partial is?

Copy link
Member Author

Choose a reason for hiding this comment

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

partial is the support we put in for (one of) if2/smc2, where you get the likelihood contributed by the last single step. We don't support that but we do support running to a point that is not the end. It's confusing yes :)

Comment on lines +371 to +375
pars <- list(exp_noise = Inf)
## there's a discrepency here which ironically goes away with
## non-infinite noise due to combination of sum over small numbers
## and slightly different density calculation accuracy.
expect_equal(p1$run(pars), p2$run(pars), tolerance = 2e-5)
Copy link
Member

Choose a reason for hiding this comment

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

Different example/non-Inf noise simpler then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Using an Inf noise keeps the calculation deterministic, even if not quite at the accuracy we'd like. Getting the details out as to why this is slightly off would be potentially interesting but fairly sure that it's just that we have some poor accuracy in the densities when very unlikely (the R versions of these have lots of support to make these accurate but we can't use them due to thread safety)

Co-authored-by: John Lees <j.lees@imperial.ac.uk>
@richfitz richfitz marked this pull request as ready for review January 24, 2022 17:45
@richfitz richfitz merged commit 7485a6a into master Jan 24, 2022
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.

Support for using the compiled compare in more places
2 participants