SPB Git

Repository Public

verif-terminaisons.ts 6.2 KB 84 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : scripts/verif-terminaisons.ts9 * Module           : scripts10 * 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// Vérifie sur données réelles qu'aucune stratégie fondée sur la terminaison des prix ne dépasse sensiblement 25 %.44import { DatabaseSync } from "node:sqlite";45import { DEFAULT_NORMALIZE_OPTIONS, DIFFICULTIES, createRng, dedupeProperties, generateChoices, isEligible, normalizeListing, type RawListing } from "../packages/engine/src/index.ts";46const db = new DatabaseSync(process.argv[2] ?? "data/dev/listings.db", { readOnly: true });47const rows = db.prepare("select * from listings").all() as unknown as RawListing[];48const props = dedupeProperties(rows.map((r) => normalizeListing(r, [], { ...DEFAULT_NORMALIZE_OPTIONS, now: new Date() })).flatMap((r) => (r.ok ? [r.property] : []))).filter((p) => isEligible(p));49const half = Math.floor(props.length / 2);50const train = props.slice(0, half), test = props.slice(half);51const market = props.map((p) => p.price).sort((a, b) => a - b);52const freq = (mod: number) => { const m = new Map<number, number>(); for (const p of train) m.set(p.price % mod, (m.get(p.price % mod) ?? 0) + 1); return m; };53const med = market[Math.floor(market.length / 2)]!; const tm = train.map((p) => p.price).sort((a, b) => a - b); const lb = (a: number[], x: number) => { let lo = 0, hi = a.length; while (lo < hi) { const m = (lo + hi) >> 1; if (a[m]! < x) lo = m + 1; else hi = m; } return lo; }; const dens = (x: number) => lb(tm, x * 1.05) - lb(tm, x / 1.05); const cnt = new Map<number, number>(); for (const p of train) cnt.set(p.price, (cnt.get(p.price) ?? 0) + 1);54const cntAll = new Map<number, number>(); for (const x of market) cntAll.set(x, (cntAll.get(x) ?? 0) + 1);55const f100 = freq(100_000), f10 = freq(10_000), f1 = freq(1_000);56const round = (p: number) => { let z = 0; let v = p; while (v % 10 === 0 && v > 0) { z++; v /= 10; } return z; };57const argBest = (xs: number[], score: (x: number) => number, rng: ReturnType<typeof createRng>, sign = 1) => { let best: number[] = []; let bs = -Infinity; for (const x of xs) { const s = sign * score(x); if (s > bs + 1e-12) { bs = s; best = [x]; } else if (Math.abs(s - bs) <= 1e-12) best.push(x); } return rng.pick(best); };58for (const d of DIFFICULTIES) {59  const hits: Record<string, number> = {}; let n = 0; const rankCount = [0, 0, 0, 0]; let fallback = 0; let adj = 0; const gaps: number[] = [];60  for (let rep = 0; rep < 5; rep++) for (const p of test) {61    const g = generateChoices(p.price, d, createRng(`${d}:${rep}:${p.sourceUid}`), { marketPrices: market });62    const tie = createRng(`t:${rep}:${p.sourceUid}`); n++; rankCount[g.correctRank]!++; if (g.rankAdjusted) adj++; gaps.push(...g.gaps);63    if (g.source !== "market") fallback++;64    const strat: Record<string, number> = {65      "plus bas": g.prices[0], "2e": g.prices[1], "3e": g.prices[2], "plus haut": g.prices[3],66      "terminaison fréquente /100k": argBest([...g.prices], (x) => f100.get(x % 100_000) ?? 0, tie),67      "terminaison fréquente /10k": argBest([...g.prices], (x) => f10.get(x % 10_000) ?? 0, tie),68      "terminaison fréquente /1k": argBest([...g.prices], (x) => f1.get(x % 1_000) ?? 0, tie),69      "terminaison rare /100k": argBest([...g.prices], (x) => f100.get(x % 100_000) ?? 0, tie, -1),70      "plus proche de la médiane du marché": argBest([...g.prices], (x) => -Math.abs(Math.log(x / med)), tie),71      "prix le plus courant (densité ±5 %)": argBest([...g.prices], (x) => dens(x), tie),72      "prix exact le plus fréquent": argBest([...g.prices], (x) => cnt.get(x) ?? 0, tie),73      "prix exact le plus rare": argBest([...g.prices], (x) => cnt.get(x) ?? 0, tie, -1),74      "prix exact le plus fréquent (marché complet)": argBest([...g.prices], (x) => cntAll.get(x) ?? 0, tie),75      "prix exact le plus rare (marché complet)": argBest([...g.prices], (x) => cntAll.get(x) ?? 0, tie, -1),76      "plus rond": argBest([...g.prices], round, tie), "moins rond": argBest([...g.prices], round, tie, -1),77    };78    for (const [k, v] of Object.entries(strat)) if (v === p.price) hits[k] = (hits[k] ?? 0) + 1;79  }80  gaps.sort((a, b) => a - b); console.log(d, "n=", n, "écarts p5/p50/p95 %", [0.05, 0.5, 0.95].map((q) => (100 * gaps[Math.floor(gaps.length * q)]!).toFixed(1)).join("/"), "ajust.rang", (100 * adj / n).toFixed(2), "%", "rangs", rankCount.map((c) => (100 * c / n).toFixed(1)).join("/"), "hors pas de rangs", (100 * fallback / n).toFixed(2) + " %");81  console.log("   " + Object.entries(hits).map(([k, v]) => `${k}: ${(100 * v / n).toFixed(1)}`).join(" | "));82}83function lowerHas(x: number): boolean { let lo = 0, hi = market.length; while (lo < hi) { const m = (lo + hi) >> 1; if (market[m]! < x) lo = m + 1; else hi = m; } return market[lo] === x; }84
Files main
Menu

Theme