@@ -394,29 +394,28 @@ Simulations::
394394 >>> def trial():
395395 ... return choices('HT', cum_weights=(0.60, 1.00), k=7).count('H') >= 5
396396 ...
397- >>> sum(trial() for i in range(10000 )) / 10000
397+ >>> sum(trial() for i in range(10_000 )) / 10_000
398398 0.4169
399399
400400 >>> # Probability of the median of 5 samples being in middle two quartiles
401401 >>> def trial():
402- ... return 2500 <= sorted(choices(range(10000 ), k=5))[2] < 7500
402+ ... return 2_500 <= sorted(choices(range(10_000 ), k=5))[2] < 7_500
403403 ...
404- >>> sum(trial() for i in range(10000 )) / 10000
404+ >>> sum(trial() for i in range(10_000 )) / 10_000
405405 0.7958
406406
407407Example of `statistical bootstrapping
408408<https://en.wikipedia.org/wiki/Bootstrapping_(statistics)> `_ using resampling
409- with replacement to estimate a confidence interval for the mean of a sample of
410- size five::
409+ with replacement to estimate a confidence interval for the mean of a sample::
411410
412411 # http://statistics.about.com/od/Applications/a/Example-Of-Bootstrapping.htm
413412 from statistics import fmean as mean
414413 from random import choices
415414
416- data = 1, 2, 4, 4, 10
417- means = sorted(mean(choices(data, k=5)) for i in range(20 ))
415+ data = [41, 50, 29, 37, 81, 30, 73, 63, 20, 35, 68, 22, 60, 31, 95]
416+ means = sorted(mean(choices(data, k=len(data))) for i in range(100 ))
418417 print(f'The sample mean of {mean(data):.1f} has a 90% confidence '
419- f'interval from {means[1 ]:.1f} to {means[-2 ]:.1f}')
418+ f'interval from {means[5 ]:.1f} to {means[94 ]:.1f}')
420419
421420Example of a `resampling permutation test
422421<https://en.wikipedia.org/wiki/Resampling_(statistics)#Permutation_tests> `_
@@ -432,7 +431,7 @@ between the effects of a drug versus a placebo::
432431 placebo = [54, 51, 58, 44, 55, 52, 42, 47, 58, 46]
433432 observed_diff = mean(drug) - mean(placebo)
434433
435- n = 10000
434+ n = 10_000
436435 count = 0
437436 combined = drug + placebo
438437 for i in range(n):
@@ -445,32 +444,29 @@ between the effects of a drug versus a placebo::
445444 print(f'The one-sided p-value of {count / n:.4f} leads us to reject the null')
446445 print(f'hypothesis that there is no difference between the drug and the placebo.')
447446
448- Simulation of arrival times and service deliveries in a single server queue::
447+ Simulation of arrival times and service deliveries for a multiserver queue::
449448
449+ from heapq import heappush, heappop
450450 from random import expovariate, gauss
451451 from statistics import mean, median, stdev
452452
453453 average_arrival_interval = 5.6
454- average_service_time = 5.0
455- stdev_service_time = 0.5
456-
457- num_waiting = 0
458- arrivals = []
459- starts = []
460- arrival = service_end = 0.0
461- for i in range(20000):
462- if arrival <= service_end:
463- num_waiting += 1
464- arrival += expovariate(1.0 / average_arrival_interval)
465- arrivals.append(arrival)
466- else:
467- num_waiting -= 1
468- service_start = service_end if num_waiting else arrival
469- service_time = gauss(average_service_time, stdev_service_time)
470- service_end = service_start + service_time
471- starts.append(service_start)
472-
473- waits = [start - arrival for arrival, start in zip(arrivals, starts)]
454+ average_service_time = 15.0
455+ stdev_service_time = 3.5
456+ num_servers = 3
457+
458+ waits = []
459+ arrival_time = 0.0
460+ servers = [0.0] * num_servers # time when each server becomes available
461+ for i in range(100_000):
462+ arrival_time += expovariate(1.0 / average_arrival_interval)
463+ next_server_available = heappop(servers)
464+ wait = max(0.0, next_server_available - arrival_time)
465+ waits.append(wait)
466+ service_duration = gauss(average_service_time, stdev_service_time)
467+ service_completed = arrival_time + wait + service_duration
468+ heappush(servers, service_completed)
469+
474470 print(f'Mean wait: {mean(waits):.1f}. Stdev wait: {stdev(waits):.1f}.')
475471 print(f'Median wait: {median(waits):.1f}. Max wait: {max(waits):.1f}.')
476472
0 commit comments