@@ -2161,17 +2161,18 @@ def test_specific_cases(self):
21612161 # Quantiles should be idempotent
21622162 if len (expected ) >= 2 :
21632163 self .assertEqual (quantiles (expected , n = n ), expected )
2164- # Cross-check against other methods
2165- if len (data ) >= n :
2166- # After end caps are added, method='inclusive' should
2167- # give the same result as method='exclusive' whenever
2168- # there are more data points than desired cut points.
2169- padded_data = [min (data ) - 1000 ] + data + [max (data ) + 1000 ]
2170- self .assertEqual (
2171- quantiles (data , n = n ),
2172- quantiles (padded_data , n = n , method = 'inclusive' ),
2173- (n , data ),
2174- )
2164+ # Cross-check against method='inclusive' which should give
2165+ # the same result after adding in minimum and maximum values
2166+ # extrapolated from the two lowest and two highest points.
2167+ sdata = sorted (data )
2168+ lo = 2 * sdata [0 ] - sdata [1 ]
2169+ hi = 2 * sdata [- 1 ] - sdata [- 2 ]
2170+ padded_data = data + [lo , hi ]
2171+ self .assertEqual (
2172+ quantiles (data , n = n ),
2173+ quantiles (padded_data , n = n , method = 'inclusive' ),
2174+ (n , data ),
2175+ )
21752176 # Invariant under tranlation and scaling
21762177 def f (x ):
21772178 return 3.5 * x - 1234.675
@@ -2188,6 +2189,11 @@ def f(x):
21882189 actual = quantiles (statistics .NormalDist (), n = n )
21892190 self .assertTrue (all (math .isclose (e , a , abs_tol = 0.0001 )
21902191 for e , a in zip (expected , actual )))
2192+ # Q2 agrees with median()
2193+ for k in range (2 , 60 ):
2194+ data = random .choices (range (100 ), k = k )
2195+ q1 , q2 , q3 = quantiles (data )
2196+ self .assertEqual (q2 , statistics .median (data ))
21912197
21922198 def test_specific_cases_inclusive (self ):
21932199 # Match results computed by hand and cross-checked
@@ -2233,6 +2239,11 @@ def f(x):
22332239 actual = quantiles (statistics .NormalDist (), n = n , method = "inclusive" )
22342240 self .assertTrue (all (math .isclose (e , a , abs_tol = 0.0001 )
22352241 for e , a in zip (expected , actual )))
2242+ # Natural deciles
2243+ self .assertEqual (quantiles ([0 , 100 ], n = 10 , method = 'inclusive' ),
2244+ [10.0 , 20.0 , 30.0 , 40.0 , 50.0 , 60.0 , 70.0 , 80.0 , 90.0 ])
2245+ self .assertEqual (quantiles (range (0 , 101 ), n = 10 , method = 'inclusive' ),
2246+ [10.0 , 20.0 , 30.0 , 40.0 , 50.0 , 60.0 , 70.0 , 80.0 , 90.0 ])
22362247 # Whenever n is smaller than the number of data points, running
22372248 # method='inclusive' should give the same result as method='exclusive'
22382249 # after the two included extreme points are removed.
@@ -2242,6 +2253,11 @@ def f(x):
22422253 data .remove (max (data ))
22432254 expected = quantiles (data , n = 32 )
22442255 self .assertEqual (expected , actual )
2256+ # Q2 agrees with median()
2257+ for k in range (2 , 60 ):
2258+ data = random .choices (range (100 ), k = k )
2259+ q1 , q2 , q3 = quantiles (data , method = 'inclusive' )
2260+ self .assertEqual (q2 , statistics .median (data ))
22452261
22462262 def test_equal_inputs (self ):
22472263 quantiles = statistics .quantiles
0 commit comments