function cov
cov(x: array): number

Covariance matrix.

Calculates the covariance matrix between arrays or matrices. For a single vector, returns the variance. For two vectors, returns the 2x2 covariance matrix. For a matrix, returns the covariance matrix between columns.

Examples

Sample covariance (variance) of single array

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([1, 2, 3]), 1);

Population covariance with flag=0

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([1, 2, 3], 0), 0.6666666666666666);

Covariance matrix from 2x2 data matrix

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([[1, 2], [3, 4]]), [[2, 2], [2, 2]]);

Parameters

Input array or matrix

Return Type

number

Covariance matrix or scalar variance

Throws

When input dimensions do not agree

cov(
x: array,
flag: 0 | 1,
): number

Covariance matrix.

Calculates the covariance matrix between arrays or matrices. For a single vector, returns the variance. For two vectors, returns the 2x2 covariance matrix. For a matrix, returns the covariance matrix between columns.

Examples

Sample covariance (variance) of single array

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([1, 2, 3]), 1);

Population covariance with flag=0

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([1, 2, 3], 0), 0.6666666666666666);

Covariance matrix from 2x2 data matrix

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([[1, 2], [3, 4]]), [[2, 2], [2, 2]]);

Parameters

Input array or matrix

flag: 0 | 1

Optional Bessel's correction (0: population, 1: sample). Default is 1

Return Type

number

Covariance matrix or scalar variance

Throws

When input dimensions do not agree

cov(
x: array,
y: array,
flag?: 0 | 1,
): matrix

Covariance matrix.

Calculates the covariance matrix between arrays or matrices. For a single vector, returns the variance. For two vectors, returns the 2x2 covariance matrix. For a matrix, returns the covariance matrix between columns.

Examples

Sample covariance (variance) of single array

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([1, 2, 3]), 1);

Population covariance with flag=0

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([1, 2, 3], 0), 0.6666666666666666);

Covariance matrix from 2x2 data matrix

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([[1, 2], [3, 4]]), [[2, 2], [2, 2]]);

Parameters

Input array or matrix

Optional second input array or matrix, or flag value (0 or 1)

optional
flag: 0 | 1

Optional Bessel's correction (0: population, 1: sample). Default is 1

Return Type

Covariance matrix or scalar variance

Throws

When input dimensions do not agree

cov(
x: matrix,
flag?: 0 | 1,
): matrix

Covariance matrix.

Calculates the covariance matrix between arrays or matrices. For a single vector, returns the variance. For two vectors, returns the 2x2 covariance matrix. For a matrix, returns the covariance matrix between columns.

Examples

Sample covariance (variance) of single array

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([1, 2, 3]), 1);

Population covariance with flag=0

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([1, 2, 3], 0), 0.6666666666666666);

Covariance matrix from 2x2 data matrix

import { assertEquals } from "jsr:@std/assert";

assertEquals(cov([[1, 2], [3, 4]]), [[2, 2], [2, 2]]);

Parameters

Input array or matrix

optional
flag: 0 | 1

Optional Bessel's correction (0: population, 1: sample). Default is 1

Return Type

Covariance matrix or scalar variance

Throws

When input dimensions do not agree