1+ from random import random , randrange , shuffle , expovariate , gauss
2+ from statistics import mean , median , stdev
13from math import factorial as fact
2- from random import random , randrange , shuffle
3- from statistics import mean
4+ from heapq import heappush , heappop
5+
6+ #############################################################################
7+ # Helper functions for analysis
48
59def combinations (n , r ):
610 return fact (n ) // (fact (r ) * fact (n - r ))
@@ -11,9 +15,8 @@ def cum_binom(n, r, ph):
1115 total += ph ** h * (1.0 - ph ) ** (n - h ) * combinations (n , h )
1216 return total
1317
14- # Estimate the probability of getting 42500 or more heads
15- # from 70000 spins of biased coin (60% heads and 40% tails)
1618
19+ #############################################################################
1720# Estimate the probability of getting 5 or more heads
1821# from 7 spins of biased coin (60% heads and 40% tails)
1922print (cum_binom (7 , 5 , 0.60 ))
@@ -32,13 +35,16 @@ def trial():
3235trial = lambda : sum (random () < 0.60 for i in range (7 )) >= 5
3336print (sum (trial () for i in range (10000 )) / 10000.0 )
3437
38+
39+ #############################################################################
3540# Probability of the median of 5 samples being in the
3641# middle two quartiles of the population
3742trial = lambda : 2500 <= sorted (randrange (10000 ) for i in range (5 ))[2 ] < 7500
3843print (sum (trial () for i in range (10000 )) / 10000.0 ) # compare with the beta distribution
3944
40- # An exact permutation text
41- # Hypothesis testing
45+
46+ #############################################################################
47+ # Hypothesis testing with an exact permutation text
4248drug = [54 , 73 , 53 , 70 , 73 , 68 , 52 , 65 , 65 ]
4349placebo = [54 , 51 , 58 , 44 , 55 , 52 , 42 , 47 , 58 , 46 ]
4450obs_diff = mean (drug ) - mean (placebo )
@@ -57,3 +63,55 @@ def trial():
5763print (cases / 10000 , '<-- The p-value' )
5864print ('Accordlingly, we reject the null hypothesis' )
5965print ('and conclude the observed difference was not due to chance' )
66+
67+
68+ #############################################################################
69+ # Simulation of arrival times and service deliveries in a single server queue
70+
71+ average_arrival_interval = 5.6
72+ average_service_time = 5.0
73+ stdev_service_time = 0.5
74+
75+ num_waiting = 0
76+ arrivals = []
77+ starts = []
78+ arrival = service_end = 0.0
79+ for i in range (20000 ):
80+ if arrival <= service_end :
81+ num_waiting += 1
82+ arrival += expovariate (1.0 / average_arrival_interval )
83+ arrivals .append (arrival )
84+ else :
85+ num_waiting -= 1
86+ service_start = service_end if num_waiting else arrival
87+ service_time = gauss (average_service_time , stdev_service_time )
88+ service_end = service_start + service_time
89+ starts .append (service_start )
90+
91+ waits = [start - arrival for arrival , start in zip (arrivals , starts )]
92+ print (f'Mean wait: { mean (waits ):.1f} . Stdev wait: { stdev (waits ):.1f} .' )
93+ print (f'Median wait: { median (waits ):.1f} . Max wait: { max (waits ):.1f} .' )
94+
95+
96+ ############################################################################
97+ # Simulation of arrival times and service deliveries for a multiserver queue
98+
99+ average_arrival_interval = 5.6
100+ average_service_time = 15.0
101+ stdev_service_time = 3.5
102+ num_servers = 3
103+
104+ waits = []
105+ arrival_time = 0.0
106+ servers = [0.0 ] * num_servers # time when each server becomes available
107+ for i in range (100_000 ):
108+ arrival_time += expovariate (1.0 / average_arrival_interval )
109+ next_server_available = heappop (servers )
110+ wait = max (0.0 , next_server_available - arrival_time )
111+ waits .append (wait )
112+ service_duration = gauss (average_service_time , stdev_service_time )
113+ service_completed = arrival_time + wait + service_duration
114+ heappush (servers , service_completed )
115+
116+ print (f'Mean wait: { mean (waits ):.1f} . Stdev wait: { stdev (waits ):.1f} .' )
117+ print (f'Median wait: { median (waits ):.1f} . Max wait: { max (waits ):.1f} .' )
0 commit comments