Simulating Project Estimated Costs with Monte Carlo in Python
Monte Carlo simulation is a powerful tool to analyze project costs by considering uncertainty and generating multiple possible outcomes. Here's how to use it in Python:
1. Define the Variables:
- Project Activities: Break down your project into individual activities.
- Cost Estimates: For each activity, define a range of possible costs.
- Probability Distributions: Specify how likely each cost within the range is. Common distributions include:
- Normal Distribution: For activities with symmetrical cost variations.
- Triangular Distribution: For activities with a most likely cost, and a minimum and maximum.
- Uniform Distribution: For activities with an equal chance of any cost within the range.
2. Python Code Implementation:
python import numpy as np import matplotlib.pyplot as plt
Define project activities and their cost details
activities = {
"Activity A": {"distribution": "normal", "mean": 1000, "std": 100},
"Activity B": {"distribution": "triangular", "min": 500, "mode": 600, "max": 700},
"Activity C": {"distribution": "uniform", "low": 800, "high": 1200}
}
Number of simulations
num_simulations = 10000
Initialize array to store simulated project costs
simulated_costs = []
Perform Monte Carlo simulation
for _ in range(numsimulations):
totalcost = 0
for activity, details in activities.items():
if details["distribution"] == "normal":
cost = np.random.normal(details["mean"],
details["std"])
elif details["distribution"] == "triangular":
cost = np.random.triangular(details["min"],
details["mode"],
details["max"])
elif details["distribution"] == "uniform":
cost = np.random.uniform(details["low"],
details["high"])
else:
raise ValueError("Invalid distribution type")
totalcost += cost simulatedcosts.append(total_cost)
Analyze and visualize results
plt.hist(simulated_costs, bins=50)
plt.xlabel("Project Cost")
plt.ylabel("Frequency")
plt.title("Monte Carlo Simulation of Project Costs")
plt.show()
print(f"Mean Project Cost: {np.mean(simulatedcosts)}")
print(f"Standard Deviation: {np.std(simulatedcosts)}")
print(f"5th Percentile: {np.percentile(simulatedcosts, 5)}")
print(f"95th Percentile: {np.percentile(simulatedcosts, 95)}")
3. Interpretation:
- Histogram: The histogram shows the distribution of simulated project costs, providing insights into the likelihood of different outcomes.
- Mean Project Cost: The average cost across all simulations.
- Standard Deviation: Measures the spread or variability of the cost distribution.
- Percentiles: The 5th and 95th percentiles indicate the range within which 90% of the simulated costs fall.
Explanation of Code:
- The code first defines a dictionary
activities
containing details about each project activity, including its cost distribution type and parameters. - It then sets the number of simulations to run (
num_simulations
). - Inside the loop, for each simulation, the code iterates through the activities, generating a random cost for each based on its specified distribution.
- The total cost is calculated by summing the costs of all activities.
- After all simulations are complete, the code analyzes the results using statistical functions like
mean
, std
, and percentile
. - Finally, it plots a histogram of the simulated costs for visual representation.
Benefits of Monte Carlo Simulation:
- Uncertainty Analysis: Captures the uncertainty inherent in cost estimates.
- Risk Assessment: Identifies potential cost overruns and helps prioritize mitigation strategies.
- Decision Making: Provides a more informed basis for project decisions by considering multiple scenarios.
Key Considerations:
- Accurate Input Data: Ensure the cost estimates and distributions reflect realistic possibilities.
- Independence of Activities: Ensure costs of different activities are independent.
- Number of Simulations: Increase the number of simulations for a more accurate and reliable analysis.
By implementing Monte Carlo simulation, you can gain valuable insights into the potential costs of your project and make more informed decisions.