CrostonClassic Model
Stepbystep guide on using the CrostonClassic Model
with Statsforecast
.
Table of Contents
 Introduction
 Croston Classic Model
 Loading libraries and data
 Explore data with the plot method
 Split the data into training and testing
 Implementation of CrostonClassic with StatsForecast
 Crossvalidation
 Model evaluation
 References
Introduction
The Croston model is a method used in time series analysis to forecast demand in situations where there are intermittent data or frequent zeros. It was developed by J.D. Croston in 1972 and is especially useful in industries such as inventory management, retail sales, and demand forecasting for products with low sales frequency.
The Croston model is based on two main components:

Intermittent Demand Rate: Calculates the demand rate for periods in which sales or events occur, ignoring periods without sales. This rate is used to estimate the probability that a claim will occur in the future.

Demand Interval: Calculates the time interval between sales or events occurring, again ignoring nonsales periods. This interval is used to estimate the probability that a demand will occur in the next period.
The Croston model combines these two estimates to generate a weighted forecast that takes into account both the rate of intermittent demand and the interval between demands. This approach helps address the challenge of forecasting demand in situations where the time series has many zeros or missing values.
It is important to note that the Croston model is a simplification and does not account for other possible sources of variability or patterns in the demand data. Therefore, its accuracy may be affected in situations where there are external factors or changes in demand behavior.
Croston Classic Model
What is intermittent demand?
Intermittent demand is a demand pattern characterized by the irregular and sporadic occurrence of events or sales. In other words, it refers to situations in which the demand for a product or service occurs intermittently, with periods of time in which there are no sales or significant events.
Intermittent demand differs from constant or regular demand, where sales occur in a predictable and consistent manner over time. In contrast, in intermittent demand, periods without sales may be long and there may not be a regular sequence of events.
This type of demand can occur in different industries and contexts, such as low consumption products, seasonal products, high variability products, products with short life cycles, or in situations where demand depends on specific events or external factors.
Intermittent demand can pose challenges in forecasting and inventory management, as it is difficult to predict when sales will occur and in what quantity. Methods like the Croston model, which I mentioned earlier, are used to address intermittent demand and generate more accurate and appropriate forecasts for this type of demand pattern.
Problem with intermittent demand
Intermittent demand can present various challenges and issues in inventory management and demand forecasting. Some of the common problems associated with intermittent demand are as follows:

Unpredictable variability: Intermittent demand can have unpredictable variability, making planning and forecasting difficult. Demand patterns can be irregular and fluctuate dramatically between periods with sales and periods without sales.

Low frequency of sales: Intermittent demand is characterized by long periods without sales. This can lead to inventory management difficulties, as it is necessary to hold enough stock to meet demand when it occurs, while avoiding excess inventory during nonsales periods.

Forecast error: Forecasting intermittent demand can be more difficult to pin down than constant demand. Traditional forecast models may not be adequate to capture the variability and lack of patterns in intermittent demand, which can lead to significant errors in estimates of future demand.

Impact on the supply chain: Intermittent demand can affect the efficiency of the supply chain and create difficulties in production planning, supplier management and logistics. Lead times and inventory levels must be adjusted to meet unpredictable demand.

