MyNixOS website logo
Description

Modified Hodrick-Prescott Filter with Optimal Smoothing Parameter Selection.

High-performance implementation of the Modified Hodrick-Prescott (HP) Filter for decomposing macroeconomic time series into trend and cyclical components. Based on the methodology of Choudhary, Hanif and Iqbal (2014) <doi:10.1080/00036846.2014.894631> "On smoothing macroeconomic time series using the modified HP filter", which uses generalized cross-validation (GCV) to automatically select the optimal smoothing parameter lambda, following McDermott (1997) "An automatic method for choosing the smoothing parameter in the HP filter" (as described in Coe and McDermott (1997) <doi:10.2307/3867497>). Unlike the standard HP filter that uses fixed lambda values (1600 for quarterly, 100 for annual data), this package estimates series-specific lambda values that minimize the GCV criterion. Implements efficient C++ routines via 'RcppArmadillo' for fast computation, supports batch processing of multiple series, and provides comprehensive visualization tools using 'ggplot2'. Particularly useful for cross-country macroeconomic comparisons, business cycle analysis, and when the appropriate smoothing parameter is uncertain.

mhpfilter: Modified Hodrick-Prescott Filter

R-CMD-check CRAN status CRAN downloads Monthly downloads Lifecycle: stable License: MIT DOI

Overview

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

Key Features

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

Methodology

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

Why Use Modified HP Filter?

The standard HP filter's fixed λ values were calibrated for U.S. GDP in the 1990s. However, optimal λ varies substantially across:

  • Countries: Emerging markets vs developed economies
  • Variables: Investment is more volatile than consumption
  • Time Periods: Pre/post Great Moderation, crisis periods

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

Installation

From CRAN (recommended when available)

install.packages("mhpfilter")

From GitHub (development version)

# Install devtools if needed
install.packages("devtools")

# Install mhpfilter
devtools::install_github("myaseen208/mhpfilter")

From Source

install.packages("mhpfilter_0.1.0.tar.gz", repos = NULL, type = "source")

Quick Start

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))

Documentation

Citation

@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},
}

Authors

Muhammad Yaseen (Clemson University)
Javed Iqbal (State Bank of Pakistan)
M. Nadim Hanif (State Bank of Pakistan)

License

MIT © Muhammad Yaseen.

Metadata

Version

0.1.0

License

Unknown

Platforms (78)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows