1
00:00:11,140 --> 00:00:15,440
OK, so in this lecture, we are going to do a brief preparation for profit.

2
00:00:15,970 --> 00:00:18,850
You'll find that it's kind of like Cycad learn, but not really.

3
00:00:19,600 --> 00:00:25,300
The most remarkable thing is that profit does a lot of useful stuff for you with just single function

4
00:00:25,300 --> 00:00:25,830
calls.

5
00:00:26,230 --> 00:00:29,670
So it's very easy to use and very fast to get things done.

6
00:00:34,390 --> 00:00:40,600
OK, so let's start with how to create the model now, for some reason, the Facebook profit documentation

7
00:00:40,600 --> 00:00:47,000
uses the letter M as a variable to represent the model object instead of a full word like model.

8
00:00:47,500 --> 00:00:48,820
So just keep that in mind.

9
00:00:48,820 --> 00:00:53,440
If you're one of those programmers that always complains, when people use a single letter, variable

10
00:00:53,440 --> 00:00:57,100
names, you can take that up with the profit developers yourself.

11
00:00:58,030 --> 00:01:04,030
OK, so the simplest way to create a model is just to call the profit constructor with no arguments.

12
00:01:04,450 --> 00:01:06,060
This is fine a lot of the time.

13
00:01:06,280 --> 00:01:10,810
So if you want profit to handle everything for you automatically, just do this.

14
00:01:15,370 --> 00:01:18,980
Now, if you need some customization, let's go through a few options.

15
00:01:19,720 --> 00:01:23,720
Interestingly, there is no API reference for the Facebook profit library.

16
00:01:24,280 --> 00:01:27,520
So what I normally do is within CoLab or a Python.

17
00:01:27,820 --> 00:01:32,350
Recall that as you are typing out your code, the editor will give you suggestions.

18
00:01:32,350 --> 00:01:35,340
And in CoLab, the string will appear in a pop up.

19
00:01:35,920 --> 00:01:40,420
Basically, the way I found these arguments was by looking at what pops up in CoLab.

20
00:01:40,840 --> 00:01:44,440
Otherwise you'd have to look at the source code, which is a bit more tedious.

21
00:01:45,780 --> 00:01:49,860
OK, so the first argument is growth, which can be linear or logistic.

22
00:01:50,280 --> 00:01:53,380
I think this is pretty obvious given what we've already discussed.

23
00:01:53,880 --> 00:01:55,660
Note that the default is linear.

24
00:01:57,120 --> 00:02:02,600
The next argument is change points, which allows you to explicitly specify the change points.

25
00:02:03,000 --> 00:02:05,370
Otherwise, these will be chosen automatically.

26
00:02:07,030 --> 00:02:12,460
The next argument is and change points, which allows you to select the number of potential change points

27
00:02:12,460 --> 00:02:16,160
to include in the case where you haven't specified them explicitly.

28
00:02:16,960 --> 00:02:19,300
Note that the default value is twenty five.

29
00:02:21,330 --> 00:02:27,320
The next argument is change point range, so change points will be discussed in further detail later,

30
00:02:27,600 --> 00:02:33,210
but basically this is the percent of your data set where you want the potential change points to exist.

31
00:02:33,810 --> 00:02:36,330
This is by default, set to 80 percent.

32
00:02:36,900 --> 00:02:42,450
This means that no change points will occur in the final 20 percent of your time series.

33
00:02:42,990 --> 00:02:47,610
The reasoning behind this is it might overfit due to lack of data near the end.

34
00:02:49,710 --> 00:02:56,580
The next few arguments are for yearly seasonality, weekly seasonality and daily seasonality, the default

35
00:02:56,580 --> 00:03:01,980
argument for all of these is the string auto, but other acceptable values are true and false.

36
00:03:02,700 --> 00:03:06,560
Basically, this tells profit at what scale to look for seasonality.

37
00:03:07,260 --> 00:03:13,140
The simplest thing to do is to probably just let profit choose these automatically unless you get results

