mrc-ide / dust

:sparkles::sparkles::sparkles: Iterate multiple realisations of stochastic models
https://mrc-ide.github.io/dust
Other
18 stars 1 forks source link

Simulate data based on probabilistic odin compare #412

Open edknock opened 1 year ago

edknock commented 1 year ago

Now we have the ability to write probabilistic compare functions within odin, it makes sense to try to use them for not just log-likelihood calculation, but also random generation of data.

Consider a toy model

gen <- odin.dust::odin_dust({
  initial(y) <- 0
  update(y) <- y + rnorm(0, 1)

  initial(z) <- 0
  update(z) <- z + rpois(1)

  scale_y <- user(1)
  observed_y <- data()

  compare(observed_y) ~ normal(y, scale_y)

  shape_z <- user(1)
  observed_z <- data()

  compare(observed_z) ~ negative_binomial_mu(shape_z, z)
})

We can then simulate from this model, and compare it to some data

np <- 10
mod <- gen$new(list(), 0, np, seed = 1L)
y <- mod$run(50)

data <- data.frame(time = 50, observed_y = 3, observed_z = 4)
mod$set_data(dust::dust_data(data, "time"))
mod$compare_data()

We could perhaps instead want to simulate data based upon the simulation of the model. Something like a mod$simulate_data() function. Maybe there are inputs we would want to use for this, e.g. the time-steps at which we want to simulate data, or which data streams we want to simulate data for (other data streams appearing in the compare could just return NA values).

Welcoming thoughts or flagging up potential issues from those who contributed to previous discussion (https://github.com/mrc-ide/odin.dust/issues/130): @richfitz, @annecori, @MJomaba, @lwhittles, @cwhittaker1000. Feel free to suggest others who would provide useful input.

edknock commented 1 year ago

Potential issue - how would distributions where there are two (or more) bits of data be handled? Obvious example is binomial, where the number of trials/tests and number of "successes" might both be treated as data in the odin code. One might want to input the number of tests and simulate the number of "successes"?