¶
Load epidemic-dates.js with require in Node, or use the global EpidemicDates in a browser.
The JavaScript module epidemic-dates.js holds the maths behind the interactive page.
It simulates an epidemic in expectation, imputes the missing onsets four ways and compares each with the truth.
It can also impute a real line list.
It has no dependencies.
- Source:
epidemic-dates.js - Every function and type: API reference
- The maths: Methods
- What it relies on: Assumptions
Warning
This module was generated by an LLM and has not been reviewed by a person. Only the interactive page has been checked. Treat the results as exploratory.
Load it¶
In a browser, the script defines the global EpidemicDates:
In Node 20 or later:
Quick start on your own data¶
This reads the example line list, imputes the missing onsets and prints weekly onset counts by method.
const fs = require("node:fs");
const { imputeLinelist } = require("./epidemic-dates.js");
const t0 = Date.parse("2026-05-03"); // earliest date in the file
const day = (x) => (x ? (Date.parse(x) - t0) / 86400000 : null);
const rows = fs
.readFileSync("docs/data/example-linelist.csv", "utf8")
.trim()
.split(/\r?\n/)
.slice(1)
.map((line) => line.split(","));
const onset = rows.map((r) => day(r[1]));
const report = rows.map((r) => day(r[2]));
const res = imputeLinelist({ onset, report, cutoff: Math.max(...report) });
for (const m of ["backsampling", "mean", "gen"]) {
const weeks = [];
res.imputed[m].forEach((x, i) => {
const w = Math.floor(i / 7);
weeks[w] = (weeks[w] ?? 0) + x;
});
console.log(m, weeks.map((x) => x.toFixed(1)).join(" "));
}
backsampling 6.8 14.9 16.8 27.9 44.7 81.9 122.9 200.2 251.1 353.7 595.8 697.1 455.2 174.0
mean 7.0 15.0 14.0 31.0 38.0 84.0 119.0 197.0 250.0 351.0 609.0 708.0 488.0 132.0
gen 7.0 15.4 15.9 26.5 43.5 81.3 121.4 203.2 246.3 346.2 614.2 723.5 440.4 159.2
The worked example explains each step.
imputeLinelist takes one object (LinelistInput) with:
onset: onset day per case,nullwhere missing;report: report day per case;cutoff: the last report day to use, by default the latest;delay: a forward delay pmf indexed by day, or an array with one pmf per onset day, to hold the delay fixed and to fill in the forward draw;maxDelayanditerations: the longest delay kept and the number of EM iterations;delayPeriod: the length in days of periods that each get their own EM delay, by default none;delayShrinkage: how strongly each period's delay is pulled towards the previous period's, 10 expected cases by default.
It returns a LinelistResult.
imputed holds daily onset counts for backsampling, mean, fwd and gen.
fwd is null without delay.
generativeDelay is the delay used or estimated by EM.
generativeDelayByPeriod has one delay per period and delayPeriodStart the first day of each period.
Nowcast with uncertainty¶
nowcastLinelist imputes the missing onsets, corrects for right truncation and samples to give quantiles.
This continues the quick start above.
const { nowcastLinelist } = require("./epidemic-dates.js");
const nc = nowcastLinelist({
onset,
report,
method: "gen",
seed: 1,
origin: "2026-05-03"
});
for (const r of nc.weeklyQuantiles.slice(-7)) {
console.log(r.week_start, r.target_end_date, r.quantile, +r.value.toFixed(1));
}
2026-08-02 2026-08-08 0.025 328
2026-08-02 2026-08-08 0.1 350
2026-08-02 2026-08-08 0.25 371
2026-08-02 2026-08-08 0.5 391
2026-08-02 2026-08-08 0.75 416
2026-08-02 2026-08-08 0.9 439
2026-08-02 2026-08-08 0.975 469
The table has one row per week and quantile.
Weeks end on the cut-off.
A day with under 5% of its cases reported gets no estimate, and nor does its week (minReported); here every week has one.
method can also be backsampling, mean or fwd, and delay holds the delay fixed.
The point nowcast matches Python's nowcast_linelist, but the random draws differ.
The bands include the draw of missing onsets, the cases not yet reported and, by resampling, uncertainty in the delay.
They do not include a delay that changes over time, unless you set delayPeriod or pass a delay matrix.
See Step 6 of the worked example and the Methods.
nowcast_linelist, which gives the same point nowcast as the code above.
The solid black line is the eventual true onsets, known because the data are simulated.
The dashed line counts cases with a known onset.
The dotted line adds the imputed missing-onset cases, with no correction for right truncation.
The coloured line is the nowcast median, with 50% and 95% bands.
In the final week few cases are reported yet, and this nowcast is too low.
Delays that change over time¶
EM can use the delay in three ways:
- one static delay, estimated (the default);
- one delay per period of onset days, estimated, with
delayPeriod, each shrunk towards the one before it with strengthdelayShrinkage; - a known delay, passed as
delay: one pmf, or an array with one pmf per onset day for a delay that changes over time, used as given.
imputeLinelist and nowcastLinelist both take it.
The nowcast then uses each day's own delay for the truncation correction, the bootstrap and the draws of cases not yet reported.
This uses the second example line list, whose mean delay falls from 12 to 5 days, and the true delay of each onset day, known because the data are simulated. It prints the estimated mean delay of each 14-day period, then the point nowcast of the last four weeks with each of the three delays. This continues the quick start above.
const { pmfMean } = require("./epidemic-dates.js");
const lines2 = (path) =>
fs.readFileSync(path, "utf8").trim().split(/\r?\n/).slice(1).map((l) => l.split(","));
const rows2 = lines2("docs/data/example-linelist-changing-delays.csv");
const onset2 = rows2.map((r) => day(r[1]));
const report2 = rows2.map((r) => day(r[2]));
const trueDelay = lines2("docs/data/example-changing-delays-true-delay.csv").map((r) =>
r.slice(2).map(Number)
);
const per = imputeLinelist({ onset: onset2, report: report2, delayPeriod: 14 });
console.log(per.generativeDelayByPeriod.map((f) => pmfMean(f).toFixed(1)).join(" "));
for (const [name, opts] of [
["static", {}],
["periods", { delayPeriod: 14 }],
["plug-in", { delay: trueDelay }]
]) {
const nc = nowcastLinelist({
onset: onset2,
report: report2,
nSamples: 1,
bootstrap: false,
minReported: 0.01,
...opts
});
console.log(name.padEnd(8) + nc.pointWeekly.slice(-4).map((x) => x.toFixed(1)).join(" "));
}
8.5 10.2 12.3 11.9 9.6 8.9 7.1 5.9 5.4
static 487.5 458.0 421.2 444.1
periods 466.1 428.6 361.0 297.8
plug-in 460.7 420.6 356.5 320.4
The true mean delay falls from 12 days in the first periods to 5 in the last. The eventual true onsets in the last four weeks are 456, 414, 374 and 317. The static delay is too long for recent onsets, so its nowcast is up to 40% too high. Per-period delays bring the error down to 4% on average, and the true delay to 2%.
Delays that change over time in the worked example goes through this in all four languages, and the Methods page has the maths.
Simulate a scenario and compare the methods¶
run() simulates an epidemic, imputes the missing onsets four ways and compares each with the truth.
Every parameter has a default in EpidemicDates.DEFAULTS.
Pass only what you want to change.
The parameters are listed under ScenarioParams.
const { run, knotsFrom } = EpidemicDates;
const res = run({
knots: knotsFrom([[0, 0.05], [60, 0.05], [110, -0.01], [175, 0.03]]), // growth rate per day
mu0: 6.6, mu1: 6.6, cv: 0.92, // onset-to-report delay: mean at start and end, CV
pi0: 0.15, pi1: 0.15, // fraction of onsets missing at start and end
retro: false, cutoff: 179, // cut the data at day 179 (real time)
nowcast: true // correct every method for right truncation
});
res.truth; // weekly true onsets
res.imputed.backsampling; // weekly onsets after imputation: backsampling, mean, fwd, gen
res.rt.backsampling; // weekly Rt from the imputed counts
res.metrics.backsampling.maxRel; // largest weekly relative error
res.metrics.backsampling.maxR; // largest Rt error
The result is a RunResult.
The error measures are described under Metrics.
The page's scenarios¶
The scenarios on the page are in EpidemicDates.PRESETS.
This prints the largest weekly relative error of the back-sampling and generative methods for each one:
for (const p of EpidemicDates.PRESETS) {
const m = run(p.set).metrics;
console.log(p.name.padEnd(40), m.backsampling.maxRel.toFixed(3), m.gen.maxRel.toFixed(3));
}
Baseline (BDBV-like) 0.039 0.000
Baseline, all reported -0.019 0.000
Late cases miss onsets -0.133 -0.110
More missing later -0.030 0.000
Delays shorten more 0.032 0.000
Ascertainment falls 0.043 0.000
Longer delays, more missing 0.188 0.000
Much longer delays, far more missing 0.821 0.000
Two waves 0.044 0.000
Oscillating 0.015 0.000
Sharp turnaround 0.040 0.000
Sharp turnaround, 8 days, 40% missing 0.156 0.000
Isiro 2012: slow, noisy 0.006 0.000
Steady growth 0.000 0.000
Steady, no growth 0.003 0.000
Steady decline 0.005 -0.000
The generative method is unbiased in most scenarios because it is given the true delay. It is biased in "Late cases miss onsets", where missingness depends on the delay and every method fails.
Building blocks¶
delayPmf(mu, cv)gives a double interval censored daily Gamma delay pmf.rToR(r)turns a growth rate into Rt.convImpute,shiftImputeandemImputeare the imputation methods on daily counts.stochasticruns Poisson replicates and returns 90% bands.
const f = EpidemicDates.delayPmf(8, 1); // mean 8 days, CV 1
EpidemicDates.pmfMean(f); // 8.00
EpidemicDates.rToR(0.05); // 1.94
See also¶
- Python guide for the stochastic cross-check and the same line-list function.
- Methods for the maths.
- Assumptions for what the results rely on.