Statistical Tools#

The stats module provides pure statistical functions used by both internal validity and sensitivity analysis.

HP Filter#

Hodrick-Prescott filter for trend-cycle decomposition. Solves the penalized least-squares problem:

\[\min_{\tau} \sum_{t=1}^{T} (y_t - \tau_t)^2 + \lambda \sum_{t=3}^{T} (\Delta^2 \tau_t)^2\]

via the sparse linear system \((I + \lambda K^T K)\tau = y\), where \(K\) is the second-difference matrix. Uses scipy.sparse.linalg.spsolve for efficiency.

from validation.robustness.stats import hp_filter

trend, cycle = hp_filter(series, lamb=1600.0)

Cross-Correlation#

Computes the cross-correlation function:

\[\rho(k) = \text{corr}(x_t, \, y_{t+k})\]

for integer lags \(k = -\text{max\_lag}, \ldots, +\text{max\_lag}\). At lag \(k=0\), this is the contemporaneous correlation. Positive \(k\) means \(y\) leads \(x\).

from validation.robustness.stats import cross_correlation

corrs = cross_correlation(gdp_cycle, unemployment_cycle, max_lag=4)

AR Fitting#

Fits an autoregressive model via ordinary least squares:

\[y_t = c + \phi_1 y_{t-1} + \cdots + \phi_p y_{t-p} + \varepsilon_t\]

using np.linalg.lstsq. Returns coefficients \([c, \phi_1, \ldots, \phi_p]\) and \(R^2\).

from validation.robustness.stats import fit_ar

coeffs, r_squared = fit_ar(gdp_cycle, order=2)

Impulse-Response Function#

Simulates the response to a unit shock at \(t=0\) through the AR recursion. For a stable AR(1) with coefficient \(\phi\), the IRF is \(\phi^t\) (exponential decay).

from validation.robustness.stats import impulse_response

irf = impulse_response(coeffs, n_periods=20)

API Reference#

Statistical tools for robustness analysis.

Pure functions for time series analysis: Hodrick-Prescott filtering, lead-lag cross-correlations, autoregressive model fitting, and impulse-response function computation. No simulation dependencies.

validation.robustness.stats.hp_filter(y, lamb=1600.0)[source]#

Hodrick-Prescott filter decomposition into trend and cycle.

Solves the penalized least-squares problem:

min_τ Σ(y_t - τ_t)² + λ Σ(τ_{t+1} - 2τ_t + τ_{t-1})²

via the sparse linear system (I + λ K'K) τ = y.

Parameters:
  • y (NDArray) – Time series of length T.

  • lamb (float) – Smoothing parameter. Standard values: 1600 for quarterly data, 6.25 for annual, 129600 for monthly.

Returns:

  • trend (NDArray) – Trend component (τ).

  • cycle (NDArray) – Cyclical component (y - τ).

validation.robustness.stats.cross_correlation(x, y, max_lag=4)[source]#

Cross-correlation of x and y at integer leads and lags.

Computes corr(x_t, y_{t+k}) for k = -max_lag, ..., 0, ..., +max_lag.

At lag k=0 this is the contemporaneous correlation. Positive k means y leads x (y is shifted forward relative to x).

Parameters:
  • x (NDArray) – Time series of equal length T.

  • y (NDArray) – Time series of equal length T.

  • max_lag (int) – Maximum lead/lag to compute.

Returns:

Array of shape (2 * max_lag + 1,) with correlations. Index max_lag + k holds corr(x_t, y_{t+k}).

Return type:

NDArray

validation.robustness.stats.fit_ar(y, order=2)[source]#

Fit an AR(p) model via ordinary least squares.

Estimates the model y_t = c + φ_1 y_{t-1} + ... + φ_p y_{t-p} + ε_t.

Parameters:
  • y (NDArray) – Time series of length T.

  • order (int) – AR order (p). Must be >= 1.

Returns:

  • coeffs (NDArray) – Estimated coefficients [c, φ_1, ..., φ_p] of length order + 1.

  • r_squared (float) – Coefficient of determination (R²).

validation.robustness.stats.impulse_response(ar_coeffs, n_periods=20)[source]#

Compute impulse-response function from AR coefficients.

Simulates the response to a unit shock at t=0 through the AR recursion.

Parameters:
  • ar_coeffs (NDArray) – AR coefficients [c, φ_1, ..., φ_p] from fit_ar().

  • n_periods (int) – Number of periods to simulate.

Returns:

Impulse-response values of length n_periods.

Return type:

NDArray