The co-variance matrix
You can easily compute the co-variance matrix of a DataFrame of returns using the .cov()
method.
The correlation matrix doesn't really tell you anything about the variance of the underlying assets, only the linear relationships between assets. The co-variance (a.k.a. variance-covariance) matrix, on the other hand, contains all of this information, and is very useful for portfolio optimization and risk management purposes.
This is a part of the course
“Introduction to Portfolio Risk Management in Python”
Exercise instructions
- Calculate the co-variance matrix of the
StockReturns
DataFrame. - Annualize the co-variance matrix by multiplying it with 252, the number of trading days in a year.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Calculate the covariance matrix
cov_mat = StockReturns.____
# Annualize the co-variance matrix
cov_mat_annual = ____
# Print the annualized co-variance matrix
____
This exercise is part of the course
Introduction to Portfolio Risk Management in Python
Evaluate portfolio risk and returns, construct market-cap weighted equity portfolios and learn how to forecast and hedge market risk via scenario generation.
Level up your understanding of investing by constructing portfolios of assets to enhance your risk-adjusted returns.
Exercise 1: Portfolio composition and backtestingExercise 2: Calculating portfolio returnsExercise 3: Equal weighted portfoliosExercise 4: Market-cap weighted portfoliosExercise 5: Correlation and co-varianceExercise 6: The correlation matrixExercise 7: The co-variance matrixExercise 8: Portfolio standard deviationExercise 9: Markowitz portfoliosExercise 10: The efficient frontierExercise 11: Sharpe ratiosExercise 12: The MSR portfolioExercise 13: The GMV portfolioWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.