 Hello everyone. Today I wanted to talk about a concept that data scientists tend to tackle in their day jobs, time series analysis. Now I don't talk too much about my personal or even my professional life outside of YouTube, but now that I've worked as a full-time associate data scientist for about a year, I think I can spice up my content with some experience. This could be useful to you as a lot of you, as I've seen that are not just ML curious, you also want to get a full-time job in the field, and some of you, from at least the comments that I read, are already established professionals. But regardless, always excited to talk to new people and everyone is welcome equally. Looking at resources across the internet that talk about time series problems, they almost always take a traditional approach eyes closed. Throw all the data into some Arima model and some magic happens. Unfortunately, these models can be very difficult to tune if you aren't an expert, but luckily you really don't need to be an expert with dissecting time series concepts specifically to get usable results from a time series model. In fact, you can take some machine learning approach instead and also get results that are just as good. In this video, we are going to walk through the typical flow to solve time series problems. We're going to see what different approaches we can take to solve such time series problems and also highlight the differences between traditional approaches and machine learning approaches. So you know what techniques to use and also when to use them. But before we continue, this video is sponsored partially by Kite. They provide a code completion service for machine learning code. It integrates super well with your editors and even Jupyter notebooks. So click the link in the description to try Kite for free. Now back to the video. Let's first define a concrete problem where time series is useful. So that's step one. Think about your grandma. She started this laptop repair line two years ago and it's a hit. The way it works is a customer places an order request online. The customer then ships the broken laptop to grandma. Then she and her workers fix them and the laptops are sent back. The problem here is her workers are paid by the hour. If there are more laptops to repair, grandma calls in more workers. But as you can imagine, it's hard to know how many workers we need without knowing the number of laptops that we get per day. Now grandma hired you as a data scientist and you think what could be useful to know for grandma is how many laptops are we going to receive tomorrow. This way we can call the required number of workers to come in tomorrow. So now that we have this defined problem, I think it's easier to move forward. So step two. What data do we have? For now, let's say that we store every work order in an orders table. When a customer makes an order request online, a row in the orders table is added. This table has information like order ID, the price and the timestamp when the order was made. For simplicity, let's say that there are no log errors, no missing values and no sparse data. So step three. What is the data telling us? Well, from here we do some exploratory data analysis or EDA and come up with a few approaches. The natural structure of this problem is a time series problem. From the orders table we can aggregate the data at the daily level to get a time series of number of order requests per day. When doing our exploratory analysis, I'd like to understand the bread and butter of the time series that is trend and seasonality. So first of all, does this data exhibit seasonality? For this case, let's say that we see some weekly seasonality in the data. Basically, we're hit relatively hard on Mondays, low on Thursdays and so on. So weekly seasonality exists. Now question two, do we see trend in this data? Over time, we'd probably want to know if the orders have been increasing or decreasing in volume. We also want to explain the trend changes that we see. Like in the middle of July, we see a huge increase in the trend of sales. And this probably happens because we launched a promotional event that month. And we see another trend change in October. And mostly because we changed our payment strategy. Now that we have like a basic understanding of our data, how do we make predictions? If we think of this as a traditional time series forecasting problem, there are several approaches like ARIMA, profit, neural profit and vector auto regression. Let's briefly talk about these. So to make a prediction in ARIMA, we need to identify the trend and seasonality components and transform our data accordingly. This Kaggle notebook here does a good job in following the step-by-step procedure to use ARIMA. But all of this processing and testing is only good for a basic toy data set here. It might be a little tough to get these predictions right and tuned for more complex problems, especially if you aren't an expert with time series in general. As an alternative, we could use Facebook's profit model. Profit handles missing data better and it can take data with seasonality and trends, and it produces stellar results that even rival a tuned ARIMA and its other flavors like seasonal ARIMA. We can get even better results using neural profit. This increases the forecasting accuracy of the profit model by using a neural network. Scrolling down here, I can see some main features that are added to this model. But a big disadvantage of profit models, at least from what I've seen, is that we cannot add regressors for which we don't have future values. Okay, so let me explain this with grandma's laptop company. We want to know how many laptops will we get tomorrow. To make this prediction, we use the number of laptops we got yesterday, two days ago, three days ago, and so on. Aside from that, we can pass in a day of week predictor for, you know, some seasonality. And we can pass this in because for tomorrow, we already know what the day will be, even though the day hasn't happened yet. But you know what else could be useful in predicting the number of laptops tomorrow? It would be something like the number of orders that were placed online over the past few days. Laptops come in about three to four days after the orders are placed online. So using order information could come in quite handy to determine the number of laptops you'll receive. However, we cannot add this order information directly to the profit model, since we cannot feed it a predictor for which we don't have the future value. I can't tell you the number of orders that happen tomorrow, since it hasn't happened yet. In fact, the number of orders per day forms another time series on its own too. Profit and ARIMA models, they fall into the category of univariate models. We forecast and we only deal with a single time series. But with multivariate time series models, we can deal with and even forecast the output of multiple time series. An example of this is vector auto regression models. The input could be the past inbound volume time series, the past order time series and some day of week predictors. And the output could be the forecasted inbound volume and the forecasted order volume. The main downside here for vector auto regression is that we might need a lot more quality data to come up with reasonable predictions as opposed to the univariate approaches since we're forecasting multiple series here. But depending on your situation and your data, one or the other may be useful. Now, all of these methods that we've discussed so far are approaches where we treat the problem as a time series problem. But we could very well convert this to a traditional machine learning regression problem. So in grandma's laptop example, if we're making prediction today, the features could be something like how many laptops did we get this day last week? What was the standard deviation of the inbound volume over the last week? What was the number of orders what was the number of orders that have been placed but haven't been fulfilled yet? Can we also add in day of week predictors to account for the weekly seasonality and almost any other predictor and anything else that you can think of? The label is what we want to predict. And in this case, it is the inbound volume for tomorrow. And we'd want to frame our training data in this way, a set of X's and Y's. So what kind of models are we talking about here? Let's paint this picture in a hierarchy in the form of a chart. All the models for which we can do time series modeling can be classified as traditional time series models and machine learning models. Time series models can be further classified as univariate and multivariate depending on the number of values we are predicting. Univariate time series models, which predict the output of one time series, they can be Arima models, they could be the Sarima or that's seasonal Arima models, which is basically an Arima model that takes into account seasonality. They can also be like profit models, which is Facebook solution to time series forecasting that you can use without being an expert in time series analysis. Then we also have neural profit, which is the neural network version of profit. And then we have also multivariate time series models. So we can forecast multiple time series. An example of this would be the vector auto regression models that I talked about. And this, along with the other models, actually have good implementations in the Python's stats models library. So if you're looking to code this out, check out stats models. And now on the machine learning front, we can basically use any type of regression model to do the time series job. So it could be a neural net regressor, where these are neural networks with one output neuron that determines the label of your regression. We can even use something like cat booster aggressors. This is pretty cool because it allows for better feature engineering like random forest and scikit-learn. It also gives really cool diagrammatic representations of feature importance. It's really easy to check if there's overfitting. But unlike, you know, random forest decision trees, it's actually in better model altogether, based off of the gradient boosted decision trees, actually. So it's totally worth checking out cat boost. And honestly, any other regression model could work here too. Now that we've outlined some examples, let's look at the core differences between traditional time series models and machine learning models for time series forecasting. Traditional time series forecasting is recursive. In grandma's company, we thought making predictions for tomorrow would be enough, but it looks like the workers need more of a heads up than just a single day. So she now wants to know the number of laptops that we will receive three days from now. Now to determine the inbound volume three days from now, the traditional time series way would be that we determine the inbound volume one day from now, use that to determine the two day out prediction, and then use this to determine the three day out prediction. The machine learning approach, though, we can forecast this directly. So we would directly know the three day out forecast if our model is trained to do so. So here's the second difference. Time series models are easily extendable. So now in grandma's warehouse, we also need to determine the long term space arrangements in the warehouse. But to do this, we need to know the inbound volume 10 days in advance. Well, that's okay with our traditional time series model, because we just need to keep recursively making predictions until we get the 10 day out volume. No change in training data, no change in the model. Now, if we want to do that for machine learning model, though, we need to modify our training data. We need to train the model to predict 10 days out to in addition to the three days out model. And this could scale the training data linearly as we have more horizons to predict. And the third difference, well, with traditional time series approaches, they can be pretty tough to get right unless you're an expert with time series models. While the machine learning approach is a lot more tractable for people who don't know much about time series forecasting. Although I will say the profit models are an exception here. Now a fourth difference. So time series models, at least the unit variant ones, we can't add regressors for which we don't know the future values. While the machine learning models, we can add these regressors that allows us to better fine tune the models. So clearly, each has their advantages and disadvantages. And depending on the problem you're solving, the data that you have, and the hardware capacity, one of these solutions may be more preferable than the other. Hope this gives you a better intuition on different approaches to time series forecasting. You don't need to be an expert. I have a year in the field and I'm just getting my feet wet. Everything in this video is my experience dealing with time series problems. So let me know in the comments below what your experiences are with time series forecasting. Would love to listen to them. I'll see you in the next one. Take care till then. Bye bye.