This function performs a local principal component analysis (PCA) on asset returns, weighted by a specified kernel function. It extracts local factors and loadings from the weighted returns and computes a factor estimate. Optionally, previously estimated factors can be provided to align the new factors' directions.
local_pca(returns, r, bandwidth, m, kernel_func, prev_F = NULL)A list with the following components:
factors: A \(T × m\) matrix of local factors estimated from the weighted returns.
f_hat: A \(1 × m\) vector containing the factor estimate for time \(r\).
loadings: A \(p × m\) matrix of factor loadings.
w_r: A numeric vector of kernel weights used in the computation.
A numeric matrix of asset returns with dimensions \(T × p\), where \(T\) is the number of time periods and \(p\) is the number of assets.
Integer. The current time index at which to perform the local PCA.
Numeric. The bandwidth used in the kernel weighting.
Integer. The number of factors to extract.
Function. The kernel function used for weighting observations (e.g., epanechnikov_kernel).
Optional. A numeric matrix of previously estimated factors (with dimensions \(T × m\)) used for aligning eigenvector directions. Default is NULL.
The function operates in the following steps:
**Kernel Weight Computation:**
For each time point \(t = 1, \dots, T\), the kernel weight is computed using
boundary_kernel(r, t, T, bandwidth, kernel_func). The weighted returns are given by
$$X_r = \text{returns} \circ \sqrt{k_h},$$
where \(\circ\) denotes element-wise multiplication and \(k_h\) is the vector of kernel weights.
**Eigen Decomposition:** The function computes the eigen decomposition of the matrix \(X_r X_r^\top\) and orders the eigenvalues in descending order. The top \(m\) eigenvectors are scaled by \(\sqrt{T}\) to form the local factors: $$\hat{F}_r = \sqrt{T} \, \text{eigvecs}_{1:m}.$$
**Direction Alignment:**
If previous factors (prev_F) are provided, the function aligns the signs of the new factors with the previous ones
by checking the correlation and flipping the sign if the correlation is negative.
**Loadings Computation:** The loadings are computed by projecting the weighted returns onto the factors: $$\Lambda_r = \frac{1}{T} X_r^\top \hat{F}_r,$$ where the result is transposed to yield a \(p × m\) matrix.
**One-Step-Ahead Factor Estimation:** A second pass computes the factor estimate for the current time index \(r\) by solving $$\hat{F}_r = \left(\Lambda_r^\top \Lambda_r\right)^{-1} \Lambda_r^\top R_r,$$ where \(R_r\) is the return vector at time \(r\).