{"spec_id":"gain-curve","library":"muix","language":"javascript","code":"// anyplot.ai\n// gain-curve: Cumulative Gains Chart\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/100 | Updated: 2026-11-08\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst FULL = 100; // both axes run in percent, from none to all\nconst PAD_SHARE = 0.015; // axis padding beyond 0 % and 100 %, so the corner points and the perfect-model plateau sit inside\nconst X_LABEL = \"Population targeted (%)\";\nconst Y_LABEL = \"Positive cases captured (%)\";\nconst PERFECT_LABEL = \"Perfect model\";\nconst RANDOM_LABEL = \"Random selection\";\nconst TITLE = \"gain-curve · javascript · muix · anyplot.ai\";\nconst TITLE_H = 84;\nconst LEGEND_H = 44;\n\n// Columns: set these to your data's column names\nconst CONFIRMED_FRAUD = \"Confirmed fraud after review\"; // role: y_true\nconst FRAUD_SCORE = \"Model fraud score\"; // role: y_score\n\n// Data: replace this block with your own records, one object per row\n// 3000 card transactions that rule checks flagged for manual review: a latent risk drives fraud (about 15 %\n// of the flagged transactions are confirmed by the reviewers), the model score sees that risk with noise\nconst data = (() => {\n  let state = 42;\n  const rand = () => {\n    state = (1664525 * state + 1013904223) % 4294967296;\n    return state / 4294967296;\n  };\n  const gaussian = () => {\n    const u1 = Math.max(rand(), 1e-12);\n    const u2 = rand();\n    return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n  };\n  return Array.from({ length: 3000 }, () => {\n    const risk = gaussian();\n    const fraudProbability = 1 / (1 + Math.exp(-(1.4 * risk - 2.3)));\n    const fraud = rand() < fraudProbability ? 1 : 0;\n    return { [CONFIRMED_FRAUD]: fraud, [FRAUD_SCORE]: Math.round((risk + gaussian() * 0.9) * 10000) / 10000 };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst ranked = data\n  .filter((d) => Number.isFinite(d[FRAUD_SCORE]) && (d[CONFIRMED_FRAUD] === 0 || d[CONFIRMED_FRAUD] === 1))\n  .sort((a, b) => b[FRAUD_SCORE] - a[FRAUD_SCORE]);\nconst n = ranked.length;\nconst positives = ranked.reduce((sum, d) => sum + d[CONFIRMED_FRAUD], 0);\nconst positiveRate = positives / n;\n\n// One computed point per targeted sample, starting at the origin\nconst targeted = [0];\nconst captured = [0];\nlet runningPositives = 0;\nranked.forEach((d, i) => {\n  runningPositives += d[CONFIRMED_FRAUD];\n  targeted.push(((i + 1) / n) * FULL);\n  captured.push((runningPositives / positives) * FULL);\n});\nconst perfect = targeted.map((x) => Math.min(FULL, x / positiveRate));\nconst random = targeted.map((x) => x);\n\nconst subtitle = `n = ${n} · positive rate ${(positiveRate * FULL).toFixed(1)} % (${CONFIRMED_FRAUD} = 1) · ranked by ${FRAUD_SCORE}, highest first`;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  return (\n    <Box sx={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <Box sx={{ height: TITLE_H, pt: \"20px\", textAlign: \"center\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.2 }}>{TITLE}</Typography>\n        <Typography sx={{ fontSize: 15, color: t.inkSoft, mt: \"6px\" }}>{subtitle}</Typography>\n      </Box>\n      <LineChart\n        width={width}\n        height={height - TITLE_H - LEGEND_H}\n        skipAnimation\n        grid={{ horizontal: true, vertical: true }}\n        margin={{ top: 16, right: 56, bottom: 16, left: 32 }}\n        xAxis={[\n          {\n            data: targeted,\n            scaleType: \"linear\",\n            min: -PAD_SHARE * FULL,\n            max: (1 + PAD_SHARE) * FULL,\n            label: X_LABEL,\n            height: 68,\n            tickNumber: 10,\n            valueFormatter: (v: number) => `${Math.round(v)}%`,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            min: -PAD_SHARE * FULL,\n            max: (1 + PAD_SHARE) * FULL,\n            label: Y_LABEL,\n            width: 92,\n            tickNumber: 10,\n            valueFormatter: (v: number) => `${Math.round(v)}%`,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={[\n          {\n            id: \"model\",\n            data: captured,\n            label: FRAUD_SCORE,\n            color: t.palette[0],\n            curve: \"linear\",\n            area: true,\n            showMark: false,\n          },\n          {\n            id: \"perfect\",\n            data: perfect,\n            label: PERFECT_LABEL,\n            color: t.inkSoft,\n            curve: \"linear\",\n            showMark: false,\n          },\n          {\n            id: \"random\",\n            data: random,\n            label: RANDOM_LABEL,\n            color: t.ink,\n            curve: \"linear\",\n            showMark: false,\n          },\n        ]}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"top\", horizontal: \"center\" },\n            sx: {\n              fontSize: 15,\n              color: t.inkSoft,\n              mb: \"8px\",\n              \"& .MuiChartsLegend-mark\": { width: 36 },\n              '& [data-series=\"perfect\"] .MuiChartsLegend-mark path': { strokeWidth: 2, strokeDasharray: \"2 5\" },\n              '& [data-series=\"random\"] .MuiChartsLegend-mark path': { strokeWidth: 2, strokeDasharray: \"10 7\" },\n            },\n          },\n        }}\n        sx={{\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n          '& .MuiLineChart-line[data-series=\"model\"]': { strokeWidth: 3.5 },\n          '& .MuiLineChart-line[data-series=\"perfect\"]': { strokeWidth: 2, strokeDasharray: \"2 5\" },\n          '& .MuiLineChart-line[data-series=\"random\"]': { strokeWidth: 2, strokeDasharray: \"10 7\" },\n          '& .MuiLineChart-area[data-series=\"model\"]': { fillOpacity: 0.12 },\n        }}\n      />\n    </Box>\n  );\n}\n"}