idem-lab / epiwave.pipelines

0 stars 3 forks source link

rounding issues w/ mass functions #3

Closed smwindecker closed 11 months ago

smwindecker commented 11 months ago

discretised probability mass functions need to sum to 1, but sometimes don't because of rounding issues

AugustHao commented 11 months ago

I found this code from Nick for a different purpose, I think adding a normalisation might be all we need?

# greta function for the probability mass function of a discretised lognormal
# distribution, truncated at max_days
discrete_lognormal_pmf <- function(days, meanlog, sdlog, max_days) {

  lower <- lognormal_cdf(
    q = days - 1,
    meanlog = meanlog,
    sdlog = sdlog
  )

  upper <- lognormal_cdf(
    q = days,
    meanlog = meanlog,
    sdlog = sdlog
  )

  normalisation <- lognormal_cdf(
    q = max_days,
    meanlog = meanlog,
    sdlog = sdlog
  )

  pmf_unnormalised <- upper - lower
  pmf_unnormalised[days > max_days] <- 0
  pmf <- pmf_unnormalised / normalisation

  pmf
AugustHao commented 11 months ago

fixed in #4a6bcc0