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

    Function correlationBy

    • Computes the Pearson correlation coefficient between two numeric projections of source.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable that supplies the data for both projections.

      • 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.

      Returns number

      The correlation coefficient in the interval [-1, 1].

      Thrown when fewer than two elements are available.

      Thrown when the standard deviation of either numeric projection is zero.

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

      The iterable is enumerated exactly once using an online algorithm, which keeps memory usage constant even for large inputs.

      const metrics = [
      { impressions: 1_000, clicks: 50 },
      { impressions: 1_500, clicks: 75 },
      { impressions: 2_000, clicks: 100 }
      ];
      const result = correlationBy(metrics, m => m.impressions, m => m.clicks);
      console.log(result); // 1