Sub topic 10
Going further: Monte Carlo simulation
This chapter assumes you can compute a PERT mean and standard deviation for a task and sum them along a path. It also assumes you know how a normal-approximation confidence level is read. You should be comfortable reading short Python programs. Earlier chapters covered all of this.
Where the PERT path formula runs out
The path calculation from the previous chapters is quick and easy to audit. It also leans on three simplifications:
- The sum of many task durations is treated as roughly normal.
- Task durations are treated as independent.
- Only one path matters, usually the one with the longest expected duration.
The third simplification matters most. A project with two parallel paths of similar length finishes when the slower one finishes. The expected finish of "the slower of two uncertain paths" is later than the expected finish of either path alone. Summing means along the single longest path ignores this effect. In practice it makes the PERT answer optimistic.
Monte Carlo simulation addresses these gaps by brute force. Instead of deriving a formula for the distribution of the project duration, you run the project many times on a computer. Each run draws a random duration for every task, and you collect the resulting project durations. The collected results are an empirical distribution that you can read directly.
The idea in four steps
- For each task, pick a probability distribution built from its O, M and P values.
- Draw one random duration for every task.
- Compute the project duration for that draw. This means following the dependency network and taking the longest path.
- Repeat thousands of times, then study the list of project durations.
The name comes from the casino at Monte Carlo. The method is repeated random sampling, not anything specific to project management.
Sampling one task
PERT is built on the beta distribution. A beta distribution is bounded between a minimum and a maximum, which suits a task with a hard optimistic and pessimistic limit, and it can be skewed. The standard PERT shape parameters are:
- alpha = 1 + 4 × (M − O) / (P − O)
- beta = 1 + 4 × (P − M) / (P − O)
The beta variable lives on the range 0 to 1. You scale it to the range from O to P with O + (P − O) × x.
Check that this reproduces the PERT mean. Alpha plus beta always equals 6, so the mean of the beta variable is alpha / 6. The scaled mean is O + (P − O) × (1 + 4(M − O)/(P − O)) / 6, which simplifies to (O + 4M + P) / 6. The simulation therefore agrees with the PERT mean by construction, and the two methods differ only in how they combine tasks.
Here is the sampler from the topic's examples, run over the three-task path:
import random
def sample_pert(o, m, p):
alpha = 1 + 4 * (m - o) / (p - o)
beta = 1 + 4 * (p - m) / (p - o)
return o + (p - o) * random.betavariate(alpha, beta)
tasks = [(2, 4, 8), (3, 5, 13), (1, 2, 3)]
totals = sorted(sum(sample_pert(*t) for t in tasks) for _ in range(10000))
print(round(totals[int(0.85 * len(totals))], 1))The program sorts 10,000 simulated path totals. It then reads the value 85% of the way along the sorted list, which is the 85th percentile. In 85% of trials the path finished in that many days or fewer. The printed number changes from run to run because the draws are random. It should land close to the 14.38 days that the normal approximation gave for the same tasks. The two answers differ slightly because the second task, (3, 5, 13), is skewed to the right, and the normal approximation ignores skew.
Two details to handle in real use:
- If P equals O for a task, the formula divides by zero. Treat that task as fixed at that value.
- The input order must satisfy O ≤ M ≤ P. Validate this before sampling, because
betavariaterejects non-positive parameters and a swapped input produces nonsense.
Why not just use the normal approximation?
The normal approximation works well when a path has many tasks, no single task dominates, and the tasks are independent. It degrades when:
- The path has only a few tasks, so the sum has not yet become bell-shaped.
- One task has a very long right tail that dominates the total.
- Several paths compete to be the longest.
- You need the probability of meeting a specific date, not just one confidence level.
Simulation handles all of these with the same code. You do not need to know the formula for the distribution of a maximum or a skewed sum.
Simulating parallel paths
This is the main reason to run a simulation. The next program has two independent paths that both end at the same milestone. Path A is the three-task path from before, with a PERT mean of 12.33 days. Path B has a PERT mean of about 11.67 days, so PERT alone would call A critical and ignore B.
import random
def sample_pert(rng, o, m, p):
if p == o:
return o
alpha = 1 + 4 * (m - o) / (p - o)
beta = 1 + 4 * (p - m) / (p - o)
return o + (p - o) * rng.betavariate(alpha, beta)
def percentile(sorted_values, q):
index = min(int(q * len(sorted_values)), len(sorted_values) - 1)
return sorted_values[index]
paths = {
"A": [(2, 4, 8), (3, 5, 13), (1, 2, 3)],
"B": [(4, 6, 10), (3, 5, 9)],
}
def simulate(paths, trials=10000, seed=1):
rng = random.Random(seed)
totals = []
critical = {name: 0 for name in paths}
for _ in range(trials):
durations = {
name: sum(sample_pert(rng, *task) for task in tasks)
for name, tasks in paths.items()
}
longest = max(durations, key=durations.get)
critical[longest] += 1
totals.append(durations[longest])
totals.sort()
return totals, critical
trials = 10000
totals, critical = simulate(paths, trials)
print("mean:", round(sum(totals) / trials, 2))
print("P50:", round(percentile(totals, 0.50), 2))
print("P85:", round(percentile(totals, 0.85), 2))
print("P(done in 14 days):", sum(t <= 14 for t in totals) / trials)
for name, count in critical.items():
print("path", name, "was longest in", count / trials)Points about this program:
- It uses
random.Random(seed), a private generator. A fixed seed makes the run reproducible, so you can compare two scenarios without noise from different random draws. Change the seed to see how much the answer moves. - Each trial takes the maximum of the two path totals. Because the maximum is at least as large as path A's total in every trial, the simulated mean is at least 12.33 days. It will usually be noticeably higher, because path B sometimes wins.
- The
criticalcounter records how often each path was the longest. This is the idea behind a criticality index: the share of trials in which a path (or a task) determined the finish date. A path with a 0% PERT "criticality" can still be longest in a large share of trials.
This code assumes the paths share no tasks. Real schedules often have shared tasks or shared predecessors. In that case, sample each task once per trial. Then compute early finish times through the dependency network with a forward pass. The structure of the loop stays the same, but the project duration step becomes a network calculation instead of a sum and a max.
Reading the results
The output of a simulation is a sorted list of project durations. Each question is a different way of reading that list.
| Question | How to read it |
|---|---|
| What date has an 85% chance of being met? | The value 85% of the way through the sorted list (P85) |
| What is the chance of finishing within 14 days? | The fraction of results that are 14 or less |
| What is the typical outcome? | The median (P50) or the mean |
| How wide is the uncertainty? | The gap between P10 and P90 |
| Which path or task drives the risk? | The criticality index of each path or task |
The mean and the median differ when the result is skewed. A long right tail pulls the mean above the median. Quoting both tells a stakeholder more than one number.
A plain text histogram is often enough to see the shape. Count how many results fall into each one-day bucket and print a row of marks per bucket. A single peak with a long right tail is normal for project data. A second peak suggests two distinct scenarios, such as one path overtaking another.
How many trials?
A simulated percentile is an estimate, and it carries sampling error. The error shrinks with the square root of the number of trials, so quadrupling the trials halves the noise. For a percentile in the middle of the distribution, 10,000 trials is a reasonable default for schedule work. Tail percentiles like P95 or P99 need more trials, because few results fall that far out.
A practical check is to run the simulation twice with different seeds. If the P85 values differ by less than the precision you care about, such as half a day, the trial count is enough. If they differ by more, increase the trials.
Remember that more trials reduce only sampling error. They do nothing about error in the inputs. A million trials of a bad estimate give a very precise wrong answer.
Choosing the distribution
The beta-PERT shape is a common default, but it is a choice. Another common choice is the triangular distribution, which the standard library provides as random.triangular(low, high, mode). Note that the mode is the third argument, so a task with O=4, M=6, P=14 is sampled with random.triangular(4, 14, 6).
The two shapes differ in how much weight they give the tails:
- The triangular distribution places more probability near O and P. Its mean is (O + M + P) / 3, which is the triangular estimate from earlier in this tutorial.
- The beta-PERT distribution concentrates more probability around M. Its mean is (O + 4M + P) / 6.
For the task (4, 6, 14), the triangular mean is 8.0 and the PERT mean is 7.0. Which is better depends on how your team's estimates behave. If people give pessimistic values that really can happen, the triangular shape treats those tails more seriously. If the pessimistic value is an extreme case that is very rare, beta-PERT is more appropriate. Whichever you choose, use it consistently, and say which one produced a published number.
Making the model more realistic
The basic simulation still inherits PERT's assumption of independence, but simulation lets you relax it. Three common upgrades follow.
- Shared risk factor. Draw one project-wide multiplier per trial, for example from a triangular distribution around 1.0, and scale every task by it. A bad trial then makes all tasks slow together. This widens the result distribution, which matches the experience that things tend to go wrong together (a shared vendor, a shared team, a shared tool).
- Discrete risk events. Add an event with a probability and an impact, such as a 20% chance of a two-week delay from a failed review. Draw a uniform random number each trial, and add the delay when it falls below 0.2. Three-point ranges are a poor fit for events that either happen or do not.
- Resource limits. If two tasks need the same person, the dependency network alone does not capture the conflict. You need to add explicit links or model the resource directly.
Each upgrade adds assumptions of its own. Add one only when you can justify its inputs, and note it next to the result.
Limits of Monte Carlo
- Garbage in, garbage out. The output is only as good as the O, M and P values. Estimates that are systematically too narrow produce confidence levels that are too high.
- False precision. Reporting "P85 = 14.37 days" suggests accuracy the inputs cannot support. Round to the precision of the estimates, usually a day or half a day.
- Opacity. A formula can be checked on paper. A simulation needs code review, a fixed seed for reproducibility, and a record of the distributions used.
- Missing risks. The simulation only includes the uncertainty you modeled. Scope changes, staff turnover and unknown unknowns are absent unless you add them.
Summary of the working model
- Monte Carlo draws a random duration for each task, computes the project duration, and repeats many times.
- Beta-PERT sampling matches the PERT mean for each task, so any difference from the PERT result comes from how tasks are combined.
- The simulated mean is higher than the longest-path PERT mean when parallel paths compete, because the project waits for the slower path in every trial.
- Percentiles, probabilities of meeting a date, and criticality indexes all come from the same sorted list of results.
- Accuracy depends on the trial count for sampling noise, and on the quality of the estimates for everything else.
Exercise
A project has two independent parallel paths with PERT means of 12.0 and 11.5 days. A simulation reports a mean project duration of 12.9 days. Explain why the simulated mean is higher than 12.0, and say whether a lower value than 12.0 could ever appear in a correct simulation of this model.
Show answer
In each trial the project finishes when the slower path finishes, so the project duration is the maximum of the two path totals. The maximum is at least as large as the path with mean 12.0 in every trial, and it is sometimes larger because the 11.5-day path occasionally runs longer. The average of the maximum therefore exceeds 12.0. A mean below 12.0 cannot appear in a correct simulation, apart from small sampling noise, because the maximum is never smaller than either path.
Exercise
You run the three-task simulation with 10,000 trials and two different seeds. The P85 values are 14.3 and 14.5 days. Your stakeholders want the answer to the nearest half day. Is the trial count adequate? What would you do if the two values were 14.0 and 15.0?
Show answer
A difference of 0.2 days is smaller than the half-day precision required, so 10,000 trials is adequate. Report about 14.5 days, rounded to the nearest half day. If the values were 14.0 and 15.0, the sampling noise exceeds the required precision. Increase the number of trials, for example to 100,000, and compare seeds again. Noise falls with the square root of the trial count, so a tenfold increase cuts it by roughly a factor of three.
Exercise
A task has O=4, M=6, P=14. Write the call that samples it from a triangular distribution using the standard library, and say which mean the triangular distribution has compared with the beta-PERT distribution.
Show answer
Use random.triangular(4, 14, 6), because the arguments are low, high and mode in that order. The triangular distribution has mean (4 + 6 + 14) / 3 = 8.0 days. The beta-PERT distribution has mean (4 + 4 × 6 + 14) / 6 = 7.0 days. The triangular shape gives more weight to the long pessimistic tail.