covarianceMatrix
fun covarianceMatrix(vararg variables: DoubleArray, kind: PopulationKind = PopulationKind.SAMPLE): Array<DoubleArray>(source)
Computes the covariance matrix for multiple variables.
Returns a symmetric k×k matrix where element (i, j) is the covariance between variables i and j. Diagonal elements are the variances of each variable.
Example:
val x = doubleArrayOf(1.0, 2.0, 3.0, 4.0)
val y = doubleArrayOf(2.0, 3.0, 5.0, 7.0)
val matrix = covarianceMatrix(x, y)
matrix[0][0] // variance of x
matrix[0][1] // covariance of x and yContent copied to clipboard
Return
a k×k array of covariance values.
Parameters
variables
two or more arrays of observations, all with the same size.
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 the arrays have different sizes.
if there are fewer than 2 variables or fewer than 2 observations.