Install
openclaw skills install econ-detrending-correlation-timeseries-detrendingTools and techniques for detrending time series data in macroeconomic analysis. Use when working with economic time series that need to be decomposed into tr...
openclaw skills install econ-detrending-correlation-timeseries-detrendingThis skill provides guidance on decomposing economic time series into trend and cyclical components, a fundamental technique in business cycle analysis.
Economic time series like GDP, consumption, and investment contain both long-term trends and short-term fluctuations (business cycles). Separating these components is essential for:
The HP filter is the most widely used method for detrending macroeconomic data. It decomposes a time series into a trend component and a cyclical component.
Given a time series $y_t$, the HP filter finds the trend $\tau_t$ that minimizes:
$$\sum_{t=1}^{T}(y_t - \tau_t)^2 + \lambda \sum_{t=2}^{T-1}[(\tau_{t+1} - \tau_t) - (\tau_t - \tau_{t-1})]^2$$
Where:
Critical: The choice of λ depends on data frequency:
| Data Frequency | Recommended λ | Rationale |
|---|---|---|
| Annual | 100 | Standard for yearly data |
| Quarterly | 1600 | Hodrick-Prescott (1997) recommendation |
| Monthly | 14400 | Ravn-Uhlig (2002) adjustment |
Common mistake: Using λ=1600 (quarterly default) for annual data produces an overly smooth trend that misses important cyclical dynamics.
from statsmodels.tsa.filters.hp_filter import hpfilter
import numpy as np
# Apply HP filter
# Returns: (cyclical_component, trend_component)
cycle, trend = hpfilter(data, lamb=100) # For annual data
# For quarterly data
cycle_q, trend_q = hpfilter(quarterly_data, lamb=1600)
Important: The function parameter is lamb (not lambda, which is a Python keyword).
For most macroeconomic aggregates (GDP, consumption, investment), you should apply the natural logarithm before filtering:
import numpy as np
# Apply log transformation BEFORE HP filtering
log_series = np.log(real_series)
cycle, trend = hpfilter(log_series, lamb=100)
# The cycle now represents percentage deviations from trend
# e.g., cycle = 0.02 means 2% above trend
import pandas as pd
import numpy as np
from statsmodels.tsa.filters.hp_filter import hpfilter
# Load real (inflation-adjusted) data
real_consumption = pd.Series(...) # Real consumption expenditure
real_investment = pd.Series(...) # Real fixed investment
# Log transformation
ln_consumption = np.log(real_consumption)
ln_investment = np.log(real_investment)
# HP filter with λ=100 for annual data
cycle_c, trend_c = hpfilter(ln_consumption, lamb=100)
cycle_i, trend_i = hpfilter(ln_investment, lamb=100)
# Compute correlation of cyclical components
correlation = np.corrcoef(cycle_c, cycle_i)[0, 1]
print(f"Business cycle correlation: {correlation:.4f}")
Ensure these packages are installed:
pip install statsmodels pandas numpy
The HP filter is in statsmodels.tsa.filters.hp_filter.