{
  "schemaVersion": 1,
  "slug": "linear-regression-calculator",
  "category": "statistics",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "19b0526232de93c8f0ebc9f42cffa402fb2e1f05fe33f8c43f892de6cbb9b560",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/linear-regression-calculator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "b = Sxy ÷ Sxx; a = my − b × mx; r = Sxy ÷ √(Sxx × Syy); ŷ = b × x + a",
    "vars": [
      [
        "x, y",
        "paired sample values"
      ],
      [
        "mx, my",
        "sample means"
      ],
      [
        "Sxx",
        "Σ (x − mx)²"
      ],
      [
        "Sxy",
        "Σ (x − mx)(y − my)"
      ],
      [
        "b, a",
        "slope and intercept"
      ],
      [
        "r",
        "Pearson correlation; r² is R²"
      ]
    ],
    "note": "Pair values by position. X and Y need equal lists with at least two finite values; X must vary. Correlation and R² are undefined when Y is constant."
  },
  "externalEvidence": {
    "reviewedAt": "2026-10-12",
    "sources": [
      {
        "name": "NIST — Least-squares regression",
        "url": "https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd141.htm"
      }
    ],
    "referenceRole": "definition",
    "implementationHash": "19b0526232de93c8f0ebc9f42cffa402fb2e1f05fe33f8c43f892de6cbb9b560",
    "basis": "b = Sxy ÷ Sxx; a = my − b × mx; r = Sxy ÷ √(Sxx × Syy); ŷ = b × x + a",
    "limitations": "The references explain definitions and methods; any extra preset must be checked against the assumptions on this page."
  },
  "contract": {
    "compute": "const parse=s=>{const t=String(s).trim();const num='[+-]?(?:\\\\d+(?:\\\\.\\\\d*)?|\\\\.\\\\d+)(?:[eE][+-]?\\\\d+)?';if(!t||!new RegExp('^\\\\s*'+num+'(?:[\\\\s,]+'+num+')*\\\\s*$').test(t))return null;return t.match(new RegExp(num,'g')).map(Number);};const xs=parse(xValues),ys=parse(yValues);if(!xs||!ys||xs.length<2||ys.length!==xs.length||xs.some(v=>!Number.isFinite(v))||ys.some(v=>!Number.isFinite(v)))return{slope:null,intercept:null,rSquared:null,predicted:null,correlation:null,__error:'inputError'};const n=xs.length,mx=xs.reduce((a,b)=>a+b,0)/n,my=ys.reduce((a,b)=>a+b,0)/n;let sxy=0,sxx=0,syy=0;for(let k=0;k<n;k++){const dx=xs[k]-mx,dy=ys[k]-my;sxy+=dx*dy;sxx+=dx*dx;syy+=dy*dy;}if(!Number.isFinite(sxx)||!Number.isFinite(sxy)||!Number.isFinite(syy)||sxx===0)return{slope:null,intercept:null,rSquared:null,predicted:null,correlation:null,__error:'inputError'};const slope=sxy/sxx,intercept=my-slope*mx,r=syy>0?sxy/Math.sqrt(sxx*syy):null;return{slope,intercept,rSquared:r===null?null:r*r,predicted:slope*predictX+intercept,correlation:r};",
    "inputs": [
      {
        "name": "xValues",
        "type": "text",
        "default": "1, 2, 3, 4, 5",
        "step": 1
      },
      {
        "name": "yValues",
        "type": "text",
        "default": "2.1, 3.9, 6.2, 7.8, 10.1",
        "step": 1
      },
      {
        "name": "predictX",
        "type": "number",
        "default": 6,
        "min": -1000000000000,
        "max": 1000000000000,
        "step": 0.01
      }
    ],
    "outputs": [
      {
        "name": "slope",
        "precision": 6,
        "unit": ""
      },
      {
        "name": "intercept",
        "precision": 6,
        "unit": ""
      },
      {
        "name": "rSquared",
        "precision": 4,
        "unit": ""
      },
      {
        "name": "predicted",
        "precision": 6,
        "unit": ""
      },
      {
        "name": "correlation",
        "precision": 4,
        "unit": ""
      }
    ],
    "interpret": null
  }
}
