@@ -2158,6 +2158,20 @@ def test_specific_cases(self):
2158
2158
result = quantiles (map (datatype , data ), n = n )
2159
2159
self .assertTrue (all (type (x ) == datatype ) for x in result )
2160
2160
self .assertEqual (result , list (map (datatype , expected )))
2161
+ # Quantiles should be idempotent
2162
+ if len (expected ) >= 2 :
2163
+ 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
+ )
2161
2175
# Invariant under tranlation and scaling
2162
2176
def f (x ):
2163
2177
return 3.5 * x - 1234.675
@@ -2219,6 +2233,15 @@ def f(x):
2219
2233
actual = quantiles (statistics .NormalDist (), n = n , method = "inclusive" )
2220
2234
self .assertTrue (all (math .isclose (e , a , abs_tol = 0.0001 )
2221
2235
for e , a in zip (expected , actual )))
2236
+ # Whenever n is smaller than the number of data points, running
2237
+ # method='inclusive' should give the same result as method='exclusive'
2238
+ # after the two included extreme points are removed.
2239
+ data = [random .randrange (10_000 ) for i in range (501 )]
2240
+ actual = quantiles (data , n = 32 , method = 'inclusive' )
2241
+ data .remove (min (data ))
2242
+ data .remove (max (data ))
2243
+ expected = quantiles (data , n = 32 )
2244
+ self .assertEqual (expected , actual )
2222
2245
2223
2246
def test_equal_inputs (self ):
2224
2247
quantiles = statistics .quantiles
0 commit comments