adasPathOf = rows => {
const p = rows.filter(d => d.series === "inflation");
const g = rows.filter(d => d.series === "output gap");
return p.filter(d => d.t >= 1 && d.t <= 6)
.map(d => ({t: d.t, pi: d.value, gap: g.find(e => e.t === d.t).value}));
}
{
const base = pinned("adassim-1", {sim}, pinA);
const live = adasPathOf(sim), old = adasPathOf(base.sim);
const xsAll = [...live, ...old].map(d => d.gap).concat([0]);
const ysAll = [...live, ...old].map(d => d.pi).concat([pi_star, pi_e]);
const span = (lo, hi, min) => { const w = Math.max(hi - lo, min), m = (lo + hi) / 2; return [m - 0.6 * w, m + 0.6 * w]; };
const [xLo, xHi] = span(Math.min(...xsAll), Math.max(...xsAll), 2);
const [yLo, yHi] = span(Math.min(...ysAll), Math.max(...ysAll), 1);
// short-run AS lines for periods 1–3 (each anchored on the previous period's inflation)
const asLines = [1, 2, 3].map(t => {
const prev = t === 1 ? pi_e : live[t - 2].pi;
return {t, pts: [{gap: xLo, pi: prev + lamb * xLo}, {gap: xHi, pi: prev + lamb * xHi}]};
});
const rho = 1 - lamb * alpha * (phi_pi - 1);
const kAD = rho > 1e-9 ? -alpha * (phi_pi - 1) / rho : null;
const adLine = kAD === null ? [] : [{gap: kAD * (yLo - pi_star), pi: yLo}, {gap: kAD * (yHi - pi_star), pi: yHi}];
// vertical AD lines for periods 1–3 (skip one that would sit on top of the previous one)
const adV = live.filter(d => d.t <= 3)
.filter((d, j, a) => j === 0 || Math.abs(d.gap - a[j - 1].gap) > 0.03 * (xHi - xLo));
// label the adjustment line at a point that is inside both axes
const adLabel = (() => {
if (kAD === null) return null;
const piL = yLo + 0.88 * (yHi - yLo);
let gapL = kAD * (piL - pi_star);
const lo = xLo + 0.04 * (xHi - xLo), hi = xHi - 0.24 * (xHi - xLo);
if (gapL >= lo && gapL <= hi) return {gap: gapL, pi: piL};
gapL = Math.min(Math.max(gapL, lo), hi);
return {gap: gapL, pi: Math.abs(kAD) > 1e-9 ? pi_star + gapL / kAD : piL};
})();
return mathSvg(Plot.plot({
marginLeft: 50, height: 340,
x: {label: "output gap $(Y - \\bar Y)$ →", domain: [xLo, xHi]},
y: {label: "inflation $\\pi$", grid: true, domain: [yLo, yHi]},
marks: [
Plot.ruleX([0], {stroke: "var(--im-fg-muted)", strokeOpacity: 0.6}),
Plot.ruleY([pi_star], {stroke: "var(--im-sim)", strokeDasharray: "4 4"}),
Plot.line(old, {x: "gap", y: "pi", ...ghost, clip: true}),
...asLines.map(l => Plot.line(l.pts, {x: "gap", y: "pi", stroke: "var(--im-warn)", strokeWidth: 1.25, strokeOpacity: 1 - 0.25 * (l.t - 1), clip: true})),
...asLines.map(l => Plot.text([l.pts[1]], {x: "gap", y: "pi", text: () => `$AS_${l.t}$`, fill: "var(--im-warn)", textAnchor: "end", dx: -4, dy: -8, clip: true})),
...adV.map(d => Plot.ruleX([d.gap], {stroke: "var(--im-accent)", strokeWidth: 1.25, strokeOpacity: 1 - 0.25 * (d.t - 1)})),
...adV.map(d => Plot.text([{gap: d.gap, pi: yHi}], {x: "gap", y: "pi", text: () => `$AD_${d.t}$`, fill: "var(--im-accent)", textAnchor: "start", dx: 4, dy: 10})),
...(adLine.length ? [
Plot.line(adLine, {x: "gap", y: "pi", stroke: "var(--im-accent)", strokeWidth: 1.25, strokeDasharray: "2 3", clip: true}),
Plot.text([adLabel], {x: "gap", y: "pi", text: () => "adjustment line", fill: "var(--im-accent)", dx: 10, dy: -8, textAnchor: "start"})
] : []),
Plot.line(live, {x: "gap", y: "pi", stroke: "var(--im-accent)", strokeWidth: 2}),
Plot.dot(live, {x: "gap", y: "pi", fill: "var(--im-accent)", r: 3.5}),
Plot.text(live.filter(d => d.t <= 3), {x: "gap", y: "pi", text: d => `$t$ = ${d.t}`, dx: -8, dy: 12, textAnchor: "end", fill: "var(--im-accent)"}),
Plot.dot([{gap: 0, pi: pi_star}], {x: "gap", y: "pi", stroke: "var(--im-sim)", fill: "none", r: 6, strokeWidth: 2}),
Plot.text([{gap: 0, pi: pi_star}], {x: "gap", y: "pi", text: () => "long run: gap 0, $\\pi = \\pi^*$", dx: -8, dy: 14, textAnchor: "end", fill: "var(--im-sim)"})
],
caption: html`AD–AS view of the same run, periods 1 to 6. Vertical blue lines are demand and gold lines are short-run supply in periods 1 to 3. Each period the inflation gap is multiplied by ${math`1 - \lambda\alpha(\varphi_\pi - 1)`} = ${rho.toFixed(2)}${Math.abs(rho) < 1 ? ", so it shrinks" : Math.abs(rho) > 1 ? ", so it grows" : ", so it never shrinks"}.`
}));
}