correlation

Calculates the Pearson correlation coefficient for two same-length arrays.

import { Arith } from "@teakit/arith";

Arith.correlation(["1", "2", "3"], ["2", "4", "6"]).toString(); // "1"
Arith.correlation(["1", "2", "3"], ["6", "4", "2"]).toString(); // "-1"
Arith.correlation(["1", "2", "3"], ["1", "0", "1"]).toString(); // "0"

// Zero variance makes the coefficient undefined.
Arith.correlation(["1", "1", "1"], ["2", "3", "4"]).toString(); // "NaN"

API Reference

Signature

Arith.correlation(
  xValues: readonly ArithValue[],
  yValues: readonly ArithValue[],
): ArithInstance;

Parameters

ParameterTypeRequiredNotes
xValuesreadonly ArithValue[]YesNon-empty array of finite values.
yValuesreadonly ArithValue[]YesNon-empty array of finite values with the same length as xValues.

Returns

Returns the Pearson correlation coefficient, or NaN when either side has zero variance.

Throws

  • Throws if either input is not a non-empty array.
  • Throws if the arrays have different lengths.
  • Throws if any value is non-finite.

Agent Contract

FieldValue
Kindstatic method
Canonical namecorrelation
AliasesNone
Mutates receiverNo
ReturnsArithInstance
Accepts (string, base) overloadNo
Configuration dependenciesDECIMAL_PLACES, ROUNDING_MODE, STRICT
Related methodscovariance, std

Agent Notes

  • correlation uses Pearson correlation.
  • Expect NaN for zero-variance inputs.