The brief
A market-risk desk revalues its book under thousands of simulated days. Each day is a joint move in four risk factors: the 2-year and 10-year yields, an equity index and the EUR/USD exchange rate. If the moves have the wrong volatilities or correlations, the risk figure is wrong.
The factors come in different units: yield changes in basis points, the others in percent. A plain simulation matches the target correlations only roughly. For reports that must reproduce the model's risk figure, the desk also wants scenarios that match the targets exactly.
Your task
- Write
simulate_moves(vols, corr, n, seed): an array of daily moves with one row per scenario and one column per factor. Draw independent standard normals withnp.random.default_rng(seed), correlate them with the Cholesky factor ofcorr, and scale each column by its volatility. - Write
matched_moves(vols, corr, n, seed):nscenarios whose sample mean is exactly 0 and whose sample covariance matrix is exactly . Here is the diagonal matrix of the volatilities, iscorr, and the sample covariance divides by , asnp.covdoes. RaiseValueErrorunlessnis larger than the number of factors. - Write
pnl_volatility(vols, corr, sensitivities): the standard deviation of the book's daily P&L under the model. The P&L of a move is , where holds the dollars made for a move of one unit in each factor.
All three raise ValueError unless corr is square and symmetric, with 1 on its diagonal, and positive definite. They need one volatility per factor, none negative, and pnl_volatility needs one sensitivity per factor.
The data
FACTORS names the four factors with their units. VOLS holds their daily volatilities, CORR the target correlation matrix, and SENSITIVITIES the book's P&L for a move of one unit.
Hint: Where to start
Case study 3.7.4 turns a table of independent draws into correlated ones with Z @ L.T, where is the target. Lesson 5.5.3 gives the rule behind it: .
Hint: Matching exactly
For any sample, the sample covariance of is exactly (case study 3.7.4). So centre , then turn it into a table whose sample covariance is exactly . If (lesson 3.7.2), take , found with triangular solves instead of an inverse.
Hint: The P&L
The P&L is one linear combination of jointly normal moves. Lesson 5.5.3 gives its variance, , and case study 5.5.5 uses the same rule for a fund.