Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : UQO Éval — Le Grand Défi Immobilier8 * File : tests/helpers/stats.ts9 * Module : tests10 * Repository : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website : https://jeux.uqo-eval.app12 * Created : 2026-11-2513 * Last Modified : 2026-11-2514 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/** Outils statistiques des tests (aucune dépendance). */4445/** Statistique du χ² d'ajustement à une loi uniforme sur `counts.length` classes. */46export function chiSquareUniform(counts: readonly number[]): number {47 const n = counts.reduce((a, b) => a + b, 0);48 const expected = n / counts.length;49 return counts.reduce((acc, c) => acc + (c - expected) ** 2 / expected, 0);50}5152/** Statistique du χ² d'ajustement à des effectifs attendus quelconques. */53export function chiSquare(counts: readonly number[], expected: readonly number[]): number {54 return counts.reduce((acc, c, i) => acc + (c - expected[i]!) ** 2 / expected[i]!, 0);55}5657/**58 * Valeurs critiques du χ² au seuil α = 0,001 (une fausse alerte sur mille si59 * l'hypothèse d'uniformité est vraie ; les graines étant fixes, le résultat60 * est de toute façon reproductible).61 */62export const CHI2_CRIT_001: Record<number, number> = { 1: 10.828, 2: 13.816, 3: 16.266, 5: 20.515, 23: 49.728, 24: 51.179 };6364/** Écart-type d'une proportion sous H0 : p = p0. */65export function seProportion(p0: number, n: number): number {66 return Math.sqrt((p0 * (1 - p0)) / n);67}6869/** Cote z d'une proportion observée par rapport à p0. */70export function zProportion(successes: number, n: number, p0: number): number {71 return (successes / n - p0) / seProportion(p0, n);72}7374export function pct(x: number, digits = 2): string {75 return `${(100 * x).toFixed(digits)} %`;76}77