{"spec_id":"ice-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// ice-basic: Individual Conditional Expectation (ICE) Plot\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\n// Individual curves stay translucent so dense stretches read darker; the opacity\n// is set on the line paths only, so the legend swatches keep the full colour\nconst ICE_OPACITY = 0.5;\n\n// Columns: set these to your data's column names\nconst HOUSE = \"House ID\"; // role: observation_id\nconst SQFT = \"Square Footage (sq ft)\"; // role: feature_value\nconst PRICE = \"Predicted Price (USD)\"; // role: prediction\nconst SEGMENT = \"Year built\"; // role: extra\n// Natural order of the segment levels, oldest first; unlisted levels follow\nconst SEGMENT_ORDER = [\"Before 1960\", \"1960 or later\"];\n\n// Data: replace this block with your own records, one object per observation and grid value\n// ICE curves of a gradient-boosting house-price model: for each of 70 houses the\n// predicted price over 55 square-footage values, every other feature held fixed.\n// About a quarter of the houses, built before 1960, show diminishing returns:\n// in older stock, extra floor space adds less and less to the predicted price.\nconst data = (() => {\n  let seed = 42;\n  const rng = () => {\n    seed |= 0;\n    seed = (seed + 0x6d2b79f5) | 0;\n    let x = Math.imul(seed ^ (seed >>> 15), 1 | seed);\n    x = (x + Math.imul(x ^ (x >>> 7), 61 | x)) ^ x;\n    return ((x ^ (x >>> 14)) >>> 0) / 4294967296;\n  };\n  const gaussian = (mean: number, sd: number) =>\n    mean + sd * Math.sqrt(-2 * Math.log(Math.max(rng(), 1e-9))) * Math.cos(2 * Math.PI * rng());\n  const gridCount = 55;\n  const sqftMin = 900;\n  const sqftMax = 3800;\n  const rows = [];\n  for (let house = 1; house <= 70; house += 1) {\n    const basePrice = gaussian(210000, 22000);\n    const pricePerSqft = gaussian(145, 22);\n    const older = rng() < 0.25;\n    const curvature = older ? gaussian(-0.028, 0.006) : gaussian(-0.003, 0.004);\n    for (let i = 0; i < gridCount; i += 1) {\n      const sqft = sqftMin + (i * (sqftMax - sqftMin)) / (gridCount - 1);\n      const delta = sqft - sqftMin;\n      rows.push({\n        [HOUSE]: house,\n        [SQFT]: Math.round(sqft),\n        [PRICE]: Math.round(basePrice + pricePerSqft * delta + curvature * delta * delta),\n        [SEGMENT]: older ? \"Before 1960\" : \"1960 or later\",\n      });\n    }\n  }\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\nconst rows = data.filter((d) => d[SQFT] != null && d[PRICE] != null);\nconst grid = [...new Set(rows.map((d) => d[SQFT]))].sort((a, b) => a - b);\nconst rank = (s) => (SEGMENT_ORDER.includes(s) ? SEGMENT_ORDER.indexOf(s) : SEGMENT_ORDER.length);\nconst segments = [...new Set(rows.map((d) => d[SEGMENT]))].sort((a, b) => rank(a) - rank(b));\n// Ordered levels take evenly spaced steps of the Imprint sequential ramp, oldest → newest\nconst mix = (a: string, b: string, f: number) =>\n  `#${[1, 3, 5]\n    .map((i) => Math.round(parseInt(a.slice(i, i + 2), 16) * (1 - f) + parseInt(b.slice(i, i + 2), 16) * f))\n    .map((v) => v.toString(16).padStart(2, \"0\"))\n    .join(\"\")}`;\nconst segmentColor = new Map(segments.map((s, i) => [s, mix(t.seq[0], t.seq[1], segments.length > 1 ? i / (segments.length - 1) : 0)]));\nconst curves = [...new Set(rows.map((d) => d[HOUSE]))]\n  .map((id) => {\n    const own = rows.filter((d) => d[HOUSE] === id);\n    const byX = new Map(own.map((d) => [d[SQFT], d[PRICE]]));\n    return { id, segment: own[0][SEGMENT], data: grid.map((x) => byX.get(x) ?? null) };\n  })\n  .sort((a, b) => segments.indexOf(a.segment) - segments.indexOf(b.segment));\n// Partial dependence: the mean prediction over all observations at each grid value\nconst partialDependence = grid.map((_, k) => {\n  const at = curves.map((c) => c.data[k]).filter((v) => v != null);\n  return at.reduce((s, v) => s + v, 0) / at.length;\n});\n// One legend entry per segment: only the segment's first curve carries the label\nconst labelled = new Set(segments.map((s) => curves.find((c) => c.segment === s).id));\n\nconst TITLE = \"House Price Predictions · ice-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${curves.length} ICE curves × ${grid.length} grid values · coloured by ${SEGMENT.toLowerCase()} · bold line: partial dependence`;\nconst PDP_LABEL = \"Partial dependence (mean of all curves)\";\nconst HEADER_H = 76;\nconst LEGEND_H = 44;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <Box sx={{ width, height, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\", overflow: \"hidden\" }}>\n      <Box sx={{ height: HEADER_H, pt: \"14px\", textAlign: \"center\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.3 }}>{TITLE}</Typography>\n        <Typography sx={{ fontSize: 15, color: t.inkSoft, lineHeight: 1.5 }}>{SUBTITLE}</Typography>\n      </Box>\n      <LineChart\n        width={width}\n        height={height - HEADER_H - LEGEND_H}\n        skipAnimation\n        grid={{ horizontal: true }}\n        margin={{ top: 12, right: 40, bottom: 16, left: 24 }}\n        // Individual curves first, the partial dependence line last so it draws on top\n        series={[\n          ...curves.map((c) => ({\n            id: `obs-${c.id}`,\n            data: c.data,\n            color: segmentColor.get(c.segment),\n            showMark: false,\n            labelMarkType: \"line\",\n            label: labelled.has(c.id) ? `${SEGMENT}: ${c.segment}` : undefined,\n          })),\n          { id: \"pdp\", data: partialDependence, color: t.ink, label: PDP_LABEL, showMark: false, labelMarkType: \"line\" },\n        ]}\n        xAxis={[\n          {\n            data: grid,\n            scaleType: \"linear\",\n            min: grid[0],\n            max: grid[grid.length - 1],\n            tickNumber: 12,\n            height: 64,\n            label: SQFT,\n            valueFormatter: (v: number) => v.toLocaleString(\"en-US\"),\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            width: 84,\n            label: PRICE,\n            valueFormatter: (v: number) => `${(v / 1000).toFixed(0)}k`,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"top\", horizontal: \"end\" },\n            sx: { fontSize: 15, color: t.ink, pr: \"40px\", gap: \"24px\", \"& .MuiChartsLegend-mark.MuiChartsLabelMark-line\": { width: 30, height: 10, \"& path\": { strokeWidth: 6 } } },\n          },\n          tooltip: { trigger: \"item\" },\n        }}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: 1.5, strokeOpacity: ICE_OPACITY },\n          '& .MuiLineChart-line[data-series=\"pdp\"]': { strokeWidth: 4, strokeOpacity: 1 },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      />\n    </Box>\n  );\n}\n"}