viewof gamma = Inputs.range([0.1, 5.0], {value: 1.5, step: 0.1, label: html`supply sensitivity ${math`\gamma`}`})
viewof phi = Inputs.range([0.01, 0.99], {value: 0.33, step: 0.01, label: html`output elasticity of capital ${math`\phi`}`})
viewof deltap = Inputs.range([0.01, 0.20], {value: 0.08, step: 0.01, label: html`depreciation ${math`\delta`}`})
viewof sy = Inputs.range([0.0, 0.3], {value: 0.10, step: 0.01, label: html`investment sensitivity ${math`s_y`}`})
viewof rho = Inputs.range([0.0, 0.99], {value: 0.80, step: 0.01, label: html`monetary persistence ${math`\rho`}`})
viewof epsm = Inputs.range([-0.10, 0.10], {value: 0.05, step: 0.01, label: html`shock size ${math`\epsilon_m`}`})
viewof pinA = Inputs.button("Pin this run for comparison")
import {pinned, ghost, math, mathSvg} from "../theme/im-sim.js"
// --- model (ported 1:1 from the v1 Python notebook) ---
// Deterministic: one unanticipated shock ε_m at t = 5, horizon T = 50.
sim = {
const T = 50, shock_time = 5;
const k = new Array(T + 1).fill(0);
const m = new Array(T + 1).fill(0);
const y = new Array(T).fill(0);
const p = new Array(T).fill(0);
const p_e = new Array(T).fill(0);
for (let t = 0; t < T; t++) {
const shock_now = (t === shock_time) ? epsm : 0.0;
m[t] = t > 0 ? rho * m[t - 1] + shock_now : shock_now; // m_t = ρ m_{t−1} + ε_t
p_e[t] = (t > 0 ? rho * m[t - 1] : 0) - phi * k[t]; // pᵉ_t = ρ m_{t−1} − ϕ k_t
p[t] = (m[t] - phi * k[t] + gamma * p_e[t]) / (1.0 + gamma); // AD = AS solved for p_t
y[t] = m[t] - p[t]; // y_t = m_t − p_t
k[t + 1] = (1.0 - deltap) * k[t] + sy * y[t]; // k_{t+1} = (1−δ)k_t + s_y y_t
}
const pts = [];
for (let t = 0; t < T; t++) {
pts.push({t, series: "money m (impulse)", v: m[t]});
pts.push({t, series: "price level p", v: p[t]});
pts.push({t, series: "output y", v: y[t]});
pts.push({t, series: "capital k (propagation)", v: k[t]});
}
let iy = 0;
for (let t = 1; t < T; t++) if (Math.abs(y[t]) > Math.abs(y[iy])) iy = t;
return {pts, peakT: iy, peakY: y[iy], shock_time, impactY: y[shock_time], echoY: y[shock_time + 1]};
}
// λ: the factor by which the echo in output and capital shrinks each period
lam_eq = 1 - deltap + sy * phi
// The echo fades only when λ < 1 (s_y·ϕ < δ). At λ ≥ 1 it grows instead: the
// chart and the readout below say so, and the chart keeps its scale.
eq_fades = lam_eq < 1 - 1e-9
{
// comparison run for the real responses (output and capital); money and
// prices are left out to keep the gray lines readable
const real = sim.pts.filter(d => d.series === "output y" || d.series === "capital k (propagation)");
const base = pinned("equilibrium_model-1", {real}, pinA);
// When the echo grows without limit, hold the y-axis at the scale of the
// impulse and let the runaway lines leave the frame. Otherwise Plot would
// stretch the axis until the shock itself is too small to see.
const lim = 1.3 * Math.max(Math.abs(epsm), 0.01);
const live = eq_fades ? sim.pts : sim.pts.filter(d => Math.abs(d.v) <= 4 * lim);
const ghostPts = base.real.filter(d => Math.abs(d.v) <= 0.15); // a pinned runaway run must not stretch the axis
const series = name => live.filter(d => d.series === name);
const mark = eq_fades ? {t: sim.peakT, v: sim.peakY} : {t: sim.shock_time, v: sim.impactY};
return mathSvg(Plot.plot({
marginLeft: 55,
x: {label: "time $t$ →"},
y: {label: "log deviation from steady state", grid: true, ...(eq_fades ? {} : {domain: [-lim, lim]})},
color: {
domain: ["money m (impulse)", "price level p", "output y", "capital k (propagation)"],
range: ["#9aa0a6", "#c77d2e", "var(--im-accent)", "var(--im-sim)"],
legend: true
},
marks: [
Plot.ruleY([0], {stroke: "#ccc"}),
Plot.ruleX([sim.shock_time], {stroke: "#9aa0a6", strokeDasharray: "4 4"}),
Plot.line(ghostPts, {x: "t", y: "v", z: "series", clip: true, ...ghost}),
Plot.line(series("money m (impulse)"), {x: "t", y: "v", stroke: "series", strokeWidth: 2, clip: true}),
Plot.line(series("price level p"), {x: "t", y: "v", stroke: "series", strokeWidth: 1.5, clip: true}),
Plot.line(series("output y"), {x: "t", y: "v", stroke: "series", strokeWidth: 2, clip: true}),
Plot.line(series("capital k (propagation)"), {x: "t", y: "v", stroke: "series", strokeWidth: 2, strokeDasharray: "4 3", clip: true}),
Plot.dot([mark], {x: "t", y: "v", fill: "var(--im-accent)"}),
Plot.text([mark], {
x: "t", y: "v",
text: d => `${eq_fades ? "peak" : "impact"} $y$ = ${d.v.toFixed(3)} at $t$ = ${d.t}`,
dy: -10, dx: 4, textAnchor: "start", fill: "var(--im-accent)"
}),
...(eq_fades ? [] : [
Plot.text([`$\\lambda$ = ${lam_eq.toFixed(3)} is not below 1: the echo does not fade`], {
frameAnchor: "top-right", dx: -6, dy: 6, fontWeight: 600, fill: "#c77d2e"
})
])
]
}));
}