Calculate the Covariance Matrix
Notation
The covariance matrix $\mathbf{A}$ (see the notation reference in Notation) is computed from the demeaned log-differenced yields $\widetilde{\mathbf{Y}}_c \in \mathbb{R}^{(T-12)\times N}$ produced in Step 5:
$$\mathbf{A} = \frac{1}{T-13}\widetilde{\mathbf{Y}}_c^\top \widetilde{\mathbf{Y}}_c$$
$\mathbf{A} \in \mathbb{R}^{N \times N}$ — one row and one column per maturity, so entry $a_{i,j}$ is the sample covariance between maturity $i$ and maturity $j$.
step 1 - calculate the covariance matrix
import subprocess
%run ./dataframes/step6_covariance.py
%run ./dataframes/truncate_with_ellipsis.py
df = covariance_matrix()
subprocess.run(['wl-copy'], input=truncate_with_ellipsis(df, 3).to_html(index=True).encode())
| 5 | 5.5 | 6 | ... | 39 | 39.5 | 40 | |
|---|---|---|---|---|---|---|---|
| 5 | 2.017099 | 1.845525 | 1.697341 | ... | 0.539654 | 0.543767 | 0.543508 |
| 5.5 | 1.845525 | 1.740435 | 1.611747 | ... | 0.528277 | 0.532003 | 0.53189 |
| 6 | 1.697341 | 1.611747 | 1.537304 | ... | 0.502346 | 0.506029 | 0.505854 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 39 | 0.539654 | 0.528277 | 0.502346 | ... | 0.273896 | 0.275327 | 0.275966 |
| 39.5 | 0.543767 | 0.532003 | 0.506029 | ... | 0.275327 | 0.276798 | 0.277426 |
| 40 | 0.543508 | 0.53189 | 0.505854 | ... | 0.275966 | 0.277426 | 0.27808 |
Python
filename: step6_covariance.py
from dataframes.step5_demeaning import *
def covariance_matrix():
df = demeaning()
value_cols = df.columns[1:]
Y_c = df[value_cols]
n = Y_c.shape[0]
A = (Y_c.T @ Y_c) / (n - 1)
return A
covariance matrix of the demeaned, log-differenced yields