MCPcopy Index your code
hub / github.com/RustPython/RustPython / covariance

Function covariance

Lib/statistics.py:657–683  ·  view source on GitHub ↗

Covariance Return the sample covariance of two inputs *x* and *y*. Covariance is a measure of the joint variability of two inputs. >>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> y = [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> covariance(x, y) 0.75 >>> z = [9, 8, 7, 6, 5, 4, 3, 2, 1] >

(x, y, /)

Source from the content-addressed store, hash-verified

655## Statistics for relations between two inputs #############################
656
657def covariance(x, y, /):
658 """Covariance
659
660 Return the sample covariance of two inputs *x* and *y*. Covariance
661 is a measure of the joint variability of two inputs.
662
663 >>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
664 >>> y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
665 >>> covariance(x, y)
666 0.75
667 >>> z = [9, 8, 7, 6, 5, 4, 3, 2, 1]
668 >>> covariance(x, z)
669 -7.5
670 >>> covariance(z, x)
671 -7.5
672
673 """
674 # https://en.wikipedia.org/wiki/Covariance
675 n = len(x)
676 if len(y) != n:
677 raise StatisticsError('covariance requires that both inputs have same number of data points')
678 if n < 2:
679 raise StatisticsError('covariance requires at least two data points')
680 xbar = fsum(x) / n
681 ybar = fsum(y) / n
682 sxy = sumprod((xi - xbar for xi in x), (yi - ybar for yi in y))
683 return sxy / (n - 1)
684
685
686def correlation(x, y, /, *, method='linear'):

Callers

nothing calls this directly

Calls 4

fsumFunction · 0.90
sumprodFunction · 0.90
lenFunction · 0.85
StatisticsErrorClass · 0.85

Tested by

no test coverage detected