Hodrick-Prescott Filter
Install and import
npm install fintech-algorithmsimport { hpFilter } from "fintech-algorithms/statistical-time-series/decomposition-and-cycles/hodrick-prescott-filter";Signature
hpFilter(values, smoothing)Separates trend from cycle by penalising trend curvature. Ubiquitous in macro and heavily criticised: it produces spurious cycles at the ends of the sample, so the most recent values — the ones you care about — are the least reliable.
Parameters
| Name | Type | Notes |
|---|---|---|
values | number[] | Observation series, chronological. |
smoothing | number | The λ penalty. Convention is 1600 for quarterly, 129600 for monthly — the choice largely determines the answer. min: 0 |
Returns
{ trend, cycle }
Trend and cycle components summing to the input.
Errors
- When smoothing is negative — throws
Complexity: time O(n),
space O(n).
Worked example
executed Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.
Input
[
100.0901805654,
101.3367333472,
102.4083163971,
102.9128416721,
103.198587938,
102.4311805579
]Showing 6 of 192 elements.
1600Call
hpFilter(values, smoothing)Returns
object with 6 fields: trend, cycle, lambda, iterations, normal_equation_residual, reconstruction_max_error
{
"trend": [
101.84532271785126,
101.7763438481692,
101.70626801464202,
101.63372349686104,
101.55777735465698,
101.47829609671997
],
"cycle": [
-1.7551421524512563,
-0.4396105009692093,
0.7020483824579742,
1.2791181752389633,
1.6408105833430255,
0.9528844611800338
],
"lambda": 1600,
"iterations": 637,
"normal_equation_residual": 5.744738005865808e-10,
"reconstruction_max_error": 0
}Other exports
This module also exports
stlDecompose, bkFilter, cfFilter, fftPeriodogram, haarWavelet, runTopic. Every module additionally exports run as an alias of its
primary function, and a meta object carrying its catalog id, domain, family,
shape and article URL.
Diagrams
How it works
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References
- Evidence table
- Source records
- Evidence policy
- Version notes