covariance

fun covariance(x: DoubleArray, y: DoubleArray, kind: PopulationKind = PopulationKind.SAMPLE): Double(source)

Computes the covariance between two arrays.

Covariance measures how two variables change together. A positive value means they tend to increase together, a negative value means one tends to increase when the other decreases, and a value near zero means no linear association. Unlike correlation, the magnitude of covariance depends on the scale of the variables.

Example:

val x = doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0)
val y = doubleArrayOf(2.0, 4.0, 6.0, 8.0, 10.0)
covariance(x, y) // 5.0 (sample covariance)

Return

the covariance between x and y.

Parameters

x

the first array of observations.

y

the second array of observations, must have the same size as x.

kind

whether to compute sample or population covariance. Defaults to PopulationKind.SAMPLE, which divides by n-1 (Bessel's correction) to produce an unbiased estimate.

Throws

if x and y have different sizes.

if there are fewer than 2 observations.