A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
1 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
2 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
import pandas as pd
import numpy as np
import [Link] as plt
import matplotlib as mlp
#Loading data
data = pd.read_csv('Datasets/[Link]',
index_col='Month', parse_dates=['Month'])
data = [Link](columns
={'#Passengers':'no_passengers'})
data
read_csv().
parse_dates
to_datetime().
index_col='Month'
3 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
[Link](data)
[Link]()
4 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
data_for_dist_fitting = data[-70:]
5 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
data_train =
data[~[Link](data_for_dist_fitting).all(1)]
test_data = data_for_dist_fitting[-20:]
data_for_dist_fitting=data_for_dist_fitting[~data_for_dist
_fitting.isin(test_data).all(1)]
train = [Link](data_train,color='blue', label = 'Train
data')
data_f_mc = [Link](data_for_dist_fitting, color ='red',
label ='Data for distribution fitting')
test = [Link](test_data, color ='black', label = 'Test
data')
[Link](loc='best')
[Link]('Data division')
[Link](block=False)
6 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
7 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
from [Link] import adfuller
def test_stationarity(timeseries):
#Determining rolling statistics
rolmean = [Link](window=12).mean()
rolstd = [Link](window=12).std()
#plot rolling statistics:
orig = [Link](timeseries,color='blue', label =
'Original')
mean = [Link](rolmean, color ='red', label ='Rolling
Mean')
std = [Link](rolstd, color ='black', label =
'Rolling Std')
[Link](loc='best')
[Link]('Rolling Mean and Standard Deviation')
[Link](block=False)
8 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
#Perform Dickey Fuller test:
print('Results of Dickey Fuller Test:')
dftest = adfuller(timeseries, autolag= 'AIC')
dfoutput = [Link](dftest[0:4], index=['Test
Statistic','p-value','#Lags Used','Number of Observations
Used'])
for key,value in dftest[4].items():
dfoutput['Critical Value (%s)'%key] = value
print (dfoutput)
test_stationarity(data_train)
9 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
statsmodels seasonal_decompose
from [Link] import seasonal_decompose
decomposition = seasonal_decompose(data_train)
trend = [Link]
seasonal = [Link]
residual = [Link]
[Link](411)
[Link](ts_log, label='Original')
[Link](loc='best')
[Link](412)
[Link](trend, label='Trend')
[Link](loc='best')
[Link](413)
[Link](seasonal,label='Seasonality')
[Link](loc='best')
[Link](414)
[Link](residual, label='Residuals')
[Link](loc='best')
plt.tight_layout()
10 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
SARIMA(p,d,q).
(P,D,Q).m
11 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
import itertools
p = d = q = range(0, 2)
pdq = list([Link](p, d, q))
seasonal_pdq = [(x[0], x[1], x[2], 12) for x in
list([Link](p, d, q))]
print('Examples of parameter for SARIMA...')
print('SARIMAX: {} x {}'.format(pdq[1], seasonal_pdq[1]))
print('SARIMAX: {} x {}'.format(pdq[1], seasonal_pdq[2]))
print('SARIMAX: {} x {}'.format(pdq[2], seasonal_pdq[3]))
print('SARIMAX: {} x {}'.format(pdq[2], seasonal_pdq[4]))
12 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
import warnings
[Link]('ignore')
import [Link] as sm
for param in pdq:
for param_seasonal in seasonal_pdq:
try:
mod =
[Link](data_train,order=param,seasonal_
order=param_seasonal,enforce_stationarity=False,enforce_in
vertibility=False)
results = [Link]()
print('SARIMA{}x{}12 -
AIC:{}'.format(param,param_seasonal,[Link]))
except Exception as E:
print(E)
continue
13 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
14 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
SARIMAX(1, 1, 1)x(1, 1, 1, 12).
from [Link] import SARIMAX
mod= SARIMAX(data_train,order=(1,1,1),seasonal_order=(1,
1, 1, 12),enforce_invertibility=False,
enforce_stationarity=False)
results = [Link](disp=0)
print([Link]())
15 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
pred_sarima = [Link](50)
predicted =[Link](pred_sarima,label='Prediction by
SARIMA', color='red')
Actual = [Link](data_for_dist_fitting,label='Actual
data')
[Link](loc='best')
[Link]('SARIMA MODEL')
[Link](block=False)
16 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
# plot residual errors of the training data
residual_error = [Link]([Link])
residual_error.plot()
[Link]()
residual_error.plot(kind='kde')
[Link]()
print(residual_error.describe())
17 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
#to suppress warnings
[Link]('ignore')
from [Link] import mean_squared_error
#creating new dataframe for rolling forescast
18 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
history = [Link](data_train.astype(float))
predictions = list()
for i in range(len(data_for_dist_fitting)):
model = SARIMAX(history,order=
(1,1,1),seasonal_order=(1, 1, 1,
12),enforce_invertibility=False,
enforce_stationarity=False)
model_fit = [Link](disp = 0)
# generate forcecast for next period
output = model_fit.forecast()
#Save the prediction value in yhat
yhat = np.e ** output[0]
#Append yhat to the list of prediction
[Link](yhat)
# grabs the observation at the ith index
obs = data_for_dist_fitting[i : i + 1]
# appends the observation to the estimation data set
history = [Link]([Link]([Link](float)))
# prints the MSE of the model for the rolling forecast
period
error = mean_squared_error(data_for_dist_fitting,
predictions)
print('Test MSE: %.3f' % error)
# converts the predictions list to a pandas dataframe with
the same index as the actual values
# for plotting purposes
predictions = [Link](predictions)
[Link] = data_for_dist_fitting.index
# sets the plot size to 12x8
[Link]['[Link]'] = (12,8)
# plots the predicted and actual stock prices
[Link](data_for_dist_fitting,label='Actual values')
[Link](predictions, color = 'red', label='predicted
rolling forecast')
[Link](loc='best')
[Link]('week')
[Link]('#passengers')
[Link]('Predicted vs. Actual #of passengers')
[Link]()
19 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
# to suppress warnings
[Link]('ignore')
# sets the plot size to 12x8
[Link]['[Link]'] = (12,8)
# plots the rolling forecast error
rf_errors = data_for_dist_fitting.no_passengers -
predictions[0]
rf_errors.plot(kind = 'kde')
# produces a summary of rolling forecast error
rf_errors.astype(float).describe()
20 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
# to suppress warnings
21 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
import warnings
[Link]('ignore')
# imports the fitter function and produces estimated fits
for our rsarima_errors
from fitter import
Fitter,get_common_distributions,get_distributions
f = Fitter(rf_errors, distributions=
['binomial','norm','laplace','uniform'])
[Link]()
[Link]()
22 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
data_for_dist_fitting
[Link](loc,scale,size)
def lapace_mc_randv_distribution(mean, rf_errors, n_sim):
#gets the estimated beta or mean absolute distance from
the mean
var = (sum(abs(rf_errors - [Link](rf_errors)))
/ len(rf_errors))
# uses the numpy function to generate an array of
simulated values
est_range = [Link](mean,var,n_sim)
# converts the array to a list
est_range = list(est_range)
# returns the simulated values
return(est_range)
23 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
def rolling_forecast_MC(train, test, std_dev, n_sims):
# create a new dataframe that will be added to as the
forecast rolls
history = [Link](data_train.astype(float))
# create an empty list that will hold predictions
predictions = list()
# loops through the indexes of the set being forecasted
for i in range(len(test_data)):
model = SARIMAX(history,order=
(1,1,1),seasonal_order=(1, 1, 1,
12),enforce_invertibility=False,
enforce_stationarity=False)
model_fit = [Link](disp = 0)
# generate forcecast for next period
output = model_fit.forecast().values
#Save the prediction value in yhat
yhat = np.e ** output[0]
# performs monte carlo simulation using the
predicted price as the mean, user-specified
# standard deviation, and number of simulations
randv_range =
lapace_mc_randv_distribution(yhat,std_dev,n_sims)
#Append yhat to the list of prediction
[Link]([float(i) for i in
randv_range])
# grabs the observation at the ith index
obs = test_data[i : i + 1]
# appends the observation to the estimation data
set
history =
[Link]([Link]([Link](float)))
24 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
# converts the predictions list to a pandas
dataframe with the same index as the actual
# values for plotting purposes
predictions = [Link](predictions)
[Link] = test_data.index
# returns predictions
return(predictions)
data_train = data_train.append(data_for_dist_fitting)
test_preds = rolling_forecast_MC(data_train,
test_data,
rf_errors,
1000)
MC = [Link](test_preds)
Actual=[Link](test_data,color='black',label='Actual
Demand')
[Link](loc='best')
[Link]()
25 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
print('Expected demand:',[Link](test_preds.values))
print('Quantile(5%):',[Link](test_preds,5))
print('Quantile(95%):',[Link](test_preds,95))
26 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
[Link]().
def rolling_forecast_MC_for_minmax_range(train, test,
std_dev, n_sims):
# create a new dataframe that will be added to as the
forecast rolls
history = [Link](data_train.astype(float))
# create an empty list that will hold predictions
predictions = list()
# loops through the indexes of the set being forecasted
for i in range(len(test_data)):
model = SARIMAX(history,order=
(1,1,1),seasonal_order=(1, 1, 1,
12),enforce_invertibility=False,
enforce_stationarity=False)
model_fit = [Link](disp = 0)
# generate forcecast for next period
output = model_fit.forecast().values
#Save the prediction value in yhat
yhat = np.e ** output[0]
# performs monte carlo simulation using the
predicted price as the mean, user-specified
# standard deviation, and number of simulations
randv_range =
lapace_mc_randv_distribution(yhat,std_dev,n_sims)
#Append yhat to the list of prediction
[Link]([float(i) for i in
randv_range])
# grabs the observation at the ith index
obs = test_data[i : i + 1]
# appends the observation to the estimation data set
history =
[Link]([Link]([Link](float)))
# converts the predictions list to a pandas dataframe
27 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
with the same index as the actual
# values for plotting purposes
predictions = [Link](predictions)
# converts all the estimated yhats in each column to
one list per row
predictions['predicted_range'] =
[Link]()
# grabs only the column with all values in a list
predictions =
[Link](predictions['predicted_range'])
[Link] = test_data.index
# returns predictions
return(predictions)
# produces a rolling forecast with prediction intervals
using 1000 MC sims
test_preds_minmax =
rolling_forecast_MC_for_minmax_range(data_train,
test_data,
rf_errors,
1000)
test_preds_minmax.head()
28 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
# creates an empty list
prediction_interval = []
# loops through the rows in the testing data set
for i in range(len(test_data)):
# appends true if the actual price is in the interval
of predicted prices and false
# otherwise
prediction_interval.append([Link](min(test_preds_minmax.
predicted_range[i]) <=
test_data.no_passengers[i]
<=
max(test_preds_minmax.predicted_range[i]),
True, False))
# prints the percentage of actual prices in the prediction
intervals
print('Percentage of Demand in Predicted Demand Range: %f'
%
(100 * sum(prediction_interval) /
len(prediction_interval)))
29 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
# creates empty lists to append to with minimum and maximum
values for each weeks prediction
min_range = []
max_range = []
# loops through the rows in test_preds
for i in range(len(test_preds_minmax)):
# appends to the list the min or max value as appropriate
min_range.append(min(test_preds_minmax.predicted_range[i]))
max_range.append(max(test_preds_minmax.predicted_range[i]))
# converts the lists to data frames and makes their indexes
match up with the dates they're
# predicting
min_range = [Link](min_range)
min_range.index = test_data.index
max_range = [Link](max_range)
max_range.index = test_data.index
# plots the actual stock price with prediction intervals
[Link](test_data, color ='red',label='Actual Data')
[Link](min_range, color = 'm', label='Min range')
[Link](max_range, color = 'b', label ='Max range')
[Link](loc='best')
[Link]('Month')
[Link]('No of Passengers')
[Link]('Actual Demand with Prediction Intervals')
[Link]()
30 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
31 of 32 3/15/2022, 9:49 AM
A Stochastic Model For Demand Forecating In Python | by Ju... [Link]
32 of 32 3/15/2022, 9:49 AM