import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import norm
from scipy.linalg import inv, solve, eig
from sklearn.decomposition import PCAGraduate Quantitative Economics and Datascience
An SVD for any \(X \in {\mathbb{R}}^{N\times M}\) is:
\[ X = U \Sigma V^{\top} \]
A key result is that we can decompose the data into a sum of outer products of the singular vectors and singular values. Assume ordered so that \(\sigma_1 \geq \sigma_2 \geq \ldots \geq \sigma_M\) and \(M \leq N\):
\[ X = U \Sigma V^{\top} = \sum_{m=1}^M \sigma_m u_m v_m^{\top} \]
Note that
Build \(X = Z F^{\top}\) with latent \(Z \in {\mathbb{R}}^{N \times 2}\), loadings \(F \in {\mathbb{R}}^{3 \times 2}\), both of full column rank, and no noise, so \(X \in {\mathbb{R}}^{N \times 3}\) with \(N > 3\).
How many nonzero singular values does \(X\) have?
A. \(1\)
B. \(2\)
C. \(3\)
D. \(N\)
\[ [X^{\top} X]_{ij} = \sum_{k=1}^N x_{ki} x_{kj} \]
\[ [X X^{\top}]_{ij} = x_i^{\top} x_j \]
\[ X \approx \sum_{m=1}^L \sigma_m u_m v_m^{\top} \]
Create a dataset with two latent factors, the first dominating
N = 50 # number of observations
L, M = 2, 3 # number of latent factors and observed features
np.random.seed(77)
Z = np.random.randn(N, L) # latent factors
F = np.array([[1.0, 0.05], # X_1 = Z_1 + 0.05 Z_2
[2.0, 0.0], # X_2 = 2 Z_1
[3.0, 0.1]]) # X_3 = 3 Z_1 + 0.1 Z_2
X = Z @ F.T + 0.1 * np.random.randn(N, M) # added noise
print(f"Z is {Z.shape}, X is {X.shape}")Z is (50, 2), X is (50, 3)
Singular Values (sqrt eigenvalues):
[24.644 0.8199 0.6185]
Explained Variance Ratio (ordered):
[0.9983 0.0011 0.0006]
pca = PCA(n_components=2) # one less, and correctly specified
Z_hat = pca.fit_transform(X) # transformed by dropping last factor
# Sign is arbitrary, and the components can mix the latent factors
print(f"Correlation of Z_1 to Z_hat_1 = {np.corrcoef(Z.T, Z_hat.T)[0,2]}")
print(f"Correlation of Z_2 to Z_hat_2 = {np.corrcoef(Z.T, Z_hat.T)[1,3]}")Correlation of Z_1 to Z_hat_1 = 0.999329900857125
Correlation of Z_2 to Z_hat_2 = -0.479077909271834
\(X = U \Sigma V^{\top}\) is an SVD of \(X\).
Which change leaves it a valid SVD of the same \(X\)?
A. Flip the sign of \(u_1\) only
B. Double \(\sigma_1\) and halve \(u_1\)
C. Swap \(u_1\) with \(u_2\) only
D. Flip the signs of \(u_1\) and \(v_1\) together
The data are demeaned and the three features have similar variances. Feature \(3\) switches from dollars to cents, so its column is multiplied by \(100\).
What happens to the first principal direction \(v_1\)?
A. Unchanged; only \(\sigma_1\) changes
B. Tilts away from feature \(3\)
C. Tilts toward feature \(3\)
D. Unchanged because the data are demeaned
\[ g(f(x; \theta_e); \theta_d) \approx x \]
If we had a distribution for \(x\) then can solve
\[ \min_{\theta_e, \theta_d} {\mathbb{E}}\|g(f(x; \theta_e); \theta_d) - x\|_2^2 \]
But typically in practice we replace expectation with empirical distribution \(\{x_1,\ldots x_N\}\)
\[ \min_{\theta_e, \theta_d} \frac{1}{N} \sum_{n=1}^N \|g(f(x_n; \theta_e); \theta_d) - x_n\|_2^2 \]
\[ \min_{W} \frac{1}{N} \sum_{n=1}^N \|W \overbrace{W^{\top} x_n}^{z_n = f(x_n;W)} - x_n\|_2^2,\quad \text{with } W^{\top} W = I \]
An auto-encoder is trained on data \(x_1, \ldots, x_N\).
What does its loss compare?
A. The reconstruction \(\hat{x} = g(f(x))\) to \(x\) itself
B. The latent \(z = f(x)\) to \(x\)
C. The reconstruction \(\hat{x}\) to some label \(y\)
The PCA example in this lecture used \(X = Z F^{\top} + 0.1\, \varepsilon\) with latent \(Z \in {\mathbb{R}}^{50 \times 2}\), loadings \(F \in {\mathbb{R}}^{3 \times 2}\), and Gaussian noise \(\varepsilon\), and PCA gave a third explained-variance ratio of \(0.0006\).
Set the noise to zero, so \(X = Z F^{\top}\) exactly, and refit PCA(n_components=3).
What is the third explained-variance ratio?
A. Zero up to roundoff
B. About \(0.0006\), as before
C. Undefined: PCA fails on a rank-deficient matrix
D. Depends on \(N\)
Fit PCA(n_components=M) on \(X \in {\mathbb{R}}^{N \times M}\).
What does explained_variance_ratio_ sum to?
A. \(\sigma_1\)
B. \(M\)
C. \(1\)
D. \(N\)
n_components=L for \(L < M\) the sum is the share of variance the truncation keeps.An auto-encoder has latent dimension \(L = M\), and \(f\) and \(g\) can be any functions.
What is the smallest reconstruction error it can reach?
A. The sum of the smallest singular values
B. Never zero, because of noise in the data
C. Zero, with \(f\) and \(g\) both the identity
D. It depends on the data
Ten financial ratios per firm are linear combinations of two latent factors of comparable importance, plus noise well below both.
What do the singular values of the demeaned ratio matrix look like?
A. Two large, eight near zero
B. All about the same size
C. One large, nine near zero
D. Decaying at a constant rate
Three tasks:
Which tool fits each?
A. k-means, PCA, eigendecomposition of \(A\)
B. PCA, k-means, eigendecomposition of \(A\)
C. PCA, eigendecomposition of \(A\), k-means
D. k-means for all three
In the dynamics lecture we introduced code for simulation
A = np.array([[0.6619469, 0.49646018],[0.5840708, 0.4380531]])
G = np.array([[10.0, 4.0]])
d_0 = np.array([1.0, 1.0])
T, beta = 10, 0.9
p_0 = G @ solve(np.eye(2) - beta * A, d_0)
d = simulate(A, d_0, T)
y = G @ d # total dividends from portfolio
print(f"Portfolio value at t=0 is {p_0[0]:.5g}, "
f"total dividends at time {T} is {y[0,T]:.5g}")Portfolio value at t=0 is 1424.5, total dividends at time 10 is 36.955

If we stack columns \(Q \equiv \begin{bmatrix} q_1 & q_2 \end{bmatrix}\) and let \(\tilde{q}_1\) be the first row of \(Q^{-1}\) then,
\[ A = Q \Lambda Q^{-1} = Q \begin{bmatrix} \lambda_1 & 0 \\ 0 & 0 \end{bmatrix} Q^{-1} = \lambda_1 q_1 \tilde{q}_1 \]
norm = 2.349441087662224e-10
Let’s see if these line up perfectly
