Skip to main content
Module

x/statistics/src/variance.ts

Deno basic statistics module.
Latest
File
import { sumNthPowerDeviations } from "./sum_nth_power_deviations.ts";
/** * The [variance](http://en.wikipedia.org/wiki/Variance) * is the sum of squared deviations from the mean. * * This is an implementation of variance, not sample variance: * see the `sampleVariance` method if you want a sample measure. * * @param {number[]} x a population of one or more data points * @returns {number} variance: a value greater than or equal to zero. * zero indicates that all values are identical. * @throws {Error} if x's length is 0 * @example * variance([1, 2, 3, 4, 5, 6]); // => 2.9166666666666665 */export function variance(x: number[]): number { if (!x.length) { throw new Error("variance requires at least one data point"); }
// Find the mean of squared deviations between the // mean value and each value. return sumNthPowerDeviations(x, 2) / x.length;}