# Lags, Leads, and Temporal Dependence

`D00-F10-A03` · Financial Mathematics, Statistics, and Data Foundations → Financial Time-Series Foundations · archetype `record-transform` · difficulty 1/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/foundations/financial-time-series-foundations/lags-leads-and-temporal-dependence/
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 { lagsLeadsAndTemporalDependence } from "fintech-algorithms/foundations/financial-time-series-foundations/lags-leads-and-temporal-dependence";
```

## Signature

```ts
lagsLeadsAndTemporalDependence(input)
```

Pairs every observation with the value `lag` positions behind it, and exposes the same series shifted forward as the lead.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `input` | `D00Input` | yes | Reads `values`, a non-empty list of finite numbers, `timestamps` of the same length, and `lag`, the whole number of positions to shift by. |

## Returns

`D00Output`

`laggedPairs` holds one object per shiftable position with `current` and `lagged`, `leadValues` is the series with its first `lag` entries dropped, and `lag` is echoed back.

## Errors

- When `values` is absent, empty, or holds a non-finite number — throws RangeError
- When `timestamps` and `values` have different lengths — throws RangeError
- When the series holds fewer than two observations — throws RangeError
- When `lag` is not an integer between one and the observation count minus one — throws RangeError

## Complexity

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

## Worked example

This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

### Input

`input`:

```json
{
  "timestamps": [
    "2025-01-01T00:00:00Z",
    "2025-01-02T00:00:00Z",
    "2025-01-03T00:00:00Z",
    "2025-01-04T00:00:00Z",
    "2025-01-05T00:00:00Z",
    "2025-01-06T00:00:00Z"
  ],
  "values": [100, 102, 101, 104, 106, 105],
  "lag": 1,
  "window": 3,
  "resampleSize": 2,
  "period": 3,
  "stationarityTolerance": 3,
  "alpha": 0.4,
  "splitIndex": 4
}
```

### Call

```ts
lagsLeadsAndTemporalDependence(input)
```

### Returns

object with 2 fields: laggedPairs, leadValues

```json
{
  "laggedPairs": [
    {
      "current": 102,
      "lagged": 100
    },
    {
      "current": 101,
      "lagged": 102
    },
    {
      "current": 104,
      "lagged": 101
    }
  ],
  "leadValues": [102, 101, 104, 106, 105]
}
```

## Verification and provenance

Tier: **verified** (via input-expected).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

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.0.
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/foundations/financial-time-series-foundations/lags-leads-and-temporal-dependence/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/foundations/financial-time-series-foundations/lags-leads-and-temporal-dependence/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/foundations/llms.txt
