|
| 1 | +""" |
| 2 | +Internal function for calculating the log of the infections with an informative |
| 3 | + error message if the infections are not positive definite. |
| 4 | +""" |
| 5 | +function _calc_log_infections(I_t) |
| 6 | + @assert all(I_t .> 0) "Infections must be positive definite." |
| 7 | + log.(I_t) |
| 8 | +end |
| 9 | + |
| 10 | +""" |
| 11 | +Internal function for calculating the exponential growth rate with an informative |
| 12 | + error message if the infections are not positive definite. |
| 13 | +""" |
| 14 | +function _calc_rt(I_t, I0) |
| 15 | + @assert all(I_t .> 0) "Infections must be positive definite." |
| 16 | + @assert I0>0 "Initial infections must be positive definite." |
| 17 | + log.([I0; I_t]) .- log(I0) |> diff |
| 18 | +end |
| 19 | + |
| 20 | +""" |
| 21 | +Internal function for seeding the infections. Method dispatches on the pipeline |
| 22 | +type to determine the seeding method. This is the default seeding method which |
| 23 | +assumes backward exponential growth with initial infections `I0` from initial |
| 24 | +estimate of `rt`. |
| 25 | +
|
| 26 | +""" |
| 27 | +function _infection_seeding(I_t, I0, data::EpiData, pipeline::AbstractEpiAwarePipeline) |
| 28 | + n = length(data.gen_int) |
| 29 | + init_rt = _calc_rt(I_t[1:2], I0) |> x -> x[1] |
| 30 | + [I0 * exp(-init_rt * (n - i)) for i in 1:n] |
| 31 | +end |
| 32 | + |
| 33 | +""" |
| 34 | +Internal function for calculating the _instantaneous_ reproduction number `Rt` |
| 35 | +using the method of [Fraser (2007)](https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0000758). |
| 36 | +Left truncation handling method is determined by the pipeline type. The default |
| 37 | +left truncation in `I_t` is handled by extending the series with backwards exponential |
| 38 | +growth from the initial infections `I0` and the exponential growth rate `init_rt`. |
| 39 | +
|
| 40 | +# Arguments |
| 41 | +- `I_t`: Incident infections. |
| 42 | +- `I0`: Initial infections at time zero. |
| 43 | +- `init_rt`: Initial exponential growth rate. |
| 44 | +- `data::EpiData`: An instance of the `EpiData` type containing generation interval data. |
| 45 | +- `pipeline::AbstractEpiAwarePipeline`: An instance of the `AbstractEpiAwarePipeline` type. |
| 46 | +""" |
| 47 | +function _calc_Rt(I_t, I0, data::EpiData, pipeline::AbstractEpiAwarePipeline) |
| 48 | + @assert all(I_t .> 0) "Log infections must be positive definite." |
| 49 | + @assert I0>0 "Initial infections must be positive definite." |
| 50 | + |
| 51 | + aug_I_t = vcat(_infection_seeding(I_t, I0, data, pipeline), I_t) |
| 52 | + |
| 53 | + Rt = expected_Rt(data, aug_I_t) |
| 54 | + |
| 55 | + return Rt |
| 56 | +end |
| 57 | + |
| 58 | +""" |
| 59 | +Calculate the log of infections `log_I_t`, exponential growth values `rt`, and |
| 60 | +instaneous reproductive number `Rt` for a given time series of infections. The |
| 61 | +reproductive number calculation deals with left truncation in `I_t` by extending |
| 62 | +`I_t` with backwards exponential growth using the mean exponential growth rate |
| 63 | +from the first 7 time steps of `rt`. |
| 64 | +
|
| 65 | +# Arguments |
| 66 | +- `I_t`: An array representing the time series of infections. |
| 67 | +- `I0`: The initial number of infections. |
| 68 | +- `pmf`: The probability mass function used to calculate Rt. |
| 69 | +
|
| 70 | +# Returns |
| 71 | +A named tuple containing the calculated values for `log_I_t`, `rt`, and `Rt`. |
| 72 | +
|
| 73 | +""" |
| 74 | +function calculate_processes(I_t, I0, data::EpiData, pipeline::AbstractEpiAwarePipeline) |
| 75 | + log_I_t = _calc_log_infections(I_t) |
| 76 | + rt = _calc_rt(I_t, I0) |
| 77 | + Rt = _calc_Rt(I_t, I0, data, pipeline) |
| 78 | + return (; log_I_t, rt, Rt) |
| 79 | +end |
0 commit comments