Julia API reference¶
The public API of EpidemicDates.jl, generated from its docstrings.
For a guided tour see the Julia guide.
For the Python equivalent see the Python API reference.
Package¶
Impute missing symptom onsets in a line list.
The package compares three ways of placing cases with a missing onset:
back-sampling from the pooled empirical delays, a shift by the rounded
mean delay, and EM back-projection with an estimated delay.
It is the Julia version of the core line-list tools in the Python
package epidemic_dates.
Exports: delay_pmf, impute_linelist,
correct_truncation and weekly.
Warning
This code was generated by an LLM. Sam Abbott has only checked the interactive page, not this package. Treat the results as exploratory.
delay_pmf¶
Double interval censored daily Gamma delay pmf on delays 0:dmax.
The primary event (onset) is uniform within its day and the secondary event (report) is censored to its day, as in primarycensored. The probability of a delay of \(d\) days is
where \(F(x; k, \theta)\) is the Gamma CDF with shape
\(k = (\mu / \sigma)^2\) and scale \(\theta = \sigma^2 / \mu\), and
\(G(x) = 0\) for \(x \le 0\).
Negative values from rounding are set to 0.
The pmf is truncated at dmax and renormalised.
Arguments
mean: mean \(\mu\) of the continuous Gamma delay, in days. Must be positive and finite.sd: standard deviation \(\sigma\) of the continuous Gamma delay, in days. Must be positive and finite.dmax: maximum delay. Must be non-negative.
Returns
A Vector{Float64} of length dmax + 1 that sums to 1.
Element d + 1 is the probability of a delay of d days.
Throws
ArgumentError if mean or sd is not positive and finite, or if
dmax is negative.
Examples
julia> pmf = delay_pmf(8, 8);
julia> length(pmf)
61
julia> round(sum(pmf); digits = 12)
1.0
julia> round.(pmf[1:3]; digits = 4)
3-element Vector{Float64}:
0.06
0.1105
0.0975
Warning
This code was generated by an LLM. Sam Abbott has only checked the interactive page, not this package. Treat the results as exploratory.
impute_linelist¶
impute_linelist(onset, report; cutoff = nothing, delay = nothing,
max_delay = 60, n_iter = 150, delay_period = nothing,
delay_shrinkage = 10.0)
Impute missing onsets in a line list with each method.
Days are whole numbers, for example days since the first report.
onset holds the onset day of each case, with missing (or NaN)
where it is unknown.
report holds the report day of each case and must have no missing
values.
onset and report can instead be Date vectors.
Days are then counted from the earliest date in either vector, and
cutoff can be a Date.
Daily outputs have length T + 1, where T is the cut-off, and element
i is onset day i - 1.
They are expected counts, so they need not be whole numbers.
Sum them to weeks with weekly before comparing methods.
Methods
backsamplingspreads each missing case over report day minus each delay, weighted byempirical_delay. It is the expected allocation of the random draw, not a draw.meanshifts each missing case back by the mean ofempirical_delay, rounded with halves up (as JavaScript'sMath.round).emis EM back-projection (seeEpidemicDates.em_fit). By default it estimates a static delay jointly with the onset curve, allowing for right truncation at the cut-off. Withdelay_periodit estimates one delay per period of onset days, each shrunk towards the previous period's delay. Withdelayit holds that delay fixed (a plug-in) and only estimates the onset curve.fwd, only whendelayis given, back-samples from that delay instead of the empirical one. With a delay matrix it usesP(s | t) ∝ f_s(t - s)over onset days0:t.
All methods assume onsets are missing at random given the report day. If missingness depends on the delay, all are biased.
Arguments
onset: onset day per case,missingwhere unknown.report: report day per case.cutoff: data cut-off dayT, by default the last report day. Must be a non-negative whole number. Cases reported before day 0 or afterTare dropped.delay: optional forward delay pmf on delays0, 1, ..., for example fromdelay_pmf, or a matrix with one such pmf per onset day0:Tin its rows, for a delay that changes over time. It is normalised before use and used as given. Cannot be combined withdelay_period.max_delay: longest delay kept. Complete cases with a longer delay are dropped.n_iter: number of EM iterations. Must be at least 1.delay_period: length in days of the periods of onset days that get their own EM delay, counted back from the cut-off.nothing(the default) estimates one static delay.delay_shrinkage: strength, in expected cases, of the shrinkage of each period's delay towards the previous period's delay. Must be positive.
Returns
A NamedTuple with:
- complete: onset counts of cases with a known onset;
- missing_by_report: missing-onset cases by report day;
- empirical_delay: pooled delay pmf on 0:max_delay of complete cases;
- imputed: a NamedTuple of daily onset counts (complete plus imputed)
for backsampling, mean, em and, with delay, fwd;
- em_delay: the delay pmf on 0:max_delay used by EM: the static
estimate, the pooled estimate with delay_period, or the given
delay (its onset-weighted mean for a matrix);
- em_delay_by_period: a matrix with the delay pmf of each period in
its rows, one row without delay_period and one row per onset day
for a delay matrix;
- delay_period_start: the first onset day of each period;
- imputed_before_day0: expected missing-onset cases that
backsampling, mean and fwd place before day 0, which are not in
imputed;
- dropped: the number of cases dropped;
- cutoff: the cut-off used;
- day0: the date of day 0 for Date input, otherwise nothing.
Complete cases with a negative onset, an onset after report or a delay
over max_delay are dropped, as are cases reported outside 0:T.
Throws
ArgumentError if the line list is empty, onset and report differ
in length, a report day is missing or infinite, an onset is infinite, a
day is not a whole number, cutoff is negative or not whole,
max_delay is negative, n_iter is below 1, delay is not a valid
pmf or matrix of pmfs, both delay and delay_period are given,
delay_period or delay_shrinkage is out of range, or no complete case
is left after dropping.
Examples
julia> res = impute_linelist([0, 2, missing, 5, missing], [3, 6, 7, 9, 12]);
julia> length(res.imputed.backsampling)
13
julia> res.imputed.mean
13-element Vector{Float64}:
1.0
0.0
1.0
1.0
0.0
1.0
0.0
0.0
1.0
0.0
0.0
0.0
0.0
julia> round(sum(res.imputed.em); digits = 6)
5.0
Warning
This code was generated by an LLM. Sam Abbott has only checked the interactive page, not this package. Treat the results as exploratory.
correct_truncation¶
Correct daily onset counts for right truncation at the cut-off.
The data are cut at day \(T\) = cutoff, so onsets on day \(s\) count
only cases reported by \(T\).
Each day is divided by the expected fraction of its cases reported by
then,
where \(f\) is the forward delay pmf delay on delays 0, 1, ....
delay can instead be a matrix with one pmf per day of counts in its
rows, for a delay that changes over time; day \(s\) then uses row
\(s + 1\).
Days with \(F(T - s)\) below min_cover, including days after \(T\),
are set to 0.
The correction assumes the imputed curve has the right shape and is only
incomplete.
It cannot rescue cases placed on the wrong day.
In real data the delay would itself have to be estimated.
Arguments
counts: daily onset counts, elementiis onset dayi - 1.delay: forward delay pmf, elementd + 1is a delay ofddays. It is normalised before use.cutoff: data cut-off day \(T\). Must be non-negative.min_cover: smallest fraction reported that is corrected.
Returns
A Vector{Float64} the same length as counts.
Throws
ArgumentError if cutoff is negative, min_cover is not in
\((0, 1]\), or delay is not a valid pmf.
Examples
julia> correct_truncation([10.0, 10.0, 10.0], [0.5, 0.5], 2)
3-element Vector{Float64}:
10.0
10.0
20.0
Warning
This code was generated by an LLM. Sam Abbott has only checked the interactive page, not this package. Treat the results as exploratory.
weekly¶
Sum daily counts into weeks.
Week 1 is days 0 to 6 (elements 1 to 7), week 2 is days 7 to 13, and so on. A final partial week is kept, so check it before use.
Returns
A Vector{Float64} of length cld(length(daily), 7).
Examples
Warning
This code was generated by an LLM. Sam Abbott has only checked the interactive page, not this package. Treat the results as exploratory.
Internal building blocks¶
These are not exported and may change.
Call them with the module prefix, for example EpidemicDates.em_fit.
EpidemicDates.em_fit¶
em_fit(onset, delay, missing_report, cutoff; max_delay = 60,
n_iter = 150, fixed = nothing, period = nothing,
delay_shrinkage = 10.0)
Back-project missing-onset cases by EM, estimating the delay or holding a given one fixed.
onset and delay are the onset days and delays of complete cases.
missing_report holds the report days of cases with a missing onset.
All days are whole numbers in 0:cutoff, and complete cases must have
onset + delay <= cutoff and 0 <= delay <= max_delay.
period gives the period (0, 1, ...) of each onset day, from
EpidemicDates.delay_periods; by default there is
one period.
Expected counts by onset day \(s\) and delay \(d\) are
\(\lambda(s) f_{k(s)}(d)\), observed only when \(s + d \le T\) with
\(T\) = cutoff and \(k(s)\) the period of day \(s\).
Starting values are the complete-case delay counts plus 0.5, normalised,
for every \(f_k\), and the complete-case onset counts inflated by the
overall missing fraction, plus 0.1, for \(\lambda\).
Each iteration allocates the \(m(t)\) missing cases reported on day
\(t\) as
then, with \(n = n_c + n_a\), alternates three times between
where \(F_k\) is the CDF of \(f_k\) and \(D\) = max_delay.
\(f\) is the pooled delay, floored at \(10^{-12}\) and renormalised.
With one period \(f_0 = f\).
With several, in order \(k = 0, 1, \dots\),
with \(N_k(d)\) the cases with onset in period \(k\) and delay \(d\),
\(E_k(d) = \sum_{s \in k, s \le T - d} \lambda(s)\) and \(\kappa\) =
delay_shrinkage.
If fixed is a matrix of delay pmfs, one row per period, the delays are
held fixed and only \(\lambda\) is updated.
Returns a NamedTuple with allocation (expected missing-onset cases by
onset day), onsets (\(\lambda\)), delay (the pooled \(f\) on
0:max_delay, or the onset-weighted mean of a fixed delay matrix),
delay_by_period (one row per period) and period_start.
EpidemicDates.backsampling_allocate¶
Expected allocation of missing-onset cases under back-sampling.
Each case reported on day t is spread over onset days t - d with
weight pmf[d + 1].
It is the expected value of the random draw, not a draw.
Element i of both missing_by_report and the result is day i - 1.
Mass that falls before day 0 is dropped, so the sum can be below
sum(missing_by_report).
EpidemicDates.mean_shift_allocate¶
Move each missing-onset case back from its report day by shift days.
This is the mean shift method.
Element i of both missing_by_report and the result is day i - 1.
Cases moved before day 0 are dropped.
Throws ArgumentError if shift is negative.
EpidemicDates.round_half_up¶
Round x to the nearest integer with halves rounded up, as
floor(x + 0.5).
This matches JavaScript's Math.round and the Python package.
Julia's round rounds halves to even.
Throws ArgumentError if x is not finite.
EpidemicDates.DMAX¶
Default maximum onset-to-report delay in days.
Delay pmfs cover delays 0:DMAX.