Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Monte Carlo Simulation

Open In Colab

Monte Carlo Simulation

Every December, Qahwa House orders its Ramadan gift boxes (a box of dates, a bag of the house blend and two cups) from a supplier, once, for the whole season. Nothing can be reordered once Ramadan starts, and boxes left after Eid are cleared at a loss. Order too few and customers leave empty-handed; order too many and the boxes pile up in the back room. Ramadan 2026 starts on about 18 February. How many boxes should each branch order?

The answer depends on a number nobody knows in December: the season’s demand. This reading builds a Monte Carlo simulation: it replaces the unknown input by a distribution, plays the season thousands of times, and reads the distribution of profit, not a single number. Then it uses the same method on a second decision: what price to bid for a catering contract against competitors whose bids are unknown.

After this reading you can:

  • describe an uncertain input by a distribution estimated from data;

  • write a simulation model in NumPy and read its output as a distribution;

  • find the best order quantity by simulation and check it with the critical ratio (the newsvendor model);

  • read risk from a simulation: the probability of a loss, percentiles, value at risk;

  • choose the number of runs from the standard error, and compare plans with common random numbers;

  • choose a bid that balances the chance of winning, the margin and the risk.

The data

One row per season and branch (qahwa_giftbox_demand.csv, 40 rows): the nine seasons from 2016 to 2025 (there was no season in 2020, when the shops were closed) and the 2026 season, which is the decision.

ColumnMeaning
season, ramadan_startthe year and the first day of Ramadan
branch, citywhere
forecastthe marketing team’s forecast of the branch’s demand, made in December
orderedboxes ordered from the supplier
sold, leftoverboxes sold, and boxes left after Eid
demandboxes customers asked for: sales plus the requests logged after a sell-out

The 2026 rows hold only the forecast.

Loading...

How have past orders worked out? In 2016 and 2017 each branch ordered exactly its forecast. In 2017 three branches sold out, and from 2018 the chain added a safety margin: it orders the forecast plus 20%.

Loading...
since 2018: 11,880 boxes ordered, 2,490 left over (21%), 51 requests turned away

The safety margin did its job too well: since 2018, one box in five was left over (2,490 boxes), while only 51 customer requests went unserved. Is that a good trade? It depends on what a leftover box and a missed sale each cost, and on how uncertain demand really is. A simulation answers both at once.

1. Inputs as distributions

A simulation model has three parts:

  1. inputs: the decision (here, the order) and the uncertain quantities (here, demand), each uncertain input described by a distribution;

  2. the model: the formulas that turn the inputs into an output (here, the season’s profit);

  3. outputs: the model is run many times with random draws of the inputs, and the outputs form a distribution of their own.

Where does the demand distribution come from? In December the branch has a forecast, and the data show how wrong past forecasts were. The ratio demand / forecast measures it: 1.10 means demand came in 10% above the forecast.

36 seasons and branches: demand / forecast has mean 0.955, standard deviation 0.187, from 0.61 to 1.29
<Figure size 700x340 with 1 Axes>

Two facts stand out:

  • The forecasts run high. On average demand reached 95.5% of the forecast: marketing is a little optimistic, every year.

  • They miss by about 19% (the standard deviation of the ratio), in both directions, and the histogram is roughly symmetric, so a normal distribution with the same mean and standard deviation is a reasonable model.

So for the Olaya branch, whose 2026 forecast is 640 boxes, demand is modelled as 640×R640 \times R with R∼normal(0.955,0.187)R \sim \text{normal}(0.955, 0.187):

Olaya 2026 demand: mean 611 boxes, standard deviation 120

2. A simulation model of one season

The economics of a box, from the supplier’s quote and last year’s prices:

SAR per box
selling price150
supplier’s price95
value of a box left after Eid (sold to staff and partners)20

So each box sold earns 150−95=55150 - 95 = 55 SAR, and each box left over loses 95−20=7595 - 20 = 75 SAR. The profit of a season is

profit=150×sold+20×leftover−95×order,sold=min⁡(order,demand).\text{profit} = 150 \times \text{sold} + 20 \times \text{leftover} - 95 \times \text{order}, \qquad \text{sold} = \min(\text{order}, \text{demand}).
order 640, demand 500: profit 17,000 SAR
order 640, demand 640: profit 35,200 SAR
order 640, demand 750: profit 35,200 SAR

