corrcoef(): 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.
Perfect positive correlation between identical arrays
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
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
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
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
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
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
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
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
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
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)));
Matrix of correlation coefficients
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.
Perfect positive correlation between identical arrays
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
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
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
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
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
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
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
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
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
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)));
x: matrix
Input array or matrix
Matrix of correlation coefficients