SPB Git

Repository Public

ordre-presentation.test.ts 6.3 KB 134 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : tests/statistical/ordre-presentation.test.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/**44 * Ordre présenté au participant.45 *46 * Reproduit à l'identique la dérivation de apps/api/src/game.ts (startSession, lignes 213-219) :47 *   rng = createRng(orderSeed) ; participantOrder(questions, rng) ;48 *   choice_order = rng.fork(`choix:${question.id}`).shuffle(choix triés par rang de prix)49 * Le test d'intégration vérifie le même point sur les `choice_order` réellement enregistrés en base.50 */51import { afterAll, describe, expect, it } from "vitest";52import { createRng, participantOrder } from "../../packages/engine/src/index.ts";53import { CHI2_CRIT_001, chiSquareUniform } from "../helpers/stats.ts";5455const measured: string[] = [];56afterAll(() => {57  console.log(`\n[MESURES · ordre de présentation]\n${measured.join("\n")}\n`);58});5960const QUESTIONS = Array.from({ length: 100 }, (_, i) => ({61  id: `00000000-0000-4000-8000-${String(i).padStart(12, "0")}`,62  phase: Math.floor(i / 25) + 1,63  // rang de prix de la bonne réponse, connu du serveur seulement64  correctRank: (i * 7) % 4,65}));6667function sessionOrders(sessions: number): { position: number[]; permutations: Map<string, number>; firstSlot: number[]; slotOfQuestion0: number[] } {68  const position = [0, 0, 0, 0];69  const firstSlot = [0, 0, 0, 0];70  const permutations = new Map<string, number>();71  const slotOfQuestion0 = new Array<number>(25).fill(0);72  for (let s = 0; s < sessions; s++) {73    const rng = createRng(`jeton-de-session-${s}`);74    const ordered = participantOrder(QUESTIONS, rng);75    slotOfQuestion0[ordered.findIndex((q) => q.id === QUESTIONS[0]!.id)]! += 1;76    for (const q of ordered) {77      const order = rng.fork(`choix:${q.id}`).shuffle([0, 1, 2, 3]);78      position[order.indexOf(q.correctRank)]! += 1;79      firstSlot[order[0]!]! += 1;80      const key = order.join("");81      permutations.set(key, (permutations.get(key) ?? 0) + 1);82    }83  }84  return { position, permutations, firstSlot, slotOfQuestion0 };85}8687describe("Ordre des choix présenté au participant (choice_order)", () => {88  const SESSIONS = 400; // 400 parties × 100 propriétés = 40 000 ordres89  const result = sessionOrders(SESSIONS);9091  it("place la bonne réponse uniformément aux quatre positions d'affichage (χ², 3 ddl, α = 0,001)", () => {92    const chi2 = chiSquareUniform(result.position);93    measured.push(`position affichée de la bonne réponse | n = ${SESSIONS * 100} | effectifs ${result.position.join(" / ")} | χ² = ${chi2.toFixed(2)} (seuil ${CHI2_CRIT_001[3]})`);94    expect(result.position.reduce((a, b) => a + b, 0)).toBe(SESSIONS * 100);95    expect(chi2).toBeLessThan(CHI2_CRIT_001[3]!);96  });9798  it("n'affiche pas plus souvent en premier le prix le plus bas (ou tout autre rang de prix)", () => {99    const chi2 = chiSquareUniform(result.firstSlot);100    measured.push(`rang de prix affiché en premier | effectifs ${result.firstSlot.join(" / ")} | χ² = ${chi2.toFixed(2)} (seuil ${CHI2_CRIT_001[3]})`);101    expect(chi2).toBeLessThan(CHI2_CRIT_001[3]!);102  });103104  it("utilise les 24 permutations de façon uniforme (χ², 23 ddl, α = 0,001)", () => {105    const counts = [...result.permutations.values()];106    const chi2 = chiSquareUniform(counts);107    measured.push(`permutations des quatre choix | 24 permutations observées : ${counts.length} | χ² = ${chi2.toFixed(2)} (seuil ${CHI2_CRIT_001[23]})`);108    expect(counts).toHaveLength(24);109    expect(chi2).toBeLessThan(CHI2_CRIT_001[23]!);110  });111112  it("donne à deux participants des ordres de choix différents pour la même propriété", () => {113    const orderFor = (seed: string): string => createRng(seed).fork(`choix:${QUESTIONS[0]!.id}`).shuffle([0, 1, 2, 3]).join("");114    const distinct = new Set(Array.from({ length: 200 }, (_, i) => orderFor(`participant-${i}`)));115    expect(distinct.size).toBeGreaterThan(20);116  });117118  it("est stable pour une même partie (reprise après redémarrage : même graine, même ordre)", () => {119    const once = createRng("graine-de-partie").fork(`choix:${QUESTIONS[3]!.id}`).shuffle([0, 1, 2, 3]);120    const again = createRng("graine-de-partie").fork(`choix:${QUESTIONS[3]!.id}`).shuffle([0, 1, 2, 3]);121    expect(again).toEqual(once);122  });123});124125describe("Ordre des propriétés propre à chaque participant", () => {126  it("place une propriété donnée uniformément aux 25 positions de sa phase (χ², 24 ddl, α = 0,001)", () => {127    const { slotOfQuestion0 } = sessionOrders(10_000);128    const chi2 = chiSquareUniform(slotOfQuestion0);129    measured.push(`position d'une propriété dans sa phase | n = 10 000 parties | χ² = ${chi2.toFixed(2)} (seuil ${CHI2_CRIT_001[24]})`);130    expect(slotOfQuestion0.reduce((a, b) => a + b, 0)).toBe(10_000);131    expect(chi2).toBeLessThan(CHI2_CRIT_001[24]!);132  });133});134
Files main
Menu

Theme