1
00:00:00,180 --> 00:00:00,960
So welcome back.

2
00:00:01,140 --> 00:00:03,180
I think the plan is now clear.

3
00:00:03,630 --> 00:00:11,940
We know that we can estimate PI by calculating four times the ratio of the RAAF to circle divided by

4
00:00:11,940 --> 00:00:13,080
the area of the square.

5
00:00:13,620 --> 00:00:21,960
And this ratio of these two areas we can determine by generating points randomly and just by counting

6
00:00:21,960 --> 00:00:24,750
how many of them are inside and outside.

7
00:00:25,770 --> 00:00:27,450
So let's go ahead and do this.

8
00:00:28,170 --> 00:00:32,100
First of all, we must, of course, define how many points we want to have.

9
00:00:32,520 --> 00:00:35,860
So I write points is equal to and let's start small.

10
00:00:35,860 --> 00:00:37,770
Let's just use a thousand points.

11
00:00:38,760 --> 00:00:46,920
And then I can write Rand is equal to and p dot random thoughts rand.

12
00:00:47,970 --> 00:00:50,370
And then I write points.

13
00:00:51,420 --> 00:00:53,250
So maybe make this number even smaller.

14
00:00:53,250 --> 00:00:54,360
Let's just use ten.

15
00:00:54,990 --> 00:01:03,000
And so if I now call and we have an array of 10 different numbers in the range of zero and one.

16
00:01:03,970 --> 00:01:09,210
In this case, it's a bit odd because we have three of these points that are very close to one.

17
00:01:09,720 --> 00:01:13,560
But if I repeat this, then you see it changes.

18
00:01:13,950 --> 00:01:19,590
So really, these are randomly generated numbers from what we want to have here.

19
00:01:19,590 --> 00:01:23,760
When we look at our graph is we want to have points in the range of minus one and one.

20
00:01:24,810 --> 00:01:29,820
So I write two times this random number minus one.

21
00:01:30,960 --> 00:01:34,860
This time we have points in the range of minus one and plus one.

22
00:01:35,400 --> 00:01:39,930
Let's do it again c from minus one to plus one.

23
00:01:40,440 --> 00:01:42,360
This one here is pretty close to +1.

24
00:01:42,900 --> 00:01:44,190
So you see it works.

25
00:01:45,960 --> 00:01:51,290
Now the thing is here we just have 10 points, but just basically in one dimension.

26
00:01:51,300 --> 00:01:55,140
So just a single coordinates and we want to have X and Y coordinates.

27
00:01:56,160 --> 00:02:02,460
So we will generate here a number of 20 points in our case of two times points.

28
00:02:03,380 --> 00:02:10,830
So then we have 20 of them and then we will write random three shape points.

29
00:02:10,830 --> 00:02:16,740
Comma to what this does is it generates pairs of these numbers.

30
00:02:17,100 --> 00:02:24,210
So basically first we generate 20 points and then we will order them.

31
00:02:24,210 --> 00:02:26,850
We'll just take the first to make them a list.

32
00:02:27,150 --> 00:02:28,770
Second, to make them in the list.

33
00:02:29,220 --> 00:02:34,050
And so now we have these pairs of these coordinates and these will be 10 of our points.

34
00:02:35,790 --> 00:02:38,440
OK, so let's cut this, we don't need it anymore.

35
00:02:38,670 --> 00:02:42,240
And this one we can call friend points.

36
00:02:44,620 --> 00:02:45,060
OK.

37
00:02:45,520 --> 00:02:48,280
And as I said previously, we need, of course, more points.

38
00:02:48,340 --> 00:02:49,660
Let's start with a thousand.

39
00:02:50,380 --> 00:02:56,980
So now we have hour variable rent points, 1000 points with two coordinates each, and each of these

40
00:02:56,980 --> 00:02:59,800
coordinates is in the range of minus one and +1.

41
00:03:01,420 --> 00:03:03,520
Now we need the norm.

42
00:03:03,730 --> 00:03:10,510
So basically, we need the criterion to decide whether or not the points inside or outside the circle.

43
00:03:11,410 --> 00:03:18,730
So if we take the norm, so basically the distance of the points to the center, then we know if it

44
00:03:18,730 --> 00:03:20,260
is outside or inside.

45
00:03:20,350 --> 00:03:25,870
Because if this length, this distance is smaller than the radius, then it must be inside.

46
00:03:26,230 --> 00:03:29,170
And if it is larger and the radius, then it must be outside.

47
00:03:30,040 --> 00:03:32,560
So we need to know and this will be the criterion.

48
00:03:32,860 --> 00:03:42,910
So I write norm points and to calculate this, we basically take our list of red points and we take

49
00:03:42,910 --> 00:03:44,230
every single one of them.

50
00:03:46,540 --> 00:03:55,750
And for every single one of these points, we take the first coordinates, square it and we take the

51
00:03:55,750 --> 00:03:59,800
second coordinates and square and at these two values.

52
00:04:00,670 --> 00:04:05,890
So now we could right here NPR dot square root to have the actual norm.

