home / skills / benchflow-ai / skillsbench / timeseries-detrending

This skill helps analysts detrend macroeconomic time series using HP filter and log transformations to isolate trend and cyclical components.

npx playbooks add skill benchflow-ai/skillsbench --skill timeseries-detrending

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.5 KB
---
name: timeseries-detrending
description: Tools and techniques for detrending time series data in macroeconomic analysis. Use when working with economic time series that need to be decomposed into trend and cyclical components. Covers HP filter, log transformations for growth series, and correlation analysis of business cycles.
---

# Time Series Detrending for Macroeconomic Analysis

This skill provides guidance on decomposing economic time series into trend and cyclical components, a fundamental technique in business cycle analysis.

## Overview

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:
- Analyzing business cycle correlations
- Comparing volatility across variables
- Identifying leading/lagging indicators

## The Hodrick-Prescott (HP) Filter

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.

### Mathematical Foundation

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:
- First term: Minimizes deviation of data from trend
- Second term: Penalizes changes in the trend's growth rate
- $\lambda$: Smoothing parameter controlling the trade-off

### Choosing Lambda (λ)

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

### Python Implementation

```python
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).

## Log Transformation for Growth Series

### Why Use Logs?

For most macroeconomic aggregates (GDP, consumption, investment), you should apply the natural logarithm **before** filtering:

1. **Multiplicative to Additive**: Converts percentage changes to log differences
2. **Stabilizes Variance**: Growth rates become comparable across time
3. **Economic Interpretation**: Cyclical component represents percentage deviations from trend
4. **Standard Practice**: Required for business cycle statistics that compare volatilities

```python
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
```

### When NOT to Use Logs

- Series that can be negative (net exports, current account)
- Series already expressed as rates or percentages
- Series with zeros

## Complete Workflow for Detrending

### Step-by-Step Process

1. **Load and clean data**: Handle missing values, ensure proper time ordering
2. **Convert to real terms**: Deflate nominal values using appropriate price index
3. **Apply log transformation**: For positive level variables
4. **Apply HP filter**: Use appropriate λ for data frequency
5. **Analyze cyclical component**: Compute correlations, volatilities, etc.

### Example: Business Cycle Correlation

```python
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}")
```

## Dependencies

Ensure these packages are installed:
```bash
pip install statsmodels pandas numpy
```

The HP filter is in `statsmodels.tsa.filters.hp_filter`.

Overview

This skill provides practical tools and guidance for detrending macroeconomic time series to separate long-run trends from cyclical fluctuations. It focuses on the Hodrick–Prescott (HP) filter, appropriate smoothing choices, and log transformations for growth-series interpretation. Use it to prepare data for business cycle analysis, volatility comparisons, and correlation measures.

How this skill works

The skill explains how to transform level series with natural logs, apply the HP filter to decompose series into trend and cycle, and choose the smoothing parameter λ by data frequency (annual, quarterly, monthly). It also covers when logs are inappropriate and how to compute cyclical correlations and volatilities for business cycle analysis. Code snippets illustrate applying hpfilter and computing correlations on real (inflation-adjusted) series.

When to use it

  • Preparing GDP, consumption, or investment series for business cycle analysis
  • Comparing cyclical volatility across macro aggregates
  • Estimating correlations between cyclical components (leading/lagging relationships)
  • Converting multiplicative growth behavior into additive deviations
  • When you need a simple trend/cycle decomposition with clear frequency-based smoothing rules

Best practices

  • Always deflate nominal series to real terms before detrending
  • Apply the natural log to positive level variables before filtering so cycles are percentage deviations
  • Choose λ by frequency: annual≈100, quarterly≈1600, monthly≈14400
  • Use the hpfilter implementation parameter name correctly (lamb in statsmodels)
  • Avoid log transforms for series that can be zero or negative; consider alternative detrending methods in those cases

Example use cases

  • Estimate business cycle correlation between real consumption and investment using log-transformed series and HP filter
  • Compare volatility of cyclical components across countries or sectors after appropriate detrending
  • Identify leading indicators by cross-correlating detrended series
  • Produce percentage-deviation plots for policy briefings by applying logs then HP filtering
  • Clean and prepare series for input to business-cycle regressions or VAR models

FAQ

What λ should I use for quarterly GDP?

Use λ≈1600 for quarterly data; this is the canonical Hodrick–Prescott recommendation for quarterly frequency.

Should I log every series before filtering?

Log positive level macro aggregates (GDP, consumption, investment). Do not log series that can be negative, contain zeros, or are already rates; use levels or alternative detrending instead.