Topic 7 Lesson 3

AUB Moodle Videos
5 Jan 202305:32

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

00:00

πŸ” 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.

05:01

πŸ”’ 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

Monte Carlo simulation is a statistical technique that uses random sampling to estimate the probability of an event. It is used in various fields, including physics, finance, and engineering, to model complex systems and predict outcomes. In the context of this video, Monte Carlo simulation is used to approximate the value of pi by generating random points within a unit square and determining how many of these points fall within a unit circle.

πŸ’‘Probability

Probability is a measure of the likelihood that a particular event will occur. It is expressed as a number between 0 and 1, where 0 indicates impossibility and 1 indicates certainty. In the video, probability is used to estimate the ratio of the area of a unit circle to the area of a unit square, which is crucial for approximating pi.

πŸ’‘Unit circle

A unit circle is a circle with a radius of one. It is centered at the origin of a coordinate system. In the video, the unit circle is inscribed in a unit square, and the Monte Carlo simulation uses this geometric setup to approximate pi by comparing the areas of the circle and the square.

πŸ’‘Unit square

A unit square is a square with sides of length one. It is used in the video as a reference frame for generating random points and comparing their positions relative to the unit circle. The area of the unit square is 4, which is used in the calculation to approximate pi.

πŸ’‘Random sampling

Random sampling is the process of selecting a subset of data from a larger population in such a way that each member of the population has an equal chance of being chosen. In the video, random sampling is used to generate points within the unit square to estimate the probability of a point being inside the unit circle.

πŸ’‘Area

Area is a measure of the two-dimensional space enclosed within a boundary. In the video, the areas of the unit circle and the unit square are compared to determine the ratio that approximates pi. The area of the unit circle is Ο€ * radius squared, and the area of the unit square is the square of its side length.

πŸ’‘Approximation

Approximation is a process of finding a value that is close to the exact value but not necessarily exact. In the context of the video, approximation is used to estimate the value of pi by multiplying the ratio of the areas (unit circle to unit square) by 4.

πŸ’‘Random point

A random point is a point chosen at random within a given space. In the video, random points are generated within the unit square to determine if they fall within the unit circle. The coordinates of these points are chosen independently and uniformly at random.

πŸ’‘Distance

Distance is a measure of how far apart objects are. In the video, the distance from the origin to a random point is used to determine if the point lies within the unit circle. If the square of the distance (x^2 + y^2) is less than or equal to 1, the point is inside the circle.

πŸ’‘Algorithm

An algorithm is a set of rules or steps used to solve a problem. In the video, the algorithm for approximating pi involves generating random points, checking if they fall within the unit circle, and calculating the ratio of points inside the circle to the total points generated. This ratio is then multiplied by 4 to approximate pi.

πŸ’‘Error

Error in this context refers to the difference between the approximate value and the actual value. In the video, the error is calculated as the absolute value of the difference between the actual value of pi and the approximation obtained from the Monte Carlo simulation. A smaller error indicates a better approximation.

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

play00:00

In this lesson, we will study

play00:02

Monte Carlo simulation and

play00:05

namely, approximating pi.

play00:08

Monte Carlo simulation is a technique

play00:11

used to approximate the probability of

play00:13

an event by random sampling multiple

play00:16

times and averaging the results.

play00:18

The set up is a probabilistic experiment.

play00:20

we Would like to estimate the

play00:22

probability of a certain event.

play00:24

How do you do that?

play00:26

We sample a number of time and we average

play00:28

that is the approximate probability of

play00:30

the event is the number of outcomes

play00:33

which satisfy the event divided

play00:35

by the total number of outcomes.

play00:38

We'll see in this lesson how Monte Carlo

play00:41

simulation can be used to solve the problem.

play00:44

That is not inherently stochastic and

play00:47

namely approximating the value of pi

play00:51

Consider the unit circle

play00:53

inscribed in the unit square.

play00:55

This is the circle of radius one,

play00:57

and this is the square of edge length 2.

play01:01

So this is the origin.

play01:04

And this is a point (-1,0),(1,0)

play01:06

and here (0,1) and here (0,-1)

play01:12

The key observation is that the

