|
| 1 | +""" |
| 2 | +Constructs and returns a latent model based on the provided `inference_config` and `pipeline`. |
| 3 | +The purpose of this function is to make adjustments to the latent model based on the |
| 4 | +full `inference_config` provided. |
| 5 | +
|
| 6 | +The `pipeline` argument is used for dispatch purposes. |
| 7 | +
|
| 8 | +The prior decisions are based on the target standard deviation and autocorrelation of the latent process, |
| 9 | +which are determined by the infection generating process (igp) and whether the latent process is stationary or non-stationary |
| 10 | +via the `_make_target_std_and_autocorr` function. |
| 11 | +
|
| 12 | +
|
| 13 | +# Returns |
| 14 | +- A latent model object which can be one of `DiffLatentModel`, `AR`, or `RandomWalk` depending on the `latent_model_name` and `igp` specified in `inference_config`. |
| 15 | +""" |
| 16 | +function remake_latent_model( |
| 17 | + inference_config::Dict, pipeline::AbstractRtwithoutRenewalPipeline) |
| 18 | + #Baseline choices |
| 19 | + prior_dict = make_model_priors(pipeline) |
| 20 | + igp = inference_config["igp"] |
| 21 | + default_latent_model = inference_config["latent_namemodels"].second |
| 22 | + target_std, target_autocorr = default_latent_model isa AR ? |
| 23 | + _make_target_std_and_autocorr(igp; stationary = true) : |
| 24 | + _make_target_std_and_autocorr(igp; stationary = false) |
| 25 | + |
| 26 | + return _implement_latent_process( |
| 27 | + target_std, target_autocorr, default_latent_model, pipeline) |
| 28 | +end |
| 29 | + |
| 30 | +""" |
| 31 | +This function sets the target standard deviation for an infection generating process (igp) |
| 32 | +based on whether the latent process representation of its dynamics are stationary or non-stationary. |
| 33 | +
|
| 34 | +## Stationary Processes |
| 35 | +
|
| 36 | +- For Renewal process `log(R_t)` in the long run a fluctuation of 0.75 (e.g. ~ 75% of the mean) is not unexpected. |
| 37 | +- For Exponential Growth Rate process `r_t` in the long run a fluctuation of 0.2 is not unexpected e.g. going from |
| 38 | +`rt = 0.1` (7 day doubling time) to `rt = -0.1` (7 day halving time) is a 0.2 time-to-time fluctuation. |
| 39 | +- For Direct Infections process `log(I_t)` in the long run a fluctuation of 2.0 (i.e a couple of orders of magnitude) is not unexpected. |
| 40 | +
|
| 41 | +For stationary latent processes Direct Infections and rt processes the autocorrelation is expected to be high at 0.9, |
| 42 | +because persistence in residual away from mean is expected. Otherwise, the autocorrelation is expected to be 0.1. |
| 43 | +
|
| 44 | +## Non-Stationary Processes |
| 45 | +
|
| 46 | +For Renewal process `log(R_t)` in a single time step a fluctuation of 0.025 (e.g. ~ 2.5% of the mean) is not unexpected. |
| 47 | +For Exponential Growth Rate process `r_t` in a single time step a fluctuation of 0.005 is not unexpected. |
| 48 | +For Direct Infections process `log(I_t)` in a single time step a fluctuation of 0.025 is not unexpected. |
| 49 | +
|
| 50 | +The autocorrelation is expected to be 0.1. |
| 51 | +""" |
| 52 | +function _make_target_std_and_autocorr(::Type{Renewal}; stationary::Bool) |
| 53 | + return stationary ? (0.75, 0.1) : (0.025, 0.1) |
| 54 | +end |
| 55 | + |
| 56 | +function _make_target_std_and_autocorr(::Type{ExpGrowthRate}; stationary::Bool) |
| 57 | + return stationary ? (0.2, 0.9) : (0.005, 0.1) |
| 58 | +end |
| 59 | + |
| 60 | +function _make_target_std_and_autocorr(::Type{DirectInfections}; stationary::Bool) |
| 61 | + return stationary ? (2.0, 0.9) : (0.25, 0.1) |
| 62 | +end |
| 63 | + |
| 64 | +function _make_new_prior_dict(target_std, target_autocorr, |
| 65 | + pipeline::AbstractRtwithoutRenewalPipeline; beta_eff_sample_size) |
| 66 | + #Get default priors |
| 67 | + prior_dict = make_model_priors(pipeline) |
| 68 | + #Adjust priors based on target autocorrelation and standard deviation |
| 69 | + damp_prior = Beta(target_autocorr * beta_eff_sample_size, |
| 70 | + (1 - target_autocorr) * beta_eff_sample_size) |
| 71 | + corr_corrected_noise_prior = HalfNormal(target_std * sqrt(1 - target_autocorr^2)) |
| 72 | + noise_prior = HalfNormal(target_std) |
| 73 | + init_prior = prior_dict["transformed_process_init_prior"] |
| 74 | + return Dict( |
| 75 | + "transformed_process_init_prior" => init_prior, |
| 76 | + "corr_corrected_noise_prior" => corr_corrected_noise_prior, |
| 77 | + "noise_prior" => noise_prior, |
| 78 | + "damp_param_prior" => damp_prior |
| 79 | + ) |
| 80 | +end |
| 81 | + |
| 82 | +""" |
| 83 | +Constructs and returns a latent model based on an approximation to the specified target standard deviation and autocorrelation. |
| 84 | +
|
| 85 | +NB: The stationary variance of an AR(1) process is given by `σ² = σ²_ε / (1 - ρ²)` where `σ²_ε` is the variance of the noise and `ρ` is the autocorrelation. |
| 86 | +The approximation here are based on `E[1/(1 - ρ²)`] ≈ 1 / (1 - E[ρ²])` which only holds for fairly tight distributions of `ρ`. |
| 87 | +However, for priors this should get the expected order of magnitude. |
| 88 | +
|
| 89 | +# Models |
| 90 | +- `"diff_ar"`: Constructs a `DiffLatentModel` with an autoregressive (AR) process. |
| 91 | +- `"ar"`: Constructs an autoregressive (AR) process. |
| 92 | +- `"rw"`: Constructs a random walk (RW) process. |
| 93 | +
|
| 94 | +""" |
| 95 | +function _implement_latent_process( |
| 96 | + target_std, target_autocorr, default_latent_model, pipeline; beta_eff_sample_size = 10) |
| 97 | + prior_dict = make_model_priors(pipeline) |
| 98 | + new_priors = _make_new_prior_dict( |
| 99 | + target_std, target_autocorr, pipeline; beta_eff_sample_size) |
| 100 | + |
| 101 | + return _make_latent(default_latent_model, new_priors) |
| 102 | +end |
| 103 | + |
| 104 | +function _make_latent(::AR, new_priors) |
| 105 | + damp_prior = new_priors["damp_param_prior"] |
| 106 | + corr_corrected_noise_std = new_priors["corr_corrected_noise_prior"] |
| 107 | + init_prior = new_priors["transformed_process_init_prior"] |
| 108 | + return AR(damp_priors = [damp_prior], |
| 109 | + std_prior = corr_corrected_noise_std, |
| 110 | + init_priors = [init_prior]) |
| 111 | +end |
| 112 | + |
| 113 | +function _make_latent(::DiffLatentModel, new_priors) |
| 114 | + init_prior = new_priors["transformed_process_init_prior"] |
| 115 | + ar = _make_latent(AR(), new_priors) |
| 116 | + return DiffLatentModel(; model = ar, init_priors = [init_prior]) |
| 117 | +end |
| 118 | + |
| 119 | +function _make_latent(::RandomWalk, new_priors) |
| 120 | + noise_std = new_priors["noise_prior"] |
| 121 | + init_prior = new_priors["transformed_process_init_prior"] |
| 122 | + return RandomWalk(std_prior = noise_std, init_prior = init_prior) |
| 123 | +end |
| 124 | + |
| 125 | +""" |
| 126 | +Pass through fallback dispatch. |
| 127 | +""" |
| 128 | +function remake_latent_model(inference_config::Dict, pipeline::AbstractEpiAwarePipeline) |
| 129 | + inference_config["latent_namemodels"].second |
| 130 | +end |
0 commit comments