import { computeStatisticalMean } from "./computeStatisticalMean.js";

/**
 * Compute statistical variance for a given sample of numbers
 * @param {number[]} sample
 * @returns {number}
 */
export function computeStatisticalVariance(sample) {
    //compute sample mean
    const mean = computeStatisticalMean(sample);

    const N = sample.length;

    let SUM = 0;
    for (let i = 0; i < N; i++) {
        const x = sample[i];

        const delta = x - mean;

        const delta2 = delta * delta;

        SUM += delta2;
    }

    return SUM / N;
}