{"spec_id":"line-impurity-comparison","library":"highcharts","language":"javascript","code":"// anyplot.ai\n// line-impurity-comparison: Gini Impurity vs Entropy Comparison\n// Library: highcharts 12.6.0 | JavaScript 22.23.2\n// Quality: 90/100 | Created: 2026-08-26\n\nconst t = window.ANYPLOT_TOKENS;\n\n// --- Data (deterministic, 101 points so p = 0.5 lands exactly on a sample) -\nconst n = 101;\nconst probabilities = Array.from({ length: n }, (_, i) => i / (n - 1));\nconst midIndex = (n - 1) / 2; // p = 0.5, where both criteria peak\n\nfunction giniImpurity(p) {\n  return 2 * p * (1 - p);\n}\n\nfunction entropy(p) {\n  if (p === 0 || p === 1) return 0; // edge cases: entropy defined as 0\n  return -p * Math.log2(p) - (1 - p) * Math.log2(1 - p);\n}\n\nfunction seriesData(valueFn, markerColor, labelY) {\n  return probabilities.map((p, i) => {\n    const y = valueFn(p);\n    if (i !== midIndex) return [p, y];\n    return {\n      x: p,\n      y,\n      marker: { enabled: true, radius: 6, fillColor: markerColor, lineColor: t.pageBg, lineWidth: 2 },\n      dataLabels: {\n        enabled: true,\n        format: \"{y:.2f}\",\n        y: labelY,\n        style: { color: t.ink, fontSize: \"14px\", fontWeight: \"600\", textOutline: \"none\" },\n      },\n    };\n  });\n}\n\n// --- Chart -------------------------------------------------------------\nHighcharts.chart(\"container\", {\n  chart: { type: \"line\", backgroundColor: \"transparent\", animation: false,\n           style: { fontFamily: \"inherit\" } },\n  credits: { enabled: false },\n  colors: t.palette,\n  title: { text: \"line-impurity-comparison · javascript · highcharts · anyplot.ai\",\n           style: { color: t.ink, fontSize: \"22px\", fontWeight: \"600\" } },\n  xAxis: {\n    title: { text: \"Probability p\", style: { color: t.inkSoft, fontSize: \"16px\" } },\n    min: 0,\n    max: 1,\n    tickInterval: 0.1,\n    lineColor: t.inkSoft,\n    tickColor: t.inkSoft,\n    gridLineColor: t.grid,\n    gridLineWidth: 1,\n    labels: { style: { color: t.inkSoft, fontSize: \"14px\" } },\n    plotLines: [{\n      value: 0.5,\n      color: t.inkSoft,\n      dashStyle: \"Dash\",\n      width: 1.5,\n      zIndex: 3,\n      label: {\n        text: \"p = 0.5 · maximum impurity\",\n        rotation: 0,\n        align: \"center\",\n        verticalAlign: \"bottom\",\n        y: -8,\n        style: { color: t.inkSoft, fontSize: \"13px\" },\n      },\n    }],\n  },\n  yAxis: {\n    title: { text: \"Impurity Measure (normalized)\", style: { color: t.inkSoft, fontSize: \"16px\" } },\n    min: 0,\n    max: 1.05,\n    lineColor: t.inkSoft,\n    tickColor: t.inkSoft,\n    gridLineColor: t.grid,\n    labels: { style: { color: t.inkSoft, fontSize: \"14px\" } },\n  },\n  legend: {\n    align: \"center\",\n    verticalAlign: \"bottom\",\n    itemStyle: { color: t.inkSoft, fontSize: \"14px\" },\n    itemHoverStyle: { color: t.ink },\n  },\n  tooltip: {\n    backgroundColor: t.elevatedBg,\n    style: { color: t.ink, fontSize: \"13px\" },\n    valueDecimals: 3,\n  },\n  plotOptions: {\n    series: { animation: false, marker: { enabled: false }, lineWidth: 3 },\n  },\n  series: [\n    {\n      name: \"Gini impurity — 2p(1−p)\",\n      data: seriesData(giniImpurity, t.palette[0], -16),\n      color: t.palette[0],\n    },\n    {\n      name: \"Entropy (normalized) — −p·log₂p − (1−p)·log₂(1−p)\",\n      data: seriesData(entropy, t.palette[1], 22),\n      color: t.palette[1],\n      dashStyle: \"ShortDash\",\n    },\n  ],\n});\n"}