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

Deterministic filter-like device #8

Merged
merged 12 commits into from
May 20, 2024
Merged

Deterministic filter-like device #8

merged 12 commits into from
May 20, 2024

Conversation

richfitz
Copy link
Member

@richfitz richfitz commented May 13, 2024

Merge after #2, contains those commits
Merge after #11, contains those commits

This PR implements a particle-filter-like method for computing likelihoods out of a time series model by comparing to data periodically. It is the limit case of a particle filter to one particle, and has been used as the "deterministic" method in sircovid/dust1/etc.

The name unfilter is temporary, we'll probably come up with something better later.

The core implementation is in inst/dust2/filter.hpp - you'll see it's pretty straightforward - we loop over all times in the data set, do the comparison to data and accumulate a likelihood. The R wrapper around this then returns that likelihood out of the container back to R.

We'll complicate this shortly with things like returning trajectories as the model runs.

The time handling, and in particular the conversion to steps_ is pretty gross and addressed somewhat in #12, which we might use here soon

The current version only allows using initial conditions from the model, this is addressed in #13

@richfitz richfitz changed the title Add a basic sir model Deterministic filter-like device May 13, 2024
@richfitz richfitz force-pushed the mrc-5336 branch 2 times, most recently from b060d22 to 0e27238 Compare May 15, 2024 14:01
@richfitz richfitz marked this pull request as ready for review May 16, 2024 12:36
@richfitz richfitz requested a review from weshinsley May 16, 2024 12:37
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.

Good - think I undesrstand this - one typo I'll commit, and some of my train of thought running through the tests.


obj <- dust2_cpu_sir_unfilter_alloc(base, time_start, time, dt, data, 1, 0)
ptr <- obj[[1]]
expect_equal(dust2_cpu_sir_unfilter_run(ptr, NULL, FALSE), f(pars1))
Copy link
Contributor

Choose a reason for hiding this comment

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

So this one with NULL, FALSE uses base - which happens to be the same as pars1. We could have said f(base) on the end.

Copy link
Contributor

@weshinsley weshinsley May 18, 2024

Choose a reason for hiding this comment

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

General comment - I find it quite easy to forget what the args are with the functions... Lots of looking up

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, I'll sort this out in one push once we get an actual interface to any of this stuff - none of these functions will be the ones we call in the end!


obj <- dust2_cpu_sir_unfilter_alloc(pars, time_start, time, dt, data, 1, 3)
ptr <- obj[[1]]
expect_equal(dust2_cpu_sir_unfilter_run(ptr, NULL, TRUE), f(pars))
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, pars[[1 .. 3]] is an entry for each group, and f(pars) gives us 3 results, so a summed-log-likelihood over time, for each of the 3 groups.

tests/testthat/test-filter.R Outdated Show resolved Hide resolved
time <- c(4, 8, 12, 16)
data <- lapply(1:4, function(i) {
lapply(seq_len(n_groups), function(j) list(incidence = 2 * (i - 1) + j))
})
Copy link
Contributor

Choose a reason for hiding this comment

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

So data[[1 .. 4]] each contain 3 incidence points, one for each group.

Copy link
Contributor

Choose a reason for hiding this comment

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

and the 1 .. 4 in data refer to the 4 timesteps

dt <- 1

expect_error(
dust2_cpu_sir_unfilter_alloc(pars, 5, time, dt, data, 1, 0),
Copy link
Contributor

Choose a reason for hiding this comment

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

2nd arg (5) is r_time_start.
We expect (and get) errors if r_time_start >= length(time)

@richfitz richfitz merged commit 762d9a0 into main May 20, 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.

2 participants