play01:14

area of the unit circle is Ο€ * raduis squared

play01:17

R is one, so this is Ο€ * 1 squared,

play01:20

which is Ο€. To relate this to the

play01:23

probability of an event consider,

play01:25

the area of the unit square,

play01:27

the edge length is 2,

play01:29

so the area of the unit square is 2 * 2,

play01:32

which is 4. Therefore,

play01:34

the ratio area of the unit circle over

play01:36

the area of unit square is pi over 4.

play01:39

Now we need to relate this to

play01:41

the probability of an event.

play01:43

This is actually the probability

play01:45

that a random point of the unit

play01:49

square belongs to the unit circle.

play01:51

Therefore pi is 4 * P,

play01:54

where P is the probability of the

play01:56

event that a random point of the unit

play01:59

square belongs to the unit circle.

play02:02

Now in the setup of Monte Carlo simulation,

play02:05

to estimate the probability of the event,

play02:08

this event would generate n points

play02:10

uniformly at random in the unit square.

play02:12

We count the number m of those

play02:15

points inside the unit circle.

play02:17

We take the ratio.

play02:19

This is an approximation of P.

play02:22

To get the approximation of

play02:23

pi we multiply by 4.

play02:27

How do you choose one point

play02:29

uniformly at random in the unit square?

play02:32

We choose its X coordinate uniformly

play02:34

at random in the interval minus one

play02:37

one random real number and

play02:39

its y coordinate uniformly at

play02:41

random in this interval. we do so in an

play02:45

independent fashion so we choose

play02:47

n points uniformaly at random.

play02:48

we Repeat this process n time

play02:51

That is to approximate pi first

play02:53

we choose n points by choosing

play02:56

their coordinates uniformly at

play02:57

random in the unit square,

play03:00

and we do so by choosing each xi

play03:03

and each yi uniformly at random,

play03:06

in the interval minus one one.

play03:09

We find the number m of

play03:12

points in the unit circle

play03:14

We take the ratio m over n

play03:16

and we multiply by 4 and this is

play03:18

our desired approximation of Ο€.

play03:20

Now n is a parameter of this

play03:22

randomized algorithm for large

play03:24

n we get good approximations of Ο€.

play03:28

implement the function

play03:30

approximate pi of N which given n

play03:33

Returns an approximation of pi

play03:35

using this randomized algorithm,

play03:37

which is based on Monte Carlo simulation.

play03:51

First, we import Numpy.random

play03:52

then we import the

play03:53

value of Ο€ from the math module,

play03:56

because we'd like to compare our

play03:57

approximation of Pi with the actual value of Pi.

play04:00

this is how we use the function

play04:02

we call approximate Pi on n equal,

play04:04

let's say 10,000 it returns pihat

play04:07

Here we print pihat and here we

play04:10

print the absolute value of the error.

play04:12

the Absolute value of the

play04:14

difference pi minus pihat

play04:16

Now, another algorithm

play04:17

would like to sample n times,

play04:19

so we have a loop for i in range n and

play04:22

at each iteration we need to generate the

play04:26

uniformly random points in the unit square.

play04:29

So we use the random uniform function.

play04:32

we generate x uniformly at random in

play04:34

the interval -1, 1. similarly for y

play04:37

Now we need to check whether the

play04:40

point XY belongs to the unit circle.

play04:43

to do so,

play04:44

we simply compare the distance between

play04:47

the origin and the point XY to one,

play04:50

or equivalently the square

play04:52

of this distance to one.

play04:54

So if X ^2 + y ^2 is less or equal to 1,

play04:59

we need to increment some

play05:01

counter m initialized to 0.

play05:03

So before the loop initialize a

play05:05

counter m to zero and in the if condition.

play05:09

Here we increment m and at the

play05:12

end we return 4* m / n.

play05:19

Each time we run this program,

play05:21

we get a different approximation of pi

play05:23

This is the output of one run.

play05:25

Approximate Pi 3.1452

play05:28

and the error is around 0.0036.

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Monte CarloPi ApproximationProbabilityRandom SamplingNumerical MethodsCircle AreaSquare AreaPython CodeAlgorithmStatistical Learning