Skill站外收录 @skilllib/skill-2117939930272410149
timeseries-detrending
支持宏观经济时间序列的去趋势分析,通过赫迪克-普雷斯科特滤波分离长期趋势与周期波动,结合对数变换处理增长型变量,并提供频率适配的平滑参数选择方法,用于业务周期相关性研究和波动性比较。
商业与营销 AI 技能
Skills.MD
# 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`.
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「timeseries-detrending」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次