Get Started

Portfolio standard deviation

In order to calculate portfolio volatility, you will need the covariance matrix, the portfolio weights, and knowledge of the transpose operation. The transpose of a numpy array can be calculated using the .T attribute. The np.dot() function is the dot-product of two arrays.

The formula for portfolio volatility is:

$$ \sigma_{Portfolio} = \sqrt{ w_T \cdot \Sigma \cdot w } $$

  • \( \sigma_{Portfolio} \): Portfolio volatility
  • \( \Sigma \): Covariance matrix of returns
  • w: Portfolio weights (\( w_T \) is transposed portfolio weights)
  • \( \cdot \) The dot-multiplication operator

portfolio_weights and cov_mat_annual are available in your workspace.

This is a part of the course

“Introduction to Portfolio Risk Management in Python”

View Course

Exercise instructions

Calculate the portfolio volatility assuming you use the portfolio_weights by following the formula above.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import numpy as np
import numpy as np

# Calculate the portfolio standard deviation
portfolio_volatility = ____(np.dot(portfolio_weights.T, np.dot(cov_mat_annual, portfolio_weights)))
print(portfolio_volatility)
Edit and Run Code

This exercise is part of the course

Introduction to Portfolio Risk Management in Python

IntermediateSkill Level
4.5+
11 reviews

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 deviation
Exercise 9: Markowitz portfoliosExercise 10: The efficient frontierExercise 11: Sharpe ratiosExercise 12: The MSR portfolioExercise 13: The GMV portfolio

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free