import { isAnyArray } from 'is-any-array'; import Matrix from './matrix'; export function covariance(xMatrix, yMatrix = xMatrix, options = {}) { xMatrix = new Matrix(xMatrix); let yIsSame = false; if ( typeof yMatrix === 'object' && !Matrix.isMatrix(yMatrix) && !isAnyArray(yMatrix) ) { options = yMatrix; yMatrix = xMatrix; yIsSame = true; } else { yMatrix = new Matrix(yMatrix); } if (xMatrix.rows !== yMatrix.rows) { throw new TypeError('Both matrices must have the same number of rows'); } const { center = true } = options; if (center) { xMatrix = xMatrix.center('column'); if (!yIsSame) { yMatrix = yMatrix.center('column'); } } const cov = xMatrix.transpose().mmul(yMatrix); for (let i = 0; i < cov.rows; i++) { for (let j = 0; j < cov.columns; j++) { cov.set(i, j, cov.get(i, j) * (1 / (xMatrix.rows - 1))); } } return cov; }