mrc-ide / odin

ᚩ A DSL for describing and solving differential equations in R
https://mrc-ide.github.io/odin
Other
106 stars 13 forks source link

Support for differentiation of parameters in DSL #295

Closed richfitz closed 1 year ago

richfitz commented 1 year ago

This PR builds on #294 to add control over differentiation of parameters to odin. The syntax for users just replaces

scale <- user()

with

scale <- user(differentiate = TRUE)
codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (4f9f69e) 100.00% compared to head (2dffc48) 100.00%.

:exclamation: Current head 2dffc48 differs from pull request most recent head 5c02f09. Consider uploading reports for the commit 5c02f09 to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #295 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 48 48 Lines 5529 5556 +27 ========================================= + Hits 5529 5556 +27 ``` | [Impacted Files](https://app.codecov.io/gh/mrc-ide/odin/pull/295?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mrc-ide) | Coverage Δ | | |---|---|---| | [R/ir\_parse.R](https://app.codecov.io/gh/mrc-ide/odin/pull/295?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mrc-ide#diff-Ui9pcl9wYXJzZS5S) | `100.00% <100.00%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

richfitz commented 1 year ago

Code changes themselves look fine, but I'm a bit confused. Where will this be used? What does differentiate mean here? You said in the epic this is for "indicating which parameters we want the likelihood differentiated with respect to". The likelihood is the compare function? Or the compare function is used to build the likelihood after running the adjoint model? How does setting this differentiate flag relate to that? I thought user was just to let users specify the parameter value used in the model. Is it possible to work this out automatically by looking at the parameters that the compare function uses?

Only user() parameters can be used as the thing we differentiate with respect to, but not all can. For example, in the motivating SIR model we can't differentiate against freq as it is an integer, and we probably would not want to differentiate with respect to dt if we provided that directly. Other things we might not differentiate against would be user "parameters" that represent data (mixing matrices, vaccination schedules etc).

The compare function goes into the likelihood (the likelihood is the sum over all the times that the compare gets called). The parameters that we differentiate with respect to get added into the adjoint model so that it has some extra variables, each corresponding to the partial derivative of the likelihood with respect to a parameter.

All parameters will ultimately end up somewhere in the graph, and it's not just the bit in the compare function that matters. Again, in the motivating SIR model we differentiate with respect to parameters that only appear in the forward time model and not in the compare function