1
00:00:00,060 --> 00:00:02,850
So let's get started with a very famous example.

2
00:00:02,940 --> 00:00:10,380
We're going to calculate the value of PI using a Monte Carlo algorithm or better to say we're going

3
00:00:10,380 --> 00:00:12,810
to approximate the value of PI.

4
00:00:13,440 --> 00:00:19,920
And we will exploit randomness, randomness and generated numbers to determine this value.

5
00:00:20,370 --> 00:00:23,820
So if you have never seen this, I think this is a pretty impressive example.

6
00:00:23,820 --> 00:00:28,590
But if you have seen this and I think it's pretty clear what's going to happen because this is a pretty

7
00:00:28,590 --> 00:00:29,700
standard example.

8
00:00:30,840 --> 00:00:35,880
So first of all, we will note NUM Pi and model partnership PI plot as before.

9
00:00:36,180 --> 00:00:43,350
And now we will also import another module from Protip, which is called patches, and I write down

10
00:00:43,830 --> 00:00:45,540
dot patches as patches.

11
00:00:46,500 --> 00:00:51,870
So let's run this and now we can do is we can basically draw geometric figures.

12
00:00:51,900 --> 00:00:55,260
So I want to draw here square and also a circle.

13
00:00:56,400 --> 00:00:59,250
So let's go ahead and I'll show you how this works.

14
00:01:00,210 --> 00:01:04,080
So we can do this as follows right rectangle.

15
00:01:05,280 --> 00:01:09,510
So this is where it this will be where the rectangle is stored and then they are just right patches

16
00:01:10,260 --> 00:01:13,530
dot capital R Act angle.

17
00:01:15,120 --> 00:01:19,590
And then we must define some values for the rectangle.

18
00:01:20,040 --> 00:01:23,100
So first of all, we must find the bottom left corner.

19
00:01:23,550 --> 00:01:27,210
So this will be the coordinates minus one and minus one in my case.

20
00:01:27,810 --> 00:01:33,030
Then we must define the width and the height, which is two and two.

21
00:01:33,390 --> 00:01:39,780
So basically, now it goes from minus one to plus one in the X and then the Y --.

22
00:01:40,770 --> 00:01:43,210
And then we can define some color.

23
00:01:43,320 --> 00:01:48,290
For example, I write phase color equal to reds.

24
00:01:49,740 --> 00:01:51,330
So if I run this, nothing happens.

25
00:01:51,330 --> 00:01:56,910
We must first add this to a plot for this, I write.

26
00:01:58,350 --> 00:02:02,820
Figure is equal to P.L. T dot figure.

27
00:02:03,280 --> 00:02:06,180
It's basically here we are initializing to figure.

28
00:02:06,540 --> 00:02:09,990
And then I think we did this before a several times.

29
00:02:09,990 --> 00:02:13,820
I will just try to figure out, add subplots.

30
00:02:14,040 --> 00:02:15,900
So this is a bit tricky.

31
00:02:15,900 --> 00:02:20,160
I think about Python the way it has to be so difficult for plotting.

32
00:02:20,520 --> 00:02:24,540
But this is the same thing that we had to do when we wanted to change the aspect ratio.

33
00:02:25,170 --> 00:02:31,620
So here we can all rights aspects equal to equal.

34
00:02:33,240 --> 00:02:39,870
And now we can write X thoughts at underscore patch.

35
00:02:40,860 --> 00:02:44,640
Now we write what we want to add and we want to add a rectangle.

36
00:02:44,700 --> 00:02:50,970
The thing that we have to find previously and now we have two plots with a red rectangle.

37
00:02:52,530 --> 00:02:58,860
So to show you that this is really just a rectangle, I can also specify the limits.

38
00:02:59,370 --> 00:03:01,440
So please dot x limb.

39
00:03:02,850 --> 00:03:09,990
For example, we could write minus one point one and one point one and the same for Y.

40
00:03:12,960 --> 00:03:17,490
So then you see the red rectangle really goes just from minus one to one.

