Sample Covariance
Install and import#
npm install fintech-algorithmsimport { calculate } from "fintech-algorithms/volatility-and-covariance/covariance-estimation/sample-covariance";Signature#
calculate(data)Add complete rows and audit sample means, denominator and cells. Supplied-parameter educational reference; no fitted performance claim.
Parameters#
| Name | Type | Notes |
|---|---|---|
data | TopicInput | See data-contract/CONTRACT.md. |
Returns#
TopicResult
Structured result with readiness, values, parameters, and diagnostics.
Warm-up#
The first depends on window or model order positions are null prefix until minimum history exists.
Errors#
- When required data is missing, non-finite, malformed, or out of range — raises ContractError / Error
Complexity: time O(np²),
space Full diagnostic trace retained for teaching; see implementation for observation/window/matrix dimensions.
Worked example#
executed 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#
{
"returns": [
[1, 1.3660254037844386],
[1, -0.3660254037844386],
[-1, 0.3660254037844386]
],
"parameters": {}
}Call#
calculate(data)Returns#
object with 8 fields: topic_id, title, parameters, matrix, latest, ready, ready_at, diagnostics
{
"topic_id": "D10-F04-A01",
"title": "Sample Covariance",
"parameters": {},
"matrix": [
[1.3333333333333333, 0.6666666666666666],
[0.6666666666666666, 1.3333333333333333]
],
"latest": [
[1.3333333333333333, 0.6666666666666666],
[0.6666666666666666, 1.3333333333333333]
],
"ready": true,
"ready_at": 0,
"diagnostics": {
"observations": 4,
"assets": 2,
"means": [0, 0],
"causal": true,
"ml_covariance": [
[1, 0.5],
[0.5, 1]
],
"denominator": 3
}
}Diagrams#
Calculation flow#
Sample Covariance — calculation-flow
flowchart TD
N0["Align complete asset vectors"]
N1["Estimate endpoint column means"]
N2["Sum centered outer products"]
N3["Divide by n−1; singular PSD allowed"]
N0 --> N1 --> N2 --> N3
Sample Covariance — decision-boundary
flowchart TD
A["Supplied observations and parameters"] --> B{"Contract valid?"}
B -->|No| E["Reject with explicit error"]
B -->|Yes| C{"Required history available?"}
C -->|No| W["Withhold; never insert zero"]
C -->|Yes| D["Sample Covariance calculation"]
D --> F["Inspect diagnostics and stated limits"]
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#
- Scope of evidence