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

    Function correlation

    • Computes the Pearson correlation coefficient between source and other.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      • TSecond

        Type of elements within the other iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable whose elements align with other by index.

      • other: Iterable<TSecond>

        The iterable that provides the second series of aligned values.

      • Optionalselector: Selector<TElement, number>

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

      • OptionalotherSelector: Selector<TSecond, number>

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

      Returns number

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

      Thrown when the iterables do not contain the same number of elements.

      Thrown when fewer than two aligned pairs are available.

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

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

      Both iterables are enumerated simultaneously via an online algorithm that avoids buffering the full dataset. Ensure the iterables are aligned because mismatch detection occurs only after enumeration begins.

      const temperatures = [15, 18, 21, 24];
      const sales = [30, 36, 42, 48];
      const result = correlation(temperatures, sales);
      console.log(result); // 1