@mirei/ts-collections
    Preparing search index...

    Function variance

    • Calculates the variance of the numeric values produced by source.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable to inspect.

      • Optionalselector: Selector<TElement, number>

        Optional projection that extracts the numeric value for each element. Defaults to the element itself.

      • Optionalsample: boolean

        When true, computes the sample variance dividing by n - 1; when false, computes the population variance dividing by n. Defaults to true.

      Returns number

      The calculated variance, or NaN when source is empty—or for sample variance when it contains a single element.

      Re-throws any error thrown while iterating source or executing selector.

      A numerically stable single-pass algorithm (Welford's method) is used, so the iterable is enumerated exactly once regardless of size.

      const populationVariance = variance([1, 2, 3, 4, 5], x => x, false);
      console.log(populationVariance); // 2