{
  "id": "covariance",
  "version": "a0ab5eeca270",
  "status": "published",
  "name": "Covariance Calculator",
  "question": "What is the covariance?",
  "summary": "Computes the sample and population covariance of paired x and y values, with the means, the standard deviations and the correlation coefficient.",
  "category": "statistics",
  "subcategory": "descriptive",
  "url": "https://www.acalculator.org/statistics/covariance-calculator",
  "markdown": "https://www.acalculator.org/statistics/covariance-calculator.md",
  "kind": "function",
  "method": "sample cov = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ (n − 1); population cov = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ n; r = cov ÷ (sₓ s_y).",
  "assumptions": [
    "The x and y lists pair up in order: the first x with the first y, and so on.",
    "The sums are exact on the typed decimals, and each result is rounded once at the end.",
    "2 to 10,000 pairs. With n = 2 the sample covariance divides by 1."
  ],
  "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": {
    "sample": {
      "label": "Sample covariance",
      "description": "Σ(x − x̄)(y − ȳ) ÷ (n − 1): use it when the pairs are a sample from a larger group.",
      "format": "number"
    },
    "population": {
      "label": "Population covariance",
      "description": "Σ(x − x̄)(y − ȳ) ÷ n: use it when the pairs are the whole group.",
      "format": "number"
    },
    "direction": {
      "label": "Direction",
      "description": "Positive when y tends to rise with x, negative when it falls, none when the covariance is 0.",
      "format": "text"
    },
    "n": {
      "label": "Pairs (n)",
      "description": "How many (x, y) pairs there are.",
      "format": "integer"
    },
    "meanX": {
      "label": "Mean of x (x̄)",
      "description": "The sum of the x values divided by n.",
      "format": "number"
    },
    "meanY": {
      "label": "Mean of y (ȳ)",
      "description": "The sum of the y values divided by n.",
      "format": "number"
    },
    "sdX": {
      "label": "Sample SD of x",
      "description": "The sample standard deviation of x: √(Σ(x − x̄)² ÷ (n − 1)).",
      "format": "number"
    },
    "sdY": {
      "label": "Sample SD of y",
      "description": "The sample standard deviation of y: √(Σ(y − ȳ)² ÷ (n − 1)).",
      "format": "number"
    },
    "r": {
      "label": "Correlation r",
      "description": "The covariance divided by both standard deviations, from −1 to 1; left out when x or y never varies.",
      "format": "number"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "x": [
        4,
        4.2,
        3.9,
        4.3,
        4.1
      ],
      "y": [
        2,
        2.1,
        2,
        2.1,
        2.2
      ]
    },
    "outputs": {
      "sample": 0.0075,
      "population": 0.006,
      "direction": "positive",
      "n": 5,
      "meanX": 4.1,
      "meanY": 2.08,
      "sdX": 0.15811388300841897,
      "sdY": 0.08366600265340755,
      "r": 0.5669467095138409
    },
    "text": "The sample covariance of the 5 pairs is 0.0075; the population covariance is 0.006."
  },
  "examples": [
    {
      "given": {
        "x": [
          4,
          4.2,
          3.9,
          4.3,
          4.1
        ],
        "y": [
          2,
          2.1,
          2,
          2.1,
          2.2
        ]
      },
      "expect": {
        "sample": 0.0075,
        "population": 0.006,
        "n": 5,
        "meanX": 4.1,
        "meanY": 2.08,
        "direction": "positive"
      },
      "source": "NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples with covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm; hand calculation in content.mdx: Σ(x − x̄)(y − ȳ) = 0.03, 0.03 ÷ 4 = 0.0075, 0.03 ÷ 5 = 0.006"
    },
    {
      "given": {
        "x": [
          4,
          4.2,
          3.9,
          4.3,
          4.1
        ],
        "y": [
          0.6,
          0.59,
          0.58,
          0.62,
          0.63
        ]
      },
      "expect": {
        "sample": 0.00175,
        "population": 0.0014
      },
      "source": "NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples with covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm; hand calculation in content.mdx: Σ = 0.007, 0.007 ÷ 4 = 0.00175"
    },
    {
      "given": {
        "x": [
          2,
          2.1,
          2,
          2.1,
          2.2
        ],
        "y": [
          0.6,
          0.59,
          0.58,
          0.62,
          0.63
        ]
      },
      "expect": {
        "sample": 0.00135,
        "population": 0.00108
      },
      "source": "NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples with covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm; hand calculation in content.mdx: Σ = 0.0054, 0.0054 ÷ 4 = 0.00135"
    },
    {
      "given": {
        "x": [
          1,
          2,
          3
        ],
        "y": [
          6,
          4,
          2
        ]
      },
      "expect": {
        "sample": -2,
        "population": -1.3333333333333333,
        "r": -1,
        "direction": "negative"
      },
      "source": "hand calculation in content.mdx: (−1)(2) + 0 + (1)(−2) = −4, −4 ÷ 2 = −2; NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples with covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm"
    }
  ],
  "sources": [
    "NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples of three variables give covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm (retrieved 2026-10-02)"
  ],
  "related": [
    "correlation-coefficient",
    "variance",
    "standard-deviation",
    "linear-regression"
  ],
  "changelog": []
}