A single run tells us little: demand could be anything around 611. So we draw 10,000 seasons of demand from the distribution, and compute the profit of each. Fixing the generator’s seed makes the results reproducible.

order 640: mean profit 27,034 SAR, standard deviation 10,225, from -23,950 to 35,200
<Figure size 700x340 with 1 Axes>

The output is a distribution with a striking shape. Whenever demand reaches 640, every box sells and the profit hits its ceiling, 55×640=35,20055 \times 640 = 35{,}200 SAR: that is the tall bar on the right, about 41% of the seasons. Below the ceiling, profit falls with demand, down to losses in the worst seasons. The mean, about 27,000 SAR, is one summary of this picture, and not the only one we need.

3. The newsvendor problem: how many to order?

A decision made once, before an uncertain demand, with a cost for each unit too many and each unit too few, is called a newsvendor problem (after a newspaper seller who buys papers in the morning). The simulation solves it directly: try every order quantity on the same 10,000 seasons and keep the one with the highest mean profit.

best order: 590 boxes, mean profit 27,594 SAR
<Figure size 700x340 with 1 Axes>

The best order is about 590 boxes: below the forecast of 640, and far below the 768 boxes the chain’s habit would order. Why below?

The critical ratio. Think about the last box of the order. It sells if demand is larger than the order, with probability P(D>q)P(D > q), and then earns the underage cost we avoided, Cu=55C_u = 55 SAR. Otherwise it is left over and loses the overage cost Co=75C_o = 75 SAR. Adding boxes pays as long as P(D>q) Cu>P(D≤q) CoP(D > q)\,C_u > P(D \le q)\,C_o, so the best order is the quantity with

P(D≤q∗)=CuCu+Co=5555+75=0.423,P(D \le q^*) = \frac{C_u}{C_u + C_o} = \frac{55}{55 + 75} = 0.423,

the 42nd percentile of demand. A leftover box costs more than a missed sale, so the best order is below the middle of the distribution.

critical ratio 0.423: q* = the 0.423 quantile of demand = 588 boxes (0.92 x the forecast)

The formula gives 588 boxes, and the simulation’s 590 agrees (it only tried multiples of 5). The simulation needed no formula, which is its strength: it still works when the model is too complicated for one.

Now compare three plans on the same 10,000 seasons:

Loading...

Three lessons in one table:

  1. The habit is expensive. Ordering the forecast plus 20% earns about 6,300 SAR less per season at Olaya alone than the critical-ratio order (21,300 against 27,600 SAR), with four times as many boxes left over.

  2. The top of the curve is flat. Ordering the forecast (640) loses only about 560 SAR against the best order. What matters most is erring on the right side: here, the low side.

  3. Selling out is not a failure. The best plan sells out in 58% of seasons. Each sell-out costs a few missed sales at 55 SAR, which is cheaper than many leftover boxes at 75 SAR.

Would the rule have worked in the past? A back-test applies each policy to the real seasons since 2018, when the chain started its safety margin:

forecast + 20% 329,700 the forecast 425,420 0.92 x forecast 434,585 Name: profit 2018-2025 (SAR), dtype: str

Over the seven seasons from 2018 to 2025 and the four branches, ordering 0.92×0.92 \times the forecast would have earned 434,585 SAR, against 329,700 SAR for the habit: about 105,000 SAR more. The rule is not a lucky draw of the simulation; it would have worked on the real seasons.

4. Reading risk, not just the mean

Two plans with the same mean can carry very different risks. A simulation measures them directly from the simulated outcomes:

  • the probability of a loss, P(profit<0)P(\text{profit} < 0): the share of simulated seasons that lose money;

  • percentiles: the 5th percentile is the profit that only the worst 5% of seasons fall below;

  • value at risk (VaR): the loss, against the mean, that is exceeded only with a small probability (5% here).

<Figure size 700x360 with 1 Axes>
order 588: mean 27,594, 5th percentile 10,240, 5% value at risk 17,354 SAR, P(loss) 1.1%
order 768: mean 21,333, 5th percentile -3,260, 5% value at risk 24,593 SAR, P(loss) 7.9%

