From independent noise to correlated assets
A random number generator gives independent draws. Assets are correlated, so a simulation of a portfolio needs draws with the right covariance matrix Σ. The Cholesky factorisation finds a lower-triangular matrix L with LLᵀ=Σ; then if z is a vector of independent standard normals, Lz has covariance exactly L I Lᵀ=Σ. In NumPy the draws arrive as rows, so you compute z @ L.T for an (n, d) array z. scipy.linalg.cholesky(cov, lower=True) does the factorisation and raises an error if the matrix is not positive definite, which is useful: it is the alarm that your covariance matrix has a problem. Seed the generator with np.random.default_rng(seed) and the experiment is repeatable.
With the simulator you can compute risk numbers. The simulated portfolio return of each draw is draws @ w, and the one-day value at risk at level α is minus the α-quantile of those returns (np.quantile). For normal draws it must agree with the variance–covariance formula -(wᵀμ+z_α√(wᵀΣw)), and for twelve assets it does: 2.07% by simulation and 2.08% by the formula, against 2.33% by history, because real returns have fatter tails than any normal model. Estimated covariance matrices can also fail: if you set the correlation of two assets to an impossible 1.2, the smallest eigenvalue turns negative (about -6×10⁻⁵) and Cholesky refuses the matrix. The standard repair uses scipy.linalg.eigh: lift every eigenvalue below a small floor up to the floor, rebuild the matrix and symmetrise it. The result is positive definite and differs from the original by only about 3×10⁻⁵.
- Cholesky turns noise into correlation:
z @ L.Thas covariancecovwhencov = L @ L.T. - Simulation and formula should agree for normal draws: the gap to history is the cost of fat tails.
- A failed factorisation is information: repair a matrix that is not positive definite by lifting its small or negative eigenvalues.
Lab · what's graded
- The Cholesky factor10 pts
- Correlated draws20 pts
- Monte Carlo value at risk25 pts
- The parametric value at risk15 pts
- Repairing a covariance matrix15 pts
- The twelve-asset study15 pts