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, /)
| 655 | ## Statistics for relations between two inputs ############################# |
| 656 | |
| 657 | def 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 | |
| 686 | def correlation(x, y, /, *, method='linear'): |
nothing calls this directly
no test coverage detected