viewof cutoff = Inputs.range([1, 30], {
value: 7, step: 1, label: "cutoff C, days"
})
mu = 1.6
sigma = 0.6
pdf = (t) => t <= 0 ? 0 :
Math.exp(-((Math.log(t) - mu) ** 2) / (2 * sigma ** 2)) /
(t * sigma * Math.sqrt(2 * Math.PI))
grid = Array.from({length: 601}, (_, i) => i * 0.05)
step = 0.05
cdfC = grid.filter((t) => t <= cutoff)
.reduce((s, t) => s + pdf(t) * step, 0)
seenMean = grid.filter((t) => t <= cutoff)
.reduce((s, t) => s + t * pdf(t) * step, 0) / cdfC
trueMean = Math.exp(mu + sigma ** 2 / 2)
rows = grid.flatMap((t) => [
{t, density: pdf(t), which: "true delay distribution"},
{t, density: t <= cutoff ? pdf(t) / cdfC : 0,
which: `seen by day ${cutoff}`}
])
Plot.plot({
width: 1000, height: 560, marginLeft: 70, marginBottom: 60,
marginTop: 40, style: {fontSize: "20px"},
x: {label: "delay (days)", domain: [0, 30], labelAnchor: "center",
labelOffset: 50},
y: {label: "density", labelAnchor: "top", labelOffset: 30,
ticks: 5},
color: {legend: true, domain: ["true delay distribution",
`seen by day ${cutoff}`], range: ["#4a5899", "#b5432f"]},
marks: [
Plot.areaY(rows, {x: "t", y: "density", fill: "which",
fillOpacity: 0.25}),
Plot.line(rows, {x: "t", y: "density", stroke: "which",
strokeWidth: 3}),
Plot.ruleX([trueMean], {stroke: "#4a5899", strokeDasharray: "6,4"}),
Plot.ruleX([seenMean], {stroke: "#b5432f", strokeDasharray: "6,4"}),
Plot.text([{x: trueMean, y: 0.30,
label: `true mean ${trueMean.toFixed(1)}`}],
{x: "x", y: "y", text: "label", fill: "#4a5899", dx: 8,
textAnchor: "start"}),
Plot.text([{x: seenMean, y: 0.36,
label: `mean seen ${seenMean.toFixed(1)}`}],
{x: "x", y: "y", text: "label", fill: "#b5432f", dx: 8,
textAnchor: "start"})
]
})