Population and Sample Variance
Install and import#
npm install fintech-algorithmsimport { populationAndSampleVariance } from "fintech-algorithms/foundations/dispersion-shape-and-robust-statistics/population-and-sample-variance";Signature#
populationAndSampleVariance(input)Averages the squared deviations from the mean twice over the same series: once dividing by the observation count, once by that count less one.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | { values: number[] } | The observations to measure, under the key values. At least two are needed, because the sample figure divides by n - 1.values: at least two observations |
Returns#
{ populationVariance: number; sampleVariance: number }
populationVariance divides the sum of squared deviations by n, sampleVariance divides it by n - 1.
Errors#
- When
inputis null, an array, or not an object — throws TypeError - When
valuesis missing, is not an array, or is empty — throws RangeError - When any entry of
valuesdoes not coerce to a finite number — throws RangeError - When
valuesholds fewer than two observations, so the sample divisorn - 1would be zero — throws RangeError
Complexity: time O(n log n),
space O(n).
Worked example#
verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.
Input#
{
"values": [1, 2, 2, 4, 9]
}Call#
populationAndSampleVariance(input)Returns#
object with 2 fields: populationVariance, sampleVariance
{
"populationVariance": 8.24,
"sampleVariance": 10.3
}Diagrams#
How it works#
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References#
- Measures of Scale — NIST/SEMATECH e-Handbook
- Skewness and Kurtosis — NIST/SEMATECH e-Handbook
- Historical-example decision