# KPSS

`D09-F01-A04` · Statistical Time Series → Diagnostics · archetype `record-transform` · difficulty 3/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/statistical-time-series/diagnostics/kpss/
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 { kpss } from "fintech-algorithms/statistical-time-series/diagnostics/kpss";
```

## Signature

```ts
kpss(values, lags, criticalValue)
```

Tests stationarity with the null *reversed* relative to ADF: here the null is that the series is stationary. Running both is standard practice, because agreement is informative and disagreement tells you the sample cannot settle the question.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `values` | `number[]` | yes | Observation series in chronological order, oldest first. |
| `lags` | `number` | yes | Bandwidth for the long-run variance estimator. · min: 0, integer: true |
| `criticalValue` | `number` | yes | Critical value at the chosen significance level. |

## Returns

`{ method, variant, residual_mean, partial_sum_numerator, long_run_variance, statistic, decision, … }`

The statistic with the long-run variance behind it, which is where the bandwidth choice shows up.

## Errors

- When the sample is shorter than the bandwidth requires — throws

## Complexity

Time `O(n × lags)`, 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
[0.49366418, 1.54242291, -0.82556495, 0.47513288, 0.50704417, -0.3816439]
```

Showing 6 of 96 elements.

`lags`:

```json
5
```

`criticalValue`:

```json
0.463
```

### Call

```ts
kpss(values, lags, criticalValue)
```

### Returns

object with 13 fields: method, variant, nobs, lags, residual_mean, partial_sum_numerator, long_run_variance, covariance_terms, …

```json
{
  "method": "kpss",
  "variant": "level-stationary-fixed-bartlett-bandwidth",
  "nobs": 96,
  "lags": 5,
  "residual_mean": -2.0816681711721685e-17,
  "partial_sum_numerator": 0.2446756917455388,
  "long_run_variance": 4.82902222159472,
  "covariance_terms": [
    {
      "lag": 1,
      "weight": 0.8333333333333334,
      "cross_product": 102.29629606088473
    },
    {
      "lag": 2,
      "weight": 0.6666666666666667,
      "cross_product": 67.86612357881646
    },
    {
      "lag": 3,
      "weight": 0.5,
      "cross_product": 43.864068660314665
    }
  ],
  "statistic": 0.05066775022309546,
  "critical_value": 0.463,
  "reject_null": false,
  "state": "fail-to-reject-level-stationarity",
  "reason": "statistic-not-above-boundary"
}
```

## Other exports

`acf`, `pacf`, `adf`, `ljungBox`, `zivotAndrews`, `runDiagnostic`. 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/diagnostics/kpss/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/statistical-time-series/diagnostics/kpss/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
