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

    Function covariance

    • Calculates the covariance between source and other.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      • TSecond

        Type of elements within other.

      Parameters

      • source: Iterable<TElement>

        The primary iterable whose elements align by index with other.

      • other: Iterable<TSecond>

        Secondary iterable supplying the paired values.

      • Optionalselector: Selector<TElement, number>

        Optional projection that extracts the numeric value for each element in source. Defaults to treating the element itself as numeric.

      • OptionalotherSelector: Selector<TSecond, number>

        Optional projection that extracts the numeric value for each element in other. Defaults to treating the element itself as numeric.

      • Optionalsample: boolean

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

      Returns number

      The calculated covariance.

      Thrown when source and other do not contain the same number of elements.

      Thrown when fewer than two aligned pairs are available.

      Re-throws any error thrown while iterating either iterable or executing the selector projections.

      Both iterables are consumed simultaneously so streaming statistics can be computed without materialising all elements. Ensure the iterables are aligned because mismatch detection occurs only after iteration begins.

      const numbers = [1, 2, 3, 4, 5];
      const doubles = [2, 4, 6, 8, 10];
      const covarianceValue = covariance(numbers, doubles);
      console.log(covarianceValue); // 5