<jupyter_start><jupyter_text>**Monte Carlo Simulation**we can do simulations to,well,simulate the real world in a low-risk environment.Monte Carlo simulation is by far the most widely used simulation and the concept is vey simple:try to do something n number of times and calculate probabilities of landing In a particular state.Examples:   ------*   Estimating pi*   Random Walks*   Finance: predicting stock prices*   Aeronautical Engineering:Airflow around the wings*   car safety:Crash simulations*   Medicine:spread of disease and contagions **Estimating Pi**Area of a circle is given as: pi(r^2)so, pi is essentially a ration between the area and the radius square. We can use that to estimate the value of pi using Monto Carlo simulation.We can simulate throwing darts and see which ones land within the circle and which ones land outside it.Also,if we throw darts only in the top-right quadrant, it will have the same ratio.area(circle) = pi(r^2)area(square) = (2r)^2 = 4(r^2)so, if we divide the area of circle by the area of sqaure, we get pi/4.Multiply by 4 and you get an estimate of the value of pi.<jupyter_code>import random as r
from math import sqrt
def distance(a, b):
  return sqrt(a**2 + b**2)

def estimate_pi(darts):
  inside = 0

  for i in range(darts):
    x = r.random()
    y = r.random()

    if distance(x, y) < 1.0:
      inside += 1

  pi = (inside / darts) * 4
  return pi
num_sim = int( 1e+6 )
pi = estimate_pi(num_sim)
print(pi)<jupyter_output>3.142584<jupyter_text>**Random Walks**<jupyter_code>import random

def get_action():
  return random.choice( [(0, 1), (0, -1), (1, 0 ), (-1, 0)])

def random_walk(n):
  """Return path after 'n' block random walk"""
  path = []

  x, y = 0, 0
  # y = 0
  for i in range(n):

    (dx, dy) = get_action()  # get step deltas

    x = x + dx
    y = y + dy

    path.append( (x,y) )     # create new tuple and save in the 'path

  return path

walk = random_walk(150)

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(num=None, figsize=(15,10))

point_color = list(range(150))

x_numbers = [x[0] for x in walk]
y_numbers = [x[1] for x in walk]

plt.scatter(x_numbers, y_numbers, marker=',' , s=150 , c=point_color)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10878288007133304, 'fraction_numerical': 0.02229157378510923, 'mean_word_length': 3.9210526315789473, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '511893', 'license': 'Apache-2.0', 'n_tokens_mistral': 806, 'n_tokens_neox': 750, 'n_words': 295, 'path': 'Monte_Carlo_Simulations,_Estimating_Pi,_Random_Walks.ipynb', 'repo_name': 'jkbells/Monte-Carlo-Simulations', 'size': '2243'}