Hodrick–Prescott Filter in MATLAB
A standalone MATLAB implementation of the Hodrick–Prescott
(HP) filter for decomposing economic time series into
long-run trend and cyclical components.
The implementation uses sparse matrix operations and
does not require the MATLAB Econometrics Toolbox.
Overview
The Hodrick–Prescott filter is widely used in macroeconomics
to separate long-run movements in economic variables from
short-run fluctuations associated with business cycles.
Potential applications include:
- Gross domestic product (GDP)
- Household consumption
- Investment
- Employment
- Government expenditure
- Industrial production
- Business cycle analysis
Methodology
The HP filter decomposes an observed time series into
a trend component and a cyclical component:
$$
y_t = \tau_t + c_t
$$
where:
- $y_t$ is the observed time series.
- $\tau_t$ is the estimated trend.
- $c_t$ is the cyclical component.
The trend is estimated by solving the following
penalized least-squares problem:
$$
\min_{\tau}
\left[
\sum_{t=1}^{T}(y_t-\tau_t)^2
+
\lambda\sum_{t=2}^{T-1}
(\tau_{t+1}-2\tau_t+\tau_{t-1})^2
\right]
$$
The smoothing parameter $\lambda$ determines the degree
of smoothness imposed on the estimated trend.
Repository Structure
| File |
Description |
hp_filter.m |
Main HP filter function |
example_hp_filter.m |
Reproducible demonstration |
hp_filter_demo.png |
Visualization of the decomposition |
README.md |
Project documentation |
LICENSE |
MIT License |
Requirements
- MATLAB
- No additional toolbox required for the core function
- MATLAB R2020a or later for the demonstration script
Installation
Download or clone this repository and add its folder
to your MATLAB path.
Alternatively, place hp_filter.m in your current
MATLAB working directory.
Usage
The function can be called using:
[trend, cycle] = hp_filter(y, lambda);
Example
For quarterly economic data:
lambda = 1600;
[trend, cycle] = hp_filter(y, lambda);
| Parameter |
Description |
y |
Numeric time-series vector |
lambda |
Nonnegative smoothing parameter |
Outputs
| Output |
Description |
trend |
Estimated long-run trend |
cycle |
Deviation of the observed series from its trend |
Smoothing Parameters
Common frequency-adjusted smoothing parameters are:
| Data Frequency |
Lambda |
| Annual |
6.25 |
| Quarterly |
1600 |
| Monthly |
129600 |
The appropriate smoothing parameter depends on the
frequency of the data and the research objective.
Demonstration
The demonstration uses 160 simulated quarterly
observations consisting of a deterministic growth
trend, cyclical fluctuations, and random disturbances.
To reproduce the results, run:
example_hp_filter
The script applies the HP filter with a smoothing
parameter of 1600 and generates the following figure.

The upper panel compares the original time series
with its estimated long-run trend. The lower panel
shows the cyclical component.
Implementation Features
- Sparse matrix computation
- Support for row and column vectors
- Input validation
- Adjustable smoothing parameter
- Reproducible demonstration
- No Econometrics Toolbox dependency
Limitations
The HP filter is a statistical decomposition method.
Its estimated trend should not automatically be
interpreted as potential output or a structural
economic relationship.
The filter is sensitive to the choice of smoothing
parameter and may produce endpoint distortions.
Missing observations must be addressed before
applying the function.
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.
Ravn, M. O., & Uhlig, H. (2002).
On adjusting the Hodrick–Prescott filter for the
frequency of observations.
Review of Economics and Statistics, 84(2), 371–376.
License
This project is distributed under the MIT License.
See the LICENSE file for details.
评论