Repository Public
1import type { Locale } from "@/schema";2import { param, type SimConfig, type SimDef } from "../types";3import { Chart, chartSpec } from "../ui/Chart";4import { fmt } from "../ui/format";5import { Note, Readouts } from "../ui/Readouts";67export interface IrfModel {8 /** A = [[a11, a12], [a21, a22]] in y_t = A y_{t−1} + u_t. */9 a: [[number, number], [number, number]];10 /** Index (0 or 1) of the variable that receives the one-unit shock at h = 0. */11 shock: 0 | 1;12 /** irf[h] = A^h e_shock: response of (y1, y2) after h periods. */13 irf: [number, number][];14 /** Moduli of the two eigenvalues of A, largest first. */15 moduli: [number, number];16 complex: boolean;17 /** True when both moduli are below 1. */18 stationary: boolean;19}2021export function shockIndex(config: SimConfig): 0 | 1 {22 const s = config.shock;23 return s === 2 || s === "2" || s === "y2" ? 1 : 0;24}2526export function computeIrf(p: Readonly<Record<string, number>>, config: SimConfig): IrfModel {27 const a: [[number, number], [number, number]] = [[p.a11, p.a12], [p.a21, p.a22]];28 const shock = shockIndex(config), H = Math.round(p.horizon);29 let v: [number, number] = shock === 0 ? [1, 0] : [0, 1];30 const irf: [number, number][] = [v];31 for (let h = 1; h <= H; h++) { v = [a[0][0] * v[0] + a[0][1] * v[1] + 0, a[1][0] * v[0] + a[1][1] * v[1] + 0]; irf.push(v); } // + 0 turns −0 into 032 const tr = p.a11 + p.a22, det = p.a11 * p.a22 - p.a12 * p.a21, disc = tr * tr - 4 * det;33 let moduli: [number, number];34 if (disc < 0) { const r = Math.sqrt(det); moduli = [r, r]; }35 else { const s = Math.sqrt(disc), l1 = Math.abs((tr + s) / 2), l2 = Math.abs((tr - s) / 2); moduli = [Math.max(l1, l2), Math.min(l1, l2)]; }36 return { a, shock, irf, moduli, complex: disc < 0, stationary: moduli[0] < 1 };37}3839const TXT = {40 fr: { title: "Fonctions de réponse à un choc unitaire", h: "Horizon h", y: "Réponse", r: "Réponse de", shock: "Choc sur", mod: "Modules des valeurs propres", kind: "Valeurs propres", cx: "complexes conjuguées (oscillations)", re: "réelles", stat: "Stationnarité", yes: "oui (modules < 1)", no: "non", warn: "Au moins une valeur propre a un module ≥ 1 : le VAR n'est pas stationnaire et la réponse ne revient pas vers zéro.", last: "Réponses au dernier horizon" },41 en: { title: "Responses to a one-unit shock", h: "Horizon h", y: "Response", r: "Response of", shock: "Shock to", mod: "Eigenvalue moduli", kind: "Eigenvalues", cx: "complex conjugates (oscillations)", re: "real", stat: "Stationarity", yes: "yes (moduli < 1)", no: "no", warn: "At least one eigenvalue has modulus ≥ 1: the VAR is not stationary and the response does not return to zero.", last: "Responses at the last horizon" },42} as const;4344function IrfView({ model: m, locale, idPrefix }: { model: IrfModel; locale: Locale; idPrefix: string }) {45 const L = TXT[locale];46 const names = ["y₁", "y₂"];47 const last = m.irf[m.irf.length - 1];48 const chart = chartSpec({49 kind: "line", dataKind: "illustrative", title: L.title, xLabel: L.h, yLabel: L.y,50 series: [51 { name: `${L.r} y₁`, tone: "blue", points: m.irf.map((v, h) => ({ x: h, y: v[0] })) },52 { name: `${L.r} y₂`, tone: "amber", points: m.irf.map((v, h) => ({ x: h, y: v[1] })) },53 ],54 annotations: [{ y: 0, label: "0" }],55 });56 return (57 <>58 <Chart chart={chart} locale={locale} idPrefix={idPrefix} />59 {m.stationary ? null : <Note tone="warn">{L.warn}</Note>}60 <Readouts items={[61 { label: L.shock, value: names[m.shock] },62 { label: L.mod, value: `${fmt(locale, m.moduli[0], 3)} ; ${fmt(locale, m.moduli[1], 3)}`, tone: m.stationary ? "blue" : "red" },63 { label: L.kind, value: m.complex ? L.cx : L.re },64 { label: L.stat, value: m.stationary ? L.yes : L.no, tone: m.stationary ? "green" : "red" },65 { label: L.last, value: `${fmt(locale, last[0], 3)} ; ${fmt(locale, last[1], 3)}` },66 ]} />67 </>68 );69}7071export const impulseResponse: SimDef<IrfModel> = {72 kind: "impulse-response",73 title: { fr: "VAR(1) bivarié : réponses impulsionnelles", en: "Bivariate VAR(1): impulse responses" },74 description: {75 fr: "$y_t = A\\, y_{t-1} + u_t$. La réponse après $h$ périodes à un choc d'une unité sur une variable est $A^h e_j$. Le système est stationnaire si les deux valeurs propres de $A$ ont un module inférieur à 1. Choc unitaire non orthogonalisé : aucune décomposition de Cholesky, donc aucun effet contemporain sur l'autre variable.",76 en: "$y_t = A\\, y_{t-1} + u_t$. The response after $h$ periods to a one-unit shock to one variable is $A^h e_j$. The system is stationary when both eigenvalues of $A$ have modulus below 1. Non-orthogonalised unit shock: no Cholesky decomposition, hence no contemporaneous effect on the other variable.",77 },78 params: [79 param("a11", "a₁₁ (y₁ → y₁)", "a₁₁ (y₁ → y₁)", -1.2, 1.2, 0.05, 0.6),80 param("a12", "a₁₂ (y₂ → y₁)", "a₁₂ (y₂ → y₁)", -1.2, 1.2, 0.05, 0.2),81 param("a21", "a₂₁ (y₁ → y₂)", "a₂₁ (y₁ → y₂)", -1.2, 1.2, 0.05, 0.3),82 param("a22", "a₂₂ (y₂ → y₂)", "a₂₂ (y₂ → y₂)", -1.2, 1.2, 0.05, 0.5),83 param("horizon", "Horizon", "Horizon", 5, 40, 1, 20),84 ],85 stochastic: false,86 configKeys: ["shock"],87 validateConfig: (c) => (c.shock === undefined || [1, 2, "1", "2", "y1", "y2"].includes(c.shock as string | number) ? [] : [`config.shock: expected 1 or 2 (got ${JSON.stringify(c.shock)})`]),88 compute: (p, _seed, c) => computeIrf(p, c),89 View: IrfView,90};91