Risk/Portfolio Matrices (Covariance, Correlation) TODO
Concept
When you view multiple assets' returns as a vector, the covariance matrix is the symmetric positive semi-definite matrix collecting the degree of co-movement between every pair. The correlation matrix normalizes each entry by dividing by the standard deviations, removing scale so values become comparable between -1 and 1. For a portfolio weight vector w, the variance is computed as a quadratic form obtained by multiplying the covariance matrix by w on both sides, and this value shrinks below the weighted sum of individual variances the more you mix in assets with low or negative correlation. This is the mathematical essence of diversification. Eigendecomposition shows that the direction of large eigenvalues corresponds to common risk factors, and a core practical point is that with fewer samples the estimated covariance becomes unstable, requiring corrections like shrinkage.
When holding multiple positions at once, the real risk doesn't come from each individual position's volatility, but from the degree to which they move together.
Code & Formula
# 리스크·포트폴리오 행렬(공분산·상관) — 세 자산의 수익률에서 공분산·상관행렬을 구하고
# 포트폴리오 분산 = w^T Σ w 가 개별 분산의 가중합보다 작아지는 분산투자 효과를 확인한다.
import numpy as np
rng = np.random.default_rng(42)
n_days = 500
# 자산 A, B는 서로 강한 양의 상관, 자산 C는 거의 무상관이 되도록 수익률 생성
factor = rng.normal(0, 0.01, n_days)
returns_A = factor + rng.normal(0, 0.003, n_days)
returns_B = factor + rng.normal(0, 0.003, n_days)
returns_C = rng.normal(0, 0.01, n_days)
R = np.vstack([returns_A, returns_B, returns_C]) # shape (3, n_days)
cov = np.cov(R)
corr = np.corrcoef(R)
print("공분산 행렬 Σ:\n", np.round(cov, 6))
print("\n상관계수 행렬:\n", np.round(corr, 3))
w = np.array([1 / 3, 1 / 3, 1 / 3])
portfolio_var = w @ cov @ w # 이차형식 w^T Σ w
weighted_avg_var = w @ np.diag(cov) # 상관을 무시하고 개별 분산만 가중합한 값
print(f"\n동일가중 포트폴리오 분산 (w^T Σ w) = {portfolio_var:.6f}")
print(f"상관 무시한 개별분산 가중합 = {weighted_avg_var:.6f}")
print(f"→ 실제 포트폴리오 분산이 더 {'작음' if portfolio_var < weighted_avg_var else '크거나 같음'}: "
f"C가 A,B와 무상관이라 분산투자 효과가 발생")
Exercise
Compute the covariance and correlation matrices from the daily returns of three or four assets, then numerically search for the weights that minimize portfolio variance as you vary them.
Practical Connection
In prediction markets, positions across multiple markets become correlated whenever the underlying events overlap, so summing each market's risk independently underestimates the actual exposure.
If you study this on a given day, add a note link and a ✅ to this line in the source curriculum (docs/knowledge/math-50-curriculum.md) and this spot will lead straight to the note body. You can also write directly on this page — but regenerating overwrites it, so it's safer to keep anything you want to save as markdown under docs/algorithms/.