You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
930 B
36 lines
930 B
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;
|
|
}
|
|
|