Implements the Bayesian Hodrick-Prescott filter embedded in an unobserved components model, as developed by Grant and Chan (2017). This approach provides principled uncertainty quantification for the extracted trend through Markov Chain Monte Carlo sampling.
filter_hpgc(
y,
prior_config = "weak",
n_chains = 4,
iterations = 20000,
burnin = 5000,
verbose = FALSE
)A list of class "signaly_hpgc" containing:
trend: Numeric vector of posterior mean trend
trend_lower: Numeric vector of 2.5 percent posterior quantile
trend_upper: Numeric vector of 97.5 percent posterior quantile
cycle: Numeric vector of posterior mean cycle component
cycle_lower: Numeric vector of 2.5 percent posterior quantile
cycle_upper: Numeric vector of 97.5 percent posterior quantile
draws: List of posterior draws for all parameters
diagnostics: Convergence diagnostics including R-hat and ESS
dic: Deviance Information Criterion
settings: Parameters used in the analysis
Numeric vector of the time series. Will be internally scaled for numerical stability.
Character string or list specifying prior configuration. Options: "weak" (default), "informative", or "empirical". Alternatively, a named list with prior parameters (see Details).
Integer number of MCMC chains to run. Default is 4.
Integer total number of MCMC iterations per chain. Default is 20000.
Integer number of burn-in iterations to discard. Default is 5000.
Logical indicating whether to print progress messages.
Diffuse priors allowing data to dominate. Good for initial exploration.
Tighter priors based on typical macroeconomic dynamics. Suitable when strong smoothness is desired.
Priors calibrated from data moments. Balances flexibility with data-driven regularization.
Custom priors can be specified as a list with elements:
phi_mu: Mean of phi prior (2-vector)
phi_v_i: Precision matrix for phi prior (2x2)
gamma_mu: Mean of gamma (initial trend growth) prior
gamma_v_i: Precision matrix for gamma prior
s_tau: Upper bound for uniform prior on \(\sigma_\tau^2\)
s_c_shape: Shape parameter for inverse-gamma prior on
\(\sigma_c^2\)
s_c_rate: Rate parameter for inverse-gamma prior on
\(\sigma_c^2\)
The standard HP filter solves: $$\min_\tau \sum_t (y_t - \tau_t)^2 + \lambda \sum_t (\Delta^2 \tau_t)^2$$
The Grant-Chan approach embeds this within a probabilistic model where \(\lambda = \sigma_c^2 / \sigma_\tau^2\), allowing this ratio to be estimated from data with full uncertainty quantification.
The Grant-Chan model decomposes the observed series \(y_t\) as: $$y_t = \tau_t + c_t$$
where \(\tau_t\) is the trend component and \(c_t\) is the cyclical component.
Trend Model (Second-Order Markov Process): $$\Delta^2 \tau_t = u_t^\tau, \quad u_t^\tau \sim N(0, \sigma_\tau^2)$$
This implies the trend growth rate follows a random walk, allowing for time-varying trend growth.
Cycle Model (Stationary AR(2)): $$c_t = \phi_1 c_{t-1} + \phi_2 c_{t-2} + u_t^c, \quad u_t^c \sim N(0, \sigma_c^2)$$
with stationarity constraints on \(\phi\).
Grant, A. L., & Chan, J. C. C. (2017). Reconciling output gaps: Unobserved components model and Hodrick-Prescott filter. Journal of Economic Dynamics and Control, 75, 114-121. tools:::Rd_expr_doi("10.1016/j.jedc.2016.12.007")
Chan, J., Koop, G., Poirier, D. J., & Tobias, J. L. (2019). Bayesian Econometric Methods (2nd ed.). Cambridge University Press.
filter_wavelet, filter_emd
# \donttest{
set.seed(123)
y <- cumsum(rnorm(100)) + sin(seq(0, 4*pi, length.out = 100))
result <- filter_hpgc(y, prior_config = "weak", n_chains = 2,
iterations = 5000, burnin = 1000)
plot(y, type = "l", col = "gray")
lines(result$trend, col = "red", lwd = 2)
# }
Run the code above in your browser using DataLab