The smaller exercises embedded in the other Lecture 12 notebooks check what you just read. This notebook collects the larger exercises for Lecture 12: independent problems worth more time. Exercises 4 and 6 ask you to read Python code; you do not need to write a simulation from scratch.
Homework Exercises¶
Solution to Exercise 1 #
a. A discrete distribution, with possible outcomes .
b. Still a discrete distribution in principle (there are only finitely many possible averages of 100 die-max outcomes), but by the central limit theorem it will look close to a normal (continuous) distribution.
c. The confidence interval is . We are 95% confident that the true expected outcome lies in this interval. More precisely: if we repeated this experiment many times and built a 95%-CI each time in the same way, about 95% of those intervals would contain the true expected value. Any single realized interval either does or does not contain it; no probabilistic statement applies to it on its own.
d. All 36 equally likely outcomes of (die 1, die 2) and their maximum:
| die 2 \ die 1 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| 6 | 6 | 6 | 6 | 6 | 6 | 6 |
| 5 | 5 | 5 | 5 | 5 | 5 | 6 |
| 4 | 4 | 4 | 4 | 4 | 5 | 6 |
| 3 | 3 | 3 | 3 | 4 | 5 | 6 |
| 2 | 2 | 2 | 3 | 4 | 5 | 6 |
| 1 | 1 | 2 | 3 | 4 | 5 | 6 |
Each outcome has probability , and the value appears times (1, 3, 5, 7, 9, 11 times for ), so
Solution to Exercise 2 #
a. takes countably many values, so it is discrete.
b. .
c. Using , for a constant , and for a constant :
using part b for the last step.
d. .
e. Split into consecutive intervals with widths equal to the probabilities: for , for , for , for . Draw and return the -value whose interval contains .
f. Using the intervals from part e: falls in , so ; falls in , so ; falls in , so . Then , and the sample average is .
g. Only 3 samples is far too few for the law of large numbers to give a reliable approximation of ; the estimate in part f should be treated with strong suspicion.
Solution to Exercise 3 #
a. : revenue from what is sold, minus the purchase cost of everything ordered, plus salvage revenue on leftovers ( leftover units when , 0 otherwise).
b. For : demand 50 gives profit euro; demand 45 gives euro; demand 39 gives euro. The sample average is euro, and the sample variance (dividing by ) is .
Solution to Exercise 4 #
a. A and B are uniform on , with expected duration 3; C is uniform on , with expected duration 2.
b. np.maximum(a, b) compares the two arrays run by run, giving
[3.5, 4.5, 2.5, 4.0], so finish is [4.5, 7.0, 4.0, 6.0]. The four statements
print:
the array [4.5, 7.0, 4.0, 6.0];
the average ;
finish > 5is [False, True, False, True], and its mean is the fraction ofTrue, 0.5;np.max(finish)is the largest value in the array, 7.0.
So np.maximum takes the maximum of two arrays per run and returns an array,
while np.max returns one number: the largest value of a single array.
c. . The true expected finish time is larger (flaw of averages): the project waits for the slower of A and B, and . A delay of either activity delays the project, while an early finish of one of them does not help if the other is late. (In fact , so the expected finish time is about 5.67.)
d. The deviations from the mean are , so
and
. The CI is , so
about . With only four runs this CI is not reliable: the normal
approximation needs many more runs. The width decreases with , so a ten
times narrower CI needs times as many runs: n_runs = 400.
e. (a > b).mean(). For the four runs, a > b is [False, True, False, False], which
gives 0.25. (The true probability is 0.5, by symmetry.)
Solution to Exercise 5 #
a. Inverse-CDF sampling for an exponential with mean 2 (rate ) is . This gives inter-arrival times hours.
b. Inverse-CDF sampling for is . This gives service times hours.
c. A job can only start once it has arrived and the machine is idle. Using parts a and b:
| job | arrival time | departure time |
|---|---|---|
| 1 | 0.89 | 2.62 |
| 2 | 1.34 | 4.61 |
| 3 | 3.50 | 6.20 |
| 4 | 3.99 | 8.09 |
The number of jobs at the machine over time is then a step function: 0 until 0.89, up to 1 when job 1 arrives, up to 2 when job 2 arrives (at 1.34) while job 1 is still in service, down to 1 when job 1 departs (2.62), up to 2 when job 3 arrives (3.50), up to 3 when job 4 arrives (3.99) while job 2 is still in service, down to 2 when job 2 departs (4.61), down to 1 when job 3 departs (6.20), and down to 0 when job 4 departs (8.09).
d. Integrating the step function from part c and dividing by the 10-hour horizon:
Solution to Exercise 6 #
a. The state is the stock (stock), together with whether an order is on its way.
There are two types of events: a demand (a customer arrives) and a delivery. Their
times are next_demand and next_delivery; next_delivery is np.inf when no
order is on its way.
b. It makes sure that the shop does not place a new order while an earlier order is still on its way. Without it, every demand that leaves the stock at or below the reorder point would schedule a new delivery, overwriting the time of the earlier one.
c. Following the events:
| time | event | stock after the event | remark |
|---|---|---|---|
| 0.4 | demand of 4 | 4 | |
| 0.9 | demand of 2 | 2 | : order, delivered at 1.9 |
| 1.2 | demand of 3 | 0 | stockout 1; an order is already on its way |
| 1.8 | demand of 1 | 0 | stockout 2 |
| 1.9 | delivery | 8 | |
| 2.7 | demand of 5 | 3 | : order, delivered at 3.7 |
The next demand would be at , so the loop stops. The code prints:
time 0.4: stock 4
time 0.9: stock 2
time 1.2: stock 0
time 1.8: stock 0
time 1.9: stock 8
time 2.7: stock 3
stockouts: 2 out of 5 demandsd. In the delivery event, replace stock = order_up_to by stock = stock + 6 (or
stock += 6).
e. Add lost_units = 0 before the loop, and in the stockout case add
lost_units += demand_size - stock before stock = 0 (after it, stock is 0
and the result would be wrong). Print lost_units at the end.
References¶
Koole, G. (2019). An Introduction to Business Analytics. Chapter 5, “Simulation.”