53
00:04:06,520 --> 00:04:12,700
But to be honest, it makes the code a bit slower and it's not really required, so we can just leave

54
00:04:12,700 --> 00:04:13,300
it out here.

55
00:04:13,540 --> 00:04:17,589
So this will now actually be the norm square points list.

56
00:04:17,769 --> 00:04:19,720
But I just call it norm points.

57
00:04:19,720 --> 00:04:23,530
Even though this is not the actual norm, this is the square of the norm.

58
00:04:24,460 --> 00:04:30,750
However, the good thing is when the radius is one and also the square radius will be one.

59
00:04:30,760 --> 00:04:36,790
So we can now just see it say if the square of the norm is smaller than one, then it's inside.

60
00:04:36,970 --> 00:04:40,360
And if the square of the norm is larger than one, then it's outside.

61
00:04:40,780 --> 00:04:41,890
So there's value here.

62
00:04:41,890 --> 00:04:47,920
This list of values will contain valuable information to determine this ratio here.

63
00:04:49,240 --> 00:04:57,190
And so what we have to do next is we have to distinguish these points based on this criterion that I

64
00:04:57,190 --> 00:04:57,970
just mentioned.

65
00:04:58,150 --> 00:05:04,840
So, all right, points out, is equal to random points.

66
00:05:05,320 --> 00:05:08,290
So Rand's points recorded it.

67
00:05:08,740 --> 00:05:14,200
So it will be this list, but only the elements which fulfil a certain criterion.

68
00:05:14,950 --> 00:05:17,460
And this is pretty easy to implement in Python.

69
00:05:17,500 --> 00:05:25,270
It just writes the criterion that you want to use, which in this case is also a list of the same length

70
00:05:26,080 --> 00:05:27,490
and larger than one.

71
00:05:27,970 --> 00:05:31,030
So what happens here is the following.

72
00:05:31,240 --> 00:05:38,140
So it will start with the first entry in the round points and then it will determine based on the first

73
00:05:38,140 --> 00:05:39,460
entry of this list.

74
00:05:40,090 --> 00:05:44,470
So it will see OK, round points.

75
00:05:45,490 --> 00:05:50,330
First element will be this one norm points.

76
00:05:51,160 --> 00:05:54,550
The first element will be

77
00:05:57,460 --> 00:05:58,210
what's happening.

78
00:06:00,600 --> 00:06:01,170
No points.

79
00:06:01,200 --> 00:06:02,030
Oh, I didn't try it.

80
00:06:02,490 --> 00:06:12,720
OK, so now Norm points, the first element is this one, so the length of the of this off the distance

81
00:06:12,720 --> 00:06:18,240
of this point compared to the center, Kornet squared is equal to zero point four two.

82
00:06:18,750 --> 00:06:20,220
So there's is smaller than one.

83
00:06:20,940 --> 00:06:24,960
So it will not be in this case, so it will not be in points out.

84
00:06:25,230 --> 00:06:32,860
Instead, it will be in points in where the condition will be like this.

85
00:06:33,540 --> 00:06:38,790
So it doesn't really matter if you use your smaller and equal or two smaller because due to the random

86
00:06:38,790 --> 00:06:45,370
function, the chance is practically zero that there is a point where the norm is exactly equal to one.

87
00:06:45,390 --> 00:06:47,520
So it doesn't really matter if we use this or this.

88
00:06:48,150 --> 00:06:49,890
So like this, it should work.

89
00:06:50,190 --> 00:07:03,900
And now if we look at brands, if we look at points out the first element we see again our point from

90
00:07:03,900 --> 00:07:04,560
before.

91
00:07:10,900 --> 00:07:11,560
No, we don't.

92
00:07:11,590 --> 00:07:17,710
Of course, no, we don't, because it's not in here, but it's instead and points in.

93
00:07:18,130 --> 00:07:19,120
So here we see it.

94
00:07:19,630 --> 00:07:21,180
Yeah, here it is.

95
00:07:21,190 --> 00:07:22,060
It's the same point.

96
00:07:22,100 --> 00:07:25,140
And you see or distinction worked.

97
00:07:25,150 --> 00:07:29,290
So now this point is only and points in and it's not and points out.

98
00:07:30,460 --> 00:07:35,680
So no, I will delete all of these test cases here that I've just added to show you what is happening

99
00:07:35,680 --> 00:07:36,040
here.

100
00:07:36,670 --> 00:07:39,850
And these are the commands that we need.

101
00:07:40,900 --> 00:07:45,010
Rand runs, points, norm points, points out, points in.

102
00:07:45,670 --> 00:07:48,310
And to be honest, we only really need the points in.

103
00:07:48,310 --> 00:07:51,730
So we could also comment this one because this is not really a requirement.

104
00:07:52,690 --> 00:07:57,820
This is because we know already how many points we have, and we only need to know how many of them

105
00:07:57,820 --> 00:07:58,690
are in site.

106
00:08:00,210 --> 00:08:00,660
OK.

