How To Calculate Covariance Of Two Stocks Using Excel

Stock Covariance Calculator

Calculate the covariance between two stocks using their historical returns

Enter monthly returns as pairs separated by colons, with each month separated by commas

Calculation Results

Covariance:
Correlation Coefficient:
Interpretation:

Comprehensive Guide: How to Calculate Covariance of Two Stocks Using Excel

Understanding how two stocks move in relation to each other is crucial for portfolio diversification and risk management. Covariance measures this relationship, indicating whether stocks tend to move together (positive covariance) or in opposite directions (negative covariance). This guide will walk you through calculating covariance in Excel, interpreting the results, and applying this knowledge to your investment strategy.

What is Covariance?

Covariance is a statistical measure that calculates the extent to which two random variables (in this case, stock returns) change in tandem. The formula for covariance between two stocks X and Y is:

Cov(X,Y) = Σ[(Xi – μX)(Yi – μY)] / (n – 1)

Where:

  • Xi and Yi are individual returns for stocks X and Y
  • μX and μY are the mean returns of stocks X and Y
  • n is the number of observations (months)

Why Covariance Matters for Investors

Covariance helps investors:

  1. Diversify portfolios by combining assets with negative covariance
  2. Manage risk by understanding how assets move together
  3. Optimize asset allocation based on historical relationships
  4. Predict portfolio performance under different market conditions

Step-by-Step: Calculating Covariance in Excel

Step 1: Gather Historical Price Data

Before calculating covariance, you need historical price data for both stocks. You can obtain this from:

  • Yahoo Finance (free)
  • Bloomberg Terminal (paid)
  • Your brokerage platform
  • Financial APIs like Alpha Vantage or Quandl

For this example, let’s use monthly closing prices for Apple (AAPL) and Microsoft (MSFT) over 12 months.

Step 2: Calculate Monthly Returns

The formula for monthly return is:

Return = (Current Price – Previous Price) / Previous Price

In Excel:

  1. Create columns for Date, Stock 1 Price, Stock 2 Price
  2. Add columns for Stock 1 Return and Stock 2 Return
  3. Use the formula =((B3-B2)/B2) for the first return, then drag down
Date AAPL Price MSFT Price AAPL Return MSFT Return
Jan 2023 $129.93 $239.82
Feb 2023 $138.98 $242.36 0.070 0.011
Mar 2023 $143.63 $250.71 0.033 0.034
Apr 2023 $148.97 $262.49 0.037 0.047
May 2023 $150.87 $265.12 0.013 0.010

Step 3: Calculate the Mean Return

Use Excel’s AVERAGE function to calculate the mean return for each stock:

  1. Select a cell for AAPL’s average return
  2. Enter =AVERAGE(D3:D14) (assuming returns are in column D)
  3. Repeat for MSFT in the adjacent cell

Step 4: Calculate Deviations from Mean

For each month, calculate how much each stock’s return deviates from its average:

  1. Add columns for “AAPL Deviation” and “MSFT Deviation”
  2. Use formula =D3-$F$1 (where F1 contains AAPL’s average)
  3. Drag the formula down for all months

Step 5: Calculate the Product of Deviations

Multiply the deviations for each month:

  1. Add a column for “Product of Deviations”
  2. Use formula =F3*G3 (where F3 is AAPL deviation and G3 is MSFT deviation)

Step 6: Calculate Covariance

Finally, calculate covariance using:

Covariance = SUM(Product of Deviations) / (Number of Periods – 1)

In Excel:

  1. Sum the products of deviations: =SUM(H3:H14)
  2. Divide by (n-1): =H15/11 (for 12 months)

Step 7: Using Excel’s COVARIANCE.S Function

Excel has a built-in function that simplifies this process:

  1. Select a cell for the covariance result
  2. Enter =COVARIANCE.S(D3:D14,E3:E14)
  3. Press Enter to get the covariance value

Interpreting Covariance Results

The covariance value can be positive, negative, or zero:

  • Positive covariance: Stocks tend to move together (both up or both down)
  • Negative covariance: Stocks tend to move in opposite directions
  • Zero covariance: No apparent relationship between the stocks’ movements

However, covariance alone doesn’t indicate the strength of the relationship. For that, we calculate the correlation coefficient:

Correlation = Covariance(X,Y) / (σX × σY)

Where σX and σY are the standard deviations of returns for stocks X and Y.

Correlation Range Interpretation Investment Implication
0.8 to 1.0 Very strong positive Stocks move almost identically; little diversification benefit
0.6 to 0.8 Strong positive Stocks move similarly; some diversification benefit
0.4 to 0.6 Moderate positive Stocks show some relationship; good diversification potential
0.2 to 0.4 Weak positive Stocks show slight tendency to move together
0 to 0.2 Very weak/none Stocks move independently; excellent diversification
-0.2 to 0 Very weak negative Stocks show slight inverse relationship
-0.4 to -0.2 Weak negative Stocks tend to move oppositely; good hedging potential
-0.6 to -0.4 Moderate negative Stocks often move oppositely; strong hedging potential
-0.8 to -0.6 Strong negative Stocks typically move in opposite directions
-1.0 to -0.8 Very strong negative Stocks move almost perfectly oppositely; excellent hedging

Practical Applications of Covariance in Portfolio Management

1. Portfolio Diversification