38
00:03:13,350 --> 00:03:15,090
and you want to customize your model.

39
00:03:17,060 --> 00:03:23,090
The next argument is holidays by default, this is set for none, but if you want to pass in your own

40
00:03:23,090 --> 00:03:27,570
holidays, you can pass in a specially formatted data frame with those dates.

41
00:03:28,100 --> 00:03:34,100
I recommend checking the profit documentation to see the exact format to use, since this allows you

42
00:03:34,100 --> 00:03:38,240
to specify surrounding days as well, among other interesting options.

43
00:03:40,310 --> 00:03:46,480
The next argument is seasonality mode, which can either be additive, which is the default or multiplicative,

44
00:03:48,260 --> 00:03:53,990
the next few arguments are seasonality, prior school holidays, prior scale and change point, prior

45
00:03:53,990 --> 00:03:54,470
scale.

46
00:03:55,040 --> 00:04:00,380
As you recall, the profit model is a Bayesian model which allows you to specify priors.

47
00:04:00,980 --> 00:04:03,990
Basically, you have to tune these based on the results you see.

48
00:04:04,520 --> 00:04:10,290
So if the default values aren't working for you, try other values until you get more reasonable results.

49
00:04:12,090 --> 00:04:18,030
OK, so there are a few more arguments which we will not discuss, which have to do with Mxy and uncertainty

50
00:04:18,030 --> 00:04:20,880
intervals which we haven't talked about in this course.

51
00:04:25,520 --> 00:04:31,280
OK, so once you've created your model, fitting the model is very simple, you simply call make fit

52
00:04:31,550 --> 00:04:32,690
passing in your data.

53
00:04:33,560 --> 00:04:38,320
Now, one detail you have to be aware of is that your data must have a specific format.

54
00:04:38,870 --> 00:04:46,510
This format is a data frame with two columns, D and a Y, and yes, they must have these exact names.

55
00:04:47,150 --> 00:04:52,430
So the D column is for the date or the date time and why is for the Time series itself.

56
00:04:53,360 --> 00:04:56,900
Note that the date should be specified in the usual format.

57
00:04:57,050 --> 00:05:02,770
See in programming, which is the full year dash, two digit month dash, two digit day.

58
00:05:03,290 --> 00:05:09,170
And if you have the timestamp as well, then you'd have a space, then a two digit hour call in two

59
00:05:09,170 --> 00:05:11,990
digit minute colon and two digit seconds.

60
00:05:12,500 --> 00:05:16,010
OK, so hopefully you've seen this before if you work in this field.

61
00:05:16,430 --> 00:05:21,920
And of course, since there is only one to Y column, it should be clear that profit is meant for univariate

62
00:05:21,920 --> 00:05:22,820
time series.

63
00:05:27,410 --> 00:05:30,060
OK, so the next step is to make predictions.

64
00:05:30,710 --> 00:05:35,500
This part is kind of confusing because it's unlike both second learn and stat's models.

65
00:05:36,230 --> 00:05:42,260
Basically the prediction takes place in several steps, but produces both in sample and out of sample

66
00:05:42,260 --> 00:05:44,210
predictions at the same time.

67
00:05:45,260 --> 00:05:48,840
So the first function you have to call is make future data frame.

68
00:05:49,610 --> 00:05:55,390
This will take in an argument called periods, which is essentially your forecast tourism and Freeth,

69
00:05:55,640 --> 00:06:01,790
which specifies the frequency of your predictions, the return value from this function as a data frame,

70
00:06:01,970 --> 00:06:03,980
which we will name future by convention.

71
00:06:04,760 --> 00:06:11,150
Basically, it's a data frame that contains only timestamps, both for the sample dates and the forecast.

72
00:06:12,790 --> 00:06:18,550
So if you're a time series is monthly going from January 20, 20, up to December twenty twenty and

73
00:06:18,550 --> 00:06:24,490
you want to forecast three months, then this future data frame will contain all the dates from January

