So let's get started with a very famous example.
We're going to calculate the value of PI using a Monte Carlo algorithm or better to say we're going
to approximate the value of PI.
And we will exploit randomness, randomness and generated numbers to determine this value.
So if you have never seen this, I think this is a pretty impressive example.
But if you have seen this and I think it's pretty clear what's going to happen because this is a pretty
standard example.
So first of all, we will note NUM Pi and model partnership PI plot as before.
And now we will also import another module from Protip, which is called patches, and I write down
dot patches as patches.
So let's run this and now we can do is we can basically draw geometric figures.
So I want to draw here square and also a circle.
So let's go ahead and I'll show you how this works.
So we can do this as follows right rectangle.
So this is where it this will be where the rectangle is stored and then they are just right patches
dot capital R Act angle.
And then we must define some values for the rectangle.
So first of all, we must find the bottom left corner.
So this will be the coordinates minus one and minus one in my case.
Then we must define the width and the height, which is two and two.
So basically, now it goes from minus one to plus one in the X and then the Y --.
And then we can define some color.
For example, I write phase color equal to reds.
So if I run this, nothing happens.
We must first add this to a plot for this, I write.
Figure is equal to P.L. T dot figure.
It's basically here we are initializing to figure.
And then I think we did this before a several times.
I will just try to figure out, add subplots.
So this is a bit tricky.
I think about Python the way it has to be so difficult for plotting.
But this is the same thing that we had to do when we wanted to change the aspect ratio.
So here we can all rights aspects equal to equal.
And now we can write X thoughts at underscore patch.
Now we write what we want to add and we want to add a rectangle.
The thing that we have to find previously and now we have two plots with a red rectangle.
So to show you that this is really just a rectangle, I can also specify the limits.
So please dot x limb.
For example, we could write minus one point one and one point one and the same for Y.
So then you see the red rectangle really goes just from minus one to one.
Two looks pretty nice, I think.
And now I will add a circle.
So circle equals patches.
Dots circle here.
The optional.
No, they are not optional.
They are required arguments.
They are at the center coordinate position.
So 0.0 in our case and then the radius and then for the face color.
All right.
None.
So then it's transparent.
And for edge color.
All right, blue.
And when I ran this, nothing happens because first person must have this two hour plot.
So we must write x dot and underscore patches circle.
And now you see, we have now there's redirect angle and the blue circle.
So now let me show you what I wrote down previously by deleting these cells.
So the whole trick about this?
So, yeah, the whole trick is that we consider this as a sketch for what is going to happen next.
We will generate randomly some points in this red square.
So just randomly, we pick some points in here and then we count how many points are inside and how
many points are outside of the circle.
And by doing this, we can determine the ratio of these two areas.
So for example, if we have 1000 points and 700 of them would be inside the circle and 300 would be
outside, then the ratio of the circle to the square would be 700 over 1000.
So zero point seven.
However, this is not the actual value we would get.
So we will see this in the second.
But the thing is, we know already from mathematics what is the area of the circle and what is the area
of the square?
So of course, from the circle, it's Pi Times Square.
In our case, the radius is one, so the area would just be PI and the area of the square would be this
length, he squared.
And this length is two and two squared is four.
So we know that the ratio of a circle divided by a square is PI over four.
And so we can solve this for PI and we will get PI is equal to four times the ratio.
And the thing is that as I have just explained, by generating these random numbers and by counting
how many of them are inside and outside of the circle, we can determine this ratio just numerically
just by generating a large number of points and just by counting them.
And so when we multiply this ratio by four, then we get the approximate value of PI.
And the next lecture, I want to show you how this works and how you can implement this.