import type { NumberArray } from 'cheminfo-types'; import { isAnyArray } from 'is-any-array'; import { xMean } from './xMean.ts'; export interface XVarianceOptions { /** * Unbiased option * @default true */ unbiased?: boolean; /** * Precalculated mean of the data. If undefined it will be recalculated internally * @default mean calculated */ mean?: number; } /** * Finds the variance of the data * @param values - the values of the array * @param options - options * @returns variance */ export function xVariance(values: NumberArray, options: XVarianceOptions = {}) { if (!isAnyArray(values)) { throw new TypeError('input must be an array'); } const { unbiased = true, mean = xMean(values) } = options; let sqrError = 0; for (const value of values) { const x = value - mean; sqrError += x * x; } if (unbiased) { return sqrError / (values.length - 1); } else { return sqrError / values.length; } }