41
00:03:17,910 --> 00:03:19,290
Two looks pretty nice, I think.

42
00:03:19,920 --> 00:03:22,570
And now I will add a circle.

43
00:03:23,730 --> 00:03:26,340
So circle equals patches.

44
00:03:26,790 --> 00:03:29,250
Dots circle here.

45
00:03:29,250 --> 00:03:30,030
The optional.

46
00:03:30,340 --> 00:03:31,400
No, they are not optional.

47
00:03:31,410 --> 00:03:32,860
They are required arguments.

48
00:03:32,910 --> 00:03:35,580
They are at the center coordinate position.

49
00:03:35,590 --> 00:03:41,250
So 0.0 in our case and then the radius and then for the face color.

50
00:03:42,630 --> 00:03:43,380
All right.

51
00:03:43,920 --> 00:03:44,370
None.

52
00:03:44,880 --> 00:03:46,290
So then it's transparent.

53
00:03:47,850 --> 00:03:49,890
And for edge color.

54
00:03:51,240 --> 00:03:53,220
All right, blue.

55
00:03:56,450 --> 00:04:02,180
And when I ran this, nothing happens because first person must have this two hour plot.

56
00:04:02,750 --> 00:04:07,520
So we must write x dot and underscore patches circle.

57
00:04:08,840 --> 00:04:13,640
And now you see, we have now there's redirect angle and the blue circle.

58
00:04:14,990 --> 00:04:18,709
So now let me show you what I wrote down previously by deleting these cells.

59
00:04:19,100 --> 00:04:21,890
So the whole trick about this?

60
00:04:22,670 --> 00:04:31,190
So, yeah, the whole trick is that we consider this as a sketch for what is going to happen next.

61
00:04:32,360 --> 00:04:37,890
We will generate randomly some points in this red square.

62
00:04:38,390 --> 00:04:44,960
So just randomly, we pick some points in here and then we count how many points are inside and how

63
00:04:44,960 --> 00:04:47,630
many points are outside of the circle.

64
00:04:48,770 --> 00:04:53,720
And by doing this, we can determine the ratio of these two areas.

65
00:04:54,560 --> 00:05:01,190
So for example, if we have 1000 points and 700 of them would be inside the circle and 300 would be

66
00:05:01,190 --> 00:05:08,540
outside, then the ratio of the circle to the square would be 700 over 1000.

67
00:05:08,540 --> 00:05:10,340
So zero point seven.

68
00:05:11,360 --> 00:05:14,180
However, this is not the actual value we would get.

69
00:05:14,840 --> 00:05:16,130
So we will see this in the second.

70
00:05:17,030 --> 00:05:22,640
But the thing is, we know already from mathematics what is the area of the circle and what is the area

71
00:05:22,640 --> 00:05:23,390
of the square?

72
00:05:24,110 --> 00:05:27,170
So of course, from the circle, it's Pi Times Square.

73
00:05:27,410 --> 00:05:34,910
In our case, the radius is one, so the area would just be PI and the area of the square would be this

74
00:05:34,910 --> 00:05:36,260
length, he squared.

75
00:05:36,740 --> 00:05:39,380
And this length is two and two squared is four.

76
00:05:40,940 --> 00:05:48,440
So we know that the ratio of a circle divided by a square is PI over four.

77
00:05:49,730 --> 00:05:56,210
And so we can solve this for PI and we will get PI is equal to four times the ratio.

78
00:05:57,320 --> 00:06:02,270
And the thing is that as I have just explained, by generating these random numbers and by counting

79
00:06:02,270 --> 00:06:09,140
how many of them are inside and outside of the circle, we can determine this ratio just numerically

80
00:06:09,500 --> 00:06:13,070
just by generating a large number of points and just by counting them.

81
00:06:14,150 --> 00:06:20,180
And so when we multiply this ratio by four, then we get the approximate value of PI.

82
00:06:21,050 --> 00:06:25,040
And the next lecture, I want to show you how this works and how you can implement this.

