@@ -2842,6 +2842,32 @@ def test_correlation_spearman(self):
28422842 with self .assertRaises (ValueError ):
28432843 statistics .correlation (reading , mathematics , method = 'bad_method' )
28442844
2845+ def test_iterator_inputs (self ):
2846+ x = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
2847+ y = [1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 ]
2848+ expected_cov = statistics .covariance (x , y )
2849+ expected_cor = statistics .correlation (x , y )
2850+ # iter() inputs should give same results as list inputs
2851+ self .assertAlmostEqual (statistics .covariance (iter (x ), iter (y )), expected_cov )
2852+ self .assertAlmostEqual (statistics .correlation (iter (x ), iter (y )), expected_cor )
2853+ # generator expressions should also work
2854+ self .assertAlmostEqual (
2855+ statistics .covariance ((v for v in x ), (v for v in y )), expected_cov
2856+ )
2857+ self .assertAlmostEqual (
2858+ statistics .correlation ((v for v in x ), (v for v in y )), expected_cor
2859+ )
2860+ # ranked method should also accept iterators
2861+ expected_ranked = statistics .correlation (x , y , method = 'ranked' )
2862+ self .assertAlmostEqual (
2863+ statistics .correlation (iter (x ), iter (y ), method = 'ranked' ), expected_ranked
2864+ )
2865+ # mismatched lengths should still raise StatisticsError
2866+ with self .assertRaises (statistics .StatisticsError ):
2867+ statistics .covariance (iter ([1 , 2 , 3 ]), iter ([1 , 2 ]))
2868+ with self .assertRaises (statistics .StatisticsError ):
2869+ statistics .correlation (iter ([1 , 2 , 3 ]), iter ([1 , 2 ]))
2870+
28452871class TestLinearRegression (unittest .TestCase ):
28462872
28472873 def test_constant_input_error (self ):
@@ -2881,6 +2907,26 @@ def test_float_output(self):
28812907 self .assertTrue (isinstance (slope , float ))
28822908 self .assertTrue (isinstance (intercept , float ))
28832909
2910+ def test_iterator_inputs (self ):
2911+ x = [1 , 2 , 3 , 4 , 5 ]
2912+ y = [2 , 4 , 6 , 8 , 10 ]
2913+ expected = statistics .linear_regression (x , y )
2914+ # iter() inputs should give same results as list inputs
2915+ result = statistics .linear_regression (iter (x ), iter (y ))
2916+ self .assertAlmostEqual (result .slope , expected .slope )
2917+ self .assertAlmostEqual (result .intercept , expected .intercept )
2918+ # generator expressions should also work
2919+ result = statistics .linear_regression ((v for v in x ), (v for v in y ))
2920+ self .assertAlmostEqual (result .slope , expected .slope )
2921+ self .assertAlmostEqual (result .intercept , expected .intercept )
2922+ # proportional=True should also accept iterators
2923+ expected_prop = statistics .linear_regression (x , y , proportional = True )
2924+ result_prop = statistics .linear_regression (
2925+ iter (x ), iter (y ), proportional = True
2926+ )
2927+ self .assertAlmostEqual (result_prop .slope , expected_prop .slope )
2928+ self .assertEqual (result_prop .intercept , 0.0 )
2929+
28842930class TestNormalDist :
28852931
28862932 # General note on precision: The pdf(), cdf(), and overlap() methods
0 commit comments