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

don't defer negative binomial to Poisson at low levels of overdispersion #432

Merged
merged 7 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions inst/stan/functions/observation_model.stan
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,22 @@ void truncation_lp(real[] truncation_mean, real[] truncation_sd,
void report_lp(int[] cases, vector reports,
real[] rep_phi, real phi_mean, real phi_sd,
int model_type, real weight) {
real sqrt_phi = 1e5;
if (model_type) {
// the reciprocal overdispersion parameter (phi)
real sqrt_phi; // the reciprocal overdispersion parameter (phi)
rep_phi[model_type] ~ normal(phi_mean, phi_sd) T[0,];
sqrt_phi = 1 / sqrt(rep_phi[model_type]);
// defer to poisson if phi is large, to avoid overflow or
// if poisson specified
}
if (sqrt_phi > 1e4) {
if (weight == 1) {
cases ~ poisson(reports);
}else{
target += poisson_lpmf(cases | reports) * weight;
cases ~ neg_binomial_2(reports, sqrt_phi);
} else {
target += neg_binomial_2_lpmf(cases | reports, sqrt_phi) * weight;
}
} else {
if (weight == 1) {
cases ~ neg_binomial_2(reports, sqrt_phi);
}else{
target += neg_binomial_2_lpmf(cases | reports, sqrt_phi);
cases ~ poisson(reports);
} else {
target += poisson_lpmf(cases | reports) * weight;
}
}

}
// update log likelihood (as above but not vectorised and returning log likelihood)
vector report_log_lik(int[] cases, vector reports,
Expand All @@ -85,11 +79,11 @@ vector report_log_lik(int[] cases, vector reports,
real sqrt_phi = 1e5;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can also drop this.

if (model_type) {
// the reciprocal overdispersion parameter (phi)
sqrt_phi = 1 / sqrt(rep_phi[model_type]);
sqrt_phi = 1 / sqrt(rep_phi[model_type]);
}

// defer to poisson if phi is large, to avoid overflow
if (sqrt_phi > 1e4) {
if (model_type == 0) {
for (i in 1:t) {
log_lik[i] = poisson_lpmf(cases[i] | reports[i]) * weight;
}
Expand Down
13 changes: 6 additions & 7 deletions tests/testthat/test-estimate_secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ cases[
# with a secondary case
inc <- estimate_secondary(cases[1:60],
obs = obs_opts(scale = list(mean = 0.2, sd = 0.2), week_effect = FALSE),
control = list(adapt_delta = 0.98),
verbose = FALSE
)

Expand Down Expand Up @@ -88,12 +87,12 @@ test_that("estimate_secondary can recover simulated parameters", {
)
# These tests currently fail indicating the model is not recovering the
# simulated parameters.
# expect_equal(
# prev_posterior[, mean], c(1.6, 0.8, 0.3), tolerance = 0.1
# )
# expect_equal(
# prev_posterior[, median], c(1.6, 0.8, 0.3), tolerance = 0.1
# )
expect_equal(
prev_posterior[, mean], c(1.6, 0.8, 0.3), tolerance = 0.1
)
expect_equal(
prev_posterior[, median], c(1.6, 0.8, 0.3), tolerance = 0.1
)
})

test_that("forecast_secondary can return values from simulated data and plot
Expand Down