function corrcoef
corrcoef(
x: array,
y: array,
flag?: number,
): matrix

Correlation coefficients of arrays.

Calculates the correlation coefficients between arrays or matrices. Returns the sample correlation coefficient matrix for matrix input, or the correlation matrix between two input arrays/matrices.

Examples

Perfect positive correlation between identical arrays

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

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

Perfect negative correlation between reversed arrays

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

assertEquals(corrcoef([1, 2, 3], [3, 2, 1]), [
  [1, -1],
  [-1, 1],
]);

Zero correlation between orthogonal sequences

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

assertEquals(corrcoef([1, -1, 1, -1], [1, 1, -1, -1]), [
  [1, 0],
  [0, 1],
]);

Explicit sample flag matches the default behaviour

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

assertEquals(
  corrcoef([2, 4, 6, 8], [1, 2, 3, 4]),
  corrcoef([2, 4, 6, 8], [1, 2, 3, 4], 1),
);

Population flag (0) is supported for array inputs

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

assertEquals(corrcoef([1, 2, 3], [3, 2, 1], 0), [
  [1, -1],
  [-1, 1],
]);

Matrix input with linearly related columns

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

assertEquals(corrcoef([
  [1, 2, -1],
  [2, 4, -2],
  [3, 6, -3],
]), [
  [1, 1, -1],
  [1, 1, -1],
  [-1, -1, 1],
]);

Matrix input with independent columns

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

assertEquals(corrcoef([
  [1, 1, -1],
  [-1, 1, 1],
  [1, -1, 1],
  [-1, -1, -1],
]), [
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1],
]);

Symmetry for swapped array inputs

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

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

Population flag also works for matrix inputs

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

assertEquals(corrcoef([
  [1, 1, -1],
  [-1, 1, 1],
  [1, -1, 1],
  [-1, -1, -1],
], 0), [
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1],
]);

Matrix correlation handles alternating columns

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

assertEquals(corrcoef([
  [1, 0],
  [0, 1],
  [1, 0],
  [0, 1],
]), [
  [1, -1],
  [-1, 1],
]);

Parameters

Input array or matrix

Optional second input array or matrix

optional
flag: number

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

Return Type

Matrix of correlation coefficients

Throws

When input dimensions are incompatible

corrcoef(
x: matrix,
flag?: number,
): matrix

Correlation coefficients of arrays.

Calculates the correlation coefficients between arrays or matrices. Returns the sample correlation coefficient matrix for matrix input, or the correlation matrix between two input arrays/matrices.

Examples

Perfect positive correlation between identical arrays

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

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

Perfect negative correlation between reversed arrays

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

assertEquals(corrcoef([1, 2, 3], [3, 2, 1]), [
  [1, -1],
  [-1, 1],
]);

Zero correlation between orthogonal sequences

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

assertEquals(corrcoef([1, -1, 1, -1], [1, 1, -1, -1]), [
  [1, 0],
  [0, 1],
]);

Explicit sample flag matches the default behaviour

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

assertEquals(
  corrcoef([2, 4, 6, 8], [1, 2, 3, 4]),
  corrcoef([2, 4, 6, 8], [1, 2, 3, 4], 1),
);

Population flag (0) is supported for array inputs

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

assertEquals(corrcoef([1, 2, 3], [3, 2, 1], 0), [
  [1, -1],
  [-1, 1],
]);

Matrix input with linearly related columns

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

assertEquals(corrcoef([
  [1, 2, -1],
  [2, 4, -2],
  [3, 6, -3],
]), [
  [1, 1, -1],
  [1, 1, -1],
  [-1, -1, 1],
]);

Matrix input with independent columns

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

assertEquals(corrcoef([
  [1, 1, -1],
  [-1, 1, 1],
  [1, -1, 1],
  [-1, -1, -1],
]), [
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1],
]);

Symmetry for swapped array inputs

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

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

Population flag also works for matrix inputs

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

assertEquals(corrcoef([
  [1, 1, -1],
  [-1, 1, 1],
  [1, -1, 1],
  [-1, -1, -1],
], 0), [
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1],
]);

Matrix correlation handles alternating columns

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

assertEquals(corrcoef([
  [1, 0],
  [0, 1],
  [1, 0],
  [0, 1],
]), [
  [1, -1],
  [-1, 1],
]);

Parameters

Input array or matrix

optional
flag: number

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

Return Type

Matrix of correlation coefficients

Throws

When input dimensions are incompatible