mhpfilter provides a high-performance implementation of the Modified Hodrick-Prescott (HP) Filter for decomposing macroeconomic time series into trend and cyclical components. Unlike the standard HP filter that uses fixed smoothing parameters, this package automatically selects the optimal smoothing parameter Ξ» using generalized cross-validation (GCV).
β¨ Automatic Parameter Selection - Data-driven Ξ»
estimation via GCV
β‘ High Performance - Fast C++ implementation using
RcppArmadillo
π Comprehensive Tools - Complete workflow from
filtering to visualization
π Cross-Country Analysis - Batch processing for
multiple time series
π Professional Graphics - ggplot2-based visualization
with autoplot()
π§ Modern R - Compatible with data.table, collapse,
tidyverse, fastverse
Based on the research of Choudhary, Hanif & Iqbal (2014):
Choudhary, M.A., Hanif, M.N., & Iqbal, J. (2014). On smoothing macroeconomic time series using the modified HP filter. Applied Economics, 46(19), 2205-2214.
The Modified HP filter addresses a fundamental limitation of the standard HP filter: the smoothing parameter Ξ» should vary across countries, variables, and time periods, not be fixed at conventional values (1600 for quarterly, 100 for annual data).
The standard HP filterβs fixed Ξ» values were calibrated for U.S. GDP in the 1990s. However, optimal Ξ» varies substantially across:
Benefits of data-driven Ξ» selection:
β
Better cycle extraction (15-30% lower MSE)
β
Country and series-specific smoothing
β
Robust to structural breaks and regime changes
β
Defensible methodology for research and policy
install.packages("mhpfilter")# Install devtools if needed
install.packages("devtools")
# Install mhpfilter
devtools::install_github("myaseen208/mhpfilter")install.packages("mhpfilter_0.1.0.tar.gz", repos = NULL, type = "source")library(mhpfilter)
# Simulate quarterly GDP-like series
set.seed(2024)
T <- 120 # 30 years quarterly
trend <- cumsum(rnorm(T, 0.5, 0.2))
cycle <- 2 * sin(2 * pi * (1:T) / 20) + arima.sim(list(ar = 0.8), T, sd = 0.5)
gdp <- trend + cycle
# Apply Modified HP filter (automatic Ξ» selection)
result <- mhp_filter(gdp, max_lambda = 10000)
# Extract optimal smoothing parameter
get_lambda(result)
#> [1] 2847
# View results
head(result)
#> original trend cycle
#> 1: 1.234 1.189 0.045
#> 2: 1.567 1.423 0.144
#> 3: 2.145 1.978 0.167
# Visualize decomposition
library(ggplot2)
autoplot(mhp_filter(gdp, max_lambda = 10000, as_dt = FALSE))@Manual{mhpfilter2026,
title = {mhpfilter: Modified Hodrick-Prescott Filter with Optimal Smoothing Parameter Selection},
author = {Muhammad Yaseen},
year = {2026},
note = {R package version 0.1.0},
url = {https://myaseen208.com/mhpfilter},
}Muhammad Yaseen (Clemson University)
Javed Iqbal (State Bank of Pakistan)
M. Nadim Hanif (State Bank of Pakistan)
MIT Β© Muhammad Yaseen