So in this lecture, we will be looking at the concepts behind Facebook profit.
Yes, we know it's a Time series modeling and forecasting tool, but we'd like to have a bit of detail
about how it works.
Now, if you're not really concerned with how it works, then feel free to skip ahead to the code preparation.
However, BE warns that not knowing how it works has led to silly use cases of this library, for instance,
predicting stocks.
In fact, we will go through such an example later in this section to show you that this will actually
perform poorly.
So that's one downside of only looking at the API without any knowledge of how the model works, you
may end up doing things that don't make sense.
However, what I think you will find is that the principles of profit are actually quite simple and
intuitive.
We're not going to go over any mathematical details in depth, but just a high level overview.
In fact, Facebook's on paper also does not go into these details.
So it's one of those things where if you really want to know the low level details, you just have to
check the code.
It is open source, so it's available for you to do that.
Otherwise, I think the high level principles are good enough for most of us.
Please note that the paper has been linked in extra reading.
It's called forecasting at scale.
So check that out if you want to read the primary source material.
OK, so as you know, the Facebook profit package is optimized for Business Time series, this means
that a lot of its features are geared towards complexities that businesses have to deal with.
One example is seasonality at multiple scales.
For example, you may have weekly seasonality where you have less customers on Sundays, but you also
may have yearly seasonality based around holidays such as Christmas, New Year's, Valentine's Day and
so forth.
Another example is events which are cyclical but not exactly seasonal with the predefined frequency.
Some examples are Thanksgiving, Black Friday and Easter.
You may also have events which are based on the lunar calendar, such as the Lunar New Year.
This would be difficult to model with the other tools we've learned about, but profit makes this kind
of thing easy.
Another feature that prophet can model is changes in trend, this may happen if, for example, you
create a new website design or perhaps you released some new products.
Yet another issue that Facebook profit can easily deal with is outliers and missing data.
We'll see how these can be dealt with quite elegantly.
OK, so the one and only equation you really need to know in order to understand how Facebook profit
works is this it says that our model of the Time series is simply the addition of three components,
along with an unpredictable error term.
The three components are the trend that the seasonality and the holidays.
So although some seasonality could be due to holidays, you can see that these are modeled separately.
So in this case, Gifty represents the trend, which represents the non periodic changes in the series.
S of T represents periodic changes which may happen at different scales.
For example, weekly or yearly HFT represents holidays which could be irregular and could occur over
more than one day.
Note that like other many time series models, Epsilon T is assumed to come from a normal distribution.
One interesting fact, which is not totally obvious at this point, is that this model is not auto regressive,
but rather the paper states that time itself is the only agressor.
We'll see exactly what this means when we examine each of the components in more detail.
One consequence of this is that missing data is immaterial.
No prediction depends on any other value, but only some equation based on time itself.
Again, if this is not clear, you'll see what we mean by this in the next few slides.
OK, so let's start by discussing the trend G of T. So essentially there are two ways that profit models
trends.
The first method is piecewise linear.
This automatically presumes that the model will also figure out the boundaries of each linear piece,
something we call change point detection.
The second method is logistic growth or decay.
So an example of this is covid-19.
Of course, there is a limit to the number of people who can be infected since there is a limited population.
Another example is sales of a product.
The sales of a product would most likely be limited by the size of the market.
The most basic form of logistic growth is shown by the equation here, which you might recognize as
the sigmoid from deep learning.
The value of C is the maximum and in profits terminology, this is called the carrying capacity.
Now, the actual equations representing piecewise linear trend and logistic trend are a bit more complex,
but they follow the same usual forms.
Note that for both of these equations, you can see that they are not auto regressive, unlike Arima
and the other models we've studied.
Instead, you can see that the time t is the only input argument.
So for the linear case, we can see that it has the usual form slope multiplied by input plus intercept
for the logistic case.
If we squint really hard, we can see the same form we saw earlier.
It just allows for some adjustment and the carrying capacity, rate of change and the offset.
These aren't too important for our use case, but they may be useful if you want to explain the model
to your clients.
So have a look at the paper and read about these other parameters.
If your clients are interested in those details.
So for the seasonal component, we have this interesting equation which should look familiar if you're
an engineer or a physicist, basically this is just a four year series.
If you're not an engineer or a physicist, that's OK, too.
You can see that it's just the sum of science and cosigns, which, of course, produce a periodic time
series.
What's important to note about this is it's the sum of multiple signs and cosigns, and each term in
the sum has a different period thanks to the argument to buy A..
Over P in this case, Big P is like the overall period.
So it's three sixty five point two five four yearly data and seven for weekly data.
When the time variable is scaled in days, the parameters to be optimized are the A's and B's, which
are found by optimizing some lost function as per usual.
OK, so the final component is the holiday component, which is essentially represented as a one hot
vector, thus the influence of any single holiday is governed by a single parameter, which is additive.
Note that the paper mentions that the model also accounts for days surrounding the holiday, but it
doesn't specify exactly how.
OK, so one interesting aspect of the profit model is that it is Bayesian.
This is not obvious from the way it's been presented so far, but all of the model parameters have priors.
Behind the scenes, profit is built using STEM, which is a library for Bazian Machine Learning, although
that sounds complex and allows for some human in the loop optimization.
So in other words, you can include your client in the modeling process.
For example, if you find that the change points for the trends are too sensitive, you can try different
settings for the priors governing the change points to fit the expectations of your client.