Topic 7 Lesson 3
Summary
TLDRThis lesson introduces Monte Carlo simulation, a method for approximating probabilities through random sampling and averaging. It creatively applies this technique to estimate the value of pi by randomly generating points within a unit square and determining how many fall within an inscribed unit circle. The ratio of points inside the circle to the total points, multiplied by 4, provides an approximation of pi. The script guides through the algorithm's implementation using Python, highlighting the process and potential error in approximation.
Takeaways
- 🎲 Monte Carlo simulation is a probabilistic technique used to approximate the probability of events through random sampling and averaging results.
- 📐 The script introduces the concept of approximating the value of pi using Monte Carlo simulation with a unit circle inscribed in a unit square.
- 🔍 The area of the unit circle (π * radius squared) is used to relate to the probability of a random point within the square falling inside the circle.
- 🔢 The ratio of the area of the unit circle to the unit square is π/4, which is key to approximating pi through probability.
- 📊 Monte Carlo simulation involves generating 'n' random points uniformly in the unit square and counting how many fall inside the unit circle.
- 📍 To choose a point uniformly at random in the unit square, both the x and y coordinates are selected randomly within the interval [-1, 1].
- 🔄 The process is repeated 'n' times to approximate pi by taking the ratio of points inside the circle (m) to the total points (n) and multiplying by 4.
- 👨💻 An implementation of the function 'approximate_pi_of_N' is suggested, which uses the Monte Carlo method to estimate pi given 'n' random points.
- 📈 The approximation of pi improves as 'n' increases, demonstrating the randomized algorithm's effectiveness for large sample sizes.
- 🔬 The script includes a practical example of using the function with 10,000 points to approximate pi and compare the error to the actual value.
- 👀 Each run of the program yields a different approximation of pi due to the random nature of the Monte Carlo simulation, illustrating the variability in results.
Q & A
What is Monte Carlo simulation?
-Monte Carlo simulation is a technique used to approximate the probability of an event by performing random sampling multiple times and averaging the results.
How does Monte Carlo simulation relate to estimating the value of pi?
-Monte Carlo simulation can be used to estimate the value of pi by generating random points within a unit square and determining the proportion that fall within the inscribed unit circle, using the ratio of their areas.
What is the significance of the unit circle in the context of approximating pi?
-The area of the unit circle is π * radius squared, with the radius being one. This area can be related to the probability of a random point in the unit square belonging to the circle, which helps in approximating pi.
How is the area of the unit square calculated?
-The area of the unit square is calculated by multiplying the length of one side by itself. Since the edge length is 2, the area is 2 * 2, which equals 4.
What is the ratio of the area of the unit circle to the area of the unit square?
-The ratio of the area of the unit circle to the area of the unit square is pi over 4, which is used to relate the probability of a point being inside the circle to pi.
How is a random point generated within the unit square for the Monte Carlo simulation?
-A random point is generated by choosing its X and Y coordinates independently and uniformly at random from the interval [-1, 1].
What is the event in the Monte Carlo simulation setup for approximating pi?
-The event is that a randomly generated point within the unit square falls inside the inscribed unit circle.
How is the approximation of pi calculated from the simulation results?
-The approximation of pi is calculated by taking the ratio of the number of points inside the circle (m) to the total number of points generated (n), and then multiplying by 4.
What is the role of the parameter 'n' in the Monte Carlo algorithm for approximating pi?
-The parameter 'n' represents the number of random points generated in the unit square. A larger 'n' generally results in a better approximation of pi.
How can the approximation of pi be compared to the actual value?
-The approximation of pi obtained from the Monte Carlo simulation can be compared to the actual value of pi by calculating the absolute difference between them.
What is the expected output of the Monte Carlo simulation for approximating pi after one run?
-The output is an approximation of pi, such as 3.1452 in the script example, along with the error, which is the absolute difference from the actual value of pi.
Outlines
🔍 Monte Carlo Simulation to Approximate Pi
This paragraph introduces the concept of Monte Carlo simulation, a method used to estimate probabilities through random sampling and averaging. The focus is on using this technique to approximate the value of pi. The setup involves a unit circle inscribed in a unit square, and the key observation is that the ratio of the area of the circle to the square is pi/4. The Monte Carlo method involves generating n random points uniformly in the square, counting how many fall within the circle, and using this ratio to approximate pi. The process is detailed, including how to choose points uniformly at random and how to check if they lie within the circle. The function 'approximate pi of N' is described, which uses this algorithm to provide an approximation of pi. The paragraph concludes with an example of how to use the function and compare the approximation to the actual value of pi.
🔢 Implementing the Monte Carlo Pi Approximation Algorithm
This paragraph delves into the implementation details of the Monte Carlo simulation algorithm for approximating pi. It starts by initializing a counter m to zero, which will be used to count the number of points that fall within the unit circle. The algorithm involves a loop that runs n times, generating random points (x, y) within the interval [-1, 1]. For each point, the algorithm checks if the distance squared from the origin (x^2 + y^2) is less than or equal to 1, indicating that the point is inside the circle. If so, the counter m is incremented. After the loop, the approximation of pi is calculated by multiplying the ratio of m to n by 4. The paragraph also includes an example output from running the program, showing an approximation of pi and the error in the approximation. The emphasis is on the variability of the results due to the random nature of the Monte Carlo method.
Mindmap
Keywords
💡Monte Carlo simulation
💡Probability
💡Unit circle
💡Unit square
💡Random sampling
💡Area
💡Approximation
💡Random point
💡Distance
💡Algorithm
💡Error
Highlights
Monte Carlo simulation is introduced as a technique to approximate the probability of an event through random sampling and averaging results.
The concept of estimating the probability of an event by sampling multiple times and averaging is explained.
A probabilistic experiment setup is described to estimate the area ratio of a unit circle to a unit square, relating to the value of pi.
The area of the unit circle is shown to be π, and the area of the unit square is 4, leading to the ratio π/4.
The probability that a random point in the unit square belongs to the unit circle is equated to pi/4.
The Monte Carlo simulation setup for approximating pi involves generating n random points in the unit square.
The number of points inside the unit circle, m, is used to approximate the probability P of the event.
The approximation of pi is obtained by multiplying the ratio m/n by 4.
A method for choosing a point uniformly at random in the unit square by selecting its coordinates independently in the interval [-1, 1] is described.
The process of repeating the random point selection n times to approximate pi is outlined.
The function 'approximate pi of N' is introduced to return an approximation of pi using the Monte Carlo algorithm.
The use of Numpy.random and the math module to compare the approximation with the actual value of pi is mentioned.
A loop is used to generate n uniformly random points in the unit square for each iteration of the Monte Carlo simulation.
The condition to check if a point belongs to the unit circle is explained by comparing the distance squared to one.
A counter m is initialized and incremented when a point is inside the unit circle to track the event occurrences.
The final approximation of pi is calculated by returning 4 times the ratio of m to n.
The variability of the approximation of pi with each run of the program is highlighted, showing the algorithm's randomness.
An example output of the approximation Pi 3.1452 with an error of around 0.0036 is provided.
Transcripts
In this lesson, we will study
Monte Carlo simulation and
namely, approximating pi.
Monte Carlo simulation is a technique
used to approximate the probability of
an event by random sampling multiple
times and averaging the results.
The set up is a probabilistic experiment.
we Would like to estimate the
probability of a certain event.
How do you do that?
We sample a number of time and we average
that is the approximate probability of
the event is the number of outcomes
which satisfy the event divided
by the total number of outcomes.
We'll see in this lesson how Monte Carlo
simulation can be used to solve the problem.
That is not inherently stochastic and
namely approximating the value of pi
Consider the unit circle
inscribed in the unit square.
This is the circle of radius one,
and this is the square of edge length 2.
So this is the origin.
And this is a point (-1,0),(1,0)
and here (0,1) and here (0,-1)
The key observation is that the
area of the unit circle is π * raduis squared
R is one, so this is π * 1 squared,
which is π. To relate this to the
probability of an event consider,
the area of the unit square,
the edge length is 2,
so the area of the unit square is 2 * 2,
which is 4. Therefore,
the ratio area of the unit circle over
the area of unit square is pi over 4.
Now we need to relate this to
the probability of an event.
This is actually the probability
that a random point of the unit
square belongs to the unit circle.
Therefore pi is 4 * P,
where P is the probability of the
event that a random point of the unit
square belongs to the unit circle.
Now in the setup of Monte Carlo simulation,
to estimate the probability of the event,
this event would generate n points
uniformly at random in the unit square.
We count the number m of those
points inside the unit circle.
We take the ratio.
This is an approximation of P.
To get the approximation of
pi we multiply by 4.
How do you choose one point
uniformly at random in the unit square?
We choose its X coordinate uniformly
at random in the interval minus one
one random real number and
its y coordinate uniformly at
random in this interval. we do so in an
independent fashion so we choose
n points uniformaly at random.
we Repeat this process n time
That is to approximate pi first
we choose n points by choosing
their coordinates uniformly at
random in the unit square,
and we do so by choosing each xi
and each yi uniformly at random,
in the interval minus one one.
We find the number m of
points in the unit circle
We take the ratio m over n
and we multiply by 4 and this is
our desired approximation of π.
Now n is a parameter of this
randomized algorithm for large
n we get good approximations of π.
implement the function
approximate pi of N which given n
Returns an approximation of pi
using this randomized algorithm,
which is based on Monte Carlo simulation.
First, we import Numpy.random
then we import the
value of π from the math module,
because we'd like to compare our
approximation of Pi with the actual value of Pi.
this is how we use the function
we call approximate Pi on n equal,
let's say 10,000 it returns pihat
Here we print pihat and here we
print the absolute value of the error.
the Absolute value of the
difference pi minus pihat
Now, another algorithm
would like to sample n times,
so we have a loop for i in range n and
at each iteration we need to generate the
uniformly random points in the unit square.
So we use the random uniform function.
we generate x uniformly at random in
the interval -1, 1. similarly for y
Now we need to check whether the
point XY belongs to the unit circle.
to do so,
we simply compare the distance between
the origin and the point XY to one,
or equivalently the square
of this distance to one.
So if X ^2 + y ^2 is less or equal to 1,
we need to increment some
counter m initialized to 0.
So before the loop initialize a
counter m to zero and in the if condition.
Here we increment m and at the
end we return 4* m / n.
Each time we run this program,
we get a different approximation of pi
This is the output of one run.
Approximate Pi 3.1452
and the error is around 0.0036.
Посмотреть больше похожих видео
Topic 3 Lesson 9
Bayes: Markov chain Monte Carlo
What is Monte Carlo Simulation?
2. Gr 11 Life Sciences - Population Ecology - Theory 2 Quadrat Method
Unveiling The Remarkable Discovery Of Pi By A Genius - Prepare To Be Amazed!
Integration and the fundamental theorem of calculus | Chapter 8, Essence of calculus
5.0 / 5 (0 votes)