SPB Git

Repository Public

correlation.tsx 3.4 KB 64 lines tsx

1import type { Locale } from "@/schema";2import { createRng, normalInv, pearson } from "../core";3import { param, type SimDef } from "../types";4import { Chart, chartSpec } from "../ui/Chart";5import { fmt, fmtInt } from "../ui/format";6import { Readouts } from "../ui/Readouts";78export interface CorrelationModel {9  rho: number; n: number;10  points: { x: number; y: number }[];11  /** Sample Pearson correlation. */12  r: number;13  /** 95 % interval for ρ by Fisher's z transform: tanh(atanh r ± 1.96/√(n−3)). */14  ciLo: number; ciHi: number;15}1617export function computeCorrelation(p: Readonly<Record<string, number>>, seed: number): CorrelationModel {18  const n = Math.round(p.n), rho = p.rho;19  const rng = createRng(seed, "corr");20  const c = Math.sqrt(1 - rho * rho);21  const points = Array.from({ length: n }, () => { const z1 = rng.normal(), z2 = rng.normal(); return { x: z1, y: rho * z1 + c * z2 }; });22  const r = pearson(points.map((q) => q.x), points.map((q) => q.y));23  const z = Math.atanh(r), h = normalInv(0.975) / Math.sqrt(n - 3);24  return { rho, n, points, r, ciLo: Math.tanh(z - h), ciHi: Math.tanh(z + h) };25}2627const TXT = {28  fr: { title: "Échantillon d'une loi normale bivariée", s: "observations", rho: "Corrélation vraie ρ", r: "Corrélation de l'échantillon r", ci: "Intervalle à 95 % pour ρ (Fisher)", n: "Observations", r2: "r²" },29  en: { title: "Sample from a bivariate normal distribution", s: "observations", rho: "True correlation ρ", r: "Sample correlation r", ci: "95% interval for ρ (Fisher)", n: "Observations", r2: "r²" },30} as const;3132function CorrelationView({ model: m, locale, idPrefix }: { model: CorrelationModel; locale: Locale; idPrefix: string }) {33  const L = TXT[locale];34  const chart = chartSpec({ kind: "scatter", dataKind: "synthetic", title: L.title, xLabel: "x", yLabel: "y", series: [{ name: L.s, tone: "blue", points: m.points }] });35  return (36    <>37      <Chart chart={chart} locale={locale} idPrefix={idPrefix} height={360} />38      <Readouts items={[39        { label: L.rho, value: fmt(locale, m.rho, 2) },40        { label: L.r, value: fmt(locale, m.r, 3), tone: "blue" },41        { label: L.ci, value: `[${fmt(locale, m.ciLo, 3)} ; ${fmt(locale, m.ciHi, 3)}]` },42        { label: L.r2, value: fmt(locale, m.r * m.r, 3) },43        { label: L.n, value: fmtInt(locale, m.n) },44      ]} />45    </>46  );47}4849export const correlation: SimDef<CorrelationModel> = {50  kind: "correlation",51  title: { fr: "Corrélation : valeur vraie et valeur d'échantillon", en: "Correlation: true value and sample value" },52  description: {53    fr: "$n$ couples tirés d'une loi normale bivariée centrée réduite de corrélation $\\rho$ : $x = z_1$, $y = \\rho\\, z_1 + \\sqrt{1 - \\rho^2}\\, z_2$. Le coefficient de Pearson $r$ de l'échantillon diffère de $\\rho$, d'autant plus que $n$ est petit ; l'intervalle utilise la transformation $z$ de Fisher.",54    en: "$n$ pairs drawn from a standard bivariate normal distribution with correlation $\\rho$: $x = z_1$, $y = \\rho\\, z_1 + \\sqrt{1 - \\rho^2}\\, z_2$. The sample Pearson coefficient $r$ differs from $\\rho$, the more so when $n$ is small; the interval uses Fisher's $z$ transform.",55  },56  params: [57    param("rho", "Corrélation vraie ρ", "True correlation ρ", -0.99, 0.99, 0.01, 0.6),58    param("n", "Observations n", "Observations n", 10, 500, 10, 150),59  ],60  stochastic: true,61  compute: (p, seed) => computeCorrelation(p, seed),62  View: CorrelationView,63};64
Files main
Menu

Theme