{"spec_id":"acf-pacf","library":"muix","language":"javascript","code":"// anyplot.ai\n// acf-pacf: Autocorrelation and Partial Autocorrelation (ACF/PACF) Plot\n// Library: muix 7.29.1 | JavaScript 22.22.3\n// Quality: 84/100 | Created: 2026-06-10\n//# anyplot-orientation: landscape\n// anyplot.ai\n// acf-pacf: Autocorrelation and Partial Autocorrelation (ACF/PACF) Plot\n// Library: MUI X Charts | React | Node 22\n// License: @mui/x-charts — MIT (community). Pro/Premium are out of scope.\n// Quality: pending | Created: 2026-06-10\n\nimport { BarChart } from \"@mui/x-charts/BarChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// --- Deterministic LCG RNG (seed = 42) ---------------------------------------\nfunction lcgRng(seed) {\n  let s = seed >>> 0;\n  return () => {\n    s = (Math.imul(s, 1664525) + 1013904223) >>> 0;\n    return s / 4294967296;\n  };\n}\n\nfunction randn(rng) {\n  const u = Math.max(rng(), 1e-10);\n  return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * rng());\n}\n\n// --- AR(2) time series: x_t = 0.65·x_{t-1} + 0.15·x_{t-2} + ε_t -----------\nconst rng = lcgRng(42);\nconst N = 300;\nconst ts = [0, randn(rng) * 0.5];\nfor (let i = 2; i < N; i++) {\n  ts.push(0.65 * ts[i - 1] + 0.15 * ts[i - 2] + randn(rng));\n}\n\n// Centre and scale\nconst mu = ts.reduce((a, b) => a + b, 0) / N;\nconst cx = ts.map((x) => x - mu);\nconst c0 = cx.reduce((s, v) => s + v * v, 0) / N;\n\n// ACF at lag h\nfunction acfAt(h) {\n  let s = 0;\n  for (let i = h; i < N; i++) s += cx[i] * cx[i - h];\n  return s / (N * c0);\n}\n\nconst MAX_LAG = 30;\nconst rho = Array.from({ length: MAX_LAG + 1 }, (_, k) => acfAt(k));\n\n// PACF via Durbin–Levinson recursion\nfunction computePACF(rhoArr, maxLag) {\n  const pacf = [1, rhoArr[1]];\n  let phi = [rhoArr[1]];\n  for (let k = 2; k <= maxLag; k++) {\n    let num = rhoArr[k];\n    let den = 1;\n    for (let j = 0; j < k - 1; j++) {\n      num -= phi[j] * rhoArr[k - 1 - j];\n      den -= phi[j] * rhoArr[j + 1];\n    }\n    const pkk = den !== 0 ? num / den : 0;\n    pacf.push(pkk);\n    const next = Array(k).fill(0);\n    next[k - 1] = pkk;\n    for (let j = 0; j < k - 1; j++) next[j] = phi[j] - pkk * phi[k - 2 - j];\n    phi = next;\n  }\n  return pacf;\n}\n\nconst pacf = computePACF(rho, MAX_LAG);\n\n// 95% CI: ±1.96 / √N\nconst CI = 1.96 / Math.sqrt(N);\n\nconst round4 = (v) => Math.round(v * 10000) / 10000;\n\nconst acfLags = Array.from({ length: MAX_LAG + 1 }, (_, i) => String(i));\nconst pacfLags = Array.from({ length: MAX_LAG }, (_, i) => String(i + 1));\nconst acfData = rho.map(round4);\nconst pacfData = pacf.slice(1).map(round4);\n\n// --- Chart -------------------------------------------------------------------\nexport default function Chart() {\n  const W = window.ANYPLOT_SIZE.width;\n  const H = window.ANYPLOT_SIZE.height;\n\n  const TITLE_H = 54;\n  const PANEL_H = Math.floor((H - TITLE_H - 6) / 2);\n\n  const barColor = t.palette[0]; // #009E73 — brand green\n\n  const ciLineStyle = {\n    stroke: t.inkSoft,\n    strokeDasharray: \"7 4\",\n    strokeWidth: 1.5,\n    strokeOpacity: 0.85,\n  };\n\n  const zeroLineStyle = {\n    stroke: t.inkSoft,\n    strokeWidth: 0.7,\n    strokeOpacity: 0.45,\n  };\n\n  const tickStyle = { fontSize: 12, fill: t.inkSoft };\n  const labelStyle = { fontSize: 14, fill: t.ink };\n\n  const axisLineSx = {\n    \"& .MuiChartsAxis-line\": { stroke: t.grid },\n  };\n\n  const xAxisBase = {\n    scaleType: \"band\" as const,\n    categoryGapRatio: 0.62,\n    tickLabelStyle: tickStyle,\n  };\n\n  const yAxisBase = {\n    tickMinStep: 0.25,\n    tickLabelStyle: tickStyle,\n    labelStyle,\n  };\n\n  return (\n    <div\n      style={{\n        width: W,\n        height: H,\n        background: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily: \"'Roboto', 'Helvetica Neue', Arial, sans-serif\",\n      }}\n    >\n      {/* Title */}\n      <div\n        style={{\n          height: TITLE_H,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          fontSize: 22,\n          fontWeight: 600,\n          color: t.ink,\n          letterSpacing: 0.15,\n        }}\n      >\n        AR(2) Process · acf-pacf · javascript · muix · anyplot.ai\n      </div>\n\n      {/* ACF panel */}\n      <div style={{ height: PANEL_H, flexShrink: 0 }}>\n        <BarChart\n          width={W}\n          height={PANEL_H}\n          skipAnimation\n          colors={[barColor]}\n          xAxis={[{ ...xAxisBase, data: acfLags }]}\n          yAxis={[{ ...yAxisBase, label: \"ACF\", min: -0.35, max: 1.05 }]}\n          series={[{ data: acfData, label: \"ACF\" }]}\n          margin={{ top: 20, bottom: 28, left: 80, right: 65 }}\n          slotProps={{ legend: { hidden: true } }}\n          sx={axisLineSx}\n        >\n          <ChartsReferenceLine\n            y={CI}\n            lineStyle={ciLineStyle}\n            label=\"95% CI\"\n            labelStyle={{ fontSize: 11, fill: t.inkSoft }}\n            labelAlign=\"end\"\n          />\n          <ChartsReferenceLine y={-CI} lineStyle={ciLineStyle} />\n          <ChartsReferenceLine y={0} lineStyle={zeroLineStyle} />\n        </BarChart>\n      </div>\n\n      {/* PACF panel */}\n      <div style={{ flex: 1 }}>\n        <BarChart\n          width={W}\n          height={PANEL_H}\n          skipAnimation\n          colors={[barColor]}\n          xAxis={[{\n            ...xAxisBase,\n            data: pacfLags,\n            label: \"Lag\",\n            labelStyle,\n          }]}\n          yAxis={[{ ...yAxisBase, label: \"PACF\" }]}\n          series={[{ data: pacfData, label: \"PACF\" }]}\n          margin={{ top: 10, bottom: 50, left: 80, right: 65 }}\n          slotProps={{ legend: { hidden: true } }}\n          sx={axisLineSx}\n        >\n          <ChartsReferenceLine y={CI} lineStyle={ciLineStyle} />\n          <ChartsReferenceLine y={-CI} lineStyle={ciLineStyle} />\n          <ChartsReferenceLine y={0} lineStyle={zeroLineStyle} />\n        </BarChart>\n      </div>\n    </div>\n  );\n}\n"}