107
00:08:02,250 --> 00:08:10,260
So to get the result, we are almost finished now, we can just write pipe approx.

108
00:08:11,520 --> 00:08:19,440
Is equal to four times the ratio of the area circle divided by area of square.

109
00:08:19,740 --> 00:08:24,360
So it would be the number of points inside divided by the total number of points.

110
00:08:24,540 --> 00:08:35,520
So it would be the length of points in divided by the total number of points and to plot the or two

111
00:08:35,520 --> 00:08:37,370
to output the result.

112
00:08:37,390 --> 00:08:40,200
We just write print pi approx.

113
00:08:41,309 --> 00:08:43,799
And you see it's three point one two four.

114
00:08:44,910 --> 00:08:47,490
So we can compare this with the actual value.

115
00:08:49,410 --> 00:08:51,780
And you see the difference is not that big.

116
00:08:52,410 --> 00:08:53,730
So quite a good result.

117
00:08:54,450 --> 00:08:57,390
Of course, we can now go ahead and just increase this number.

118
00:09:00,170 --> 00:09:02,290
And the result got a bit better.

119
00:09:02,810 --> 00:09:07,190
Christie's number even more, and the result got even better.

120
00:09:08,150 --> 00:09:13,940
So of course, if we just repeat the same thing, then the value can change a bit because it is really

121
00:09:13,940 --> 00:09:15,470
given by random.

122
00:09:16,670 --> 00:09:21,770
So to show you or to visualize this a bit more, let's make a nice plot.

123
00:09:21,800 --> 00:09:24,590
Let's take the plot from before, and let's change it a bit.

124
00:09:25,250 --> 00:09:33,080
That's just add here these points that we have generated, and I will do this in the following way.

125
00:09:33,080 --> 00:09:41,090
I would just add here to scatter plots, so I will write points in.

126
00:09:42,970 --> 00:09:50,860
And we use all the points and the X coordinates, and for the Y coordinates, we ride like this.

127
00:09:51,610 --> 00:09:55,570
Then I use color for the inside points to be,

128
00:09:58,960 --> 00:10:05,920
I don't know, green green for positive and then the size of the points we can also specify, so make

129
00:10:06,490 --> 00:10:07,540
the points a bit smaller.

130
00:10:09,130 --> 00:10:15,990
So here we don't see anything does this because these patches are executed last, so they are intel

131
00:10:16,000 --> 00:10:16,960
on top of the points.

132
00:10:17,470 --> 00:10:20,650
And actually, we don't really need the rectangle anymore.

133
00:10:21,400 --> 00:10:25,060
So here you see all the points that are inside, so you see it worked.

134
00:10:25,510 --> 00:10:28,310
And then we can also add the points that are outside.

135
00:10:28,330 --> 00:10:30,280
So I will remove this comment here.

136
00:10:31,630 --> 00:10:38,560
So you see, we we only need the list points outside for plotting what not really to determine the value

137
00:10:38,560 --> 00:10:39,070
of PI.

138
00:10:39,760 --> 00:10:44,650
So, all right, points out comes out and the color will be red.

139
00:10:45,940 --> 00:10:50,020
And you see like this, we have now a nice plot of all of the points.

140
00:10:50,320 --> 00:10:55,960
So we have now how many have been 100000 points?

141
00:10:56,530 --> 00:11:03,940
We generate 100000 points with two coordinates each and we have put it inside the square and there are

142
00:11:03,940 --> 00:11:07,530
some of them are inside the circle and some of them are not inside the circle.

143
00:11:07,540 --> 00:11:13,390
And just by comparing the ratio of the of the of the number of points, we have determined the value

144
00:11:13,390 --> 00:11:17,950
of PI and we determined this value pretty accurately.

145
00:11:19,330 --> 00:11:23,770
So this is a method that works very, very well.

146
00:11:23,980 --> 00:11:26,950
It's based on lists and based on a race.

147
00:11:27,370 --> 00:11:31,660
So you see, we have first generated this parade and we've reshaped it.

148
00:11:32,020 --> 00:11:38,020
Then we've generated the second array with the norm than we have generated a new array where we have

149
00:11:38,020 --> 00:11:42,200
just taken some of the values from the old array based on this condition.

150
00:11:43,060 --> 00:11:45,370
And then we have just calculated the length.

151
00:11:46,420 --> 00:11:52,540
So maybe if you are not so familiar with these arrays and all of these commands, this looks a bit difficult

152
00:11:52,540 --> 00:11:52,930
to you.

153
00:11:53,290 --> 00:12:00,310
So it is sometimes even easier to just use a loop methods and it would, I think, even be less lines

154
00:12:00,310 --> 00:12:01,510
of code to do this.

155
00:12:02,170 --> 00:12:06,430
And as we will do in the next lecture, what I can tell you already, this is such a loop method is

156
00:12:06,430 --> 00:12:09,490
much, much slower, but it's sometimes easier.

157
00:12:09,490 --> 00:12:11,260
And I want to show it to you anyway.

