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

Lags, Leads, and Temporal Dependence

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#

lagsLeadsAndTemporalDependence(input)

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

Parameters#

NameTypeNotes
inputD00InputReads 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#

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

Input#

input
{
  "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#

lagsLeadsAndTemporalDependence(input)

Returns#

object with 2 fields: laggedPairs, leadValues

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

Diagrams#

Lags, Leads, and Temporal Dependence — article hero
Lags, Leads, and Temporal Dependence — calculation ledger
Lags, Leads, and Temporal Dependence — concept anatomy
Lags, Leads, and Temporal Dependence — failure boundary
Lags, Leads, and Temporal Dependence — method map
Lags, Leads, and Temporal Dependence — scenario contrast

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#

The rest of the Financial Time-Series Foundations family#