Leveraging Monte Carlo simulations

Monte Carlo Simulations in Python

Izzy Weber

Curriculum Manager, DataCamp

Wide applicability

Monte Carlo simulations are used in fields such as...

  • Finance and business
  • Engineering
  • Physical sciences
Monte Carlo Simulations in Python

Stock price prediction

A graph showing simulated stock prices over time

1 https://marketxls.com/monte-carlo-simulation-excel
Monte Carlo Simulations in Python

Risk management

A graph showing VaR confidence interval

1 https://www.investopedia.com/articles/04/092904.asp https://corporatefinanceinstitute.com/course/modeling-risk-monte-carlo-simulation/
Monte Carlo Simulations in Python

Binding site identification

 

protein binding site graphic

1 https://pubs.rsc.org/en/content/articlelanding/2020/ra/d0ra01127d
Monte Carlo Simulations in Python

Reliability analysis in engineering

a graph showing safe and failure domains

1 www.researchgate.net/publication/228814883_Probabilistic_Transformation_Method_in_Reliability_Analysis
Monte Carlo Simulations in Python

Benefits of Monte Carlo Simulations

 

  • Take into consideration a range of values for various inputs
  • Show not only what could happen, but how likely each outcome is
  • Make it easy to visualize the range of possible outcomes
  • Can examine what would have happened under different circumstances

 

optimize with Monte Carlo simulations

Monte Carlo Simulations in Python

Bags of biased dice

Roll two separate dice from two bags, each containing three biased dice:

bag1 = [[1, 2, 3, 6, 6, 6], [1, 2, 3, 4, 4, 6], [1, 2, 3, 3, 3, 5]]
bag2 = [[2, 2, 3, 4, 5, 6], [3, 3, 3, 4, 4, 5], [1, 1, 2, 4, 5, 5]]

 

Simulation:

  • Pick one die from each bag randomly; roll both dice
  • Success if the dice outcomes add up to eight; otherwise, failure
  • We want to calculate the probability of success for each unique combination of dice
Monte Carlo Simulations in Python

Biased dice simulation

def roll_biased_dice(n):
    results = {}

for i in range(n): bag_index1 = random.randint(0, 2) die_index1 = random.randint(0, 5) bag_index2 = random.randint(0, 2) die_index2 = random.randint(0, 5)
point1 = bag1[bag_index1][die_index1] point2 = bag2[bag_index2][die_index2]
key = "%s_%s" % (point1, point2)
if point1 + point2 == 8: if key not in results: results[key] = 1 else: results[key] += 1
Monte Carlo Simulations in Python

Biased dice results

dice1_dice2 probability_of_success
6_2 5.54
3_5 2.67
2_6 1.45
4_4 4
Monte Carlo Simulations in Python

Biased dice results

bag1 = [[1, 2, 3, 6, 6, 6], [1, 2, 3, 4, 4, 6], [1, 2, 3, 3, 3, 5]]
bag2 = [[2, 2, 3, 4, 5, 6], [3, 3, 3, 4, 4, 5], [1, 1, 2, 4, 5, 5]]

Simulation results of 10,000 trials: a bar chart showing probabilities of dice combination success

Monte Carlo Simulations in Python

Biased dice results

bag1 = [[2, 2, 3, 4, 6, 6], [1, 2, 2, 4, 6, 6], [1, 2, 3, 3, 3, 3]]
bag2 = [[1, 2, 3, 4, 5, 6], [1, 3, 3, 4, 4, 6], [2, 2, 2, 3, 5, 5]]

Simulation results of 10,000 trials: a bar chart showing probabilities of dice combination success

Monte Carlo Simulations in Python

Limitations of Monte Carlo simulations

  • Model output is only as good as model input
  • Probability of extreme events is often underestimated
Monte Carlo Simulations in Python

Let's practice!

Monte Carlo Simulations in Python

Preparing Video For Download...