Operating costs: Managing inventory in situations of intermittent demand can increase operating costs. Maintaining adequate inventory during nonsales periods and managing stock levels may require additional investments in storage and logistics.
To address these issues, specific approaches to intermittent demand management are used, such as specialized forecasting models, product classification techniques, and tailored inventory strategies. These solutions seek to minimize the impacts of variability and lack of patterns in intermittent demand, optimizing inventory management and improving supply chain efficiency.
Croston’s method(CR)
Croston’s method(CR) is a classic method that specifically dealing with intermittent demand, it was developed base upon the Simple Exponential Smoothing method. When Croston dealing with the intermittent demand, he found out that by using the SES, the level of forecasting in each period’s demand are normally higher than it’s actual value, which lead to a very low accuracy. After a period of times of research, he came out a method that optimize the result of the intermittent demand forecasting.
This method basically decompose the intermittent demand into two parts: the size of nonzero demand and the time interval of those demand occurred, and then apply the simple exponential smoothing on both part. Where the formula is follow:
if $Z_t=0$ then:
$Z'_t= Z'_{t1}$
$P'_t= P'_{t1}$
Otherwise
$Z'_t=\alpha Z_t +(1\alpha) Z'_{t1}$
$P'_t=\alpha P_t +(1\alpha) P'_{t1}$
where $0< \alpha < 1$
And finally by combining these forecasts
${Y'}_t = \frac{{Z'}_t}{{P'}_t}$
Where
 ${Y'}_t:$ Average demand per period.
 $Z_t:$ Actual demand at period $t$.
 $Z'_t:$ Time between two positive demand.
 $P:$ Demand size forecast for next period.
 $P_t:$ Forecast of demand interval.
 $\alpha :$ Smoothing constant.
Croston’s method converse the intermittent demand time series into a nonzero demand time series and a demand interval time series, many cases show that this method work quite well, but before apply Croston’s method, three assumptions should be made:
 The nonzero demand are independent and obey normal distribution;
 The demand intervals are independent and obey geometric distribution;
 There are mutual independence between the demand size and demand intervals.
According to many real cases show that, Croston’s method is suitable for the situation which the lead time obey normal distribution, for those demand series which contain large amount of zero values, Croston’s method did not shows a outstanding performance, sometimes even worse than SES method.
Additionally, Croston’s method can only provide the average demand for each period, it can not give a forecast of the demand size for each period, it can not forecast which period will occurred a demand, and it also can not come out a probability of whether a period will occurred a demand.
After all, although Croston’s method is a very classic and wide use method, it still has a lots of limitations, but after years of research carried by statisticians and scholars, few variations of Croston’s method were brought up.
Croston’s variations
Croston’s method is the main model used in demand forecasting area, most of the works are based upon this model. However, in 2001 Syntetos and Boylan proposed that Croston’s method is no a unbiased method, while some empirical evidence also showed that the losses in performance which use the Croston’s method (Sani and Kingsman, 1997). Plenty of further research is done in improving the Croston’s method. Syntetos and Boylan (2005) proposed an approximate unbiased procedure that provide less variance in the result of estimate, which is known as SBA (Syntetos and Boylan Approximate). Recently, Teunter et al. (2011) also proposed a intermittent forecasting method that can deal with obsolescence, which is based on Croston’s method known as TSB method (Teunter, Syntetos and Babai).
Area of application of the Croston method
The Croston method is commonly applied in the field of inventory management and demand forecasting in situations of intermittent demand. Some specific areas where the Croston model can be applied are:

Inventory management: The Croston model is used to forecast demand for products with sporadic or intermittent sales. Helps determine optimal inventory levels and replenishment policies, minimizing inventory costs and ensuring adequate availability to meet intermittent demand.

Retail sales: In the retail sector, especially in products with low sales frequency or irregular sales, the Croston model can be useful for forecasting demand and optimizing inventory planning in stores or warehouses.

Demand forecasting: In general, the Croston model is applied in demand forecasting when there is a lack of clear patterns or high variability in the time series. It can be used in various industries, such as the pharmaceutical industry, the automotive industry, the perishable goods industry, and other sectors where intermittent demand is common.

Supply Chain Planning: The Croston model can be used in supply chain planning and management to improve the accuracy of intermittent demand forecasts. This helps streamline production, inventory management, supplier order scheduling, and other aspects of the supply chain.
It is important to note that Croston’s model is just one of many approaches available to address intermittent demand. Depending on the context and the specific characteristics of the time series, there may be other more appropriate methods and techniques.
Croston Method for Stationary Time Series
No, the time series in the Croston method does not have to be stationary. The Croston method is an effective forecasting method for intermittent time series, even if they are not stationary. However, if the time series is stationary, the Croston method may be more accurate.
The Croston method is based on the idea that intermittent time series can be decomposed into two components: a demand component and a time between demands component. The demand component is forecast using a standard time series forecasting method, such as single or double exponential smoothing. The time component between demands is forecast using a probability distribution function, such as a Poisson distribution or a Weibull distribution.
The Croston method then combines the forecasts for the two components to obtain a total demand forecast for the next period.
If the time series is stationary, the two components of the time series will be stationary as well. This means that the Croston method will be able to forecast the two components more accurately.
However, even if the time series is not stationary, the Croston method can still be an effective forecasting method. The Croston method is a robust method that can handle time series with irregular demand patterns.
If you are using the Croston method to forecast an intermittent time series that is not stationary, it is important to choose a standard time series forecast method that is effective for nonstationary time series. Double exponential smoothing is an effective forecasting method for nonstationary time series.
Loading libraries and data
Tip
Statsforecast will be needed. To install, see instructions.
Next, we import plotting libraries and configure the plotting style.
date  sales  

0  20220101 00:00:00  0 
1  20220101 01:00:00  10 
2  20220101 02:00:00  0 
3  20220101 03:00:00  0 
4  20220101 04:00:00  100 
The input to StatsForecast is always a data frame in long format with three columns: unique_id, ds and y:

The
unique_id
(string, int or category) represents an identifier for the series. 
The
ds
(datestamp) column should be of a format expected by Pandas, ideally YYYYMMDD for a date or YYYYMMDD HH:MM:SS for a timestamp. 
The
y
(numeric) represents the measurement we wish to forecast.
ds  y  unique_id  

0  20220101 00:00:00  0  1 
1  20220101 01:00:00  10  1 
2  20220101 02:00:00  0  1 
3  20220101 03:00:00  0  1 
4  20220101 04:00:00  100  1 
We can see that our time variable (ds)
is in an object format, we need
to convert to a date format
Explore Data with the plot method
Plot some series using the plot method from the StatsForecast class. This method prints a random series from the dataset and is useful for basic EDA.
Autocorrelation plots
Decomposition of the time series
How to decompose a time series and why?
In time series analysis to forecast new values, it is very important to know past data. More formally, we can say that it is very important to know the patterns that values follow over time. There can be many reasons that cause our forecast values to fall in the wrong direction. Basically, a time series consists of four components. The variation of those components causes the change in the pattern of the time series. These components are:
 Level: This is the primary value that averages over time.
 Trend: The trend is the value that causes increasing or decreasing patterns in a time series.
 Seasonality: This is a cyclical event that occurs in a time series for a short time and causes shortterm increasing or decreasing patterns in a time series.
 Residual/Noise: These are the random variations in the time series.
Combining these components over time leads to the formation of a time series. Most time series consist of level and noise/residual and trend or seasonality are optional values.
If seasonality and trend are part of the time series, then there will be effects on the forecast value. As the pattern of the forecasted time series may be different from the previous time series.
The combination of the components in time series can be of two types: * Additive * Multiplicative
Additive time series
If the components of the time series are added to make the time series. Then the time series is called the additive time series. By visualization, we can say that the time series is additive if the increasing or decreasing pattern of the time series is similar throughout the series. The mathematical function of any additive time series can be represented by: $y(t) = level + Trend + seasonality + noise$
Multiplicative time series
If the components of the time series are multiplicative together, then the time series is called a multiplicative time series. For visualization, if the time series is having exponential growth or decline with time, then the time series can be considered as the multiplicative time series. The mathematical function of the multiplicative time series can be represented as.
$y(t) = Level * Trend * seasonality * Noise$
Split the data into training and testing
Let’s divide our data into sets
 Data to train our
Croston Classic Model
.  Data to test our model
For the test data we will use the last 500 hours to test and evaluate the performance of our model.
Now let’s plot the training data and the test data.
Implementation of CrostonClassic with StatsForecast
To also know more about the parameters of the functions of the
CrostonClassic Model
, they are listed below. For more information,
visit the
documentation.
Load libraries
Instantiating Model
Import and instantiate the models. Setting the argument is sometimes
tricky. This article on Seasonal
periods by the
master, Rob Hyndmann, can be useful for season_length
.
We fit the models by instantiating a new StatsForecast object with the following parameters:
models: a list of models. Select the models you want from models and import them.

freq:
a string indicating the frequency of the data. (See pandas’ available frequencies.) 
n_jobs:
n_jobs: int, number of jobs used in the parallel processing, use 1 for all cores. 
fallback_model:
a model to be used if a model fails.
Any settings are passed into the constructor. Then you call its fit method and pass in the historical data frame.
Fit the Model
Let’s see the results of our Croston Classic Model
. We can observe it
with the following instruction:
Forecast Method
If you want to gain speed in productive settings where you have multiple
series or models we recommend using the
StatsForecast.forecast
method instead of .fit
and .predict
.
The main difference is that the .forecast
doest not store the fitted
values and is highly scalable in distributed environments.
The forecast method takes two arguments: forecasts next h
(horizon)
and level
.
h (int):
represents the forecast h steps into the future. In this case, 25 week ahead.
The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals. Depending on your computer, this step should take around 1min.
ds  CrostonClassic  

unique_id  
1  20230221 16:00:00  23.606695 
1  20230221 17:00:00  23.606695 
1  20230221 18:00:00  23.606695 
…  …  … 
1  20230314 09:00:00  23.606695 
1  20230314 10:00:00  23.606695 
1  20230314 11:00:00  23.606695 
unique_id  ds  CrostonClassic  

0  1  20230221 16:00:00  23.606695 
1  1  20230221 17:00:00  23.606695 
2  1  20230221 18:00:00  23.606695 
…  …  …  … 
497  1  20230314 09:00:00  23.606695 
498  1  20230314 10:00:00  23.606695 
499  1  20230314 11:00:00  23.606695 
ds  y  unique_id  CrostonClassic  

0  20220101 00:00:00  0.0  1  NaN 
1  20220101 01:00:00  10.0  1  NaN 
2  20220101 02:00:00  0.0  1  NaN 
…  …  …  …  … 
497  20230314 09:00:00  NaN  1  23.606695 
498  20230314 10:00:00  NaN  1  23.606695 
499  20230314 11:00:00  NaN  1  23.606695 
Predict method with confidence interval
To generate forecasts use the predict method.
The predict method takes two arguments: forecasts the next h
(for
horizon) and level
.
h (int):
represents the forecast h steps into the future. In this case, 500 hours ahead.
The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals.
This step should take less than 1 second.
ds  CrostonClassic  

unique_id  
1  20230221 16:00:00  23.606695 
1  20230221 17:00:00  23.606695 
1  20230221 18:00:00  23.606695 
…  …  … 
1  20230314 09:00:00  23.606695 
1  20230314 10:00:00  23.606695 
1  20230314 11:00:00  23.606695 
We can join the forecast result with the historical data using the
pandas function pd.concat()
, and then be able to use this result for
graphing.
y  unique_id  CrostonClassic  

ds  
20220101 00:00:00  0.0  1  NaN 
20220101 01:00:00  10.0  1  NaN 
20220101 02:00:00  0.0  1  NaN 
…  …  …  … 
20230314 09:00:00  NaN  NaN  23.606695 
20230314 10:00:00  NaN  NaN  23.606695 
20230314 11:00:00  NaN  NaN  23.606695 
y  unique_id  CrostonClassic  

ds  
20220818 04:00:00  0.0  1  NaN 
20220818 05:00:00  80.0  1  NaN 
20220818 06:00:00  0.0  1  NaN 
…  …  …  … 
20230314 09:00:00  NaN  NaN  23.606695 
20230314 10:00:00  NaN  NaN  23.606695 
20230314 11:00:00  NaN  NaN  23.606695 
Now let’s visualize the result of our forecast and the historical data of our time series.
Let’s plot the same graph using the plot function that comes in
Statsforecast
, as shown below.
Crossvalidation
In previous steps, we’ve taken our historical data to predict the future. However, to asses its accuracy we would also like to know how the model would have performed in the past. To assess the accuracy and robustness of your models on your data perform CrossValidation.
With time series data, Cross Validation is done by defining a sliding window across the historical data and predicting the period following it. This form of crossvalidation allows us to arrive at a better estimation of our model’s predictive abilities across a wider range of temporal instances while also keeping the data in the training set contiguous as is required by our models.
The following graph depicts such a Cross Validation Strategy:
Perform time series crossvalidation
Crossvalidation of time series models is considered a best practice but most implementations are very slow. The statsforecast library implements crossvalidation as a distributed operation, making the process less timeconsuming to perform. If you have big datasets you can also perform Cross Validation in a distributed cluster using Ray, Dask or Spark.
In this case, we want to evaluate the performance of each model for the
last 5 months (n_windows=)
, forecasting every second hour
(step_size=50)
. Depending on your computer, this step should take
around 1 min.
The cross_validation method from the StatsForecast class takes the following arguments.

df:
training data frame 
h (int):
represents $h$ steps into the future that are being forecasted. In this case, 500 hours ahead. 
step_size (int):
step size between each window. In other words: how often do you want to run the forecasting processes. 
n_windows(int):
number of windows used for cross validation. In other words: what number of forecasting processes in the past do you want to evaluate.
The crossvaldation_df object is a new data frame that includes the following columns:
unique_id:
index. If you dont like working with index just runcrossvalidation_df.resetindex()
.ds:
datestamp or temporal indexcutoff:
the last datestamp or temporal index for then_windows
.y:
true valuemodel:
columns with the model’s name and fitted value.
ds  cutoff  y  CrostonClassic  

unique_id  
1  20230123 12:00:00  20230123 11:00:00  0.0  23.655830 
1  20230123 13:00:00  20230123 11:00:00  0.0  23.655830 
1  20230123 14:00:00  20230123 11:00:00  0.0  23.655830 
…  …  …  …  … 
1  20230221 13:00:00  20230131 19:00:00  60.0  27.418417 
1  20230221 14:00:00  20230131 19:00:00  20.0  27.418417 
1  20230221 15:00:00  20230131 19:00:00  20.0  27.418417 
Model Evaluation
We can now compute the accuracy of the forecast using an appropiate
accuracy metric. Here we’ll use the Root Mean Squared Error (RMSE). To
do this, we first need to install datasetsforecast
, a Python library
developed by Nixtla that includes a function to compute the RMSE.
The function to compute the RMSE takes two arguments:
 The actual values.
 The forecasts, in this case,
Croston Classic Model
.
Acknowledgements
We would like to thank Naren Castellon for writing this tutorial.
References
 Changquan Huang • Alla Petukhina. Springer series (2022). Applied Time Series Analysis and Forecasting with Python.
 Ivan Svetunkov. Forecasting and Analytics with the Augmented Dynamic Adaptive Model (ADAM)
 James D. Hamilton. Time Series Analysis Princeton University Press, Princeton, New Jersey, 1st Edition, 1994.
 Nixtla Parameters.
 Pandas available frequencies.
 Rob J. Hyndman and George Athanasopoulos (2018). “Forecasting principles and practice, Time series crossvalidation”..
 Seasonal periods Rob J Hyndman.