The primary use of covariance is in portfolio diversification. By combining assets with low or negative covariance, investors can reduce portfolio volatility without sacrificing returns. Modern Portfolio Theory (MPT), developed by Harry Markowitz, is built on this principle.

For example, stocks and bonds typically have negative covariance – when stock prices fall, bond prices often rise as investors seek safer assets. This inverse relationship helps stabilize portfolio returns.

2. Risk Management

Understanding covariance helps in:

  • Identifying which assets will offset each other’s losses
  • Determining optimal position sizes
  • Creating hedging strategies using negatively correlated assets
  • Assessing portfolio risk through value-at-risk (VaR) calculations

3. Asset Allocation

Covariance matrices are used in:

  • Mean-variance optimization
  • Efficient frontier analysis
  • Black-Litterman model for asset allocation
  • Risk parity strategies

4. Performance Attribution

Covariance helps explain:

  • Why a portfolio performed differently than expected
  • Which asset pairs contributed most to portfolio volatility
  • How correlation breakdowns affected performance

Common Mistakes When Calculating Covariance

1. Using Prices Instead of Returns

Covariance should be calculated using returns, not absolute prices. Price-based covariance can be misleading because it’s affected by the magnitude of prices rather than their relative changes.

2. Ignoring Time Period Consistency

Always use the same time periods for both assets. Mixing daily returns for one stock with monthly returns for another will yield meaningless results.

3. Small Sample Size

Covariance calculations with fewer than 20-30 data points are statistically unreliable. The more data points you have, the more confidence you can have in the result.

4. Assuming Stationarity

Covariance isn’t constant over time. Economic conditions, market regimes, and company-specific factors can cause covariance to change. Always use recent, relevant data.

5. Confusing Covariance with Correlation

While related, these are different metrics:

  • Covariance measures how much two variables change together
  • Correlation standardizes this to a -1 to 1 scale, making it easier to interpret the strength of the relationship

Advanced Techniques

Rolling Covariance

Instead of using a fixed time period, calculate covariance over rolling windows (e.g., 12-month rolling covariance) to see how the relationship between assets changes over time.

Exponentially Weighted Covariance

Give more weight to recent observations when calculating covariance, which is particularly useful in volatile markets where older data may not be relevant.

Conditional Covariance

Calculate covariance separately for different market conditions (bull markets vs. bear markets) to understand how relationships change with market regimes.

Excel Alternatives for Covariance Calculation

Python (Pandas)

import pandas as pd

# Create DataFrame with returns
data = {
    'AAPL': [0.07, 0.033, 0.037, 0.013, -0.02, 0.045, 0.018, -0.012, 0.031, 0.024, -0.008, 0.015],
    'MSFT': [0.011, 0.034, 0.047, 0.01, 0.022, 0.031, 0.005, -0.008, 0.025, 0.018, -0.012, 0.021]
}
df = pd.DataFrame(data)

# Calculate covariance matrix
cov_matrix = df.cov()
print(cov_matrix)
        

R

# Create vectors of returns
aapl <- c(0.07, 0.033, 0.037, 0.013, -0.02, 0.045, 0.018, -0.012, 0.031, 0.024, -0.008, 0.015)
msft <- c(0.011, 0.034, 0.047, 0.01, 0.022, 0.031, 0.005, -0.008, 0.025, 0.018, -0.012, 0.021)

# Calculate covariance
cov(aapl, msft)

# Calculate correlation
cor(aapl, msft)
        

Financial Calculators

Many online platforms offer covariance calculators, including:

  • Portfolio Visualizer
  • QuantConnect
  • TradingView (for visual correlation analysis)
  • Bloomberg Terminal (COVR function)

Frequently Asked Questions

Can covariance be greater than 1?

Yes, unlike correlation, covariance isn’t bounded between -1 and 1. Its value depends on the units of measurement (returns in this case). This is why we often standardize it by calculating correlation.

What’s the difference between population covariance and sample covariance?

Population covariance divides by N (total observations), while sample covariance divides by N-1 (Bessel’s correction). Excel’s COVARIANCE.S function calculates sample covariance, which is what we typically use for financial analysis.

How often should I recalculate covariance for my portfolio?

Most professionals recalculate at least quarterly, but more frequent updates (monthly) may be warranted during volatile markets. The key is consistency in your time periods.

Is negative covariance always good for diversification?

Not necessarily. While negative covariance reduces portfolio volatility, you also want assets that can generate positive returns. The optimal portfolio balances diversification with return potential.

Can I use covariance to predict future stock movements?

Covariance measures historical relationships and isn’t predictive by itself. However, it’s a key input for models that do attempt to forecast portfolio behavior under different scenarios.

Conclusion

Calculating covariance between stocks is a fundamental skill for investors seeking to build well-diversified portfolios. While Excel provides powerful tools to compute this metric, the real value comes from understanding what the numbers mean and how to apply them to your investment strategy.

Remember that:

  • Covariance measures how two stocks move together
  • Negative covariance can reduce portfolio risk
  • Correlation standardizes covariance for easier interpretation
  • Historical covariance may not predict future relationships
  • Regular recalculation is important as relationships change over time

By mastering covariance calculations and interpretation, you’ll be better equipped to construct portfolios that balance risk and return according to your investment objectives.

Leave a Reply

Your email address will not be published. Required fields are marked *