local_pca: Perform Local Principal Component Analysis

View source: R/pca_function.R

local_pcaR Documentation

Perform Local Principal Component Analysis

Description

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.

Usage

local_pca(returns, r, bandwidth, m, kernel_func, prev_F = NULL)

Arguments

returns

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.

r

Integer. The current time index at which to perform the local PCA.

bandwidth

Numeric. The bandwidth used in the kernel weighting.

m

Integer. The number of factors to extract.

kernel_func

Function. The kernel function used for weighting observations (e.g., epanechnikov_kernel).

prev_F

Optional. A numeric matrix of previously estimated factors (with dimensions T × m) used for aligning eigenvector directions. Default is NULL.

Details

The function operates in the following steps:

  1. **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.

  2. **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}.

  3. **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.

  4. **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.

  5. **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.

Value

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.


TVMVP documentation built on June 28, 2025, 1:08 a.m.