// The static graphs on the tuning pages. These have no state, so the build // computes them once and writes the SVG into the page. import { Chart, type Series } from '@/components/chart' import { fmt } from '@/lib/scale' import { BETA, MU, SIGMA, TAU, alternating, byPosition, duelDelta, ffaDeltas, gapCurve, improvement, MARGIN_GAP, linspace, marginDelta, models, muSeries, ordinalSeries, player, playSeries, sigmaAfterOneGame, sigmaSeries, scaledGamma, streak, unevenTeamDeltas, weightedTeamDeltas, winProbability, type ModelName, type Point, } from '@/lib/scenarios' import { bradleyTerryPart, phiMajor, rating, thurstoneMostellerFull } from '@/lib/openskill' import type { ReactNode } from 'react' const BETAS = [2, BETA, 8, 12] const TAUS = [0, TAU, 0.5, 1] const EPSILONS = [0.1, 5, 10, 20] const named = (name: string, value: number): string => `${name} = ${fmt(value)}` function Figure({ children, caption }: { children: ReactNode; caption: ReactNode }) { return (
{children}
{caption}
) } export function BetaWinFigure() { const series: Series[] = BETAS.map((beta) => ({ label: named('beta', beta), points: gapCurve((gap) => winProbability(gap, 2, { beta })), })) return (
) } export function BetaDeltaFigure() { const series: Series[] = BETAS.map((beta) => ({ label: named('beta', beta), points: gapCurve((gap) => duelDelta(gap, SIGMA, 'win', { beta })), })) return (
) } export function TauFloorFigure() { const series: Series[] = TAUS.map((tau) => ({ label: named('tau', tau), points: sigmaSeries(playSeries(rating(), alternating(100), { tau })), })) return (
) } export function TauResponseFigure() { const series: Series[] = TAUS.map((tau) => ({ label: named('tau', tau), points: muSeries(playSeries(rating(), improvement(100), { tau })), })) return (
) } export function LimitSigmaFigure() { const start = player(MU, 1.5) const series: Series[] = [ { label: 'limitSigma: false', points: sigmaSeries(playSeries(start, alternating(60), { tau: 1 })) }, { label: 'limitSigma: true', points: sigmaSeries(playSeries(start, alternating(60), { tau: 1, limitSigma: true })), }, ] return (
) } export function SigmaConvergenceFigure() { const series: Series[] = [2, 5, SIGMA, 15].map((sigma) => ({ label: named('starting sigma', sigma), points: muSeries(playSeries(player(MU, sigma), streak(20))), })) return (
) } const epsilonSeries = (outcome: 'win' | 'draw'): Series[] => EPSILONS.map((epsilon) => ({ label: named('epsilon', epsilon), points: gapCurve((gap) => duelDelta(gap, SIGMA, outcome, { model: thurstoneMostellerFull, epsilon })), })) export function EpsilonDrawFigure() { return (
) } export function EpsilonWinFigure() { return (
) } export function KappaFigure() { const scales = linspace(0, 30, 121) const series: Series[] = [0.0001, 0.01, 0.1].map((kappa) => ({ label: named('kappa', kappa), points: scales.map((scale): Point => [scale, sigmaAfterOneGame(scale, { kappa })]), })) return (
) } export function OrdinalFigure() { const ratings = playSeries(rating(), alternating(50)) const series: Series[] = [0, 1, 2, 3].map((z) => ({ label: named('z', z), points: ordinalSeries(ratings, { z }), })) return (
) } export function BalanceFigure() { const gaps = linspace(0, 40, 41) const curve = (outcome: 'win' | 'loss', balance: boolean): Point[] => gaps.map((gap) => [gap, unevenTeamDeltas(gap, outcome, { balance }, 2)[0]]) const series: Series[] = [ { label: 'win, balance', points: curve('win', true) }, { label: 'win, no balance', points: curve('win', false), dashed: true }, { label: 'loss, balance', points: curve('loss', true) }, { label: 'loss, no balance', points: curve('loss', false), dashed: true }, ] return (
) } export function WeightFigure() { const weights = linspace(0, 3, 61) const series: Series[] = [ { label: 'weightBounds: [1, 2]', points: weights.map((w): Point => [w, weightedTeamDeltas([w, 1])[0]]), }, { label: 'weightBounds: null', points: weights.map((w): Point => [w, weightedTeamDeltas([w, 1], { weightBounds: null })[0]]), dashed: true, }, ] return (
) } export function MarginFigure() { const gaps = linspace(0, 80, 81) const series: Series[] = [ ...[5, 10, 20].map((margin) => ({ label: named('margin', margin), points: gaps.map((gap): Point => [gap, marginDelta(gap, margin)]), })), { label: 'no margin', points: gaps.map((gap): Point => [gap, duelDelta(MARGIN_GAP, SIGMA, 'win')]), dashed: true, }, ] return (
) } const positions = (n: number): number[] => linspace(1, n, n) export function WindowFigure() { const n = 10 const series: Series[] = [ { label: 'no windowSize', points: byPosition(ffaDeltas(n, { model: bradleyTerryPart })), dashed: true, dots: true }, ...[1, 2, 4, 9].map((windowSize) => ({ label: named('windowSize', windowSize), points: byPosition(ffaDeltas(n, { model: bradleyTerryPart, windowSize })), dots: true, })), ] return (
) } export function WindowTieFigure() { const n = 5 const series: Series[] = [ { label: 'no windowSize', points: byPosition(ffaDeltas(n, { model: bradleyTerryPart }, [2])), dashed: true, dots: true, }, { label: named('windowSize', 4), points: byPosition(ffaDeltas(n, { model: bradleyTerryPart, windowSize: 4 }, [2])), dots: true, }, ] return (
) } export function GammaFigure() { const series: Series[] = [0.25, 0.5, 1, 2, 4].map((scale) => ({ label: `gamma × ${fmt(scale)}`, points: sigmaSeries(playSeries(rating(), alternating(50), { gamma: scaledGamma(scale) })), })) return (
) } export function ModelsFigure() { const n = 5 const series: Series[] = (Object.keys(models) as ModelName[]).map((name) => ({ label: name, points: byPosition(ffaDeltas(n, { model: models[name] })), dots: true, })) return (
) } const logistic = (x: number): number => 1 / (1 + Math.exp(-x)) export function LinkFunctionsFigure() { const xs = linspace(-6, 6, 121) const series: Series[] = [ { label: 'logistic(x), Bradley-Terry', points: xs.map((x): Point => [x, logistic(x)]) }, { label: 'Φ(x), Thurstone-Mosteller', points: xs.map((x): Point => [x, phiMajor(x)]) }, { label: 'Φ(x / 1.70)', points: xs.map((x): Point => [x, phiMajor(x / 1.702)]), dashed: true }, ] return (
) }