1
00:00:00,180 --> 00:00:07,020
So I come back, as I have just explained, we have now used all of these list or array methods to get

2
00:00:07,020 --> 00:00:12,330
the value of PI, which was pretty accurate, and then we could also generate this nice plot.

3
00:00:13,020 --> 00:00:14,610
But we actually don't need this plot.

4
00:00:14,610 --> 00:00:20,260
We just need the value of PI and this we can get very easily by using a loop.

5
00:00:21,240 --> 00:00:24,190
And I want to show you how this works.

6
00:00:24,210 --> 00:00:25,350
This is the solution.

7
00:00:25,920 --> 00:00:30,210
And basically, we just have to use your two counters.

8
00:00:30,720 --> 00:00:33,800
So I called them I and I called them counter here.

9
00:00:33,810 --> 00:00:41,340
The AI counter will loop or will be the index for to loop over the 100000 points that we are investigating.

10
00:00:41,820 --> 00:00:48,330
And the counter called counter counts, the points that are inside the the circle.

11
00:00:49,380 --> 00:00:51,780
So here I use a while loop.

12
00:00:51,870 --> 00:00:58,860
You could also use any other loop and I write while I is smaller than points, which is 100000 in our

13
00:00:58,860 --> 00:01:01,410
case, then we do something.

14
00:01:01,650 --> 00:01:07,440
And of course, we also increase the value of the counter off the running index by one.

15
00:01:08,820 --> 00:01:13,130
And what we do is dependent on an if statement.

16
00:01:13,140 --> 00:01:20,250
So if the norm can write this, if the norm and Peter O'Toole in our thought norm.

17
00:01:22,780 --> 00:01:29,830
Of a randomly generated pair of numbers is smaller than one.

18
00:01:31,690 --> 00:01:32,140
Then

19
00:01:35,470 --> 00:01:43,510
at the counter or increased counter by one, so we must generate a pair of two numbers.

20
00:01:43,930 --> 00:01:50,700
This would be A. Dodge RAM and the daughter and two.

21
00:01:51,220 --> 00:01:55,920
So this would be two numbers in the range of zero and one, but we want minus want one.

22
00:01:55,930 --> 00:01:59,530
So we write like this as we did before.

23
00:02:00,160 --> 00:02:04,010
And you see, now we have no information about the points.

24
00:02:04,030 --> 00:02:07,360
None of this is stored, but we don't really need it.

25
00:02:07,750 --> 00:02:10,900
We just need to have, in the end, the value of counter.

26
00:02:12,400 --> 00:02:19,000
So I run the cell and you see, it took already a second or so and to get the value of PI approx.

27
00:02:20,110 --> 00:02:26,950
All right, this is equal to four times the value of counter divided by points.

28
00:02:27,940 --> 00:02:30,880
And then print pi approx.

29
00:02:33,530 --> 00:02:39,760
So you see, again, pretty close to pie, and we can, of course, also calculate the difference and

30
00:02:39,780 --> 00:02:41,500
p dot minus pie.

31
00:02:41,870 --> 00:02:43,040
And you see, it's pretty good.

32
00:02:44,000 --> 00:02:48,440
So qualitatively, it's the same result as before.

33
00:02:48,450 --> 00:02:52,040
Of course, the value is now a bit different, but this is just due to randomness.

34
00:02:52,460 --> 00:02:57,560
If we would repeat this many, many times and an average and the result would get even better.

35
00:02:58,910 --> 00:03:05,450
And you see, this one was just a few lines of code, and it looks pretty simple.

36
00:03:05,540 --> 00:03:06,940
I think it was very intuitive.

37
00:03:06,950 --> 00:03:11,450
We just had to loop here using a while loop and then just use a single if statement.

38
00:03:12,290 --> 00:03:18,440
And basically, you wouldn't have even been required to use such a command line.

39
00:03:18,440 --> 00:03:29,030
Argument could have also wrote two times and p rand squared plus another and p random rand square and

40
00:03:29,030 --> 00:03:30,320
then smaller than one.

41
00:03:30,350 --> 00:03:32,420
This would also be totally fine.

42
00:03:33,800 --> 00:03:35,270
So you see, it's pretty simple.

43
00:03:35,270 --> 00:03:36,680
But what's the downside of this?

44
00:03:36,690 --> 00:03:43,430
The downside is the speed, and I want to show you now a speed comparison of the two methods.

45
00:03:44,150 --> 00:03:51,530
So you see here I have copied the two methods on how we can calculate the approximate value of PI based

46
00:03:51,530 --> 00:03:54,890
on the aerialist method and based on the loop methods.

47
00:03:55,640 --> 00:04:00,620
So for the loop method, I have just copied what we have written here and then just a single line of

48
00:04:00,620 --> 00:04:00,980
code.

