# Hodrick-Prescott Filter

`D09-F05-A02` · Statistical Time Series → Decomposition and Cycles · archetype `record-transform` · difficulty 3/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/statistical-time-series/decomposition-and-cycles/hodrick-prescott-filter/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { hpFilter } from "fintech-algorithms/statistical-time-series/decomposition-and-cycles/hodrick-prescott-filter";
```

## Signature

```ts
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 | Required | Notes |
| --- | --- | --- | --- |
| `values` | `number[]` | yes | Observation series, chronological. |
| `smoothing` | `number` | yes | 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

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`:

```json
[
  100.0901805654,
  101.3367333472,
  102.4083163971,
  102.9128416721,
  103.198587938,
  102.4311805579
]
```

Showing 6 of 192 elements.

`smoothing`:

```json
1600
```

### Call

```ts
hpFilter(values, smoothing)
```

### Returns

object with 6 fields: trend, cycle, lambda, iterations, normal_equation_residual, reconstruction_max_error

```json
{
  "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

`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.

## Verification and provenance

Tier: **contract**.

The module loads, the entry point is callable and its declared signature matches the compiled code. The example below is real captured output, but no independently published figure asserts the numbers.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/statistical-time-series/decomposition-and-cycles/hodrick-prescott-filter/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/statistical-time-series/decomposition-and-cycles/hodrick-prescott-filter/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/statistical-time-series/llms.txt