74
00:06:24,490 --> 00:06:27,100
twenty twenty up to March twenty twenty one.

75
00:06:28,450 --> 00:06:34,660
The next step is do call me predict passing in the future data frame, which presumably tells the model

76
00:06:34,660 --> 00:06:36,550
which dates to make predictions for.

77
00:06:38,320 --> 00:06:44,380
This will return another data frame called forecast, the forecast data frame has a lot of information

78
00:06:44,380 --> 00:06:49,160
in it, in addition to the forecast itself, which comes into the column name White Hat.

79
00:06:49,840 --> 00:06:54,420
Basically, you get the prediction intervals, the trends and a bunch of other stuff as well.

80
00:06:59,110 --> 00:07:04,510
Now, once we have the forecast, it's very easy to do things you'd want to do with a forecast like

81
00:07:04,510 --> 00:07:06,580
plaudit and examine the components.

82
00:07:07,210 --> 00:07:12,770
So to plot the forecast, we simply call it passing in the forecast data frame.

83
00:07:13,360 --> 00:07:19,420
This will generate a very nice plot, including the original data, the model predictions and the prediction

84
00:07:19,420 --> 00:07:20,180
intervals.

85
00:07:20,740 --> 00:07:26,620
You can also call them plot components, which will plot the trend, seasonal components if they exist

86
00:07:26,770 --> 00:07:29,400
and holiday components if they exist.

87
00:07:29,890 --> 00:07:35,200
So, for example, if there's a peak at the end of the year due to holiday sales, you'll be able to

88
00:07:35,200 --> 00:07:36,960
see that clearly from this plot.

89
00:07:41,690 --> 00:07:47,810
So the next thing to discuss in this lecture is how profit does cross-validation, interestingly, the

90
00:07:47,810 --> 00:07:50,060
way it does cross-validation is very flexible.

91
00:07:50,330 --> 00:07:56,000
And just by coincidence, precisely how I described it in the walk forward validation lecture earlier

92
00:07:56,000 --> 00:08:01,720
on this course, this is kind of interesting because I wrote that lecture before I even looked at profit.

93
00:08:02,150 --> 00:08:06,040
So it's nice to know that the people behind profit had the same ideas.

94
00:08:07,100 --> 00:08:12,220
Basically, you'll recall that Time series cross-validation insight can learn is very limited.

95
00:08:12,650 --> 00:08:18,050
It requires every block to have the same size, which makes the initial train set very small and it

96
00:08:18,050 --> 00:08:21,470
does not allow them to overlap cross-validation.

97
00:08:21,470 --> 00:08:27,680
And profit works like how I described it before, where by default the initial training period is large,

98
00:08:27,830 --> 00:08:32,090
the horizon is smaller and you can walk for it in any size increments.

99
00:08:32,630 --> 00:08:39,020
So these are specified by the three arguments, initial period and horizon by default.

100
00:08:39,050 --> 00:08:44,810
The initial training period is set to three times the horizon and the period or the size of the walk

101
00:08:44,810 --> 00:08:47,390
forward step is half that of the horizon.

102
00:08:51,960 --> 00:08:53,520
OK, so how does this work?

103
00:08:54,150 --> 00:08:59,610
Basically, this is going to give you a data frame, which includes the columns you see here, the important

104
00:08:59,610 --> 00:09:03,480
columns for us are D y hat y and cut off.

105
00:09:04,230 --> 00:09:09,480
We can ignore why had lower and why had upper, since those aren't used to compute metrics such as the

106
00:09:09,480 --> 00:09:11,220
map, masc and so forth.

107
00:09:12,090 --> 00:09:13,860
Now why hadn't why are obvious.

108
00:09:13,860 --> 00:09:15,980
These are just the predictions and the targets.

109
00:09:16,410 --> 00:09:18,180
But what about cutoff ends?

110
00:09:19,080 --> 00:09:24,360
Well as you recall, because our walk forward step can be of any size, this means that our predictions

111
00:09:24,360 --> 00:09:28,370
can overlap in the case with a step size is smaller than the horizon.

