{"spec_id":"logistic-regression","library":"highcharts","language":"javascript","code":"// anyplot.ai\n// logistic-regression: Logistic Regression Curve Plot\n// Library: highcharts 13.1.1 | JavaScript 22.23.5\n// Quality: 87/100 | Updated: 2026-12-01\n\nconst t = window.ANYPLOT_TOKENS;\n\n// --- Data (in-memory, deterministic) ----------------------------------------\n// Marketing conversion: probability of converting as a function of an\n// engagement score (0-100). A small LCG stands in for a seeded RNG (the\n// browser has no seedable Math.random).\nfunction lcg(seed) {\n  let state = seed >>> 0;\n  return () => {\n    state = (Math.imul(state, 1664525) + 1013904223) >>> 0;\n    return state / 4294967296;\n  };\n}\nconst rand = lcg(42);\n\nconst midpoint = 52; // engagement score at 50% conversion probability\nconst steepness = 0.11;\nconst sigmoid = (x) => 1 / (1 + Math.exp(-steepness * (x - midpoint)));\n\n// Fitted probability curve, sampled across the full engagement range.\nconst curveX = [];\nfor (let x = 0; x <= 100; x += 1) curveX.push(x);\nconst curveY = curveX.map(sigmoid);\n\n// Approximate 95% confidence band, built on the logit scale (the scale a\n// logistic fit's standard error lives on) and mapped back through the\n// sigmoid, so it stays smooth and inside 0-1. The error is smallest at the\n// midpoint and grows toward the extremes, where observations are sparse.\nconst seIntercept = 0.15;\nconst seSlope = 0.03;\nconst logitSe = (x) => Math.hypot(seIntercept, seSlope * (x - midpoint));\nconst logit = (x) => steepness * (x - midpoint);\nconst bandBound = (x, sign) => 1 / (1 + Math.exp(-(logit(x) + sign * 1.96 * logitSe(x))));\nconst ciLower = curveX.map((x) => [x, bandBound(x, -1)]);\nconst ciWidth = curveX.map((x, i) => [x, bandBound(x, 1) - ciLower[i][1]]);\n\n// Observed binary outcomes, jittered on the y-axis so points near 0/1 don't\n// stack exactly on top of each other.\nconst notConverted = [];\nconst converted = [];\nconst pointCount = 180;\nfor (let i = 0; i < pointCount; i += 1) {\n  const x = rand() * 100;\n  const p = sigmoid(x);\n  const outcome = rand() < p ? 1 : 0;\n  const jitter = (rand() - 0.5) * 0.09;\n  const point = [x, outcome + jitter];\n  (outcome === 1 ? converted : notConverted).push(point);\n}\n\n// --- Chart -------------------------------------------------------------------\nconst classColor0 = t.palette[0]; // brand green — always the first series\nconst classColor1 = t.palette[1];\nconst curveColor = t.ink;\nconst bandColor = Highcharts.color(curveColor).setOpacity(0.16).get(\"rgba\");\nconst markerFill0 = Highcharts.color(classColor0).setOpacity(0.6).get(\"rgba\");\nconst markerFill1 = Highcharts.color(classColor1).setOpacity(0.6).get(\"rgba\");\n\nHighcharts.chart(\"container\", {\n  palette: { colorScheme: t.theme },\n  chart: {\n    type: \"scatter\",\n    backgroundColor: \"transparent\",\n    animation: false,\n    style: { fontFamily: \"inherit\" },\n  },\n  credits: { enabled: false },\n  title: {\n    text: \"logistic-regression · javascript · highcharts · anyplot.ai\",\n    style: { color: t.ink, fontSize: \"22px\", fontWeight: \"600\" },\n  },\n  xAxis: {\n    title: { text: \"Customer Engagement Score\", style: { color: t.inkSoft, fontSize: \"16px\" } },\n    min: 0,\n    max: 100,\n    lineColor: t.inkSoft,\n    tickColor: t.inkSoft,\n    gridLineColor: t.grid,\n    gridLineWidth: 1,\n    labels: { style: { color: t.inkSoft, fontSize: \"14px\" } },\n  },\n  yAxis: {\n    title: { text: \"Probability\", style: { color: t.inkSoft, fontSize: \"16px\" } },\n    min: -0.07,\n    max: 1.07,\n    startOnTick: false,\n    endOnTick: false,\n    tickPositions: [0, 0.25, 0.5, 0.75, 1],\n    lineColor: t.inkSoft,\n    tickColor: t.inkSoft,\n    gridLineColor: t.grid,\n    labels: { style: { color: t.inkSoft, fontSize: \"14px\" } },\n    plotLines: [\n      {\n        value: 0.5,\n        color: t.amber,\n        width: 2,\n        dashStyle: \"Dash\",\n        zIndex: 4,\n        label: {\n          text: \"Decision threshold (p = 0.5)\",\n          align: \"right\",\n          x: -8,\n          y: -6,\n          style: { color: t.inkSoft, fontSize: \"14px\" },\n        },\n      },\n    ],\n  },\n  legend: {\n    itemStyle: { color: t.inkSoft, fontSize: \"14px\" },\n    itemHoverStyle: { color: t.ink },\n  },\n  plotOptions: {\n    series: { animation: false, enableMouseTracking: true },\n    scatter: { marker: { radius: 4.5, lineWidth: 0 } },\n  },\n  series: [\n    // Highcharts stacks area series in reverse order: the last series sits at the\n    // bottom, so the width series comes first and the transparent lower bound last.\n    {\n      name: \"95% confidence interval\",\n      type: \"area\",\n      data: ciWidth,\n      color: bandColor,\n      fillOpacity: 1,\n      lineWidth: 0,\n      marker: { enabled: false },\n      enableMouseTracking: false,\n      showInLegend: true,\n      stacking: \"normal\",\n      stack: \"ci\",\n    },\n    {\n      name: \"ci-lower\",\n      type: \"area\",\n      data: ciLower,\n      color: \"transparent\",\n      fillOpacity: 0,\n      lineWidth: 0,\n      marker: { enabled: false },\n      enableMouseTracking: false,\n      showInLegend: false,\n      stacking: \"normal\",\n      stack: \"ci\",\n    },\n    {\n      name: \"Did not convert (0)\",\n      type: \"scatter\",\n      data: notConverted,\n      color: classColor0,\n      marker: { fillColor: markerFill0 },\n    },\n    {\n      name: \"Converted (1)\",\n      type: \"scatter\",\n      data: converted,\n      color: classColor1,\n      marker: { fillColor: markerFill1, lineColor: t.ink, lineWidth: 1 },\n    },\n    {\n      name: \"Fitted probability\",\n      type: \"line\",\n      data: curveX.map((x, i) => [x, curveY[i]]),\n      color: curveColor,\n      lineWidth: 2.5,\n      marker: { enabled: false },\n      enableMouseTracking: false,\n    },\n  ],\n});\n"}