Statistics

Aggregations take an optional dimension: no argument reduces the whole matrix to a number; 'row' or 'column' returns a number[].

import { Matrix } from "@teakit/matrix";

m.sum(); // number
m.sum("row"); // number[] (one per row)
m.sum("column"); // number[]

m.product(); // number | number[]
m.mean(); // number | number[]
m.variance(); // number; options: { unbiased = true, mean }
m.variance("column", { unbiased: false });
m.standardDeviation(); // number | number[]

Min / max

m.max(); // number
m.max("row"); // number[]
m.maxIndex(); // [row, col]
m.maxRow(i); // number; also maxRowIndex(i)
m.maxColumn(j); // number; also maxColumnIndex(j)
// min, minIndex, minRow, minColumn, … mirror these
m.diag(); // number[] of the diagonal (alias: diagonal)

Center / scale (in place)

m.center(); // subtract the global mean; returns this
m.center("column"); // subtract each column's mean; options: { center }
m.scale(); // divide by std; options: { scale }
m.scale("row");
m.cumulativeSum(); // in place, row by row

scaleRows(options?) / scaleColumns(options?) return a new rescaled matrix (default range [0, 1], options { min, max }).

Covariance / correlation (top-level)

import { covariance, correlation } from "@teakit/matrix";

covariance(X); // Matrix; options: { center = true }
covariance(X, Y, { center: true });

correlation(X); // Matrix; options: { center = true, scale = true }
correlation(X, Y);

Both accept a single matrix (compared with itself) or two matrices with the same number of rows.