@@ -425,29 +425,28 @@ Simulations::
425
425
>>> def trial():
426
426
... return choices('HT', cum_weights=(0.60, 1.00), k=7).count('H') >= 5
427
427
...
428
- >>> sum(trial() for i in range(10000 )) / 10000
428
+ >>> sum(trial() for i in range(10_000 )) / 10_000
429
429
0.4169
430
430
431
431
>>> # Probability of the median of 5 samples being in middle two quartiles
432
432
>>> def trial():
433
- ... return 2500 <= sorted(choices(range(10000 ), k=5))[2] < 7500
433
+ ... return 2_500 <= sorted(choices(range(10_000 ), k=5))[2] < 7_500
434
434
...
435
- >>> sum(trial() for i in range(10000 )) / 10000
435
+ >>> sum(trial() for i in range(10_000 )) / 10_000
436
436
0.7958
437
437
438
438
Example of `statistical bootstrapping
439
439
<https://en.wikipedia.org/wiki/Bootstrapping_(statistics)> `_ using resampling
440
- with replacement to estimate a confidence interval for the mean of a sample of
441
- size five::
440
+ with replacement to estimate a confidence interval for the mean of a sample::
442
441
443
442
# http://statistics.about.com/od/Applications/a/Example-Of-Bootstrapping.htm
444
443
from statistics import fmean as mean
445
444
from random import choices
446
445
447
- data = 1, 2, 4, 4, 10
448
- means = sorted(mean(choices(data, k=5)) for i in range(20 ))
446
+ data = [41, 50, 29, 37, 81, 30, 73, 63, 20, 35, 68, 22, 60, 31, 95]
447
+ means = sorted(mean(choices(data, k=len(data))) for i in range(100 ))
449
448
print(f'The sample mean of {mean(data):.1f} has a 90% confidence '
450
- f'interval from {means[1 ]:.1f} to {means[-2 ]:.1f}')
449
+ f'interval from {means[5 ]:.1f} to {means[94 ]:.1f}')
451
450
452
451
Example of a `resampling permutation test
453
452
<https://en.wikipedia.org/wiki/Resampling_(statistics)#Permutation_tests> `_
@@ -463,7 +462,7 @@ between the effects of a drug versus a placebo::
463
462
placebo = [54, 51, 58, 44, 55, 52, 42, 47, 58, 46]
464
463
observed_diff = mean(drug) - mean(placebo)
465
464
466
- n = 10000
465
+ n = 10_000
467
466
count = 0
468
467
combined = drug + placebo
469
468
for i in range(n):
@@ -476,32 +475,29 @@ between the effects of a drug versus a placebo::
476
475
print(f'The one-sided p-value of {count / n:.4f} leads us to reject the null')
477
476
print(f'hypothesis that there is no difference between the drug and the placebo.')
478
477
479
- Simulation of arrival times and service deliveries in a single server queue::
478
+ Simulation of arrival times and service deliveries for a multiserver queue::
480
479
480
+ from heapq import heappush, heappop
481
481
from random import expovariate, gauss
482
482
from statistics import mean, median, stdev
483
483
484
484
average_arrival_interval = 5.6
485
- average_service_time = 5.0
486
- stdev_service_time = 0.5
487
-
488
- num_waiting = 0
489
- arrivals = []
490
- starts = []
491
- arrival = service_end = 0.0
492
- for i in range(20000):
493
- if arrival <= service_end:
494
- num_waiting += 1
495
- arrival += expovariate(1.0 / average_arrival_interval)
496
- arrivals.append(arrival)
497
- else:
498
- num_waiting -= 1
499
- service_start = service_end if num_waiting else arrival
500
- service_time = gauss(average_service_time, stdev_service_time)
501
- service_end = service_start + service_time
502
- starts.append(service_start)
503
-
504
- waits = [start - arrival for arrival, start in zip(arrivals, starts)]
485
+ average_service_time = 15.0
486
+ stdev_service_time = 3.5
487
+ num_servers = 3
488
+
489
+ waits = []
490
+ arrival_time = 0.0
491
+ servers = [0.0] * num_servers # time when each server becomes available
492
+ for i in range(100_000):
493
+ arrival_time += expovariate(1.0 / average_arrival_interval)
494
+ next_server_available = heappop(servers)
495
+ wait = max(0.0, next_server_available - arrival_time)
496
+ waits.append(wait)
497
+ service_duration = gauss(average_service_time, stdev_service_time)
498
+ service_completed = arrival_time + wait + service_duration
499
+ heappush(servers, service_completed)
500
+
505
501
print(f'Mean wait: {mean(waits):.1f}. Stdev wait: {stdev(waits):.1f}.')
506
502
print(f'Median wait: {median(waits):.1f}. Max wait: {max(waits):.1f}.')
507
503
0 commit comments