@@ -425,29 +425,28 @@ Simulations::
425425 >>> def trial():
426426 ... return choices('HT', cum_weights=(0.60, 1.00), k=7).count('H') >= 5
427427 ...
428- >>> sum(trial() for i in range(10000 )) / 10000
428+ >>> sum(trial() for i in range(10_000 )) / 10_000
429429 0.4169
430430
431431 >>> # Probability of the median of 5 samples being in middle two quartiles
432432 >>> 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
434434 ...
435- >>> sum(trial() for i in range(10000 )) / 10000
435+ >>> sum(trial() for i in range(10_000 )) / 10_000
436436 0.7958
437437
438438Example of `statistical bootstrapping
439439<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::
442441
443442 # http://statistics.about.com/od/Applications/a/Example-Of-Bootstrapping.htm
444443 from statistics import fmean as mean
445444 from random import choices
446445
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 ))
449448 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}')
451450
452451Example of a `resampling permutation test
453452<https://en.wikipedia.org/wiki/Resampling_(statistics)#Permutation_tests> `_
@@ -463,7 +462,7 @@ between the effects of a drug versus a placebo::
463462 placebo = [54, 51, 58, 44, 55, 52, 42, 47, 58, 46]
464463 observed_diff = mean(drug) - mean(placebo)
465464
466- n = 10000
465+ n = 10_000
467466 count = 0
468467 combined = drug + placebo
469468 for i in range(n):
@@ -476,32 +475,29 @@ between the effects of a drug versus a placebo::
476475 print(f'The one-sided p-value of {count / n:.4f} leads us to reject the null')
477476 print(f'hypothesis that there is no difference between the drug and the placebo.')
478477
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::
480479
480+ from heapq import heappush, heappop
481481 from random import expovariate, gauss
482482 from statistics import mean, median, stdev
483483
484484 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+
505501 print(f'Mean wait: {mean(waits):.1f}. Stdev wait: {stdev(waits):.1f}.')
506502 print(f'Median wait: {median(waits):.1f}. Max wait: {max(waits):.1f}.')
507503
0 commit comments