liveBook · Manning

Covariance

As explained in Investing for Programmers

Covariance is a statistical measure of how two variables vary together, with positive values indicating movement in the same direction and negative values indicating opposite movement.

Covariance measures how two assets’ returns vary together. Positive values indicate movement in the same direction; negative values indicate movement in opposite directions. Because share prices have different scales, compare percentage or log returns rather than absolute prices. section 7.2.2

Use covariance carefully when comparing portfolio assets:

  • Calculate it from a DataFrame of log returns, then multiply by the number of trading days to annualize the covariance matrix. section 7.2.2
  • Read the sign as direction, but not the magnitude as relationship strength; correlation standardizes covariance and supplies that missing scale. section 7.2.2
  • Treat observed dependencies as period-specific: relationships among Microsoft, Amazon, and Alphabet can differ in other years. section 7.2.2

Three-stock covariance matrix

For annualized log returns, Microsoft and Amazon have covariance 0.03887, Microsoft and Alphabet 0.03239, and Amazon and Alphabet 0.04290. All are positive, suggesting these returns tended to move together during the tested period. section 7.2.2

cov_matrix = log_returns.cov() * TRADING_DAYS
cov_matrix

Build the annualized covariance matrix with `log_returns.cov() * TRADING_DAYS`. section 7.2.2

How Investing for Programmers covers it

How deep it goes
Defines covariance, demonstrates annualized calculation, and stops short of treating it as a strength measure.
Examples
Compares annualized covariance among Microsoft, Amazon, and Alphabet using return matrices and tables.
What it assumes
Assumes basic stock-price data and return calculations, while distinguishing absolute prices from percentage or log returns.
What you can do after
Apply it via pandas to calculate and interpret an annualized covariance matrix from asset log returns.
How it connects
Links covariance with log returns, variance, standard deviation, correlation, and portfolio risk management.

Also explained in

Compare how 5 books explain covariance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Investing for Programmers. The section links point to the parts of the book it draws on.