Skip to content
Ubiquev1.0.4
functioncorrcoef
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, assertAlmostEquals } from "jsr:@std/assert";

const actual = corrcoef([1, 2, 3], [1, 2, 3]);
const expected = [
  [1, 1],
  [1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Perfect negative correlation between reversed arrays

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

const actual = corrcoef([1, 2, 3], [3, 2, 1]);
const expected = [
  [1, -1],
  [-1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Zero correlation between orthogonal sequences

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

const actual = corrcoef([1, -1, 1, -1], [1, 1, -1, -1]);
const expected = [
  [1, 0],
  [0, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

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, assertAlmostEquals } from "jsr:@std/assert";

const actual = corrcoef([1, 2, 3], [3, 2, 1], 0);
const expected = [
  [1, -1],
  [-1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Matrix input with linearly related columns

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

const actual = corrcoef([
  [1, 2, -1],
  [2, 4, -2],
  [3, 6, -3],
]);
const expected = [
  [1, 1, -1],
  [1, 1, -1],
  [-1, -1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Matrix input with independent columns

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

const actual = corrcoef([
  [1, 1, -1],
  [-1, 1, 1],
  [1, -1, 1],
  [-1, -1, -1],
]);
const expected = [
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

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, assertAlmostEquals } from "jsr:@std/assert";

const actual = corrcoef([
  [1, 1, -1],
  [-1, 1, 1],
  [1, -1, 1],
  [-1, -1, -1],
], 0);
const expected = [
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Matrix correlation handles alternating columns

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

const actual = corrcoef([
  [1, 0],
  [0, 1],
  [1, 0],
  [0, 1],
]);
const expected = [
  [1, -1],
  [-1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

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, assertAlmostEquals } from "jsr:@std/assert";

const actual = corrcoef([1, 2, 3], [1, 2, 3]);
const expected = [
  [1, 1],
  [1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Perfect negative correlation between reversed arrays

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

const actual = corrcoef([1, 2, 3], [3, 2, 1]);
const expected = [
  [1, -1],
  [-1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Zero correlation between orthogonal sequences

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

const actual = corrcoef([1, -1, 1, -1], [1, 1, -1, -1]);
const expected = [
  [1, 0],
  [0, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

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, assertAlmostEquals } from "jsr:@std/assert";

const actual = corrcoef([1, 2, 3], [3, 2, 1], 0);
const expected = [
  [1, -1],
  [-1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Matrix input with linearly related columns

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

const actual = corrcoef([
  [1, 2, -1],
  [2, 4, -2],
  [3, 6, -3],
]);
const expected = [
  [1, 1, -1],
  [1, 1, -1],
  [-1, -1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Matrix input with independent columns

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

const actual = corrcoef([
  [1, 1, -1],
  [-1, 1, 1],
  [1, -1, 1],
  [-1, -1, -1],
]);
const expected = [
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

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, assertAlmostEquals } from "jsr:@std/assert";

const actual = corrcoef([
  [1, 1, -1],
  [-1, 1, 1],
  [1, -1, 1],
  [-1, -1, -1],
], 0);
const expected = [
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

Matrix correlation handles alternating columns

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

const actual = corrcoef([
  [1, 0],
  [0, 1],
  [1, 0],
  [0, 1],
]);
const expected = [
  [1, -1],
  [-1, 1],
];
assertEquals(actual.map(row => row.length), expected.map(row => row.length));
actual.forEach((row, i) => row.forEach((v, j) => assertAlmostEquals(v, expected[i][j], 1e-12)));

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