Correlation, Causation, Confounding, and Spurious Relationships
Install and import#
npm install fintech-algorithmsimport { correlationCausationConfoundingAndSpuriousRelationships } from "fintech-algorithms/foundations/dependence-regression-and-model-foundations/correlation-causation-confounding-and-spurious-relationships";Signature#
correlationCausationConfoundingAndSpuriousRelationships(input)Contrasts the pooled correlation of input.x and input.y with the correlation recomputed inside each label in input.groups, which is where a confounded or reversed relationship shows itself.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | D00Input | One record holding the aligned series x and y plus groups, a parallel list of group labels, one per observation.x: non-empty list of finite numbers, at least two, not all identical · y: finite numbers, same length as x |
Returns#
D00Output
An object with rawCorrelation (over all observations), withinGroupCorrelations (a map keyed by group label, whose value is null for any group with fewer than three members) and causalClaimSupported, which is always false.
Errors#
- When x or y is missing, empty, or contains a non-finite number — throws RangeError
- When x and y have different lengths, or fewer than two observations — the least-squares fit is computed for every topic in the family before the topic branch is taken — throws RangeError
- When x is constant, which leaves the regression slope undefined — throws RangeError
- When y is constant, or a group with three or more members has a constant x or y within it — throws RangeError
Complexity: time O(n*g),
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#
{
"x": [1, 2, 3, 4, 5, 6],
"y": [2, 3, 5, 4, 6, 7],
"groups": ["A", "A", "A", "B", "B", "B"],
"predictX": 7,
"otherPredictor": [2, 4, 5, 8, 9, 13]
}Call#
correlationCausationConfoundingAndSpuriousRelationships(input)Returns#
object with 2 fields: rawCorrelation, withinGroupCorrelations
{
"rawCorrelation": 0.9428571428571428,
"withinGroupCorrelations": {
"A": 0.9819805060619656,
"B": 0.9819805060619659
}
}Diagrams#
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#
- Linear Least Squares Regression — NIST/SEMATECH e-Handbook
- Correlation — NIST/SEMATECH e-Handbook
- Historical-example decision