| hp_filter | R Documentation |
Decomposes a time series into trend and cycle components by solving the HP penalized least-squares problem using a sparse Cholesky factorization. This avoids the dense O(n^3) inversion used by other implementations and scales linearly in the number of observations.
hp_filter(x, lambda = NULL, freq = NULL, boot_iter = 0, block_size = "auto")
x |
Numeric vector, |
lambda |
Smoothing parameter.
If |
freq |
Numeric frequency override (1 = annual, 4 = quarterly,
12 = monthly). Used only when |
boot_iter |
Non-negative integer.
Number of block-bootstrap iterations for uncertainty quantification
(default |
block_size |
Positive integer or |
The HP filter minimises
\sum (y_t - \tau_t)^2 + \lambda \sum (\Delta^2 \tau_t)^2
which admits the closed-form solution
(I + \lambda D'D)\,\tau = y
where D is the second-difference operator.
The implementation builds D as a banded sparse matrix
(Matrix::bandSparse()) and solves the symmetric positive-definite system
with a sparse Cholesky decomposition (Matrix::solve()).
When lambda is not supplied the Ravn-Uhlig (2002) rule is applied:
lambda = 6.25 * freq^4, yielding 6.25 (annual), 1600 (quarterly), and
129 600 (monthly).
A list of class c("macrofilter", "list") with trend, cycle,
data, and meta. When boot_iter > 0 it also carries trend_lower
and trend_upper (95% normal-approximation bootstrap band).
Hodrick, R.J. and Prescott, E.C. (1997). Postwar U.S. Business Cycles: An Empirical Investigation. Journal of Money, Credit and Banking, 29(1), 1–16.
Ravn, M.O. and Uhlig, H. (2002). On Adjusting the Hodrick-Prescott Filter for the Frequency of Observations. Review of Economics and Statistics, 84(2), 371–376.
# Quarterly GDP-like series
y <- ts(cumsum(rnorm(200)), start = c(2000, 1), frequency = 4)
result <- hp_filter(y)
print(result)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.