Last Updated on December 3, 2026 by Rajeev Bagra
From MIT’s Python Programming Lessons to a Real-World Business Problem
Can we use Python to estimate how much revenue a website might generate next month?
Suppose you operate a website that earns money through Google AdSense and affiliate marketing.
Your monthly revenue depends on several uncertain factors:
- Number of website visitors.
- Number of pages viewed.
- Advertising revenue per thousand pageviews.
- Percentage of visitors clicking affiliate links.
- Percentage of affiliate clicks resulting in purchases.
- Commission earned from each purchase.
These variables change over time. Consequently, calculating revenue using a single set of assumptions may not provide a realistic picture of what could happen.
Python offers an interesting solution: simulation.
Instead of calculating just one possible outcome, we can use random numbers to generate thousands of possible business scenarios and analyse their results.
This technique is commonly known as Monte Carlo simulation.
1. Learning Simulation from MIT
In Lecture 26 of MIT’s 6.100L Introduction to Computer Science and Programming Using Python, Professor Ana Bell introduces computational simulations.
The central idea is straightforward:
- Observe a real-world event.
- Design a computational experiment representing that event.
- Repeat the experiment many times.
- Record the outcomes.
- Calculate the result of interest.
The lecture demonstrates this using dice experiments.
For example, suppose we want to estimate the probability of obtaining a particular face when rolling a die.
We can represent the possible outcomes using a Python list:
import random
dice = ['.', ':', ':.', '::', '::.', ':::']
roll = random.choice(dice)
print(roll)
The random.choice() function selects one element from the list.
We can repeat this experiment 10,000 times and count how frequently a particular outcome occurs.
import random
dice = ['.', ':', ':.', '::', '::.', ':::']
simulations = 10000
count = 0
for i in range(simulations):
roll = random.choice(dice)
if roll == '::':
count += 1
probability = count / simulations
print(probability)
This is our first simulation.
Notice the importance of the for loop. Instead of performing the experiment manually, Python repeats it 10,000 times.
We then calculate the proportion of experiments that produced our desired outcome.
The same principle can be applied to much more complicated problems.
MIT learning resources:
Lecture 26:
Lecture video:
2. Applying Simulation to a Business Problem
Let’s move from dice to a website.
Imagine that a website receives approximately 4,000 visitors per month.
However, monthly traffic is not always the same. It might be 3,000 visitors in one month and 5,000 in another.
We also cannot assume that advertising RPM and affiliate conversion rates will remain constant.
Our objective is to estimate possible monthly revenue.
We will model five variables:
| Variable | Example assumption |
|---|---|
| Monthly visitors | 2,000–6,000 |
| Pages per visitor | 1.8 |
| AdSense RPM | ₹80–₹220 |
| Affiliate click rate | 3%–6% |
| Affiliate conversion rate | 2%–4% |
These are illustrative assumptions, not actual website statistics.
3. Generating Random Business Conditions
Python provides several functions for generating random numbers.
For example:
import random
visitors = random.randint(2000, 6000)
print(visitors)
This generates an integer between 2,000 and 6,000.
We can also generate decimal values:
rpm = random.uniform(80, 220)
print(rpm)
This generates a random advertising RPM between ₹80 and ₹220.
Another useful function is:
random.triangular(2000, 6000, 4000)
This generates a random number using three parameters:
- Minimum: 2,000.
- Maximum: 6,000.
- Most likely: 4,000.
This is useful when we have an estimated most likely value but also want to account for higher and lower possibilities.
4. Calculating Advertising Revenue
Suppose a website receives 5,000 pageviews and earns ₹150 per thousand pageviews.
Advertising revenue is calculated as:
Therefore:
Revenue=₹750
In Python:
pageviews = 5000
rpm = 150
adsense_revenue = (
pageviews / 1000
) * rpm
print(adsense_revenue)
5. Calculating Affiliate Revenue
Now suppose the same website receives 4,000 visitors.
Assume:
- 5% click affiliate links.
- 3% of affiliate clicks result in purchases.
- Average commission is ₹800.
First, calculate affiliate clicks:
Next, calculate purchases:
Finally, calculate commission:
6 x 800=₹4800
The Python implementation is:
visitors = 4000
click_rate = 0.05
conversion_rate = 0.03
commission = 800
clicks = visitors * click_rate
sales = clicks * conversion_rate
affiliate_revenue = sales * commission
print(affiliate_revenue)
We now have two sources of revenue: advertising and affiliate marketing.
6. Combining Everything into a Simulation
The next step is to combine these calculations and repeat them thousands of times.
Here is a simplified Monte Carlo simulation:
import random
import statistics
SIMULATIONS = 10000
revenues = []
for i in range(SIMULATIONS):
# Generate random website traffic
visitors = int(
random.triangular(2000, 6000, 4000)
)
# Generate random advertising RPM
rpm = random.uniform(80, 220)
# Generate random affiliate assumptions
click_rate = random.uniform(0.03, 0.06)
conversion_rate = random.uniform(0.02, 0.04)
commission = random.uniform(400, 1000)
# Calculate advertising revenue
pageviews = visitors * 1.8
adsense = (pageviews / 1000) * rpm
# Calculate affiliate revenue
clicks = visitors * click_rate
sales = clicks * conversion_rate
affiliate = sales * commission
# Combine revenue sources
total_revenue = adsense + affiliate
revenues.append(total_revenue)
# Analyse the simulated outcomes
average = statistics.mean(revenues)
median = statistics.median(revenues)
print("Average revenue:", round(average, 2))
print("Median revenue:", round(median, 2))
What is happening here?
The for loop runs 10,000 times.
During each iteration, Python generates a new combination of visitors, advertising RPM, affiliate click rate, conversion rate and commission.
It then calculates the resulting revenue and stores it in the revenues list.
After 10,000 experiments, we have 10,000 possible revenue outcomes.
We can calculate their average and median.
This is the essence of Monte Carlo simulation.
7. What Is the Probability of Achieving a Revenue Target?
Suppose our business wants to generate at least ₹15,000 per month.
We can examine our 10,000 simulated outcomes and count how many reach that target.
target = 15000
successful = 0
for revenue in revenues:
if revenue >= target:
successful += 1
probability = successful / len(revenues)
print(
"Probability of reaching target:",
round(probability * 100, 2),
"%"
)
Suppose 4,000 of our 10,000 experiments generate revenue of ₹15,000 or more.
Our estimated probability under the model would be:
This does not guarantee that the website will achieve the target.
It tells us that 40% of the simulated scenarios achieved it under our chosen assumptions.
8. Understanding Percentiles
Average revenue alone does not tell us everything.
Suppose we want to understand the lower and upper ranges of possible outcomes.
Python provides the NumPy library for this purpose.
import numpy as np
p10 = np.percentile(revenues, 10)
p50 = np.percentile(revenues, 50)
p90 = np.percentile(revenues, 90)
print("10th percentile:", p10)
print("Median:", p50)
print("90th percentile:", p90)
The 10th percentile indicates that approximately 10% of simulated outcomes fall at or below that value.
The 50th percentile is the median.
The 90th percentile indicates that approximately 90% of outcomes fall at or below that value.
These measurements help us understand the distribution of possible revenue rather than relying on one average.
9. Why Simulation Is More Useful Than a Single Calculation
Consider two approaches.
Approach A: Ordinary Calculation
We assume:
- 4,000 visitors.
- ₹150 RPM.
- 5% affiliate click rate.
- 3% conversion rate.
- ₹800 commission.
We calculate one revenue figure.
Approach B: Simulation
We allow these variables to change within specified ranges.
We repeat the experiment 10,000 times.
We then examine:
- Average revenue.
- Median revenue.
- Lower-end outcomes.
- Higher-end outcomes.
- Probability of reaching a revenue target.
The second approach provides a much richer understanding of uncertainty.
This is why simulation is particularly useful when several variables influence the outcome.
Website Revenue Simulation Lab
Change the assumptions and explore 10,000 possible months.
—
Probability of revenue exceeding monthly costs: —
Estimate affiliate earnings from traffic, click rate, conversion rate and commission.
This calculator shows expected values. Actual purchases are whole transactions and may vary.
10. An Important Lesson from MIT’s Water-Filling Example
Lecture 26 also presents a fascinating simulation involving a swimming pool.
A pool needs 600 gallons of water, and the water flow rate varies randomly between one and three gallons per minute.
The lecture demonstrates that the average filling time is not simply obtained by dividing the pool capacity by the average flow rate.
Why?
Because filling time is inversely proportional to flow rate.
The formula is:
A lower flow rate produces a disproportionately longer filling time.
This illustrates an important principle: applying a mathematical formula to average inputs does not always give the average output.
The same issue arises in business modelling. We should be careful when estimating revenue using averages, especially when several uncertain variables interact.
Simulation allows us to examine these interactions computationally.
11. Limitations of Simulation
A simulation does not automatically predict the future.
Its results depend on the assumptions we provide.
For example:
- If traffic estimates are unrealistic, revenue estimates will also be unrealistic.
- If affiliate conversion rates change significantly, the model may become inaccurate.
- If advertising RPM varies seasonally, a simple random range may not capture that behaviour.
- If multiple websites experience the same search engine update, their traffic changes may be correlated.
A more sophisticated simulation can incorporate historical data, seasonal patterns and relationships between variables.
For now, our model is an educational experiment that demonstrates the underlying technique.
Conclusion: Turning Business Questions into Computational Experiments
One of the most interesting lessons from MIT’s Python course is that programming is not merely about writing instructions for a computer.
It is also about representing real-world problems in a form that a computer can experiment with.
We started with a simple business question: How much revenue might a website generate next month?
We identified the variables, introduced randomness, wrote a simulation, repeated the experiment 10,000 times and analysed the results.
This is the practical value of Monte Carlo simulation.
Instead of relying entirely on intuition or a single estimate, we can use Python to explore thousands of possible scenarios.
Further Learning:
MIT OpenCourseWare – 6.100L, Lecture 26:
Lecture video:
An educational exploration of Python programming, probability and business simulation by Digital Splendid.
Discover more from Progaiz.com
Subscribe to get the latest posts sent to your email.


Leave a Reply