0% found this document useful (0 votes)
26 views5 pages

Practical 5

The document outlines a Python program that uses the Monte Carlo method to estimate the value of π through random sampling within a unit square. It explains key concepts of the Monte Carlo method, its applications in various fields, and provides code for simulating the estimation of π for different sample sizes. The results indicate that larger sample sizes yield more accurate estimates of π.

Uploaded by

nisargbhatt.n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Practical 5

The document outlines a Python program that uses the Monte Carlo method to estimate the value of π through random sampling within a unit square. It explains key concepts of the Monte Carlo method, its applications in various fields, and provides code for simulating the estimation of π for different sample sizes. The results indicate that larger sample sizes yield more accurate estimates of π.

Uploaded by

nisargbhatt.n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

MA262-SNTD24IT179

Week 5: Monte Carlo Method and Applications


Write a Python program that estimates the value of π using random sampling within
a unit square.

● Generate random points within a 1x1 square.


● Count how many points fall inside the quarter-circle.
● Estimate π using the formula: pi = 4 * (number of pints inside circle/number of points
outside circle)
● Run the simulation for different sample sizes (10,000, 100,000, 1,000,000) and analyze the
accuracy.

Introduction to the Monte Carlo Method


The Monte Carlo method is a powerful statistical technique that uses random sampling to
solve mathematical problems. Named after the Monte Carlo casino in Monaco, where
randomness plays a central role, this method relies on the generation of random numbers to
simulate and approximate complex systems and processes. It is particularly useful for problems
involving uncertainty, variability, or systems that cannot be solved analytically.
Key Concepts of the Monte Carlo Method:
1. Random Sampling:
The core idea behind Monte Carlo is to use random numbers to simulate the behavior of
a system or process. Instead of solving a problem using traditional deterministic
methods (which involve fixed inputs and exact outputs), Monte Carlo relies on repeated
random sampling to approximate solutions.
2. Statistical Inference: The method typically involves running a simulation many times,
gathering statistical data from the results, and then making inferences based on this data.
As the number of samples increases, the approximation becomes more accurate.
3. Random Variables: Random variables are used to model uncertain outcomes
in a system. They can be either discrete (taking specific values, such as integers) or
continuous (taking any value within a range, such as real numbers).

Applications of Monte Carlo Method:


Monte Carlo simulations are used in various fields and for various purposes, such as:

CSPIT-IT 1
MA262-SNTD24IT179

1. Finance and Risk Analysis

● Option Pricing: Monte Carlo simulations are widely used in financial modeling, particularly in
pricing options (such as European, American, and Asian options). The method can model the
random behavior of asset prices over time and calculate the expected payoff.

2. Physics and Engineering

● Particle Transport and Radiation: In nuclear physics, the Monte Carlo method is used for
simulating the behavior of particles (like neutrons, photons, or electrons) as they move
through materials, helping in radiation shielding and dosimetry.

3. Machine Learning

● Markov Chain Monte Carlo (MCMC): Used in Bayesian inference, MCMC methods generate
samples from probability distributions and are applied in a wide range of machine learning
tasks, including training probabilistic models and approximate posterior inference.

One of the simplest examples of using the Monte Carlo method is estimating the value
of π. This can be done by simulating random points within a square and counting how
many fall inside a quarter-circle inscribed in that square. The ratio of points inside the
circle to the total number of points is proportional to the area of the circle, which is
related to π.
Why is Monte Carlo Important?
Monte Carlo is important because it provides a simple and flexible approach to problems that
are difficult or impossible to solve analytically. Instead of relying on complex formulas or exact
solutions, Monte Carlo allows for the estimation of answers based on the probability of
random events, making it applicable in a wide range of fields.
Its strengths include:

● Ability to handle complex, high-dimensional problems.


● Works well for problems with uncertainty or randomness.
● Easy to implement using basic random sampling techniques.
● Can be adapted to a variety of applications, from physical simulations to financial
modeling.

Code : -
import random
import math
import matplotlib.pyplot as plt
def estimate_pi(sample_size):
inside_circle = 0

CSPIT-IT 2
MA262-SNTD24IT179

for _ in range(sample_size):
x = random.random()
y = random.random()

if x**2 + y**2 <= 1:


inside_circle += 1

pi_estimate = 4 * (inside_circle / sample_size)


return pi_estimate

def run_simulation(sample_sizes):
results = {}
for size in sample_sizes:
pi_estimate = estimate_pi(size)
results[size] = pi_estimate
return results

sample_sizes = [10000, 100000, 1000000]

results = run_simulation(sample_sizes)

for size, pi_estimate in results.items():


print(f"Estimated Pi for sample size {size}: {pi_estimate}")

plt.plot(sample_sizes, list(results.values()), marker='o',


label='Estimated Pi')
plt.axhline(y=math.pi, color='r', linestyle='-', label='Actual Pi')
plt.xscale('log')
plt.yscale('linear')
plt.xlabel('Sample Size (log scale)')
plt.ylabel('Estimated Pi')
plt.title('Monte Carlo Estimation of Pi')
plt.legend()
plt.show()

Output : -

CSPIT-IT 3
MA262-SNTD24IT179

CSPIT-IT 4
MA262-SNTD24IT179

The program runs the Monte Carlo simulation for three different sample sizes:
10,000, 100,000, and 1,000,000, and estimates π for each. Here's an explanation of
how the results may vary:
● Sample Size: 10,000
With a sample size of 10,000, the estimate of π may not be very close to
the actual value because the number of points is relatively small, and the
randomness can cause deviations.
● Sample Size: 100,000
Increasing the sample size to 100,000 should improve the estimate of π. The
larger number of points better approximates the true ratio of points inside the
quarter-circle to total points, leading to a more accurate estimation.
● Sample Size: 1,000,000
With 1,000,000 points, the estimate of π should be quite close to the actual value
of π (3.14159...), as the larger sample size reduces the impact of random
fluctuations.

CSPIT-IT 5

You might also like