OK, so in this lecture, we are going to do a brief preparation for profit.
You'll find that it's kind of like Cycad learn, but not really.
The most remarkable thing is that profit does a lot of useful stuff for you with just single function
calls.
So it's very easy to use and very fast to get things done.
OK, so let's start with how to create the model now, for some reason, the Facebook profit documentation
uses the letter M as a variable to represent the model object instead of a full word like model.
So just keep that in mind.
If you're one of those programmers that always complains, when people use a single letter, variable
names, you can take that up with the profit developers yourself.
OK, so the simplest way to create a model is just to call the profit constructor with no arguments.
This is fine a lot of the time.
So if you want profit to handle everything for you automatically, just do this.
Now, if you need some customization, let's go through a few options.
Interestingly, there is no API reference for the Facebook profit library.
So what I normally do is within CoLab or a Python.
Recall that as you are typing out your code, the editor will give you suggestions.
And in CoLab, the string will appear in a pop up.
Basically, the way I found these arguments was by looking at what pops up in CoLab.
Otherwise you'd have to look at the source code, which is a bit more tedious.
OK, so the first argument is growth, which can be linear or logistic.
I think this is pretty obvious given what we've already discussed.
Note that the default is linear.
The next argument is change points, which allows you to explicitly specify the change points.
Otherwise, these will be chosen automatically.
The next argument is and change points, which allows you to select the number of potential change points
to include in the case where you haven't specified them explicitly.
Note that the default value is twenty five.
The next argument is change point range, so change points will be discussed in further detail later,
but basically this is the percent of your data set where you want the potential change points to exist.
This is by default, set to 80 percent.
This means that no change points will occur in the final 20 percent of your time series.
The reasoning behind this is it might overfit due to lack of data near the end.
The next few arguments are for yearly seasonality, weekly seasonality and daily seasonality, the default
argument for all of these is the string auto, but other acceptable values are true and false.
Basically, this tells profit at what scale to look for seasonality.
The simplest thing to do is to probably just let profit choose these automatically unless you get results
and you want to customize your model.
The next argument is holidays by default, this is set for none, but if you want to pass in your own
holidays, you can pass in a specially formatted data frame with those dates.
I recommend checking the profit documentation to see the exact format to use, since this allows you
to specify surrounding days as well, among other interesting options.
The next argument is seasonality mode, which can either be additive, which is the default or multiplicative,
the next few arguments are seasonality, prior school holidays, prior scale and change point, prior
scale.
As you recall, the profit model is a Bayesian model which allows you to specify priors.
Basically, you have to tune these based on the results you see.
So if the default values aren't working for you, try other values until you get more reasonable results.
OK, so there are a few more arguments which we will not discuss, which have to do with Mxy and uncertainty
intervals which we haven't talked about in this course.
OK, so once you've created your model, fitting the model is very simple, you simply call make fit
passing in your data.
Now, one detail you have to be aware of is that your data must have a specific format.
This format is a data frame with two columns, D and a Y, and yes, they must have these exact names.
So the D column is for the date or the date time and why is for the Time series itself.
Note that the date should be specified in the usual format.
See in programming, which is the full year dash, two digit month dash, two digit day.
And if you have the timestamp as well, then you'd have a space, then a two digit hour call in two
digit minute colon and two digit seconds.
OK, so hopefully you've seen this before if you work in this field.
And of course, since there is only one to Y column, it should be clear that profit is meant for univariate
time series.
OK, so the next step is to make predictions.
This part is kind of confusing because it's unlike both second learn and stat's models.
Basically the prediction takes place in several steps, but produces both in sample and out of sample
predictions at the same time.
So the first function you have to call is make future data frame.
This will take in an argument called periods, which is essentially your forecast tourism and Freeth,
which specifies the frequency of your predictions, the return value from this function as a data frame,
which we will name future by convention.
Basically, it's a data frame that contains only timestamps, both for the sample dates and the forecast.
So if you're a time series is monthly going from January 20, 20, up to December twenty twenty and
you want to forecast three months, then this future data frame will contain all the dates from January
twenty twenty up to March twenty twenty one.
The next step is do call me predict passing in the future data frame, which presumably tells the model
which dates to make predictions for.
This will return another data frame called forecast, the forecast data frame has a lot of information
in it, in addition to the forecast itself, which comes into the column name White Hat.
Basically, you get the prediction intervals, the trends and a bunch of other stuff as well.
Now, once we have the forecast, it's very easy to do things you'd want to do with a forecast like
plaudit and examine the components.
So to plot the forecast, we simply call it passing in the forecast data frame.
This will generate a very nice plot, including the original data, the model predictions and the prediction
intervals.
You can also call them plot components, which will plot the trend, seasonal components if they exist
and holiday components if they exist.
So, for example, if there's a peak at the end of the year due to holiday sales, you'll be able to
see that clearly from this plot.
So the next thing to discuss in this lecture is how profit does cross-validation, interestingly, the
way it does cross-validation is very flexible.
And just by coincidence, precisely how I described it in the walk forward validation lecture earlier
on this course, this is kind of interesting because I wrote that lecture before I even looked at profit.
So it's nice to know that the people behind profit had the same ideas.
Basically, you'll recall that Time series cross-validation insight can learn is very limited.
It requires every block to have the same size, which makes the initial train set very small and it
does not allow them to overlap cross-validation.
And profit works like how I described it before, where by default the initial training period is large,
the horizon is smaller and you can walk for it in any size increments.
So these are specified by the three arguments, initial period and horizon by default.
The initial training period is set to three times the horizon and the period or the size of the walk
forward step is half that of the horizon.
OK, so how does this work?
Basically, this is going to give you a data frame, which includes the columns you see here, the important
columns for us are D y hat y and cut off.
We can ignore why had lower and why had upper, since those aren't used to compute metrics such as the
map, masc and so forth.
Now why hadn't why are obvious.
These are just the predictions and the targets.
But what about cutoff ends?
Well as you recall, because our walk forward step can be of any size, this means that our predictions
can overlap in the case with a step size is smaller than the horizon.
So the cutoff date represents the day right before the first forecast and we're assuming our time increments
are days.
So just keep that in mind.
So D represents the date of the forecast, so hopefully that makes sense.
D represents the date which the prediction is for and cutoff represents the date from which the prediction
is being made.
So, for example, if your horizon is five and your cutoff is December thirty one, then you're down
for this particular forecast will be January one, up to January five.
And of course, this data frame will contain every such forecast for every possible cutoff point specified
by your function call.
In other words, you have a prediction for every time you walk forward in your walk forward validation.
OK, so once you retrieve the result from cross-validation, you can compute metrics and plot the results.
So to compute metrics, you simply need to call a function called performance metrics.
This will return the MSI, our embassy, maybe Map SMAP and a few more things.
Now, unfortunately, the way these results are presented is a bit strange.
So imagine that we are making a forecast over 60 days because we're doing cross-validation.
We'll have multiple forecasts, one step ahead, multiple forecasts, two steps ahead and so forth,
all the way up to 60.
The way these results are presented is in a single table with the moving average of the metrics.
Now, by default, the moving average uses a rolling window, which is 10 percent the size of the horizon.
So, for example, if your horizon is 60 steps, then the rolling window will be a size six.
Note that this means there will be no metrics presented for the first five steps.
The next thing to discuss is that we can plot the cross-validation matrix using the function of plot
cross-validation metrick, this will plot the ROM matrix as a scatterplot along with the moving average
as discussed on the previous slide.
The idea behind this is essentially that we expect the error metric to increase as the horizon increases.
This makes sense since the further out our prediction is, the more wrong we will potentially be.
OK, now the final topic I want to discuss in this lecture is how to do change point detection.
Like the other functionality we've discussed, this is essentially just a single function call.
Note that the change points are already determined when you call fit.
Thus, the only step left is to plot the change points along with the trend line in between the change
points.
So when we call the function add change points to plot, this will plot both the change points and the
trend lines.
This will let you evaluate whether or not the model has found something reasonable.
Or perhaps you'll need to go back and modify the Pryors.