How To Compute Covariance In Excel

6 min read

Covariance is a fundamental statistical measure that reveals the directional relationship between two variables. Think about it: when analyzing datasets in Microsoft Excel, understanding how to compute covariance in Excel allows you to determine whether two sets of data move together in the same direction or in opposite directions. This metric is essential for portfolio management, risk assessment, and scientific research where variable interdependence plays a critical role. Excel provides built-in functions that make this calculation accessible without requiring manual formula entry, though knowing which function to choose depends entirely on whether your data represents a sample or an entire population That's the part that actually makes a difference..

Understanding the Concept Behind the Calculation

Before diving into the specific syntax, it helps to visualize what covariance actually measures. Think about it: a positive covariance indicates that as one variable increases, the other tends to increase as well. Conversely, a negative covariance suggests that as one variable rises, the other tends to fall. A covariance near zero implies little to no linear relationship.

Mathematically, the formula calculates the average of the products of deviations for each data point pair from their respective means. In practice, for a sample, the denominator is n-1 (Bessel’s correction), which provides an unbiased estimator of the population covariance. That's why for a population, the denominator is N (the total number of pairs). Excel handles this distinction through two distinct functions, removing the burden of manual arithmetic.

The Two Primary Functions: COVARIANCE.P vs. COVARIANCE.S

Modern versions of Excel (2010 and later) offer two dedicated functions for this purpose. Choosing the correct one is the single most important step in ensuring statistical validity.

COVARIANCE.P (Population Covariance)

Use COVARIANCE.P when your dataset represents the entire population you are studying. Here's one way to look at it: if you are analyzing the monthly returns of every single asset in a specific, closed fund universe, or the heights of every student in a specific classroom, this is the correct function.

Syntax: =COVARIANCE.P(array1, array2)

COVARIANCE.S (Sample Covariance)

Use COVARIANCE.S when your dataset is a sample drawn from a larger population. This is the most common scenario in business and science. If you are analyzing 36 months of stock returns to infer the behavior of the market over decades, you are working with a sample.

Syntax: =COVARIANCE.S(array1, array2)

Note on Legacy Versions: In Excel 2007 and earlier, the single function COVAR existed. In real terms, s). It calculates sample covariance (equivalent to COVARIANCE.If you open an old workbook in a new version, COVAR still works for compatibility but Microsoft recommends using the newer explicit functions.

Step-by-Step Guide to Computing Covariance

Let’s walk through a practical example. Imagine you have monthly return data for Stock A in column B (cells B2:B13) and Stock B in column C (cells C2:C13). You want to know how these assets move relative to each other.

1. Prepare Your Data

Ensure your two data ranges are aligned vertically or horizontally.

  • Critical Requirement: Both arrays must have the exact same number of data points. If Stock A has 12 months of data and Stock B only has 11, Excel will return a #N/A error.
  • Data Types: The arrays must contain numbers. Text, logical values (TRUE/FALSE), or empty cells are ignored by the function, but zeros are counted. If you have missing months, do not leave the cell blank if it represents a zero return; however, if the data is truly missing, the arrays will become unequal in length, causing an error.

2. Select the Output Cell

Click on the cell where you want the result to appear (e.g., cell E2) It's one of those things that adds up..

3. Enter the Function

Type the formula based on your data context.

For Sample Data (Most Common):

=COVARIANCE.S(B2:B13, C2:C13)

For Population Data:

=COVARIANCE.P(B2:B13, C2:C13)

4. Press Enter

Excel will compute the value instantly. The result will be a single number. In financial contexts, this number is often very small (e.g., 0.0004) because returns are percentages. You may want to format the cell as Number with 4–6 decimal places or Scientific notation for readability Still holds up..

Interpreting the Output: Magnitude vs. Direction

A common pitfall for beginners is obsessing over the magnitude of the covariance number. In real terms, unlike correlation, which is standardized between -1 and +1, covariance is unbounded. Its magnitude depends entirely on the units of measurement (dollars, percentages, meters, kilograms).

  • Positive Value: Variables move in the same direction.
  • Negative Value: Variables move in opposite directions.
  • Magnitude: Difficult to interpret in isolation. A covariance of 500 might be "high" for data measured in centimeters but "low" for data measured in kilometers.

Pro Tip: If you need a standardized, unitless measure of relationship strength to compare across different datasets, calculate the Correlation Coefficient using =CORREL(array1, array2) immediately after finding the covariance. Correlation is essentially covariance divided by the product of the two standard deviations That's the part that actually makes a difference..

Alternative Method: The Data Analysis ToolPak

For users who prefer a dialog-box interface or need to generate a full covariance matrix for multiple variables simultaneously, the Analysis ToolPak add-in is superior to writing individual formulas The details matter here. Took long enough..

Enabling the ToolPak

  1. Go to File > Options > Add-ins.
  2. At the bottom, next to "Manage:", select Excel Add-ins and click Go.
  3. Check the box for Analysis ToolPak and click OK.

Running the Covariance Tool

  1. work through to the Data tab on the ribbon.
  2. Click Data Analysis (far right).
  3. Select Covariance from the list and click OK.
  4. Input Range: Select your entire data block including headers (e.g., $B$1:$C$13).
  5. Check Labels in first row if you selected headers.
  6. Output Range: Choose a destination cell (e.g., $E$1).
  7. Click OK.

The Result: Excel outputs a symmetric matrix. The diagonal shows the variance of each variable (covariance with itself). The off-diagonal cells show the covariance between Variable 1 and Variable 2. Important: This tool calculates Population Covariance (denominator N) exclusively. There is no option to switch to Sample Covariance within the ToolPak. If you require sample covariance for a matrix, you must use the COVARIANCE.S function manually or adjust the ToolPak output by multiplying by n/(n-1).

Manual Calculation (For Verification or Learning)

While unnecessary for daily work, building the calculation manually using basic functions reinforces the underlying logic. This is useful for auditing or teaching purposes.

Assuming data in B2:B13 (X) and C2:C13 (Y):

  1. Calculate Means:
    • Mean X: =AVERAGE(B2:B13)
    • Mean Y: =AVERAGE(C2:C13)
  2. Calculate Deviations: In helper columns (e.g., D and E), subtract the mean from each value.
    • D2: =B2 - $B$15 (assuming mean is in B15)

To finish the manual routine, first record the mean of each series in separate cells—you’ve already placed Mean X in B15 and Mean Y in C15. Next, create two helper columns that hold each observation minus its respective mean.

Deviation for Variable X
In column D (starting at D2):

=B2-$B$15

Copy this formula down through D13 so every cell contains “observed‑mean” for column B. Do the same for column E (the deviation for Variable Y) using the mean in C15:

=C2-$C$15

Drag both sets of formulas across the entire data block. Now you have three arrays side by side:

Row B (X) C (Y) D (ΔX) E (ΔY)
2 B2 C2 B
Currently Live

Published Recently

If You're Into This

While You're Here

Thank you for reading about How To Compute Covariance In Excel. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home