AI at Rescue: Demand Forecasting
Last Updated on July 10, 2022 by Editorial Team
Author(s): Supreet Kaur
Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.
Demand Forecasting is a field of predictive analytics that predicts customer demand based on historical data and other related variables to drive the supply chain decision-making process.
Machine Learning models can come to your rescue to predict accurate demand. Many models are available for your disposal, but it is essential to tailor the model as per the industry and data inΒ hand.
This blog is an end-to-end guide on leveraging AI to perform demand forecasting, including preparing your data, choosing the models, and measuring their accuracy.
Impact of accurate Demand Forecasting
- Minimize wastage ofΒ products
- Reduce Inventory Costs
- Assists in planning the production of aΒ product
- Enhancing customer experience by providing the product exactly when it isΒ needed
Preparing yourΒ Data
All of us are aware of the concept βGarbage In, Garbage Out.β Hence it is inevitable to prepare your data so that it is ready to be ingested by the ML or statistical models.
Here are a few primary attributes you would require in your dataset to perform demand forecasting:
- Unique ID(Customer/Patient)
- Product Name
- A number of unitsΒ sold.
Note: This can vary from industry to industry. You must ensure that the scale is consistent in the dataset. Example: It could be the SKU of a particular product
4. Timestamp
Once you have the dataset, it is crucial to observe a few things before you proceed to your MLΒ models:
- Trend: A trend is observed to gauge whether it is an increasing or decreasing demand. Suppose you observe some unexplained highs or lows. In that case, it is crucial to analyze those outliers before performing any model, as it can create unnecessary bias in yourΒ dataset.
- Seasonality: Seasonality is influenced by a specific time of the year. For example, retail companies observe a spike in sales during the holiday season, which is cyclic, i.e., it repeats every year at the same time. An important thing to note here is that a few events like COVID influenced the sales as people hoarded, but that was one of a kind, so we canβt consider it a seasonalΒ pattern.
- Noise: This is a common observation in time-series datasets. You might observe a particular trend, but at the same time, you might see a few random points(outliers) that any business justification cannotΒ explain.
Machine LearningΒ Models
The golden rule to building any AI-driven solution is to start with vanilla models and analyze if it is a good fit for your data. This section will cover both vanilla and advanced ML algorithms:
- ARIMA
The ARIMA is a statistical technique to forecast time series data. It stands for Autoregressive Integrated Moving Average. Any machine learning model is autoregressive if it predicts future values based on past weights. Letβs break it downΒ further:
- AR(Autoregression): represents the use of previous data to predict the nextΒ value
- I(Integrated): subtracting a given value with the previous value to make the time series stationary
- MA(Moving Average): leveraging the error terms themselves to predict futureΒ values
The parameter of ARIMA is as follows and should be chosen wisely based on yourΒ data:
1. P: Number of lag observations(previous data) in theΒ model
2. D: Number of times raw observations are differenced
3. Q: Order of MovingΒ Average
In R or python, you can use Auto-Arima to specify a range for the above hyperparameters, and it will choose the best one catering to your dataset on AIC and BICΒ values.
Advantages
- Easy to interpret
- Ideal for short-term forecasts
Limitations
- Poor performance in case of long-term forecasts
- It canβt be leveraged for seasonal timeΒ series
2. HOLT-WINTERβS METHOD
Holt Winterβs method is called triple exponential smoothing as the three components of time series behavior, such as value(average), trend(slope), and seasonality, are expressed as three types of exponential smoothing. The model outputs the forecast by computing the combined effort of these three influences.
The parameters required for this modelΒ are:
- (Ι, Ξ², Ξ³) one for each smoothing
- The length of aΒ season
- The number of periods in aΒ season
The smoothing is applied across seasons, e.g., the seasonal component of the 3rd point into the season would be exponentially smoothed with the one from the 3rd point of last season, the 3rd point two seasons ago, etc. In math notation, we now have four equations (see footnote):
βx=Ξ±(yxβsxβL)+(1βΞ±)(βxβ1+bxβ1)
bx=Ξ²(βxββxβ1)+(1βΞ²)bxβ1
sx=Ξ³(yxββx)+(1βΞ³)sxβL
y^x+m=βx+mbx+sxβL+1+(mβ1)modL
Holtβs winterβs method is an extension of exponentially weighted moving average methods (EWMA). The EWMA is not suitable for time series with a linear trend. Holt-Winters method uses exponential smoothing to encode values from the past and then use them to predict typical values from theΒ future.
Advantages
- Provides accurate prediction on actual-world data as it considers seasonality and trend, which is inevitable in correct worldΒ data
Limitations
- The model might not be the best fit when we have time frames with meager amounts, such as a time frame with a data point of 10 or 1 might have an actual difference of 9, but there is a relative difference of aboutΒ 1000%.
3. PROPHET byΒ Facebook
The prophet is a library in R and Python equipped to forecast time series. It is mighty to capture change points such as the rise and fall of COVID cases. You also have the flexibility to control the power of these changepoints.
The parameters include:
- growth: choose between βlinearβ or βlogisticβ trend
- changepoints: List of dates accommodating potential changepoints (automatic if not specified)
- n_changepoints: If changepoints are not supplied, provide the number of changepoints to be automatically included
- changepoint_prior_scale: Parameter for changing flexibility of automatic changepoint selection
Advantages
- Flexibility to control uncertainty, trend, changepoint, and holidayΒ effect
- Easy to interpret and use even for people with little or no knowledge of data science/forecasting
Limitations
- Low Accuracy
- Unable to detect causal relationships between the past and futureΒ data
- Lacks the ability to forecast data with weak seasonality
4. LSTMβs
Long Short-Term Memory RNNβs are known for learning long-term sequences. It does not depend on pre-specified window lagged observation as input. RNNs suffer the loss of information due to vanishing gradient problems. Still, LSTMs donβt suffer from this issue as recurrent weights array is replaced by identity function and controlled by a series of gates such as input, output, and forgetΒ gate.
Few data preparation steps are needed to pursue before prepping thisΒ model:
Β· Make the dataset stationary: You can do this by simple techniques like differencing.
Β· Transform the data as per the scale of the activation function
Advantages
Β· HigherΒ accuracy
Validating your Model/ Measuring Accuracy
You can use different calculations to measure the accuracy of your model depending on the industry and data. Some of the standard measures are asΒ follows:
1. MAPE
MAPE (Mean Absolute Percentage Error) measures the size of the error in percentage terms. The MAPE should not be used with low-volume data as it will take on extremeΒ values.
(1/nβ|Actual-Forecast|/|Actual|) *100
2. RMSE
Root Mean Squared Error is one of the most used measures to evaluate the quality of predictions. It shows how far predictions fall off when measured from actual values using Euclidean distance. This is a good measure for LSTM forecasts.
N is the number of data points, y(i) is the ith measurement, and y Μ(i) is its corresponding prediction.
N is the number of data points, y(i) is the ith measurement, and y Μ(i) is its corresponding prediction
Happy Reading!
References:
https://machinelearningmastery.com/arima-for-time-series-forecasting-with-python/
https://www.analyticsvidhya.com/blog/2021/07/abc-of-time-series-forecasting/
https://www.rdocumentation.org/packages/forecast/versions/8.16/topics/auto.arima
https://grisha.org/blog/2016/02/17/triple-exponential-smoothing-forecasting-part-iii/
https://www.capitalone.com/tech/machine-learning/understanding-arima-models/
https://medium.com/swlh/facebook-prophet-426421f7e331
https://analyticsindiamag.com/why-are-people-bashing-facebook-prophet/
https://machinelearningmastery.com/time-series-forecasting-long-short-term-memory-network-python/
How do I measure forecast accuracy?
https://www.analyticsvidhya.com/blog/2018/05/generate-accurate-forecasts-facebook-prophet-python-r/
AI at Rescue: Demand Forecasting was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
Join thousands of data leaders on the AI newsletter. Itβs free, we donβt spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI