{
  "id": "correlation-coefficient",
  "version": "8f34a7a4325f",
  "status": "published",
  "name": "Correlation Coefficient Calculator",
  "question": "What is the correlation coefficient?",
  "summary": "Computes Pearson’s correlation coefficient r between paired x and y values, with r², the t statistic and the p-value of the test that the correlation is 0.",
  "category": "statistics",
  "subcategory": "descriptive",
  "url": "https://www.acalculator.org/statistics/correlation-coefficient-calculator",
  "markdown": "https://www.acalculator.org/statistics/correlation-coefficient-calculator.md",
  "kind": "function",
  "method": "r = Sxy ÷ √(Sxx × Syy), with Sxx = Σ(x − x̄)², Syy = Σ(y − ȳ)², Sxy = Σ(x − x̄)(y − ȳ); t = r √(n − 2) ÷ √(1 − r²).",
  "assumptions": [
    "The first x goes with the first y, and so on, so both lists must be the same length (2 to 10,000 pairs).",
    "r measures a straight-line link only; a strong curved link can still give r near 0.",
    "The p-value assumes the pairs are independent and the data are roughly normal."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "x": {
        "title": "x values",
        "description": "The first variable: one number per pair, in order.",
        "type": "array",
        "items": {
          "type": "number"
        }
      },
      "y": {
        "title": "y values",
        "description": "The second variable: one number per pair, in the same order as x.",
        "type": "array",
        "items": {
          "type": "number"
        }
      }
    }
  },
  "outputs": {
    "r": {
      "label": "Correlation coefficient r",
      "description": "Pearson’s r, from −1 (a perfect falling line) to 1 (a perfect rising line); 0 is no straight-line link.",
      "format": "number"
    },
    "r2": {
      "label": "r² (coefficient of determination)",
      "description": "r squared: the share of the variation in y that a straight line in x explains.",
      "format": "number"
    },
    "direction": {
      "label": "Direction",
      "description": "Positive when y tends to rise with x, negative when it falls, none when r = 0.",
      "format": "text"
    },
    "n": {
      "label": "Pairs (n)",
      "description": "How many (x, y) pairs there are.",
      "format": "integer"
    },
    "t": {
      "label": "t statistic",
      "description": "r √(n − 2) ÷ √(1 − r²), on n − 2 degrees of freedom; left out when r = ±1 or n = 2.",
      "format": "number"
    },
    "p": {
      "label": "p-value (two-tailed)",
      "description": "The chance of an |r| at least this large if the true correlation is 0; needs at least 3 pairs.",
      "format": "number"
    },
    "df": {
      "label": "Degrees of freedom",
      "description": "The degrees of freedom of the test: n − 2.",
      "format": "integer"
    },
    "slope": {
      "label": "Slope of the best-fit line",
      "description": "Sxy ÷ Sxx: how much y changes for each 1 of x, on the least-squares line.",
      "format": "number"
    },
    "intercept": {
      "label": "Intercept of the best-fit line",
      "description": "ȳ − slope × x̄: the line’s y at x = 0.",
      "format": "number"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "x": "1, 2, 3, 4, 5, 6",
      "y": "2, 4, 5, 4, 5, 7"
    },
    "outputs": {
      "r": 0.8783100656536799,
      "r2": 0.7714285714285715,
      "direction": "Positive",
      "n": 6,
      "t": 3.6742346141747673,
      "p": 0.02131164112875678,
      "df": 4,
      "slope": 0.7714285714285715,
      "intercept": 1.8
    },
    "text": "For your 6 pairs, the correlation coefficient is r = 0.87831 (r² = 0.771429)."
  },
  "examples": [
    {
      "given": {
        "x": [
          1,
          2,
          3,
          4,
          5,
          6
        ],
        "y": [
          2,
          4,
          5,
          4,
          5,
          7
        ]
      },
      "expect": {
        "r": 0.8783100656536799,
        "r2": 0.7714285714285715,
        "n": 6,
        "t": 3.6742346141747673,
        "p": 0.021311641128756734,
        "slope": 0.7714285714285715,
        "intercept": 1.8
      },
      "source": "hand calculation in content.mdx: Sxx = 17.5, Syy = 13.5, Sxy = 13.5, r² = 13.5² ÷ (17.5 × 13.5) = 27/35; NIST Dataplot Reference Manual, CORRELATION: r = Sxy ÷ (√Sxx √Syy); significance from (N − 2) r² ÷ (1 − r²) on the F distribution (https://itl.nist.gov/div898/software/dataplot/refman2/auxillar/correlat.htm, retrieved 2026-10-02)",
      "tolerance": 1e-9
    },
    {
      "given": {
        "x": [
          0.2,
          337.4,
          118.2,
          884.6,
          10.1,
          226.5,
          666.3,
          996.3,
          448.6,
          777,
          558.2,
          0.4,
          0.6,
          775.5,
          666.9,
          338,
          447.5,
          11.6,
          556,
          228.1,
          995.8,
          887.6,
          120.2,
          0.3,
          0.3,
          556.8,
          339.1,
          887.2,
          999,
          779,
          11.1,
          118.3,
          229.2,
          669.1,
          448.9,
          0.5
        ],
        "y": [
          0.1,
          338.8,
          118.1,
          888,
          9.2,
          228.1,
          668.5,
          998.5,
          449.1,
          778.9,
          559.2,
          0.3,
          0.1,
          778.1,
          668.8,
          339.3,
          448.9,
          10.8,
          557.7,
          228.3,
          998,
          888.8,
          119.6,
          0.3,
          0.6,
          557.6,
          339.3,
          888,
          998.5,
          778.9,
          10.2,
          117.6,
          228.9,
          668.4,
          449.2,
          0.2
        ]
      },
      "expect": {
        "r2": 0.999993745883712,
        "slope": 1.00211681802045,
        "intercept": -0.262323073774029,
        "n": 36
      },
      "source": "NIST StRD linear least squares dataset Norris, certified R-squared 0.999993745883712 (https://www.itl.nist.gov/div898/strd/lls/data/LINKS/v-Norris.shtml, retrieved 2026-10-02) (certified values to 15 digits)",
      "tolerance": 1e-12
    },
    {
      "given": {
        "x": [
          1,
          2,
          3
        ],
        "y": [
          9,
          6,
          3
        ]
      },
      "expect": {
        "r": -1,
        "r2": 1,
        "p": 0,
        "direction": "Negative"
      },
      "source": "hand calculation in content.mdx: y = 12 − 3x exactly, so r = −1; NIST Dataplot Reference Manual, CORRELATION: r = Sxy ÷ (√Sxx √Syy); significance from (N − 2) r² ÷ (1 − r²) on the F distribution (https://itl.nist.gov/div898/software/dataplot/refman2/auxillar/correlat.htm, retrieved 2026-10-02)"
    },
    {
      "given": {
        "x": [
          1,
          2,
          3,
          4
        ],
        "y": [
          1,
          3,
          3,
          1
        ]
      },
      "expect": {
        "r": 0,
        "r2": 0,
        "t": 0,
        "p": 1,
        "direction": "None (r = 0)"
      },
      "source": "hand calculation in content.mdx: Sxy = 0, so r = 0; NIST Dataplot Reference Manual, CORRELATION: r = Sxy ÷ (√Sxx √Syy); significance from (N − 2) r² ÷ (1 − r²) on the F distribution (https://itl.nist.gov/div898/software/dataplot/refman2/auxillar/correlat.htm, retrieved 2026-10-02)"
    }
  ],
  "sources": [
    "NIST Dataplot Reference Manual, CORRELATION (Sxx, Syy, Sxy and r = Sxy ÷ (√Sxx √Syy); significance from (N − 2) r² ÷ (1 − r²) on the F distribution), retrieved 2026-10-02. https://itl.nist.gov/div898/software/dataplot/refman2/auxillar/correlat.htm",
    "NIST Statistical Reference Datasets, linear least squares dataset Norris, certified values (R-squared 0.999993745883712, B1 1.00211681802045, B0 −0.262323073774029), retrieved 2026-10-02. https://www.itl.nist.gov/div898/strd/lls/data/LINKS/v-Norris.shtml"
  ],
  "related": [
    "linear-regression",
    "standard-deviation",
    "p-value",
    "t-score"
  ],
  "changelog": []
}
