KatInterview

Scale Factor-Model Portfolio Risk to a Yearly Figure

Math & Bit ManipulationAuto-graded · 18 test cases
MediumAuto-gradedMath & Bit Manipulationmatrixlinear-algebrafinance

Last updated

Compute a portfolio's one-period variance from a factor model (exposures, factor covariance, specific variances), then scale it by periods per year and take the square root.

Scale Factor-Model Portfolio Risk to a Yearly Figure

You hold a portfolio of several stocks. Risk is described by a linear factor model. Every stock loads on a set of common factors, the factors co-vary with each other, and every stock also carries its own stock-specific (idiosyncratic) variance. All inputs are measured over the same single return period, such as one day or one month.

Write a function that returns the portfolio's annualized volatility.

Inputs

  • exposures: a list of n rows, one per stock, each a list of k numbers. exposures[s][j] is the loading of stock s on factor j (the matrix B).
  • factor_cov: a k x k factor covariance matrix F, given per single period. Index it exactly as given: factor_cov[i][j].
  • specific_var: a list of n nonnegative numbers. Entry s is the specific variance of stock s for one period (the diagonal of D).
  • weights: a list of n numbers w, the portfolio weight of each stock.
  • periods_per_year: a nonnegative integer, the number of return periods in a year (for example 252 for daily or 12 for monthly).

Model

Factor returns and stock-specific returns are uncorrelated. Specific returns of different stocks are also uncorrelated with each other. The one-period portfolio variance is therefore

V = (Bᵀw)ᵀ F (Bᵀw)  +  Σ_s  w_s² · specific_var[s]

where (Bᵀw)[j] = Σ_s weights[s] · exposures[s][j], and the quadratic form is

Σ_i Σ_j (Bᵀw)[i] · factor_cov[i][j] · (Bᵀw)[j]

Task

  1. Compute the one-period variance V with the formula above.
  2. If V < 0 (floating-point error or an indefinite F), replace it by 0.0.
  3. Multiply by periods_per_year to get the annual variance (variance grows linearly with time).
  4. Return the square root of that annual variance as a float.

Do not take the square root before scaling, and do not multiply the volatility by periods_per_year.

Edge cases

  • If there are no stocks, or the annual variance is zero (for example periods_per_year = 0 or every term is zero), return 0.0.
  • Weights can be any real numbers, including negative values (short positions), and need not sum to 1.
  • factor_cov is used as given. Do not assume you must symmetrize it. The quadratic form above is the definition.
  • The checker compares floats with a small tolerance, so do not round.

Constraints

  • exposures has n >= 0 rows of length k. factor_cov is k x k, and specific_var and weights each have length n.
  • Every entry of specific_var is >= 0.
  • periods_per_year >= 0.
  • All inputs share the same universe, currency and return horizon, so no unit conversion is needed.

Examples

Example 1

Input: annualized_factor_risk([[1, 0.5], [0.2, 1]], [[0.04, 0.006], [0.006, 0.09]], [0.01, 0.0225], [0.6, 0.4], 252)

Output: 4.362111415358393

Example 2

Input: annualized_factor_risk([[1], [1], [0]], [[0.04]], [0.01, 0.01, 0.04], [0.5, 0.3, 0.2], 12)

Output: 0.6059702963017247

Example 3

Input: annualized_factor_risk([[0, 0], [1, -1]], [[0.01, 0], [0, 0.04]], [0, 0.0025], [0.25, 0.5], 252)

Output: 1.8186533479473213

Hints

Hint 1

Collapse the stock-level loadings first: the portfolio's exposure to factor j is the sum over stocks of weight times loading.

Hint 2

Factor variance is the quadratic form of that exposure vector with the factor covariance matrix. Specific variance adds w_s squared times its variance for each stock, since specific returns are uncorrelated.

Hint 3

Annualize at the variance level: clamp a negative one-period variance to zero, multiply by periods_per_year, and only then take the square root.