@@ -2402,7 +2402,7 @@ def integrate(func, low, high, steps=10_000):
2402
2402
with self .assertRaises (StatisticsError ):
2403
2403
kde (sample , h = 0.0 ) # Zero bandwidth
2404
2404
with self .assertRaises (StatisticsError ):
2405
- kde (sample , h = 0 .0 ) # Negative bandwidth
2405
+ kde (sample , h = - 1 .0 ) # Negative bandwidth
2406
2406
with self .assertRaises (TypeError ):
2407
2407
kde (sample , h = 'str' ) # Wrong bandwidth type
2408
2408
with self .assertRaises (StatisticsError ):
@@ -2426,6 +2426,14 @@ def integrate(func, low, high, steps=10_000):
2426
2426
self .assertEqual (f_hat (- 1.0 ), 1 / 2 )
2427
2427
self .assertEqual (f_hat (1.0 ), 1 / 2 )
2428
2428
2429
+ # Test online updates to data
2430
+
2431
+ data = [1 , 2 ]
2432
+ f_hat = kde (data , 5.0 , 'triangular' )
2433
+ self .assertEqual (f_hat (100 ), 0.0 )
2434
+ data .append (100 )
2435
+ self .assertGreater (f_hat (100 ), 0.0 )
2436
+
2429
2437
def test_kde_kernel_invcdfs (self ):
2430
2438
kernel_invcdfs = statistics ._kernel_invcdfs
2431
2439
kde = statistics .kde
@@ -2462,7 +2470,7 @@ def test_kde_random(self):
2462
2470
with self .assertRaises (TypeError ):
2463
2471
kde_random (iter (sample ), 1.5 ) # Data is not a sequence
2464
2472
with self .assertRaises (StatisticsError ):
2465
- kde_random (sample , h = 0 .0 ) # Zero bandwidth
2473
+ kde_random (sample , h = - 1 .0 ) # Zero bandwidth
2466
2474
with self .assertRaises (StatisticsError ):
2467
2475
kde_random (sample , h = 0.0 ) # Negative bandwidth
2468
2476
with self .assertRaises (TypeError ):
@@ -2474,10 +2482,10 @@ def test_kde_random(self):
2474
2482
2475
2483
h = 1.5
2476
2484
kernel = 'cosine'
2477
- prng = kde_random (sample , h , kernel )
2478
- self .assertEqual (prng .__name__ , 'rand' )
2479
- self .assertIn (kernel , prng .__doc__ )
2480
- self .assertIn (repr (h ), prng .__doc__ )
2485
+ rand = kde_random (sample , h , kernel )
2486
+ self .assertEqual (rand .__name__ , 'rand' )
2487
+ self .assertIn (kernel , rand .__doc__ )
2488
+ self .assertIn (repr (h ), rand .__doc__ )
2481
2489
2482
2490
# Approximate distribution test: Compare a random sample to the expected distribution
2483
2491
@@ -2507,6 +2515,14 @@ def p_expected(x):
2507
2515
for x in xarr :
2508
2516
self .assertTrue (math .isclose (p_observed (x ), p_expected (x ), abs_tol = 0.0005 ))
2509
2517
2518
+ # Test online updates to data
2519
+
2520
+ data = [1 , 2 ]
2521
+ rand = kde_random (data , 5 , 'triangular' )
2522
+ self .assertLess (max ([rand () for i in range (5000 )]), 10 )
2523
+ data .append (100 )
2524
+ self .assertGreater (max (rand () for i in range (5000 )), 10 )
2525
+
2510
2526
2511
2527
class TestQuantiles (unittest .TestCase ):
2512
2528
0 commit comments