49
00:04:01,700 --> 00:04:08,900
And for the RNA or at least method, I have basically copied these lines here and then only the ones

50
00:04:08,900 --> 00:04:09,740
that really need it.

51
00:04:09,740 --> 00:04:12,440
So this would be this one and this one.

52
00:04:12,740 --> 00:04:16,100
But of course, none of this plots because this was just optional.

53
00:04:16,100 --> 00:04:18,500
And of course, this takes more time, but it's not required.

54
00:04:19,550 --> 00:04:27,320
So we have here just a few of these commands where we will modify our array and then he or we distinguished

55
00:04:27,320 --> 00:04:30,530
and determined only the points that are inside the circle.

56
00:04:31,100 --> 00:04:38,030
And you see, when we run both of these, we get no outputs because, yeah, we didn't call for an output.

57
00:04:38,600 --> 00:04:44,900
But I want to add now a command that is called percent percent time.

58
00:04:45,590 --> 00:04:53,000
So that's a command that you can always use in Jupyter Notebook to output the time it took to run these

59
00:04:53,000 --> 00:04:53,630
cells.

60
00:04:54,470 --> 00:04:57,110
When I run this year, the first time you see it takes a bit.

61
00:04:58,310 --> 00:05:08,150
This is because it has run seven runs of 100 loops, so this whole cell has been calculated 700 times.

62
00:05:09,170 --> 00:05:13,520
And then the total time has been divided, of course, by 700.

63
00:05:13,520 --> 00:05:20,450
And you see the time for a single loop or a single run of the cell took three point twenty five milliseconds.

64
00:05:21,470 --> 00:05:23,180
And now if I do this with a loop methods.

65
00:05:26,170 --> 00:05:29,530
We get the time for the new method, and you see it's much, much higher.

66
00:05:29,860 --> 00:05:39,370
It's 522 milliseconds and you see, since this took more than 100 times longer, it's only that seven

67
00:05:39,370 --> 00:05:43,990
runs with one loop each, the only seven runs in total, compared to 700 here.

68
00:05:44,590 --> 00:05:51,490
So it's a pretty cool command that allows you to really find out if your code is fast or not.

69
00:05:52,330 --> 00:05:58,510
And I think this is a cool example because you see the loop method is probably the more intuitive one,

70
00:05:58,510 --> 00:06:04,390
the most simple one, the one that beginners will probably choose and would have come up with.

71
00:06:05,050 --> 00:06:06,970
However, it's so much slower.

72
00:06:07,330 --> 00:06:12,370
In this case, it doesn't really matter because it only takes half a second anyway.

73
00:06:13,090 --> 00:06:19,000
But if you would have to run this algorithm a thousand times, then it's of course much better to use

74
00:06:19,000 --> 00:06:26,170
this one because it only takes three seconds here and here it takes 500 seconds, which is almost 10

75
00:06:26,170 --> 00:06:26,560
minutes.

76
00:06:27,550 --> 00:06:31,660
So time matters sometimes and often.

77
00:06:31,660 --> 00:06:39,310
It's very, very helpful to think in advance if your code is required to be fast or not.

78
00:06:39,760 --> 00:06:45,160
If it's not required to be fast, then you can choose the more simple solution sometimes and get away

79
00:06:45,160 --> 00:06:45,550
with it.

80
00:06:46,150 --> 00:06:50,980
But if time really methods, if it's really something that you call very, very often, then it makes

81
00:06:50,980 --> 00:06:54,160
sense to think about finding a fast solution.

82
00:06:54,910 --> 00:07:01,060
And that's the take-home message here, working with arrays and using these functions that correspond

83
00:07:01,060 --> 00:07:07,600
to arrays like this reshape or something like this one here and also this length command.

84
00:07:08,050 --> 00:07:12,400
They are much, much faster than these loops, which are not so, well optimized.

85
00:07:13,090 --> 00:07:18,400
So if you want to have fast code and you should always tend to solutions like these.

86
00:07:19,510 --> 00:07:25,180
However, in general, the take-home message of this whole lecture is that we have determined the value

87
00:07:25,180 --> 00:07:32,200
of PI, which is generating 100000 points, and we have just counted how many of them are inside.

88
00:07:32,590 --> 00:07:40,690
And then when we divide this number by 100000, we have approximated the ratio of the area of the circle

89
00:07:40,690 --> 00:07:43,330
compared to the area of the square.

90
00:07:44,110 --> 00:07:51,340
And in the very beginning, we have derived that the value of PI can be approximated by this ratio multiplied

91
00:07:51,340 --> 00:07:51,910
by four.

92
00:07:52,930 --> 00:07:57,520
I think that's really impressive example if you have never heard of it and it really shows you how you

93
00:07:57,520 --> 00:08:04,060
can use randomness and Monte Carlo algorithms to find out useful information.

