mhpfilter: Modified and Fully Modified Hodrick-Prescott Filters

CRAN release CRAN checks Downloads Monthly downloads r-universe R-CMD-check Development version Minimal R version Dependencies License Project Status: Active Last commit

Overview

mhpfilter splits macroeconomic time series into a trend and a cycle with the Hodrick-Prescott (HP) filter and two versions whose settings come from the data:

Around them:

Version 1.0.0 keeps every function, argument and result of version 0.1.0 and adds the fully modified HP filter. Every filter is one banded linear solve, so the search over 100000 values of the smoothing parameter for 200 observations takes 1.3 s instead of 6.4 s, and longer series gain more; the fully modified HP filter reproduces the published MATLAB code, and the end-point losses of the HP filter in Tables 3a and 3b of Hanif et al. (2017) are reproduced.

Installation

install.packages("mhpfilter")
# development version
# install.packages("remotes")
remotes::install_github("myaseen208/mhpfilter")

Quick start

library(mhpfilter)
data(us_macro)
gdp <- ts(data = us_macro$gdp, start = c(1959, 1), frequency = 4)
fm <-
  fmhp_filter(
      x               = gdp
    , max_lambda      = 3000
    , k_linear        = 15
    , k_quadratic     = NULL
    , alpha_linear    = seq(from = 0, to = 2000, by = 10)
    , alpha_quadratic = seq(from = 0, to = 2000, by = 5)
    , schemes         = c("linear", "quadratic")
    , form            = "rows"
    , lambda          = NULL
    , log_scale       = TRUE
    , as_dt           = FALSE
    )
fm
Fully modified HP Filter Result
==============================
Observations: 203 
Fully modified lambda: 1448  (modified HP lambda: 683 )
End-point weights: quadratic, alpha = 15 on 22 end values; loss = 0.6163
GCV: 3.336e-04 
Filtered on the log scale; cycle = series - trend in levels

Cycle Statistics:
  Mean: -2.603 
  SD:   120.4 
  AR1:  0.9522 
  Range: 900 

Sources: Hanif et al. (2017); Hanif (2017); Bloechl (2014); McDermott (1997);
Hodrick and Prescott (1997). Full references: mhp_cite(x).
fmhp_compare(
    x          = gdp
  , frequency  = "quarterly"
  , max_lambda = 3000
  , log_scale  = TRUE
  , end_share  = 0.1
  )
              method lambda   cycle_sd    cycle_mean       ar1 cycle_range
1:                HP   1600 0.01543904  4.942713e-12 0.8668207  0.08590516
2:       Modified HP    683 0.01333176 -1.634336e-13 0.8280266  0.07801575
3: Fully modified HP   1448 0.01618038 -4.145448e-05 0.8815215  0.08713978
            gcv      loss    sd_ends  sd_middle
1:           NA 1.7617778 0.01458541 0.01568213
2: 0.0002819926 1.7652928 0.01202396 0.01366805
3: 0.0003335623 0.7898641 0.01930994 0.01538061
plot(x = fm)

Learn more

Citation

Yaseen M (2026). _mhpfilter: Modified and Fully Modified
Hodrick-Prescott Filters_. R package version 1.0.0,
<https://CRAN.R-project.org/package=mhpfilter>.

BibTeX:

@Manual{mhpfilter,
  title = {{mhpfilter}: Modified and Fully Modified Hodrick-Prescott Filters},
  author = {Muhammad Yaseen},
  year = {2026},
  note = {R package version 1.0.0},
  url = {https://CRAN.R-project.org/package=mhpfilter},
}

Please also cite the methods you use: citation("mhpfilter") lists them with the package, and mhp_cite(x) gives the references of a single result.