mhpfilter

R Package
mhpfilter: Modified Hodrick-Prescott Filter with Optimal Smoothing Parameter Selection
Authors

Muhammad Yaseen

Javed Iqbal

M. Nadim Hanif

Published

February 13, 2026

R Package Code Docs

CRAN version Downloads

mhpfilter: Modified Hodrick-Prescott Filter with Optimal Smoothing Parameter Selection [Website]. Provides a high-performance implementation for decomposing macroeconomic time series into trend and cyclical components. Uses generalized cross-validation (GCV) to automatically select the optimal smoothing parameter λ, addressing a key limitation of the standard Hodrick-Prescott (1997) filter. The methodology is based on Iqbal, J. and Hanif, M. N. (2023). Implemented with Rcpp and RcppArmadillo for computational efficiency.

Installation

# From CRAN
install.packages("mhpfilter")

# Development version from GitHub
# install.packages("pak")
pak::pak("myaseen208/mhpfilter")

Description

The standard Hodrick-Prescott (HP) filter requires the user to specify the smoothing parameter λ (lambda), typically set to 1600 for quarterly data, 100 for annual data, and 14400 for monthly data. This ad hoc choice can materially affect the decomposition.

mhpfilter solves this by automatically selecting the optimal λ via Generalized Cross-Validation (GCV), making the decomposition data-driven and reproducible. It uses RcppArmadillo for fast matrix operations, making it suitable for long macroeconomic time series.

Key Functions

Function Description
mhp_filter() Apply the Modified HP filter to a time series; returns trend and cycle components with optimal λ
gcv_lambda() Compute the GCV criterion for a grid of λ values
plot.mhpfilter() Plot trend and cycle decomposition

Example Usage

library(mhpfilter)
library(data.table)

# Simulate quarterly GDP data
set.seed(42)
n   <- 100
gdp <- cumsum(rnorm(n, mean = 0.005, sd = 0.01)) +
       sin(seq(0, 4 * pi, length.out = n)) * 0.02

# Apply Modified HP filter (lambda selected via GCV automatically)
result <- mhp_filter(gdp, freq = "quarterly")

# Inspect results
print(result)
#> Modified HP Filter Results
#> Optimal lambda (GCV): 1487.3
#> Series length      : 100

# Extract components
trend <- result$trend
cycle <- result$cycle

# Plot decomposition
plot(result, main = "GDP: Trend and Cyclical Components")

References

  • Hodrick, R. J., & Prescott, E. C. (1997). Postwar U.S. Business Cycles: An Empirical Investigation. Journal of Money, Credit and Banking, 29(1), 1–16. doi:10.2307/2953682
  • Iqbal, J., & Hanif, M. N. (2023). Modified Hodrick-Prescott filter with optimal smoothing parameter selection via generalized cross-validation.
Visitor analytics: privacy-friendly GoatCounter.