election-game/backend/apps/sim/src/calibrate-local.ts

170 lines
8.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 管線劇本的校準(2022 縣市長、2024 區域立委、2024 總統):
* node src/calibrate-local.ts [--set 2024-legislator] [--only 63000-01,10018] [--rounds 7] [--seeds 3]
*
* 每場選舉:
* 1. 歷史重演:調整每位候選人的個人魅力與投票率倍率,讓 AI 對打的全市得票率接近 2022 年真實結果。
* 2. 勢均力敵:從歷史重演的值出發,讓前兩名平分兩人的總得票率,其他人維持真實得票率。
* 只用全市總數校準;各區、各里的誤差是驗證,寫進 calibration.json 與報告。
*/
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { parseArgs } from "node:util";
import { scenariosFromSpec, CONTENT } from "@election2026/content";
import { DEFAULT_PARAMS, autoplay, regionCodeOf, type PackData, type Scenario, type World } from "@election2026/engine";
import { DEFAULT_SCENARIOS_ROOT, loadAllSpecs, loadCalibration, loadPack } from "@election2026/pack-loader";
import type { LocalCalibration, LocalElectionSpec } from "@election2026/protocol";
type Entry = LocalCalibration["counties"][string];
const mean = (xs: number[]) => xs.reduce((a, b) => a + b, 0) / (xs.length || 1);
const normalize = (row: number[]) => {
const s = row.reduce((a, b) => a + b, 0) || 1;
return row.map((x) => x / s);
};
interface Sim {
county: number[];
turnout: number;
towns: Map<string, number[]>;
villages: Map<string, number[]>;
}
function simulate(world: World, seeds: number[]): Sim {
const ids = world.scenario.candidates.map((c) => c.id);
const county: number[][] = [];
const turnout: number[] = [];
const towns = new Map<string, number[][]>();
const villages = new Map<string, number[][]>();
for (const seed of seeds) {
const r = autoplay(world, `cal-${seed}`, seed).result!;
county.push(normalize(ids.map((id) => r.votes[id] ?? 0)));
turnout.push(r.ballots / r.electors);
for (const [code, row] of Object.entries(r.byTown)) towns.set(code, [...(towns.get(code) ?? []), normalize(ids.map((id) => row[id] ?? 0))]);
for (const [code, row] of Object.entries(r.byVillage)) villages.set(code, [...(villages.get(code) ?? []), normalize(ids.map((id) => row[id] ?? 0))]);
}
const avg = (rows: number[][]) => ids.map((_, c) => mean(rows.map((r) => r[c]!)));
return {
county: avg(county),
turnout: mean(turnout),
towns: new Map([...towns].map(([k, v]) => [k, avg(v)])),
villages: new Map([...villages].map(([k, v]) => [k, avg(v)])),
};
}
function truthOf(spec: LocalElectionSpec) {
const numbers = spec.candidates.map((c) => `#${c.number}`);
let electors = 0;
let ballots = 0;
const towns = new Map<string, number[]>();
const villages = new Map<string, { shares: number[]; valid: number }>();
for (const [code, v] of Object.entries(spec.byVillage)) {
electors += v.electors;
ballots += v.ballots;
const row = numbers.map((n) => v.votes[n] ?? 0);
const town = regionCodeOf(code, spec.regionLevel);
const prev = towns.get(town) ?? numbers.map(() => 0);
towns.set(town, prev.map((x, i) => x + row[i]!));
villages.set(code, { shares: normalize(row), valid: v.valid });
}
const total = numbers.map(() => 0);
for (const row of towns.values()) row.forEach((x, i) => (total[i]! += x));
return {
county: normalize(total),
turnout: ballots / (electors || 1),
towns: new Map([...towns].map(([k, v]) => [k, normalize(v)])),
villages,
};
}
function scenarioWith(spec: LocalElectionSpec, entry: Entry, even: boolean): Scenario {
const list = scenariosFromSpec(spec, entry);
return list.find((s) => (s.hypothetical ?? false) === even) ?? list[0]!;
}
function fit(world: World, spec: LocalElectionSpec, target: number[], start: Entry, rounds: number, seeds: number[], useEven: boolean, turnoutTarget: number | null): Entry {
let entry = { ...start };
const key = useEven ? "evenAppeal" : "appeal";
for (let round = 0; round < rounds; round++) {
const scenario = scenarioWith(spec, entry, useEven);
const sim = simulate({ ...world, scenario }, seeds);
const appeal = { ...(entry[key] ?? {}) };
spec.candidates.forEach((c, i) => {
const next = (appeal[String(c.number)] ?? 0) + Math.log(Math.max(target[i]!, 1e-4) / Math.max(sim.county[i]!, 1e-4));
appeal[String(c.number)] = Math.max(-8, Math.min(6, next));
});
const center = mean(Object.values(appeal));
for (const k of Object.keys(appeal)) appeal[k] = Math.round((appeal[k]! - center) * 1000) / 1000;
entry = { ...entry, [key]: appeal };
if (turnoutTarget !== null) entry.turnoutScale = Math.round(entry.turnoutScale * (turnoutTarget / sim.turnout) * 1000) / 1000;
}
return entry;
}
function errors(sim: Sim, truth: ReturnType<typeof truthOf>): Entry["error"] {
const mae = (a: number[], b: number[]) => mean(a.map((x, i) => Math.abs(x - b[i]!)));
const county = mae(sim.county, truth.county);
const town = mean([...truth.towns].filter(([k]) => sim.towns.has(k)).map(([k, v]) => mae(sim.towns.get(k)!, v)));
let vSum = 0;
let vWeight = 0;
for (const [code, t] of truth.villages) {
const s = sim.villages.get(code);
if (!s) continue;
vSum += mae(s, t.shares) * t.valid;
vWeight += t.valid;
}
const r = (x: number) => Math.round(x * 10_000) / 100;
return { county: r(county), town: r(town), village: r(vSum / (vWeight || 1)) };
}
async function main(): Promise<void> {
const { values } = parseArgs({
options: { only: { type: "string" }, set: { type: "string" }, rounds: { type: "string", default: "7" }, seeds: { type: "string", default: "3" } },
});
const only = values.only ? new Set(values.only.split(",")) : null;
const rounds = Number(values.rounds);
const seeds = Array.from({ length: Number(values.seeds) }, (_, i) => 301 + i);
const evalSeeds = [901, 902, 903];
const specs = (await loadAllSpecs()).filter(({ key, set }) => (!only || only.has(key)) && (!values.set || values.set === set));
const results = new Map<string, LocalCalibration["counties"]>();
const lines = ["# 管線劇本校準", "", "| 選舉 | 候選人 | 全區誤差 | 各區誤差 | 各里誤差 | 投票率倍率 | 秒 |", "|---|---:|---:|---:|---:|---:|---:|"];
for (const { key, set, spec } of specs) {
if (!results.has(set)) results.set(set, { ...((await loadCalibration(set))?.counties ?? {}) });
const counties = results.get(set)!;
const started = Date.now();
const pack: PackData = await loadPack(spec.packId, undefined, {
regionLevel: spec.regionLevel,
villages: spec.kind === "legislator" ? new Set(Object.keys(spec.byVillage)) : undefined,
});
const truth = truthOf(spec);
const world: World = { scenario: scenarioWith(spec, { turnoutScale: 0.78, appeal: {}, evenAppeal: null, error: { county: 0, town: 0, village: 0 } }, false), pack, content: CONTENT, params: DEFAULT_PARAMS };
const start: Entry = counties[key] ?? { turnoutScale: 0.78, appeal: {}, evenAppeal: null, error: { county: 0, town: 0, village: 0 } };
let entry = fit(world, spec, truth.county, { ...start, evenAppeal: null }, rounds, seeds, false, truth.turnout);
const check = simulate({ ...world, scenario: scenarioWith(spec, entry, false) }, evalSeeds);
entry = { ...entry, error: errors(check, truth) };
// 勢均力敵:前兩名平分兩人的總得票率
const order = [...truth.county.keys()].sort((a, b) => truth.county[b]! - truth.county[a]!);
if (spec.candidates.length >= 2) {
const even = [...truth.county];
const top = (even[order[0]!]! + even[order[1]!]!) / 2;
even[order[0]!] = top;
even[order[1]!] = top;
entry = fit(world, spec, even, { ...entry, evenAppeal: { ...entry.appeal } }, Math.max(4, rounds - 2), seeds, true, null);
}
counties[key] = entry;
const secs = (Date.now() - started) / 1000;
const line = `| ${spec.areaName}${spec.kind === "mayor" ? spec.office : spec.kind === "president" ? "總統" : "立委"} | ${spec.candidates.length} | ${entry.error.county} | ${entry.error.town} | ${entry.error.village} | ${entry.turnoutScale} | ${secs.toFixed(0)} |`;
lines.push(line);
console.log(line);
// 每個縣市做完就存,中斷也不會白做
await writeFile(join(DEFAULT_SCENARIOS_ROOT, set, "calibration.json"), JSON.stringify({ generatedAt: new Date().toISOString(), counties } satisfies LocalCalibration, null, 1));
}
lines.push("", "誤差單位是百分點(各候選人得票率與真實結果的平均絕對誤差)。只用全市總數校準,各區、各里是驗證。");
const outDir = new URL("../out/", import.meta.url);
await mkdir(outDir, { recursive: true });
await writeFile(new URL("calibrate-local.md", outDir), lines.join("\n") + "\n");
}
await main();