forked from WikiDeck/wikideck
sim-balance: deck-adjusted card lift, v1.1 candidate variants
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+18
-13
@@ -25,6 +25,7 @@ function pearson(xs: number[], ys: number[]): number {
|
||||
return sxx && syy ? sxy / Math.sqrt(sxx * syy) : NaN;
|
||||
}
|
||||
|
||||
/** delta = deck-adjusted lift: mean over games of (won − that deck's overall WR in symmetric games). */
|
||||
export interface CardStat { id: string; label?: string; games: number; plays: number; wins: number; wr: Rate; delta: number }
|
||||
|
||||
export interface RulesetStats {
|
||||
@@ -90,7 +91,10 @@ export function aggregate(records: MatchRecord[], errors = 0, engine?: EngineAda
|
||||
};
|
||||
const deckT: Record<string, { w: number; n: number }> = {}, tagT: Record<string, { w: number; n: number }> = {};
|
||||
const mu: Record<string, Record<string, { w: number; n: number }>> = {};
|
||||
const cardT = new Map<string, { games: number; plays: number; wins: number }>();
|
||||
const cardT = new Map<string, { games: number; plays: number; wins: number; lift: number }>();
|
||||
const deckAll: Record<string, { w: number; n: number }> = {};
|
||||
for (const m of sym) for (const seat of [0, 1] as Seat[]) tally(deckAll, m.decks[seat], m.winner === seat);
|
||||
const deckRate = (d: string) => deckAll[d].w / deckAll[d].n;
|
||||
for (const m of sym) {
|
||||
for (const seat of [0, 1] as Seat[]) {
|
||||
const won = m.winner === seat, o = (1 - seat) as Seat;
|
||||
@@ -99,9 +103,9 @@ export function aggregate(records: MatchRecord[], errors = 0, engine?: EngineAda
|
||||
tally(mu[m.decks[seat]] ?? (mu[m.decks[seat]] = {}), m.decks[o], won);
|
||||
const seen = new Set<string>();
|
||||
for (const c of m.cardsPlayed[seat]) {
|
||||
const cs = cardT.get(c) ?? { games: 0, plays: 0, wins: 0 };
|
||||
const cs = cardT.get(c) ?? { games: 0, plays: 0, wins: 0, lift: 0 };
|
||||
cs.plays++;
|
||||
if (!seen.has(c)) { seen.add(c); cs.games++; if (won) cs.wins++; }
|
||||
if (!seen.has(c)) { seen.add(c); cs.games++; if (won) cs.wins++; cs.lift += Number(won) - deckRate(m.decks[seat]); }
|
||||
cardT.set(c, cs);
|
||||
}
|
||||
}
|
||||
@@ -110,7 +114,7 @@ export function aggregate(records: MatchRecord[], errors = 0, engine?: EngineAda
|
||||
const cards: CardStat[] = [...cardT].map(([id, c]) => {
|
||||
const wr = rate(c.wins, c.games);
|
||||
const label = engine?.cardMeta?.(id)?.label;
|
||||
return { id, label: typeof label === 'string' ? label : undefined, ...c, wr, delta: wr.rate - 0.5 };
|
||||
return { id, label: typeof label === 'string' ? label : undefined, games: c.games, plays: c.plays, wins: c.wins, wr, delta: c.lift / c.games };
|
||||
}).sort((a, b) => b.delta - a.delta);
|
||||
|
||||
const statCorrelation: RulesetStats['statCorrelation'] = {};
|
||||
@@ -121,7 +125,7 @@ export function aggregate(records: MatchRecord[], errors = 0, engine?: EngineAda
|
||||
for (const m of metas) for (const [k, v] of Object.entries(m)) if (typeof v === 'number') keys.add(k);
|
||||
for (const k of keys) {
|
||||
const xs: number[] = [], ys: number[] = [];
|
||||
eligible.forEach((c, i) => { const v = metas[i][k]; if (typeof v === 'number') { xs.push(v); ys.push(c.wr.rate); } });
|
||||
eligible.forEach((c, i) => { const v = metas[i][k]; if (typeof v === 'number') { xs.push(v); ys.push(c.delta); } });
|
||||
statCorrelation[k] = { r: pearson(xs, ys), n: xs.length };
|
||||
}
|
||||
// keyword / faction tags on cards
|
||||
@@ -130,10 +134,10 @@ export function aggregate(records: MatchRecord[], errors = 0, engine?: EngineAda
|
||||
for (const [k, v] of Object.entries(metas[i])) {
|
||||
const vals = Array.isArray(v) ? v : typeof v === 'string' ? [v] : [];
|
||||
if (k === 'label') continue;
|
||||
for (const x of vals) { const t = tagW[`${k}:${x}`] ?? (tagW[`${k}:${x}`] = { w: 0, n: 0 }); t.w += c.wins; t.n += c.games; }
|
||||
for (const x of vals) { const t = tagW[`${k}:${x}`] ?? (tagW[`${k}:${x}`] = { w: 0, n: 0 }); t.w += c.delta * c.games; t.n += c.games; }
|
||||
}
|
||||
});
|
||||
for (const [k, v] of Object.entries(tagW)) statCorrelation[`card ${k}`] = { r: v.w / v.n - 0.5, n: v.n };
|
||||
for (const [k, v] of Object.entries(tagW)) statCorrelation[`card ${k}`] = { r: v.w / v.n, n: v.n };
|
||||
}
|
||||
|
||||
const kwT: Record<string, { n: number; cards: number; power: number; flip: number; wins: number }> = {};
|
||||
@@ -191,11 +195,12 @@ export function toMarkdown(s: RulesetStats): string {
|
||||
for (const r of decks) L.push(`| ${r} | ${decks.map(c => (s.matchups[r][c] ? pct(s.matchups[r][c].rate) : '')).join(' | ')} |`);
|
||||
}
|
||||
const elig = s.cards.filter(c => c.games >= 50);
|
||||
L.push('', `**Cards** (${s.cards.length} seen, ${elig.length} with ≥50 games; WR = win rate of the side that played it):`, '');
|
||||
L.push('| Top cards | games | WR |', '|---|---|---|');
|
||||
for (const c of elig.slice(0, 10)) L.push(`| ${c.label ?? c.id} | ${c.games} | ${fmtRate(c.wr)}${sig(c.wr)} |`);
|
||||
L.push('', '| Bottom cards | games | WR |', '|---|---|---|');
|
||||
for (const c of elig.slice(-10).reverse()) L.push(`| ${c.label ?? c.id} | ${c.games} | ${fmtRate(c.wr)}${sig(c.wr)} |`);
|
||||
L.push('', `**Cards** (${s.cards.length} seen, ${elig.length} with ≥50 games). Lift = win rate when played minus the win rate of the deck it was in (deck-adjusted); raw WR for reference:`, '');
|
||||
const meta = (c: CardStat) => c.label ?? c.id;
|
||||
L.push('| Top cards | games | lift | raw WR |', '|---|---|---|---|');
|
||||
for (const c of elig.slice(0, 10)) L.push(`| ${meta(c)} | ${c.games} | ${c.delta >= 0 ? '+' : ''}${(c.delta * 100).toFixed(1)} pp | ${pct(c.wr.rate)} |`);
|
||||
L.push('', '| Bottom cards | games | lift | raw WR |', '|---|---|---|---|');
|
||||
for (const c of elig.slice(-10).reverse()) L.push(`| ${meta(c)} | ${c.games} | ${c.delta >= 0 ? '+' : ''}${(c.delta * 100).toFixed(1)} pp | ${pct(c.wr.rate)} |`);
|
||||
if (Object.keys(s.keywords).length) {
|
||||
L.push('', '**Keyword value** (counterfactual replay with the keyword removed from that side; same actions):', '');
|
||||
L.push('| Keyword | side-games | cards played | power per card | games decided by it | WR of side playing it |', '|---|---|---|---|---|---|');
|
||||
@@ -204,7 +209,7 @@ export function toMarkdown(s: RulesetStats): string {
|
||||
}
|
||||
const corr = Object.entries(s.statCorrelation).filter(([, v]) => v.n >= 10);
|
||||
if (corr.length) {
|
||||
L.push('', '**Card stats vs win rate** (numeric: Pearson r across cards; `card k:v`: WR − 50%):', '');
|
||||
L.push('', '**Card stats vs lift** (numeric: Pearson r across cards; `card k:v`: mean lift, as a fraction):', '');
|
||||
for (const [k, v] of corr.sort((a, b) => Math.abs(b[1].r) - Math.abs(a[1].r))) L.push(`- ${k}: ${v.r >= 0 ? '+' : ''}${v.r.toFixed(3)} (n=${v.n})`);
|
||||
}
|
||||
L.push('');
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"cheap-power": { "powerByCost": [0, 2, 3, 4, 6, 8, 10] },
|
||||
"vanilla2": { "vanillaBonus": 2 },
|
||||
"carry2": { "energyCarryMax": 2 },
|
||||
"energy2to7": { "energyByTurn": [2, 3, 4, 5, 6, 7] },
|
||||
"cap4": { "laneCapacity": 4 },
|
||||
"ontopic-cheap": { "onTopicBonus": [0, 2, 2, 2, 2, 3, 3] },
|
||||
"cheap+carry2": { "powerByCost": [0, 2, 3, 4, 6, 8, 10], "energyCarryMax": 2 },
|
||||
"combo": { "powerByCost": [0, 2, 3, 5, 6, 8, 10], "energyCarryMax": 2, "onTopicBonus": [0, 1, 1, 1, 2, 2, 3] }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"flat-a": { "powerByCost": [0, 2, 3, 5, 6, 8, 9] },
|
||||
"flat-b": { "powerByCost": [0, 2, 4, 5, 7, 8, 10] },
|
||||
"flat-c": { "powerByCost": [0, 2, 4, 6, 7, 9, 10] },
|
||||
"flat-b+e27": { "powerByCost": [0, 2, 4, 5, 7, 8, 10], "energyByTurn": [2, 3, 4, 5, 6, 7] },
|
||||
"flat-a+carry2": { "powerByCost": [0, 2, 3, 5, 6, 8, 9], "energyCarryMax": 2 },
|
||||
"flat-b+carry2": { "powerByCost": [0, 2, 4, 5, 7, 8, 10], "energyCarryMax": 2 }
|
||||
}
|
||||
Reference in New Issue
Block a user