fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Hodrick-Prescott Filter

Install and import#

bash
npm install fintech-algorithms
ts
import { 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#

NameTypeNotes
valuesnumber[]Observation series, chronological.
smoothingnumberThe λ 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#

values
[
  100.0901805654,
  101.3367333472,
  102.4083163971,
  102.9128416721,
  103.198587938,
  102.4311805579
]

Showing 6 of 192 elements.

smoothing
1600

Call#

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#

Hodrick-Prescott Filter — decomposition map
Hodrick-Prescott Filter — endpoint risk
Hodrick-Prescott Filter — family handoff
Hodrick-Prescott Filter — parameter effect
Hodrick-Prescott Filter — scenario comparison

Calculation flow#

Hodrick-Prescott Filter — Audit Flow
flowchart LR
    A["Finite evenly sampled values"] --> B["Freeze transformation and profile"]
    B --> C["Solve penalized trend system"]
    C --> D["Component or power output"]
    D --> E["Reconstruction / accounting check"]
    E --> F["Endpoint and revision audit"]
    F --> G["Profile sensitivity"]
    G --> H["White-noise control"]
    H --> I{"Decision use justified?"}
    I -->|No| J["Keep as descriptive diagnostic"]
    I -->|Yes| K["Rebuild causally and test out of sample"]

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.

Read the article →

References#

  • Evidence table
  • Source records
  • Evidence policy
  • Version notes

The rest of the Decomposition and Cycles family#