# Monte Carlo Simulation in Finance Python Part-1 ## What is Monte Carlo simulation?

Monte Carlo simulation is used to model different outcomes using probability. It is a probability distribution of possible outcomes to get an idea about what to expect in future. It is mainly used in scenarios where outcome of a process can’t be easily predicted.

For better understanding lets take a classical example :

## Estimating PI(π) using Monte Carlo method.

Calculation of PI doesn’t seems to challenging but estimation using MC method gives us insight of how the model actually works.

The solution of the problem can be simplified by a practical example of throwing darts. We will take a square shaped card board and simulate dart hitting points using computational methods.

```import numpy as np
import matplotlib.pyplot as plt

N = 1000

x = np.random.uniform(-1, 1, size=[N, 1])
y = np.random.uniform(-1, 1, size=[N,1])

plt.scatter(x,y);```

we generate two arrays of uniform distributed numbers where x represent X axis and y represent Y axis.

The results of the code will looks like this:-

The darts are pretty much uniformly distributed as above the case.

To estimate PI we will take help of Trigonometry. As we know the area of a circle is = π*r² where r is radius of the circle whereas the area of a square = a² where a is length of edge.

We can find out the value of π by formula :- 4 * Area of circle/Area of square

= 4 * π*r²/a²

= π

where a = 2*r , or r = a/2

Lets implement the concept in our python code.

```inside = (x**2 + y**2) < 1
in_x = x[inside]
in_y = y[inside]
plt.figure(figsize = [8,8]);
plt.scatter(x,y)
plt.scatter(in_x, in_y, color = ‘r’)```

output:-

As by the results it seems like we have successfully drawn a circle with radius r=a/2 = 1

Lets estimate the value of π :

π = 4 * number of darts captured by the circle / total number of darts

```pi_val = 4 * (inside.sum()/ N)
print(pi_val)```

Execution of the above line will present estimate value of π.

In this case the estimated value is around 3.256 where we know the value of PI is around 3.14….. .

Increasing the value of N (darts) helps the model to converse to closest estimation.

In case of N = 10000 .

The Image looks like a Traffic sign. 😀

pi_val = 3.1498

The estimation is very close to actual value of PI. In the end models turn out to be very accurate in estimating value of PI.

Final Python Code:

```import numpy as np
import matplotlib.pyplot as plt
N = 10000
x = np.random.uniform(-1, 1, size=[N, 1])
y = np.random.uniform(-1, 1, size=[N,1])
plt.scatter(x,y);
inside = (x**2 + y**2) < 1
in_x = x[inside]
in_y = y[inside]
plt.figure(figsize = [8,8]);
plt.scatter(x,y)
plt.scatter(in_x, in_y, color = 'r')
pi_val = 4 * (inside.sum()/ N)
print(f'Estimated PI value : {pi_val}')```

Now the question arises how can we use the model in finance ?

Can the model estimate stocks return ?

# Wrapping Things Up

In this tutorial, we learned how to perform Monte Carlo simulations for estimating PI value.

Check back soon for Part 2 of this tutorial where we’ll go over how to use the model in finance and trading.

Good luck, and as always feel free to drop questions and comments 🙂