SPB Git

Repository Public

garch.tsx 5.1 KB 85 lines tsx

1import type { Locale } from "@/schema";2import { createRng, variance } from "../core";3import { param, type SimDef } from "../types";4import { Chart, chartSpec } from "../ui/Chart";5import { fmt } from "../ui/format";6import { Note, Readouts } from "../ui/Readouts";78/** α + β is kept at or below this value so that the unconditional variance exists. */9export const GARCH_MAX_PERSISTENCE = 0.999;1011export interface GarchModel {12  omega: number; alpha: number;13  /** β actually used (reduced when α + β would reach 1). */14  beta: number;15  clamped: boolean;16  persistence: number;17  /** ω / (1 − α − β). */18  unconditionalVariance: number;19  /** Periods for a volatility shock to halve: ln 0.5 / ln(α + β). */20  halfLife: number;21  returns: number[];22  /** Conditional standard deviation σ_t. */23  sigma: number[];24  sampleVariance: number;25}2627/** r_t = σ_t z_t, z_t ~ N(0, 1), σ²_t = ω + α r²_{t−1} + β σ²_{t−1}, started at the unconditional variance. */28export function computeGarch(p: Readonly<Record<string, number>>, seed: number): GarchModel {29  const omega = p.omega, alpha = Math.min(p.alpha, GARCH_MAX_PERSISTENCE), n = Math.round(p.n);30  const beta = Math.min(p.beta, GARCH_MAX_PERSISTENCE - alpha);31  const persistence = alpha + beta;32  const uncond = omega / (1 - persistence);33  const rng = createRng(seed, "garch");34  const returns: number[] = [], sigma: number[] = [];35  let s2 = uncond;36  for (let t = 0; t < n; t++) {37    const sd = Math.sqrt(s2), r = sd * rng.normal();38    returns.push(r); sigma.push(sd);39    s2 = omega + alpha * r * r + beta * s2;40  }41  return { omega, alpha, beta, clamped: beta < p.beta || alpha < p.alpha, persistence, unconditionalVariance: uncond, halfLife: persistence > 0 ? Math.log(0.5) / Math.log(persistence) : 0, returns, sigma, sampleVariance: variance(returns) };42}4344const TXT = {45  fr: { r: "Rendements r_t", s: "Volatilité conditionnelle σ_t", t: "Période t", ry: "Rendement", sy: "σ_t", pers: "Persistance α + β", uv: "Variance inconditionnelle ω/(1−α−β)", usd: "Écart-type inconditionnel", sv: "Variance de l'échantillon", hl: "Demi-vie d'un choc (périodes)", clamp: "α + β atteindrait 1 : β est ramené à", lr: "σ de long terme" },46  en: { r: "Returns r_t", s: "Conditional volatility σ_t", t: "Period t", ry: "Return", sy: "σ_t", pers: "Persistence α + β", uv: "Unconditional variance ω/(1−α−β)", usd: "Unconditional standard deviation", sv: "Sample variance", hl: "Shock half-life (periods)", clamp: "α + β would reach 1: β is reduced to", lr: "long-run σ" },47} as const;4849function GarchView({ model: m, locale, idPrefix }: { model: GarchModel; locale: Locale; idPrefix: string }) {50  const L = TXT[locale];51  const series = (ys: number[]) => ys.map((y, i) => ({ x: i + 1, y }));52  return (53    <>54      <Chart locale={locale} idPrefix={`${idPrefix}-r`} height={240} chart={chartSpec({ kind: "line", dataKind: "synthetic", title: L.r, xLabel: L.t, yLabel: L.ry, series: [{ name: L.r, tone: "blue", points: series(m.returns) }] })} />55      <Chart locale={locale} idPrefix={`${idPrefix}-s`} height={240} chart={chartSpec({ kind: "line", dataKind: "synthetic", title: L.s, xLabel: L.t, yLabel: L.sy, series: [{ name: L.s, tone: "amber", points: series(m.sigma) }], annotations: [{ y: Math.sqrt(m.unconditionalVariance), label: L.lr }] })} />56      {m.clamped ? <Note tone="warn">{`${L.clamp} ${fmt(locale, m.beta, 3)}.`}</Note> : null}57      <Readouts items={[58        { label: L.pers, value: fmt(locale, m.persistence, 3), tone: "amber" },59        { label: L.uv, value: fmt(locale, m.unconditionalVariance, 4) },60        { label: L.usd, value: fmt(locale, Math.sqrt(m.unconditionalVariance), 4) },61        { label: L.sv, value: fmt(locale, m.sampleVariance, 4) },62        { label: L.hl, value: fmt(locale, m.halfLife, 1) },63      ]} />64    </>65  );66}6768export const garch: SimDef<GarchModel> = {69  kind: "garch",70  title: { fr: "GARCH(1,1) : grappes de volatilité", en: "GARCH(1,1): volatility clustering" },71  description: {72    fr: "$r_t = \\sigma_t z_t$ avec $z_t \\sim \\mathcal{N}(0, 1)$ et $\\sigma_t^2 = \\omega + \\alpha\\, r_{t-1}^2 + \\beta\\, \\sigma_{t-1}^2$. La variance de long terme $\\omega/(1 - \\alpha - \\beta)$ n'existe que si $\\alpha + \\beta < 1$ : au-delà, $\\beta$ est réduit automatiquement. Innovations gaussiennes, moyenne nulle : les rendements réels ont des queues plus épaisses.",73    en: "$r_t = \\sigma_t z_t$ with $z_t \\sim \\mathcal{N}(0, 1)$ and $\\sigma_t^2 = \\omega + \\alpha\\, r_{t-1}^2 + \\beta\\, \\sigma_{t-1}^2$. The long-run variance $\\omega/(1 - \\alpha - \\beta)$ exists only when $\\alpha + \\beta < 1$: beyond that, $\\beta$ is reduced automatically. Gaussian innovations, zero mean: real returns have fatter tails.",74  },75  params: [76    param("omega", "Constante ω", "Constant ω", 0.01, 1, 0.01, 0.05),77    param("alpha", "Réaction aux chocs α", "Shock reaction α", 0, 0.5, 0.01, 0.1),78    param("beta", "Mémoire β", "Memory β", 0, 0.99, 0.01, 0.85),79    param("n", "Périodes n", "Periods n", 100, 600, 50, 400),80  ],81  stochastic: true,82  compute: (p, seed) => computeGarch(p, seed),83  View: GarchView,84};85
Files main
Menu

Theme