{"spec_id":"pdp-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// pdp-basic: Partial Dependence Plot\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst CI_LEVEL = 95; // percent, the confidence level of the interval columns\n// Line widths and opacities shared by the marks and the key, so each glyph matches its mark\nconst PD_STROKE = 3.5;\nconst ICE_STROKE = 1.25;\nconst ICE_OPACITY = 0.3;\nconst CI_OPACITY = 0.2;\nconst TITLE_H = 48;\nconst SUBTITLE_H = 28;\nconst LEGEND_H = 40;\n\n// Columns: set these to your data's column names\nconst SPEND = \"Weekly marketing spend ($1,000s)\"; // role: feature_values\nconst PD = \"Partial dependence (units sold/week)\"; // role: partial_dependence\nconst CI_LOW = \"CI lower (units sold/week)\"; // role: confidence_interval\nconst CI_HIGH = \"CI upper (units sold/week)\"; // role: confidence_interval\nconst ICE = [\"Store 1041\", \"Store 1187\", \"Store 1203\", \"Store 1316\", \"Store 1422\", \"Store 1509\", \"Store 1675\", \"Store 1738\"]; // role: ice_curves\n\n// Data: replace this block with your own records, one object per row\n// PartialDependenceDisplay-style output of a GradientBoostingRegressor that predicts weekly\n// units sold from marketing spend on a 61-point grid: the ICE curves of eight sampled stores,\n// their mean at each grid point (the partial dependence) and its 95 % t interval,\n// PD ± t(0.975, n − 1) · sd / √n over the n stores (fixed-seed LCG, no browser RNG)\nconst T_975 = [\n  12.706, 4.303, 3.182, 2.776, 2.571, 2.447, 2.365, 2.306, 2.262, 2.228, 2.201, 2.179, 2.16, 2.145, 2.131,\n  2.12, 2.11, 2.101, 2.093, 2.086, 2.08, 2.074, 2.069, 2.064, 2.06, 2.056, 2.052, 2.048, 2.045, 2.042,\n]; // t(0.975, df) for df = 1 … 30; the normal 1.96 beyond\nlet lcgState = 20260905 >>> 0;\nconst draws = Array.from({ length: ICE.length * 63 }, () => {\n  lcgState = (Math.imul(lcgState, 1664525) + 1013904223) >>> 0;\n  return lcgState / 4294967296;\n});\nlet drawIndex = 0;\nconst spendGrid = Array.from({ length: 61 }, (_, i) => 5 + i);\nconst iceByStore = ICE.map(() => {\n  const midpointShift = (draws[drawIndex++] - 0.5) * 16;\n  const amplitude = 0.82 + draws[drawIndex++] * 0.36;\n  return spendGrid.map((x) => (620 * amplitude) / (1 + Math.exp(-(x - (32 + midpointShift)) / 7)) + (draws[drawIndex++] - 0.5) * 12);\n});\nconst data = spendGrid.map((x, i) => {\n  const ice = iceByStore.map((curve) => Math.round(curve[i] * 10) / 10);\n  const mean = ice.reduce((a, b) => a + b, 0) / ice.length;\n  const sd = Math.sqrt(ice.reduce((s, v) => s + (v - mean) ** 2, 0) / (ice.length - 1));\n  const df = ice.length - 1;\n  const halfWidth = ((T_975[df - 1] ?? 1.96) * sd) / Math.sqrt(ice.length);\n  return {\n    [SPEND]: x,\n    [PD]: Math.round(mean * 10) / 10,\n    [CI_LOW]: Math.round((mean - halfWidth) * 10) / 10,\n    [CI_HIGH]: Math.round((mean + halfWidth) * 10) / 10,\n    ...Object.fromEntries(ICE.map((store, j) => [store, ice[j]])),\n  };\n});\n\n// Plot: reads only data and the constants above\nconst rows = [...data].sort((a, b) => a[SPEND] - b[SPEND]);\nconst spend = rows.map((d) => d[SPEND]);\n\n// Centre every curve on the mean of the partial dependence, as the spec allows\nconst pdValues = rows.map((d) => d[PD]).filter((v) => Number.isFinite(v));\nconst centre = pdValues.reduce((a, b) => a + b, 0) / pdValues.length;\nconst centred = Object.fromEntries(\n  [PD, CI_LOW, ...ICE].map((column) => [column, rows.map((d) => (Number.isFinite(d[column]) ? d[column] - centre : null))]),\n);\nconst ciWidth = rows.map((d) => (Number.isFinite(d[CI_LOW]) && Number.isFinite(d[CI_HIGH]) ? d[CI_HIGH] - d[CI_LOW] : null));\n\nconst unitMatch = PD.match(/\\(([^)]+)\\)/);\nconst unit = unitMatch ? unitMatch[1] : \"\";\n\nconst TITLE = \"pdp-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${rows.length}-point feature grid · curves centred on the mean partial dependence of ${centre.toFixed(0)} ${unit}`;\nconst CI_LABEL = `${CI_LEVEL}% confidence interval`;\nconst ICE_LABEL = `ICE curves (${ICE.length} observations)`;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: TITLE_H, display: \"flex\", alignItems: \"flex-end\", justifyContent: \"center\", fontSize: 22, fontWeight: 600, color: t.ink }}>\n        {TITLE}\n      </div>\n      <div style={{ height: SUBTITLE_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\", fontSize: 15, color: t.inkSoft }}>\n        {SUBTITLE}\n      </div>\n      <div style={{ height: LEGEND_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\", gap: 32, fontSize: 15, color: t.inkSoft }}>\n        <span style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n          <svg width={30} height={14}>\n            <line x1={2} y1={7} x2={28} y2={7} stroke={t.palette[0]} strokeWidth={PD_STROKE} strokeLinecap=\"round\" />\n          </svg>\n          {PD}\n        </span>\n        <span style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n          <svg width={30} height={14}>\n            <line x1={2} y1={7} x2={28} y2={7} stroke={t.palette[0]} strokeWidth={ICE_STROKE} strokeOpacity={ICE_OPACITY} />\n          </svg>\n          {ICE_LABEL}\n        </span>\n        <span style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n          <svg width={30} height={14}>\n            <rect x={1} y={1} width={28} height={12} fill={t.inkSoft} fillOpacity={CI_OPACITY} />\n          </svg>\n          {CI_LABEL}\n        </span>\n      </div>\n      <LineChart\n        width={width}\n        height={height - TITLE_H - SUBTITLE_H - LEGEND_H}\n        skipAnimation\n        hideLegend\n        margin={{ top: 16, right: 48, bottom: 8, left: 16 }}\n        grid={{ horizontal: true }}\n        series={[\n          {\n            id: \"ci-lower\",\n            data: centred[CI_LOW],\n            stack: \"ci\",\n            area: true,\n            showMark: false,\n            curve: \"linear\",\n            color: t.inkSoft,\n            valueFormatter: () => null,\n          },\n          {\n            id: \"ci-band\",\n            data: ciWidth,\n            stack: \"ci\",\n            area: true,\n            showMark: false,\n            curve: \"linear\",\n            color: t.inkSoft,\n            label: CI_LABEL,\n            valueFormatter: (v) => (v === null ? null : `± ${(v / 2).toFixed(0)} ${unit}`),\n          },\n          ...ICE.map((store, j) => ({\n            id: `ice-${j}`,\n            data: centred[store],\n            showMark: false,\n            curve: \"linear\",\n            color: t.palette[0],\n            label: j === 0 ? ICE_LABEL : undefined,\n          })),\n          {\n            id: \"pd\",\n            data: centred[PD],\n            showMark: false,\n            curve: \"linear\",\n            color: t.palette[0],\n            label: PD,\n          },\n        ]}\n        xAxis={[\n          {\n            data: spend,\n            scaleType: \"linear\",\n            label: SPEND,\n            height: 64,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            label: `${PD}, centred`,\n            width: 84,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        sx={{\n          \"& .MuiLineChart-area[data-series='ci-lower']\": { fill: \"none\" },\n          \"& .MuiLineChart-area[data-series='ci-band']\": { fillOpacity: CI_OPACITY },\n          \"& .MuiLineChart-line[data-series='ci-lower'], & .MuiLineChart-line[data-series='ci-band']\": { strokeWidth: 0 },\n          \"& .MuiLineChart-line[data-series^='ice-']\": { strokeWidth: ICE_STROKE, strokeOpacity: ICE_OPACITY },\n          \"& .MuiLineChart-line[data-series='pd']\": { strokeWidth: PD_STROKE },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      >\n        <ChartsReferenceLine\n          y={0}\n          label=\"mean partial dependence\"\n          labelAlign=\"end\"\n          labelStyle={{ fontSize: 14, fill: t.inkSoft }}\n          lineStyle={{ stroke: t.inkSoft, strokeDasharray: \"6 4\" }}\n        />\n      </LineChart>\n    </div>\n  );\n}\n"}