insideout Get Pro
Learn
Quant engineering · Module 06

Portfolio and Risk

How to combine bets and measure the risk of the whole.

Start here · Lesson 01

Risk and return

Measuring both, and why they come together.

Start lesson

LessonsAll lessons

  1. 01 Risk and returnMeasuring both, and why they come together. Free
  2. 02 DiversificationWhy mixing assets lowers risk, and when it stops working. Free
  3. 03 Mean-varianceThe best mix for a level of risk, by the numbers. Free
  4. 04 The efficient frontierEvery best mix at once, and where it breaks in practice. Pro
  5. 05 CAPM and betaHow much of a return the market explains. Pro
  6. 06 Factor modelsValue, size, momentum and the few drivers behind many stocks. Pro
  7. 07 Measuring riskVolatility, VaR, expected shortfall and drawdown. Pro
  8. 08 Estimating covarianceNoisy estimates and why shrinkage helps. Pro
  9. 09 Sizing betsKelly, fractional Kelly, and the cost of betting too much. Pro
  10. 10 Risk parityGiving each part of the portfolio the same risk. Pro
  11. 11 Stress testsWhat happens on the worst days, and planning for them. Pro

Build it yourself · on your own machine

A risk report for an ETF portfolio

Four ways to mix eight funds, rebalanced each month from past data only, with a report on what each one earned and how far each one fell.
  1. Download daily adjusted closes since 2006 for eight US-listed ETFs that cover stocks, bonds, gold and property (for example SPY, EFA, EEM, IEF, TLT, LQD, GLD and VNQ), and FRED's 3-month Treasury bill rate (DTB3).
  2. At each month end, estimate the covariance matrix from the past 252 days, once as the plain sample and once with scikit-learn's LedoitWolf shrinkage.
  3. Build four long-only portfolios from them (equal weight, minimum variance on each matrix, and risk parity), and hold each one for the next month.
  4. Report each one's yearly return, volatility, Sharpe ratio, maximum drawdown, and historical 95% one-day VaR and expected shortfall.
  5. Stress each one: its loss from September 2008 to February 2009 and in March 2020, and its VaR with every correlation set to 0.9.
  6. Check that each month's weights use only earlier prices, and that each ETF carries an equal share of the risk parity portfolio's risk.

How you'll know it worksA table of the four portfolios with every number above, from the months after each rebalance only, and each ETF's share of risk parity's risk within one percentage point of 12.5% every month.

Python with NumPy, pandas, SciPy and scikit-learn. You download the price CSVs yourself from a free source such as Stooq or Yahoo Finance; FRED's rates are free. Uses lessons 01 to 11.