# Ljung-Box

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

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

## Signature

```ts
ljungBox(values, lags, modelDf, alpha)
```

Tests whether a group of autocorrelations is jointly zero. Applied to model residuals it answers the question that matters after fitting: is there structure left that the model failed to capture?

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `values` | `number[]` | yes | Observation series in chronological order, oldest first. |
| `lags` | `number` | yes | Number of lags tested jointly. · min: 1, integer: true |
| `modelDf` | `number` | yes | Parameters estimated by the model whose residuals these are. Omitting it inflates the degrees of freedom and makes a bad model look adequate. · min: 0, integer: true |
| `alpha` | `number` | yes | Significance level. · min: 0 |

## Returns

`{ method, lags, model_df, degrees_of_freedom, terms, statistic, p_value, decision, … }`

The statistic and p-value with the degrees-of-freedom adjustment shown.

## Errors

- When modelDf is not less than lags — throws

## Complexity

Time `O(n × lags)`, space `O(lags)`.

## 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.1869847, -1.93610944, 1.06953965, 0.16494849, -0.7467157]
```

Showing 6 of 96 elements.

`lags`:

```json
12
```

`modelDf`:

```json
0
```

`alpha`:

```json
0.05
```

### Call

```ts
ljungBox(values, lags, modelDf, alpha)
```

### Returns

object with 13 fields: method, variant, nobs, lags, model_df, degrees_of_freedom, terms, statistic, …

```json
{
  "method": "ljung_box",
  "variant": "demeaned-residual-portmanteau",
  "nobs": 96,
  "lags": 12,
  "model_df": 0,
  "degrees_of_freedom": 12,
  "terms": [
    {
      "lag": 1,
      "acf": -0.0475050120854155,
      "term": 0.00002375501234984708
    },
    {
      "lag": 2,
      "acf": -0.01074438668394694,
      "term": 0.0000012281047363210248
    },
    {
      "lag": 3,
      "acf": 0.10423989737175164,
      "term": 0.00011683823875347651
    }
  ],
  "statistic": 11.395763774723312,
  "p_value": 0.49534027421087556,
  "alpha": 0.05,
  "reject_null": false,
  "state": "fail-to-reject-residual-whiteness",
  "reason": "p-value-not-below-alpha"
}
```

## Other exports

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