maShocks = {
const draw = d3.randomNormal.source(d3.randomLcg(2026))(0, 1)
return Float64Array.from({length: 304}, draw)
}
// theta_0 = 1, then the four slider values
maCoefs = [1, ...theta]
// y_t = sum_i theta_i e_{t-i}
maPath = d3.range(4, maShocks.length).map(t => ({
t: t - 3,
y: d3.sum(maCoefs, (c, i) => c * maShocks[t - i])
}))
// gamma_h / sigma^2 = sum_i theta_i theta_{i+h}, which is zero for h > 4
maGamma = h => d3.sum(maCoefs.slice(0, Math.max(maCoefs.length - h, 0)), (c, i) => c * maCoefs[i + h])
maACF = d3.range(13).map(h => ({h, rho: maGamma(h) / maGamma(0)}))
// q = index of the last nonzero theta (0 means white noise)
maQ = d3.max(maCoefs.map((c, i) => (c !== 0 ? i : 0)))
maPanel = (title, plot) => html`<div>
<div style="text-align: center">${title}</div>
${plot}
</div>`
html`<div style="display: flex; gap: 20px; justify-content: center; font-size: 19px">
${maPanel(html`One path, Var(<i>y<sub>t</sub></i>) = ${maGamma(0).toFixed(2)}`, Plot.plot({
width: 590, height: 380, marginLeft: 50, marginBottom: 45,
style: {fontSize: "18px", fontFamily: "Arial, Arimo, sans-serif"},
x: {label: "Periods", labelAnchor: "center", labelArrow: "none"},
y: {label: null, domain: [-6, 6], ticks: 5},
grid: true,
marks: [
Plot.ruleY([0], {stroke: "black", strokeDasharray: "5,4"}),
Plot.line(maPath, {x: "t", y: "y", stroke: "#0072BC", strokeWidth: 1.3, clip: true})
]
}))}
${maPanel(html`Autocorrelation <i>ρ<sub>h</sub></i>, <i>ρ</i><sub>1</sub> = ${maACF[1].rho.toFixed(2)}`, Plot.plot({
width: 590, height: 380, marginLeft: 50, marginBottom: 45,
style: {fontSize: "18px", fontFamily: "Arial, Arimo, sans-serif"},
x: {label: "Lag h", domain: [-0.5, 12.5], ticks: d3.range(0, 13, 2), labelAnchor: "center", labelArrow: "none"},
y: {label: null, domain: [-1, 1], ticks: 5},
grid: true,
marks: [
Plot.ruleY([0], {stroke: "black"}),
Plot.ruleX([maQ + 0.5], {stroke: "#EB811B", strokeWidth: 2, strokeDasharray: "7,5"}),
Plot.ruleX(maACF, {x: "h", y1: 0, y2: "rho", stroke: "#0072BC", strokeWidth: 2.5}),
Plot.dot(maACF, {x: "h", y: "rho", fill: "#0072BC", r: 5}),
Plot.text([maQ === 0 ? "white noise: ρ = 0 at every lag h ≥ 1" : `ρ = 0 at every lag h > q = ${maQ}`], {frameAnchor: "top-right", dx: -10, dy: 10, fill: "#EB811B", fontWeight: "bold", fontSize: 20, stroke: "white", strokeWidth: 6})
]
}))}
</div>`