Each curve shows, for every profit level, the share of simulated seasons that end below it: where a curve crosses the vertical line at 0 is its probability of a loss, and where it crosses the dotted line is its 5th percentile. The habit’s curve starts much further left: it earns more in a great season (it does not sell out until demand reaches 768 boxes) but its bad seasons are much worse. In one season out of twenty it falls below −3,260 SAR, a loss, while the critical-ratio order still earns at least 10,240 SAR. Its chance of a loss is 7.9% against 1.1%. Here the better plan is also the safer one; in the next section that is not the case.

5. How many runs?

A simulated mean is itself an estimate from a sample of simulated seasons. Its precision is measured, as in Week 5, by the standard error: the standard deviation of the output divided by N\sqrt{N}.

   100 runs: mean profit  27,366 SAR, standard error   831, 95% interval ± 1,630
 1,000 runs: mean profit  27,635 SAR, standard error   255, 95% interval ± 500
10,000 runs: mean profit  27,594 SAR, standard error    79, 95% interval ± 155

With 100 runs the mean is only known to within about ±1,630 SAR; with 10,000, within about ±155 SAR. A hundred times as many runs make the interval ten times narrower.

Comparing plans: common random numbers. To compare two orders, we want the difference of their mean profits. If each plan is simulated with its own random seasons, the difference carries the noise of both samples. If both plans face the same simulated seasons, most of that noise cancels, because a good season is good for both. This is why the sweep in section 3 used one demand array for every order.

order 588 minus order 640, same seasons:           560 SAR, standard error   32
order 588 minus order 640, independent seasons:    597 SAR, standard error  130

Both estimates say the critical-ratio order earns a few hundred SAR more than ordering the forecast, but with common random numbers the standard error is about four times smaller. To reach the same precision with independent draws, we would need about sixteen times as many runs.

6. A second model: bidding for a catering contract

A corporate client in Riyadh invites sealed bids for a year of coffee service at its offices in 2026. The lowest bid wins, provided it is within the client’s budget of 300,000 SAR. What should Qahwa House bid?

The uncertain inputs, from the manager’s estimates and past tenders:

InputDistribution
Qahwa House’s cost of serving the contracttriangular: minimum 180, most likely 200, maximum 260 thousand SAR (overruns are likelier than savings)
number of competitors who bideach of 4 known caterers bids with probability 0.5
a competitor’s bidtriangular: 210, 245, 290 thousand SAR

The profit is bid − cost if Qahwa House wins and 0 if it loses. A higher bid earns more when it wins, but wins less often. We simulate 10,000 tenders once, and evaluate every bid on the same tenders (common random numbers again).

Loading...
<Figure size 700x340 with 2 Axes>
highest mean profit: bid 235k, mean 13.3k, P(loss) 8.4%
best bid with P(loss) <= 5%: bid 240k, mean 13.0k, P(loss) 4.2%
the safety costs 309 SAR of mean profit

The bid with the highest mean profit is 235,000 SAR (a mean of about 13,300 SAR). But it loses money in 8.4% of tenders: it wins often, and in the seasons when the cost runs to the top of its range, the contract costs more than it pays.

The owner’s rule is that a contract may have at most a 5% chance of losing money. The best bid within that limit is 240,000 SAR: it wins half the tenders, its mean profit is about 13,000 SAR, and its chance of a loss is 4.2%. Safety costs about 300 SAR of mean profit, a price most owners would pay. The mean alone would have picked the riskier bid; the simulation shows the trade-off, and the decision maker sets the limit.

Summary

StepWhat we didQahwa House result
Inputsa distribution for each uncertain input, from datademand = forecast × normal(0.955, 0.187)
Modelprofit as a function of the decision and the inputsseason_profit(order, demand)
Outputs10,000 runs; the output is a distributionorder 640: mean about 27,000 SAR, sells out in 41% of seasons
Newsvendorsweep the decision; check with the critical ratio Cu/(Cu+Co)C_u / (C_u + C_o)0.423: order 588 boxes, not 768
RiskP(loss), percentiles, value at risk768 boxes: P(loss) 7.9%; 588 boxes: 1.1%
Precisionstandard error, common random numbers±155 SAR with 10,000 runs; differences 4 × sharper on the same seasons
Biddingthe highest mean is not always acceptable235k has the best mean but an 8.4% risk of loss; bid 240k

A simulation does not make the decision. It shows each plan’s full range of outcomes, so that the decision maker can choose with the risks in view.

Next week: decisions with several criteria (AHP and TOPSIS), and how to use analytics and AI responsibly.