-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from CDCgov/41-observation-process-as-a-turing…
…-model Add observation process as a Turing model
- Loading branch information
Showing
6 changed files
with
81 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function default_delay_obs_priors() | ||
return (neg_bin_cluster_factor_prior = Gamma(3, 0.05 / 3),) | ||
end | ||
|
||
@model function delay_observations( | ||
y_t, | ||
I_t, | ||
epimodel::AbstractEpiModel; | ||
observation_process_priors = default_delay_obs_priors(), | ||
pos_shift = 1e-6, | ||
) | ||
#Parameters | ||
neg_bin_cluster_factor ~ observation_process_priors.neg_bin_cluster_factor_prior | ||
|
||
#Predictive distribution | ||
case_pred_dists = | ||
(epimodel.data.delay_kernel * I_t) .+ pos_shift .|> | ||
μ -> mean_cc_neg_bin(μ, neg_bin_cluster_factor) | ||
|
||
#Likelihood | ||
y_t ~ arraydist(case_pred_dists) | ||
|
||
return y_t, (; neg_bin_cluster_factor,) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@testitem "Testing delay obs against theoretical properties" begin | ||
using DynamicPPL, Turing | ||
# Set up test data with fixed infection | ||
I_t = [10.0, 20.0, 30.0] | ||
|
||
# Replace with your own implementation of AbstractEpiModel | ||
# Delay kernel is just event observed on same day | ||
data = EpiData([0.2, 0.3, 0.5], [1.0], 0.8, 3, exp) | ||
epimodel = DirectInfections(data) | ||
# Set up priors | ||
observation_process_priors = default_delay_obs_priors() | ||
|
||
# Call the function | ||
mdl = delay_observations( | ||
missing, | ||
I_t, | ||
epimodel; | ||
observation_process_priors = observation_process_priors, | ||
) | ||
fix_mdl = fix(mdl, neg_bin_cluster_factor = 0.00001) # Effectively Poisson sampling | ||
|
||
n_samples = 1000 | ||
mean_first_obs = | ||
sample(fix_mdl, Prior(), n_samples) |> | ||
chn -> generated_quantities(fix_mdl, chn) .|> (gen -> gen[1][1]) |> mean | ||
|
||
theoretical_std_of_empiral_mean = sqrt(I_t[1]) / sqrt(n_samples) | ||
@test mean(mean_first_obs) - I_t[1] < 5 * theoretical_std_of_empiral_mean && | ||
mean(mean_first_obs) - I_t[1] > -5 * theoretical_std_of_empiral_mean | ||
|
||
end |