/simulations

Using python simulations (monte carlo) for calculating pi

Primary LanguageJupyter Notebook

Simulations using Python and Monte Carlo (random samples)

Calculate Pi by using Python simulations, following the tutorial from: junilearning

Resulting plot of approximated PI:

png

Scenario

Imagine throwing darts on a squared dart board with a circle (radius circle = sidelength of square/2). All darts ending up in the circle are counted as "hits".

Math

Pi can be approximated by diving the number of "hits" by the number of samples (= # darts thrown) and multiplying with factor 4.

This follows from p(hit) = hits / # darts thrown = circle area / square area = (pi * r^2) / (4 * r^2).

Therefore: 4 * # darts in circle / # darts thrown = pi

Approach

  1. Simulate one dart by randomly generating x and y coordinates for dart.
  2. Create Loop (using 1000 samples) and record data in dataframe.
  3. Plot results.

Result

Table containing the sample throws:

dart_number hit estimated_pi
0 1 1 4.000000
1 2 1 4.000000
2 3 0 2.666667
3 4 1 3.000000
4 5 1 3.200000
... ... ... ...
995 996 0 3.136546
996 997 1 3.137412
997 998 1 3.138277
998 999 1 3.139139
999 1000 1 3.140000

1000 rows × 3 columns