Autocovariance and Autocorrelation
Install and import#
npm install fintech-algorithmsimport { autocovarianceAndAutocorrelation } from "fintech-algorithms/foundations/financial-time-series-foundations/autocovariance-and-autocorrelation";Signature#
autocovarianceAndAutocorrelation(input)Measures how far a series moves with its own past at a given lag, using the population convention that divides by the full observation count.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | D00Input | Reads values, a non-empty list of finite numbers, timestamps of the same length, and lag, the whole number of positions to look back. |
Returns#
D00Output
autocovariance is the mean product of deviations lag apart, autocorrelation is that figure divided by the population variance, and lag is echoed back.
Errors#
- When
valuesis absent, empty, or holds a non-finite number — throws RangeError - When
timestampsandvalueshave different lengths — throws RangeError - When the series holds fewer than two observations — throws RangeError
- When
lagis not an integer between one and the observation count minus one — throws RangeError - When the series is constant, so its population variance is zero — throws RangeError
Complexity: time O(n^2),
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#
{
"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#
autocovarianceAndAutocorrelation(input)Returns#
object with 2 fields: autocovariance, autocorrelation
{
"autocovariance": 2,
"autocorrelation": 0.42857142857142855
}Diagrams#
Calculation flow#
Reasoning flow — D00-F10-A07
flowchart LR
A["Synthetic input + metadata"] --> B{"Contract valid?"}
B -->|No| C["Reject or route with reason"]
B -->|Yes| D["Apply Autocovariance and Autocorrelation"]
D --> E["Formula: γk=(1/n)Σₜ₌k(xₜ−x̄)(xₜ₋ₖ−x̄); ρk=γk/γ0"]
E --> F["Verified fixture output"]
F --> G{"Interpretation within boundary?"}
G -->|Yes| H["Report value + convention + audit"]
G -->|No| I["Add companion view or narrower claim"]
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.
References#
- Time Series Plot — NIST/SEMATECH e-Handbook
- Common Pitfalls and Recommended Practices — scikit-learn
- Historical-example decision