11from statistics import (mean , fmean , geometric_mean , harmonic_mean ,
2- variance , stdev )
2+ variance , stdev , covariance , correlation )
33from ltypes import i32 , f64 , i64
44
55eps : f64
@@ -66,6 +66,46 @@ def test_variance():
6666 k = variance (b )
6767 assert abs (k - 0.40924 ) < eps
6868
69+ def test_covariance ():
70+ a : list [i32 ]
71+ a = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
72+ b : list [i32 ]
73+ b = [1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 ]
74+ j : f64
75+ j = covariance (a ,b )
76+ assert abs (j - 0.75 ) < eps
77+
78+ c : list [f64 ]
79+ c = [2.74 , 1.23 , 2.63 , 2.22 , 3.0 , 1.98 ]
80+ d : list [f64 ]
81+ d = [9.4 , 1.23 , 2.63 , 22.4 , 1.9 , 13.98 ]
82+ k : f64
83+ k = covariance (c ,d )
84+ assert abs (k + 0.24955999999999934 ) < eps
85+
86+ def test_correlation ():
87+ a : list [i32 ]
88+ a = [11 , 2 , 7 , 4 , 15 , 6 , 10 , 8 , 9 , 1 , 11 , 5 , 13 , 6 , 15 ]
89+ b : list [i32 ]
90+ b = [2 , 5 , 17 , 6 , 10 , 8 , 13 , 4 , 6 , 9 , 11 , 2 , 5 , 4 , 7 ]
91+
92+ j : f64
93+ j = correlation (a ,b )
94+ assert abs (j - 0.11521487988958108 ) < eps
95+
96+ c : list [i32 ]
97+ c = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
98+ d : list [i32 ]
99+ d = [9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ]
100+
101+ k : f64
102+ k = correlation (c ,c )
103+ assert k == 1.0
104+
105+ l : f64
106+ l = correlation (c ,d )
107+ assert l == - 1.0
108+
69109
70110def test_stdev ():
71111 a : list [i32 ]
@@ -88,5 +128,7 @@ def check():
88128 test_fmean ()
89129 test_variance ()
90130 test_stdev ()
131+ test_covariance ()
132+ test_correlation ()
91133
92134check ()
0 commit comments