1
00:00:11,040 --> 00:00:15,990
In this lecture, we are going to look at the code for the exponentially weighted moving average.

2
00:00:16,560 --> 00:00:21,750
As usual, you can look at the title of the notebook to determine what notebook we are currently looking

3
00:00:21,750 --> 00:00:22,190
at.

4
00:00:23,540 --> 00:00:29,180
So before we start to look at the code itself, let's talk about the data set in this lecture and for

5
00:00:29,180 --> 00:00:33,500
most of the lectures in this section, we'll be using the airline passengers data said.

6
00:00:34,100 --> 00:00:39,890
This is a famous time series analysis data set, and it's basically the mists of Time series analysis.

7
00:00:41,500 --> 00:00:45,910
That said, the first line here is to download our airline passengers CSFI.

8
00:00:46,690 --> 00:00:50,110
Next, we import pendas and non-pay, not too surprising.

9
00:00:51,520 --> 00:00:55,110
Next, we use prediabetes, D.V. to load in our CSFI.

10
00:00:55,480 --> 00:00:57,250
Again, not too surprising.

11
00:00:58,630 --> 00:01:00,630
Notice that the indexed column is month.

12
00:01:00,790 --> 00:01:03,640
So this is monthly data rather than daily data.

13
00:01:04,600 --> 00:01:08,430
Next, we do a different head to see the first few rows of our data set.

14
00:01:12,270 --> 00:01:17,790
As you can see, our index is the month and the passengers column contains the number of passengers

15
00:01:17,790 --> 00:01:18,630
in that month.

16
00:01:21,110 --> 00:01:25,070
Next, we do advocate is in L.A. to check if there are any missing values.

17
00:01:27,650 --> 00:01:30,530
Since the summer zero, there are no missing values.

18
00:01:33,640 --> 00:01:36,460
Next, we do a plot to plot our data said.

19
00:01:40,800 --> 00:01:46,830
So let's spend some time analyzing what we are seeing, first of all, this looks nothing like a typical

20
00:01:46,830 --> 00:01:48,200
stock price time series.

21
00:01:48,570 --> 00:01:51,540
There seems to be very little randomness in this data set.

22
00:01:52,140 --> 00:01:57,790
In fact, that would mean that this is a great candidate for statistical in machine learning techniques.

23
00:01:58,320 --> 00:02:01,620
Remember that we used to call machine learning pattern recognition.

24
00:02:02,130 --> 00:02:06,350
That is, machine learning is all about recognizing and modeling patterns.

25
00:02:06,570 --> 00:02:08,390
And clearly there is a pattern here.

26
00:02:09,090 --> 00:02:13,590
On the other hand, stock prices might be characterized by their lack of patterns.

27
00:02:14,040 --> 00:02:19,350
We can see that with this data set, there seems to be a trend component and the cyclical component

28
00:02:19,350 --> 00:02:23,220
of the trend means that the signal is generally going upwards.

29
00:02:23,590 --> 00:02:29,130
The cyclical component means that there's some periodicity where the same pattern repeats itself.

30
00:02:30,180 --> 00:02:35,430
We can see that this cyclical pattern actually amplifies over time, so it's not a constant cyclical

31
00:02:35,430 --> 00:02:35,920
pattern.

32
00:02:36,570 --> 00:02:42,150
It's also difficult to tell whether the trend is increasing linearly or if that is also increasing over

33
00:02:42,150 --> 00:02:45,950
time, perhaps exponentially in this Time series.

34
00:02:45,960 --> 00:02:47,530
We don't have enough data to see that.

35
00:02:47,730 --> 00:02:52,410
However, one might surmise that many real world phenomena grow exponentially.

36
00:02:52,920 --> 00:02:57,450
For example, the number of transistors on an integrated circuit grows exponentially.

37
00:02:57,780 --> 00:03:01,420
This is known as Moore's Law due to recent world events.

38
00:03:01,620 --> 00:03:05,180
We know that viral transmission exhibits exponential growth.

39
00:03:05,670 --> 00:03:09,700
Of course, this is limited at some point since the world population is finite.

40
00:03:09,930 --> 00:03:13,050
So in fact, the overall curve might look like a sigmoid.

41
00:03:16,350 --> 00:03:19,440
Let's begin by setting our alpha parameter to zero point two.

42
00:03:20,820 --> 00:03:26,400
Why did I choose a zero point to no particular reason you can think of this as a hyper parameter that

43
00:03:26,400 --> 00:03:27,540
you can optimize?

44
00:03:30,640 --> 00:03:38,290
Next, let's calculate the WEMA, this can be accomplished by first using the IWM function we pass in

45
00:03:38,290 --> 00:03:43,380
our alpha parameter and we say adjust equals false y adjust equals false.

46
00:03:43,780 --> 00:03:49,150
When you set adjust equals true, it actually does a different calculation than what we discussed in

