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

    Function covarianceBy

    • Calculates the covariance between two numeric projections of source.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable to inspect.

      • leftSelector: Selector<TElement, number>

        Projection that produces the first numeric series for each element.

      • rightSelector: Selector<TElement, number>

        Projection that produces the second numeric series for each element.

      • 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 fewer than two elements are available.

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

      source is consumed exactly once using an online algorithm that avoids buffering, making it suitable for large datasets.

      const points = [
      { x: 1, y: 2 },
      { x: 2, y: 4 },
      { x: 3, y: 6 }
      ];
      const covarianceValue = covarianceBy(points, p => p.x, p => p.y);
      console.log(covarianceValue); // 2