Skip to content

allow snapshots shorter than trunc_max #438

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

Merged
merged 3 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
allow snapshots shorter than trunc_max
  • Loading branch information
sbfnk committed Jul 30, 2023
commit 2589528354ace801b7dccae3b6ff83f9257e8052
2 changes: 1 addition & 1 deletion R/estimate_truncation.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ estimate_truncation <- function(obs, max_truncation, trunc_max = 10,
confirm := NULL
])
obs <- purrr::reduce(obs, merge, all = TRUE)
obs_start <- nrow(obs) - trunc_max - sum(is.na(obs$`1`)) + 1
obs_start <- max(nrow(obs) - trunc_max - sum(is.na(obs$`1`)) + 1, 1)
obs_dist <- purrr::map_dbl(2:(ncol(obs)), ~ sum(is.na(obs[[.]])))
obs_data <- obs[, -1][, purrr::map(.SD, ~ ifelse(is.na(.), 0, .))]
obs_data <- obs_data[obs_start:.N]
Expand Down
37 changes: 25 additions & 12 deletions inst/stan/estimate_truncation.stan
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ data {
int trunc_max;
int trunc_dist;
}
transformed data{
int<lower = 1> end_t[obs_sets];
int<lower = 1> start_t[obs_sets];
for (i in 1:obs_sets) {
end_t[i] = t - obs_dist[i];
start_t[i] = max(1, end_t[i] - trunc_max + 1);
}
}
parameters {
real logmean;
real<lower=0> logsd;
real<lower=0> phi;
real<lower=0> sigma;
}
transformed parameters{
matrix[trunc_max, obs_sets - 1] trunc_obs;
matrix[trunc_max, obs_sets - 1] trunc_obs =
rep_matrix(0, trunc_max, obs_sets - 1);
real sqrt_phi = 1 / sqrt(phi);
vector[trunc_max] rev_cmf = reverse_mf(cumulative_sum(
discretised_pmf(logmean, logsd, trunc_max, trunc_dist)
Expand All @@ -31,9 +40,8 @@ transformed parameters{
// apply truncation to latest dataset to map back to previous data sets and
// add noise term
for (i in 1:(obs_sets - 1)) {
int end_t = t - obs_dist[i];
int start_t = end_t - trunc_max + 1;
trunc_obs[, i] = truncate(last_obs[start_t:end_t], rev_cmf, 0) + sigma;
trunc_obs[1:(end_t[i] - start_t[i] + 1), i] =
truncate(last_obs[start_t[i]:end_t[i]], rev_cmf, 0) + sigma;
}
}
}
Expand All @@ -45,23 +53,28 @@ model {
sigma ~ normal(0, 1) T[0,];
// log density of truncated latest data vs that observed
for (i in 1:(obs_sets - 1)) {
int start_t = t - obs_dist[i] - trunc_max;
for (j in 1:trunc_max) {
obs[start_t + j, i] ~ neg_binomial_2(trunc_obs[j, i], sqrt_phi);
for (j in 1:(end_t[i] - start_t[i] + 1)) {
obs[start_t[i] + j - 1, i] ~ neg_binomial_2(trunc_obs[j, i], sqrt_phi);
}
}
}
generated quantities {
matrix[trunc_max, obs_sets] recon_obs;
matrix[trunc_max, obs_sets] recon_obs = rep_matrix(0, trunc_max, obs_sets);
matrix[trunc_max, obs_sets - 1] gen_obs;
// reconstruct all truncated datasets using posterior of the truncation distribution
for (i in 1:obs_sets) {
int end_t = t - obs_dist[i];
int start_t = end_t - trunc_max + 1;
recon_obs[, i] = truncate(to_vector(obs[start_t:end_t, i]), rev_cmf, 1);
recon_obs[1:(end_t[i] - start_t[i] + 1), i] = truncate(
to_vector(obs[start_t[i]:end_t[i], i]), rev_cmf, 1
);
}
// generate observations for comparing
for (i in 1:(obs_sets - 1)) {
gen_obs[, i] = to_vector(neg_binomial_2_rng(trunc_obs[, i], sqrt_phi));
for (j in 1:trunc_max) {
if (trunc_obs[j, i] == 0) {
gen_obs[j, i] = 0;
} else {
gen_obs[j, i] = neg_binomial_2_rng(trunc_obs[j, i], sqrt_phi);
}
}
}
}
8 changes: 4 additions & 4 deletions inst/stan/functions/observation_model.stan
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ vector truncate(vector reports, vector trunc_rev_cmf, int reconstruct) {
int t = num_elements(reports);
vector[t] trunc_reports = reports;
// Calculate cmf of truncation delay
int trunc_max = num_elements(trunc_rev_cmf);
int trunc_max = min(t, num_elements(trunc_rev_cmf));
int first_t = t - trunc_max + 1;
// Apply cdf of truncation delay to truncation max last entries in reports
if (reconstruct) {
trunc_reports[first_t:t] = trunc_reports[first_t:t] ./ trunc_rev_cmf;
}else{
trunc_reports[first_t:t] = trunc_reports[first_t:t] .* trunc_rev_cmf;
trunc_reports[first_t:t] ./= trunc_rev_cmf[1:trunc_max];
} else {
trunc_reports[first_t:t] .*= trunc_rev_cmf[1:trunc_max];
}
return(trunc_reports);
}
Expand Down