covariance

Calculates covariance for two same-length arrays using Arith decimal arithmetic.

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

Arith.covariance(["1", "2", "3"], ["2", "4", "6"]).toString(); // "1.33333333333333333333"
Arith.covariance(["1", "2", "3"], ["2", "4", "6"], { sample: true }).toString(); // "2"
Arith.covariance(["1", "2", "3"], ["6", "4", "2"]).toString(); // "-1.33333333333333333333"

API Reference

Signature

Arith.covariance(
  xValues: readonly ArithValue[],
  yValues: readonly ArithValue[],
  options?: ArithSampleOptions,
): 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.
options.samplebooleanNofalse or omitted uses population covariance. true uses sample covariance.

Returns

Returns sum((x - meanX) * (y - meanY)) / denominator.

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.
  • Throws if sample: true is used with fewer than two pairs.
  • Throws if options.sample is not boolean when provided.

Agent Contract

FieldValue
Kindstatic method
Canonical namecovariance
AliasesNone
Mutates receiverNo
ReturnsArithInstance
Accepts (string, base) overloadNo
Configuration dependenciesDECIMAL_PLACES, ROUNDING_MODE, STRICT
Related methodscorrelation, variance

Agent Notes

  • Population covariance is the default.
  • Use { sample: true } for sample covariance.