112
00:09:29,190 --> 00:09:35,400
So the cutoff date represents the day right before the first forecast and we're assuming our time increments

113
00:09:35,400 --> 00:09:35,890
are days.

114
00:09:35,910 --> 00:09:37,260
So just keep that in mind.

115
00:09:38,670 --> 00:09:44,190
So D represents the date of the forecast, so hopefully that makes sense.

116
00:09:44,760 --> 00:09:51,330
D represents the date which the prediction is for and cutoff represents the date from which the prediction

117
00:09:51,330 --> 00:09:52,290
is being made.

118
00:09:53,640 --> 00:09:59,490
So, for example, if your horizon is five and your cutoff is December thirty one, then you're down

119
00:09:59,550 --> 00:10:03,960
for this particular forecast will be January one, up to January five.

120
00:10:05,280 --> 00:10:11,370
And of course, this data frame will contain every such forecast for every possible cutoff point specified

121
00:10:11,370 --> 00:10:12,430
by your function call.

122
00:10:13,050 --> 00:10:18,000
In other words, you have a prediction for every time you walk forward in your walk forward validation.

123
00:10:22,690 --> 00:10:28,720
OK, so once you retrieve the result from cross-validation, you can compute metrics and plot the results.

124
00:10:29,470 --> 00:10:34,160
So to compute metrics, you simply need to call a function called performance metrics.

125
00:10:34,630 --> 00:10:40,200
This will return the MSI, our embassy, maybe Map SMAP and a few more things.

126
00:10:40,780 --> 00:10:44,720
Now, unfortunately, the way these results are presented is a bit strange.

127
00:10:45,310 --> 00:10:51,170
So imagine that we are making a forecast over 60 days because we're doing cross-validation.

128
00:10:51,220 --> 00:10:56,590
We'll have multiple forecasts, one step ahead, multiple forecasts, two steps ahead and so forth,

129
00:10:56,590 --> 00:10:57,830
all the way up to 60.

130
00:10:58,600 --> 00:11:04,420
The way these results are presented is in a single table with the moving average of the metrics.

131
00:11:04,930 --> 00:11:10,930
Now, by default, the moving average uses a rolling window, which is 10 percent the size of the horizon.

132
00:11:11,590 --> 00:11:17,060
So, for example, if your horizon is 60 steps, then the rolling window will be a size six.

133
00:11:17,680 --> 00:11:22,000
Note that this means there will be no metrics presented for the first five steps.

134
00:11:26,530 --> 00:11:32,560
The next thing to discuss is that we can plot the cross-validation matrix using the function of plot

135
00:11:32,560 --> 00:11:38,800
cross-validation metrick, this will plot the ROM matrix as a scatterplot along with the moving average

136
00:11:38,950 --> 00:11:40,910
as discussed on the previous slide.

137
00:11:41,830 --> 00:11:48,530
The idea behind this is essentially that we expect the error metric to increase as the horizon increases.

138
00:11:49,000 --> 00:11:54,010
This makes sense since the further out our prediction is, the more wrong we will potentially be.

139
00:11:58,710 --> 00:12:04,290
OK, now the final topic I want to discuss in this lecture is how to do change point detection.

140
00:12:05,100 --> 00:12:09,770
Like the other functionality we've discussed, this is essentially just a single function call.

141
00:12:10,710 --> 00:12:14,700
Note that the change points are already determined when you call fit.

142
00:12:15,750 --> 00:12:21,260
Thus, the only step left is to plot the change points along with the trend line in between the change

143
00:12:21,270 --> 00:12:21,810
points.

144
00:12:22,380 --> 00:12:28,080
So when we call the function add change points to plot, this will plot both the change points and the

145
00:12:28,080 --> 00:12:28,940
trend lines.

146
00:12:29,490 --> 00:12:33,480
This will let you evaluate whether or not the model has found something reasonable.

147
00:12:33,690 --> 00:12:36,690
Or perhaps you'll need to go back and modify the Pryors.
