Monte Carlo simulation, also known as the Monte Carlo method or multiple probability simulations, is a mathematical tool used to estimate the possible outcomes of uncertainty in an event. John von Neumann and Stanislaw Ulam first invented this method during world war 2 to improve decision making under uncertain conditions. It was named after a city of casinos called Monaco because the principle of randomness mimics a roulette game. The simulation has assessed the impact of risk in many real-time situations like artificial intelligence, sales forecasting, stock price, project management and pricing.

The simulation is based on statistical systems and uses the randomness in a defined system to evolve and approximate quantities without needing to solve the system analytically. The main concept implied in this method is that a point in a moving system will eventually visit all parts of the space that the system moves in a uniform and random sense. The model tries to predict by using a range of values in a problem domain rather than a specific input.

#### THE BELAMY

##### Sign up for your weekly dose of what's up in emerging technology.

Eventually, this method leverages probabilities such as normal, gaussian, and uniform for any variable with uncertainty. Based on the number of trials you have mentioned, this process of using random values in a domain is repeated. General assumption says the higher the number of trials, the higher likelihood of converging a specific value. Once you do all the simulations, you will develop possible outcome values with associated probability values for each result.

This simulation is also utilised for a long term prediction due to its accuracy. As the number of input increases, the number of forecasts also grows, allowing you to project outcomes farther out in time with more accuracy. When the simulation is completed, it yields a range of possible outcomes with the probability of each result.

One simple example for this simulation can be considered as calculating the probabilities of standard two rolling dice. There are a total of 36 combinations of dice rolls. Based on this, we can manually compute the probability of a particular outcome. Using this simulation, we can simply do the rolling as many times as possible to achieve more accurate predictions.

Today in this article, we will try to address the so-called famous brain teaser problem called the Monty Hall problem. The problem is like, suppose you are in a game show and you are given a choice of three doors. Behind one door is a Plane rest of the other is a car. Say you have chosen the 1st door, and the host knows what is behind all the doors, and he opens the second door, which has a car. The further host gives you another chance to switch your selection, so now you might come under a dilemma to switch or not. Here comes the application of the Monte Carlo simulation method, which will give you the probability score for switching doors and not switching doors. And we will implement this with python.

#### Implementing Monte Carlo Simulation:

```
import matplotlib.pyplot as plt
import random
```

Let’s discuss the above methods in the language of probability as it is the base for this simulation;

When the host asks you to choose the door for the first time, there is an equal probability that there is a plane behind the door i,e. ⅓ and this implies that ⅔ of times your guesses are wrong. So now the host comes and opens the door to reveal the car, and at this time, his choice of opening the door is not random; your previous choice governs it as he decided not to reveal a plane while opening the door.

Now let’s take two cases; the first one is that your first choice is correct, then the remaining two doors have a car behind them. In this scenario, the host can open those two-door. So now, if you change your initial choice you will definitely lose. But remember this happens the only ⅓ of the time because your initial choice is correct many times.

Now consider that your first choice is wrong, now out of two doors, one has a plane behind it, and one has a car behind it. Now the host cannot open the door randomly, so now he was forced to open the door car behind it as he decided not to reveal a door with a plane behind it. If you change your initial choice, you will win. If we look back to the initial probabilities, we can realize that this is the most likely scenario since ⅔ of times our initial guess is wrong.

So statistically, you will win if you change your choices. So when you play this game infinitely ⅔ times, you will win provided that each interval you change the choice.

## Code Implementation: Monte Carlo Simulation

Implement the above with python;

```
doors=['car','car','plane']
def monte_carlo(n):
switch_win_probability=[]
stick_win_probability=[]
switch_win=0
stick_win=0
for i in range(n):
random.shuffle(doors)
k=random.randrange(3)
if doors[k]!='plane':
switch_win=switch_win+1
else:
stick_win=stick_win+1
switch_win_probability.append(switch_win/(i+1))
stick_win_probability.append(stick_win/(i+1))
plt.axhline(y=0.66,color='r')
plt.axhline(y=0.33,color='b')
plt.plot(switch_win_probability)
plt.plot(stick_win_probability)
plt.xlabel('no of event')
plt.ylabel('Probability')
print('Switch winning prob--->',switch_win_probability[-1])
print('Stick winning prob--->',stick_win_probability[-1])
print('You will win {} times out of {} if you switch the door'.format(switch_win,n))
print('You will win {} times out of {} if you stick at door'.format(stick_win,n))
```

The above function takes the number n as an argument which indicates the reputation you want to perform. And in the for random loop function shuffle the list of the doors for each event based on its function to check whether there is a plane for selection that has a function chosen using random. Based on all these winning probabilities for switching the door and sticking at the door is calculated.

Below is the result above simulation for many runs;

```
monte_carlo(500)
```

Run for 1000 times;

Run for 10,000 times;

## Conclusion:

In this tutorial, we have learned how to perform a Monte Carlo simulation on a problem like Monty hall, where the position of targeted outcome changes regularly. This method of simulation is more powerful when your processes are dependent on the long run. Here I have also given the results of this simulation for various runs.