File size: 3,302 Bytes
23d418f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { CartesianGrid, ResponsiveContainer, Scatter, ScatterChart, Tooltip, XAxis, YAxis } from 'recharts'
import { INK, ORDER, SERIES } from '@/lib/chart'
import { percent } from '@/lib/format'
import type { EvalsFile, SystemId } from '@/lib/types'
import { Legend } from './Section'

/** If each system declined every answer below a confidence threshold: the share of
 * questions it would still answer (x) against the mean error cost per question (y),
 * one line per system, each point one threshold. Lower is better; moving left trades
 * answered questions for fewer costly mistakes. */
export function ThresholdChart({ data }: { data: EvalsFile }) {
  const names = Object.fromEntries(ORDER.map((s) => [s, data.systems[s].name])) as Record<SystemId, string>
  return (
    <div className="space-y-3">
      <Legend names={names} />
      <div className="h-72" role="img" aria-label="Mean error cost against the share of questions answered, as the confidence threshold rises; the table below has every value">
        <ResponsiveContainer width="100%" height="100%">
          <ScatterChart margin={{ top: 8, right: 16, bottom: 16, left: -12 }}>
            <CartesianGrid stroke={INK.grid} />
            <XAxis
              type="number"
              dataKey="answered"
              domain={[0, 1]}
              ticks={[0, 0.25, 0.5, 0.75, 1]}
              tickFormatter={(v: number) => percent(v)}
              tickLine={false}
              axisLine={{ stroke: INK.axis }}
              tick={{ fill: INK.muted, fontSize: 11 }}
              label={{ value: 'Share of questions answered', position: 'insideBottom', offset: -10, fill: INK.muted, fontSize: 11 }}
            />
            <YAxis
              type="number"
              dataKey="cost"
              domain={[0, 10]}
              ticks={[0, 2, 4, 6, 8, 10]}
              tickLine={false}
              axisLine={false}
              tick={{ fill: INK.muted, fontSize: 11 }}
            />
            <Tooltip cursor={{ stroke: INK.axis }} content={<ThresholdTooltip />} />
            {ORDER.map((s) => (
              <Scatter
                key={s}
                name={`System ${s.toUpperCase()}`}
                data={data.threshold.map((p) => ({ s, t: p.t, ...p[s] }))}
                fill={SERIES[s]}
                line={{ stroke: SERIES[s], strokeWidth: 2 }}
                lineJointType="linear"
                shape={(props: { cx?: number; cy?: number }) => <circle cx={props.cx} cy={props.cy} r={3} fill={SERIES[s]} />}
                isAnimationActive={false}
              />
            ))}
          </ScatterChart>
        </ResponsiveContainer>
      </div>
    </div>
  )
}

type Props = { active?: boolean; payload?: { payload: { s: SystemId; t: number; answered: number; cost: number } }[] }

function ThresholdTooltip({ active, payload }: Props) {
  if (!active || !payload?.length) return null
  const p = payload[0].payload
  return (
    <div className="rounded-lg border bg-background px-3 py-2 text-xs tabular-nums shadow-md">
      <p className="font-medium">
        System {p.s.toUpperCase()} · declining below confidence {p.t.toFixed(2)}
      </p>
      <p>
        answers {percent(p.answered)} of questions · mean error cost {p.cost.toFixed(2)}
      </p>
    </div>
  )
}