47
00:03:49,150 --> 00:03:54,030
the three lectures, which is not what one usually considers to be the WEMA.

48
00:03:54,550 --> 00:03:58,780
So we use adjust equals false so that Pendas uses the method we expect.

49
00:03:59,500 --> 00:04:04,360
Finally, we call the mean function because we want the exponentially weighted moving average, which

50
00:04:04,360 --> 00:04:06,820
corresponds to the exponentially weighted mean.

51
00:04:08,340 --> 00:04:11,580
We assign the result to the column called a.

52
00:04:14,080 --> 00:04:19,990
Next, just in case you're interested in the data type returned by the EWTN function, I've printed

53
00:04:19,990 --> 00:04:20,500
that out.

54
00:04:23,130 --> 00:04:29,820
So as you can see, we get back an object of type IWM, so if you're interested in looking at the documentation

55
00:04:29,820 --> 00:04:32,850
for this object now, you know which page to go to.

56
00:04:36,190 --> 00:04:43,450
Next, we do a plot to plot everything currently in our data frame, as you can see, the exponentially

57
00:04:43,450 --> 00:04:49,180
weighted moving average is kind of like a delayed and adaptive version of the original data set similar

58
00:04:49,180 --> 00:04:50,610
to the simple moving average.

59
00:04:51,190 --> 00:04:55,870
You might want to play around with the value of Alpha to get a better feel for how it affects the moving

60
00:04:55,870 --> 00:04:56,530
average.

61
00:05:01,180 --> 00:05:07,240
Next, what I always like to do if the equation is simple enough, is to check whether Pendas is doing

62
00:05:07,240 --> 00:05:08,650
what we expect it to do.

63
00:05:09,220 --> 00:05:14,580
That is, does it implement the formula we discussed in the theory lecture to check this?

64
00:05:14,590 --> 00:05:17,680
We're going to use that formula manually to start.

65
00:05:17,690 --> 00:05:21,310
We're going to create an empty list called Manual WEMA.

66
00:05:21,970 --> 00:05:27,940
Next, we're going to look through every element in the passenger's column of our data set inside the

67
00:05:27,940 --> 00:05:28,260
loop.

68
00:05:28,270 --> 00:05:33,050
We're going to check whether our manual GWM list is empty or not.

69
00:05:33,640 --> 00:05:38,040
Remember that the IWM is calculated based on its previous value.

70
00:05:38,650 --> 00:05:41,710
If there is no previous value, then we can't use the formula.

71
00:05:42,250 --> 00:05:45,010
Of course, that occurs on the very first value.

72
00:05:45,580 --> 00:05:49,020
The question is how does it WMA initialize itself?

73
00:05:49,750 --> 00:05:55,180
In fact, there are many answers to this question you may have seen in my machine learning course is

74
00:05:55,360 --> 00:05:58,740
that the typical answer is to set the first value to zero.

75
00:05:59,230 --> 00:06:04,930
This induces a bias in the moving average because if, let's say all your actual values are above one

76
00:06:04,930 --> 00:06:10,240
hundred, as they are in this case, the average is going to get pushed down from one hundred to zero

77
00:06:10,420 --> 00:06:13,080
as if zero were actually part of the data set.

78
00:06:13,840 --> 00:06:19,210
There is a way to correct this using a method called bias correction, which is used in modern optimization

79
00:06:19,210 --> 00:06:20,410
algorithms such as Atem.

80
00:06:20,620 --> 00:06:24,130
However, that's outside the scope of this course for us.

81
00:06:24,130 --> 00:06:27,250
We will simply use the method of copying the first value.

82
00:06:27,970 --> 00:06:34,000
Otherwise, when the length of manual WMA is greater than zero, we simply use our formula.

83
00:06:35,140 --> 00:06:39,060
We assign the result to X hat and then we append X had to our list.

84
00:06:39,730 --> 00:06:45,220
Finally, when we are outside this loop, we assign our list to a new column of the data frame called

85
00:06:45,220 --> 00:06:45,970
Emmanuel.

86
00:06:52,010 --> 00:06:53,990
Next, we do a doughnut plot again.

87
00:06:55,370 --> 00:07:00,060
We see that, indeed, our manual calculation appears to match what Panis does.

88
00:07:00,530 --> 00:07:04,750
Now you have a much better understanding of what Panis is doing under the hood.

89
00:07:08,280 --> 00:07:14,520
Not that we can also do a detached head to check how close the values actually are from the small print

90
00:07:14,520 --> 00:07:17,010
out, they appear to be exactly the same.

91
00:07:21,850 --> 00:07:26,800
Now, since these two columns are exactly the same, the next thing I'm going to do is remove the manual

92
00:07:26,800 --> 00:07:28,270
column since it's redundant.

93
00:07:28,780 --> 00:07:32,110
This will help us clean up our data frame for the next few lectures.
