Ejemplo n.º 1
0
def test_direct_holt_add():
    mod = SimpleExpSmoothing(housing_data)
    res = mod.fit()
    x = np.squeeze(np.asarray(mod.endog))
    alpha = res.params['smoothing_level']
    l, b, f, err, xhat = _simple_dbl_exp_smoother(x, alpha, beta=0.0,
                                                  l0=res.params['initial_level'], b0=0.0,
                                                  nforecast=5)

    assert_allclose(l, res.level)
    assert_allclose(f, res.level.iloc[-1] * np.ones(5))
    assert_allclose(f, res.forecast(5))

    mod = ExponentialSmoothing(housing_data, trend='add')
    res = mod.fit()
    x = np.squeeze(np.asarray(mod.endog))
    alpha = res.params['smoothing_level']
    beta = res.params['smoothing_slope']
    l, b, f, err, xhat = _simple_dbl_exp_smoother(x, alpha, beta=beta,
                                                  l0=res.params['initial_level'],
                                                  b0=res.params['initial_slope'], nforecast=5)

    assert_allclose(xhat, res.fittedvalues)
    assert_allclose(l + b, res.level + res.slope)
    assert_allclose(l, res.level)
    assert_allclose(b, res.slope)
    assert_allclose(f, res.level.iloc[-1] + res.slope.iloc[-1] * np.array([1, 2, 3, 4, 5]))
    assert_allclose(f, res.forecast(5))
Ejemplo n.º 2
0
 def test_simple_exp_smoothing(self):
     fit1 = SimpleExpSmoothing(self.oildata_oil).fit(0.2,optimized=False)
     fit2 = SimpleExpSmoothing(self.oildata_oil).fit(0.6,optimized=False)
     fit3 = SimpleExpSmoothing(self.oildata_oil).fit()
     assert_almost_equal(fit1.forecast(1), [484.802468], 4)
     assert_almost_equal(fit1.level,
                         [446.65652290,448.21987962,449.7084985,
                          444.49324656,446.84886283,445.59670028,
                          441.54386424,450.26498098,461.4216172,
                          474.49569042,482.45033014,484.80246797], 4)
     assert_almost_equal(fit2.forecast(1), [501.837461], 4)
     assert_almost_equal(fit3.forecast(1), [496.493543], 4)
     assert_almost_equal(fit3.params['smoothing_level'], 0.891998, 4)
     #has to be 3 for old python2.7 scipy versions
     assert_almost_equal(fit3.params['initial_level'], 447.478440, 3)
Ejemplo n.º 3
0
# Test data and left over data as train data

Train = coke.head(32)
Test = coke.tail(8)
# change the index value in pandas data frame
Test.set_index(np.arange(1, 9), inplace=True)


# Creating a function to calculate the MAPE value for test data
def MAPE(pred, org):
    temp = np.abs((pred - org) / org) * 100
    return np.mean(temp)


# Simple Exponential Method
ses_model = SimpleExpSmoothing(Train["Sales"]).fit()
pred_ses = ses_model.predict(start=Test.index[0], end=Test.index[-1])
MAPE(pred_ses, Test.Sales)  # 53.98

# Holt method
hw_model = Holt(Train["Sales"]).fit()
pred_hw = hw_model.predict(start=Test.index[0], end=Test.index[-1])
MAPE(pred_hw, Test.Sales)  # 53.42

# Holts winter exponential smoothing with additive seasonality and additive trend
hwe_model_add_add = ExponentialSmoothing(Train["Sales"],
                                         seasonal="add",
                                         trend="add",
                                         seasonal_periods=10).fit()
pred_hwe_add_add = hwe_model_add_add.predict(start=Test.index[0],
                                             end=Test.index[-1])
Ejemplo n.º 4
0
# SES example
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = SimpleExpSmoothing(data)
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)
@author: mac
"""

import numpy as np
import pandas as pd
import matplotlib
from statsmodels.tsa.seasonal import seasonal_decompose
from pylab import rcParams

df = pd.read_csv('EnergyProduction.csv', index_col=0, parse_dates=True)

df.index.freq = 'MS'

df['SMA12'] = df['EnergyIndex'].rolling(window=12).mean()

df.plot(figsize=(12, 5))

from statsmodels.tsa.holtwinters import SimpleExpSmoothing

df['SES12'] = SimpleExpSmoothing(df['EnergyIndex']).fit(
    smoothing_level=2 / (12 + 1), optimized=False).fittedvalues.shift(-1)

from statsmodels.tsa.holtwinters import ExponentialSmoothing

df['TESmul-12'] = ExponentialSmoothing(
    df['EnergyIndex'], trend='mul', seasonal='mul',
    seasonal_periods=12).fit().fittedvalues.shift(-1)

df[:'1972-01-01'].plot(figsize=(12, 5))
    plt.legend(loc=4)

seasonal_ts_add = smf.tsa.seasonal_decompose(airlines["Passengers"], freq=10)
seasonal_ts_add.plot()
train = airlines.head(92)
test = airlines.tail(4)


#MAPE####
def MAPE(pred, org):
    temp = np.abs((pred - org)) * 100 / org
    return np.mean(temp)


####Simple Exp##########
Exp = SimpleExpSmoothing(train["Passengers"]).fit()
Exp_pred = Exp.predict(start=test.index[0], end=test.index[-1])
Exp_mape = MAPE(Exp_pred, test.Passengers)  ######32.05

###Holt#######
hw = Holt(train["Passengers"]).fit()
hw_pred = hw.predict(start=test.index[0], end=test.index[-1])
hw_mape = MAPE(hw_pred, test.Passengers)  #####34.75

# Holts winter exponential smoothing with additive seasonality and additive trend
Exp_add_add = ExponentialSmoothing(train["Passengers"],
                                   damped=True,
                                   seasonal="add",
                                   seasonal_periods=12,
                                   trend="add").fit()
Exp_add_add_pred = Exp_add_add.predict(start=test.index[0], end=test.index[-1])
Ejemplo n.º 7
0
# Ensure that the index is aggregated by days and unique
train_store198_y_ets = train_store198_y_ets.groupby(
    train_store198_y_ets.index).Sales.sum()
# Set Datetime Index to Frequency = Day (Frequency = 7)
train_store198_y_ets.index.freq = 'D'

# Fit Models

# These 4 hyperparameters will be automatically tuned if optimized=True:
#    smoothing_level (alpha): the smoothing coefficient for the level.
#    smoothing_slope (beta): the smoothing coefficient for the trend.
#    smoothing_seasonal (gamma): the smoothing coefficient for the seasonal component.
#    damping_slope (phi): the coefficient for the damped trend.

# Simple Exponential Smoothing
ses_model_198 = SimpleExpSmoothing(train_store198_y_ets,
                                   initialization_method='estimated')
ses_model_198 = ses_model_198.fit(optimized=True)

# Holt (double)
holt_model_198 = Holt(train_store198_y_ets, initialization_method='estimated')
holt_model_198 = holt_model_198.fit(optimized=True)

# Holt-Winters (triple)
holt_winters_model_198 = ExponentialSmoothing(
    train_store198_y_ets,
    trend="add",
    seasonal="add",
    seasonal_periods=7,
    initialization_method='estimated')
holt_winters_model_198 = holt_winters_model_198.fit(optimized=True)
def model_ES(key, train, test_shape = 0, train_flag = 0, test = []):
    predictions = []
    rmse_val=[]

    try:
        train = train.values
    except:
        train = train
    history = [np.asscalar(x) for x in train]

#   TRAIN
    if train_flag==1:
        itr=5
        data=pd.DataFrame(history)
        for i in range(3):
            pred_temp = []
            v_train=[np.asscalar(x) for x in data[:-itr].values]
            v_expected=data.tail(itr).head(3).reset_index(drop = True)
            try:
                for t in range(3):
                    if key=='SES':
                        model = SimpleExpSmoothing(history)
                    elif key=='HWES':
                         model = ExponentialSmoothing(history)
                    model_fit = model.fit()
                    yhat= model_fit.predict(len(history), len(history))
                    if yhat < 0:
                        yhat= mu.weighted_moving_average(history,1,3)
                    yhat=yhat[0]
                    pred_temp.append(yhat)
                    v_train.append(yhat)
            except:
                pred_temp.extend(mu.moving_average(v_train, 3 - len(pred_temp), 3))
            mu.plotting(key, pred_temp, v_expected)
            rmse_val.append(mu.calculate_rmse(key, v_expected, pred_temp))

            if i == 2:
                predictions.extend(pred_temp)
            else:
                predictions.append(pred_temp[0])

            itr=itr-1
#   FORECAST
    else:
        try:
            for t in range(test_shape):
                if key=='SES':
                    model = SimpleExpSmoothing(history)
                elif key=='HWES':
                     model = ExponentialSmoothing(history)
                model_fit = model.fit()
                yhat= model_fit.predict(len(history), len(history))
                if yhat < 0:
                    yhat= mu.weighted_moving_average(history,1,3)
                yhat=yhat[0]
                predictions.append(yhat)
                history.append(yhat)
        except:
            predictions.extend(mu.moving_average(history, test_shape - len(predictions), 3))

    predictions = [int(i) for i in predictions]
    return predictions,rmse_val
Ejemplo n.º 9
0
# -*- coding: utf-8 -*-
"""
Time series
"""
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
import math
import numpy as np
from formatage import wonderframe
from sklearn.metrics import mean_squared_error

turfu = []
for i in range(10, len(wonderframe)):
    fit = SimpleExpSmoothing(wonderframe['Historique'][i][-i - 1:]).fit(
        smoothing_level=0.6, optimized=True)
    turfu.append(fit.forecast(1)[0])

diff = 0
for i in range(10, len(turfu)):
    diff += (wonderframe["Livraisons réelles"][10 + i] - turfu[i])**2
diff = math.sqrt(diff / len(turfu))

turfuDeBase = []
for i in range(len(wonderframe)):
    a = np.mean(wonderframe['Historique'][i][-i - 1:])
    turfuDeBase.append(a)

diffDeBase = 0
for i in range(len(turfuDeBase)):
    diffDeBase += (wonderframe["Livraisons réelles"][i] - turfuDeBase[i])**2
diffDeBase = math.sqrt(diffDeBase / len(turfuDeBase))
diffDeBase = math.sqrt(






# Creating a function to calculate the MAPE value for test data 
def MAPE(pred,org):
    temp = np.abs((pred-org))*100/org
    return np.mean(temp)



# Simple Exponential Method
ses_model = SimpleExpSmoothing(Train1["Passengers"]).fit()
pred_ses = ses_model.predict(start = Test1.index[0],end = Test1.index[-1])
MAPE(pred_ses,Test1.Passengers) #  14.235433039401634

# Holt method 
hw_model = Holt(Train1["Passengers"]).fit()
pred_hw = hw_model.predict(start = Test1.index[0],end = Test1.index[-1])
MAPE(pred_hw,Test1.Passengers) # 11.840943119376163



# Holts winter exponential smoothing with additive seasonality and additive trend
hwe_model_add_add = ExponentialSmoothing(Train1["Passengers"],seasonal="add",trend="add",seasonal_periods=12,damped=True).fit()
pred_hwe_add_add = hwe_model_add_add.predict(start = Test1.index[0],end = Test1.index[-1])
MAPE(pred_hwe_add_add,Test1.Passengers) # 1.6126576094462026
Ejemplo n.º 11
0
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing
alpha = 0.1


EXP_df = df['Energy']
train_bound = EXP_df.shape[0]-48*1
EXP_df_train = EXP_df[:train_bound]
EXP_df_test = EXP_df[-48:]

forecast_length = 48
EXP_results = pd.DataFrame()
EXP_results['Theoretical']=EXP_df_test


# Simple Exponential Smootheing Predictions
SES_model = SimpleExpSmoothing(EXP_df_train).fit(smoothing_level=alpha, optimized=True)
#df['SES'] = SES_model.fittedvalues
EXP_results['SES'] =np.array(SES_model.forecast(forecast_length))

# Double Exponential Smoothing Predictions
EXP_SM = ExponentialSmoothing(EXP_df_train, trend='add').fit()
#df['EXP_SM'] = EXP_SM.fittedvalues
EXP_results['EXP_SM'] = np.array(EXP_SM.forecast(forecast_length))

# Triple Exponential Smoothing Predictions
Triple_EXP_SM = ExponentialSmoothing(EXP_df_train,trend='add',seasonal='add').fit()
#df['T_EXP_SM'] = Triple_EXP_SM.fittedvalues
EXP_results['T_EXP'] = np.array(Triple_EXP_SM.forecast(forecast_length))


EXP_results.head()
Ejemplo n.º 12
0
   globals()['model%s' % a].compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.0075), loss='mean_squared_error')
   history = globals()['model%s' % a].fit(trainx, trainy, epochs=1000, batch_size=batch_size, verbose=0,
                                          callbacks = [tf.keras.callbacks.EarlyStopping(monitor = 'loss', patience = 5, restore_best_weights = True, mode='auto')])
 #deep neural network
 tf.keras.backend.clear_session()
 model_dnn = tf.keras.models.Sequential()
 model_dnn.add(tf.keras.Input(shape=(look_back,)))
 model_dnn.add(tf.keras.layers.Dense(units=64, activation='relu'))
 model_dnn.add(tf.keras.layers.Dense(units=32, activation='relu'))
 model_dnn.add(tf.keras.layers.Flatten())
 model_dnn.add(tf.keras.layers.Dense(units=1, activation='relu'))
 model_dnn.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.0075), loss='mse')
 history_dnn = model_dnn.fit(trainx_dnn, trainy_dnn, epochs=500, batch_size=batch_size_dnn, verbose=0,
                            callbacks = [tf.keras.callbacks.EarlyStopping(monitor='loss', patience=5, mode='auto', restore_best_weights=True)])
 #ses model
 model_ses = SimpleExpSmoothing(train).fit(smoothing_level=0.2)
 #croston model
 fit_crost = croston.fit_croston(train, forecast_length=28, croston_variant='sba')
 #forecast
 predict_rnn = [inverse_scaler(train, forecast(testx, modelsigmoid)), inverse_scaler(train, forecast(testx, modelrelu))]
 predict_dnn = forecast_dnn(testx, model_dnn)
 predict_dnn = inverse_scaler(train, predict_dnn)
 predcrost = fit_crost['croston_forecast']
 predict_ses = model_ses.forecast(28).reshape((28,1))
 rmssernn.append(min([rmsse(train, test, predict_rnn[0], horizon), rmsse(train, test, predict_rnn[1], horizon)]))
 rmssednn.append(rmsse(train, test, predict_dnn, horizon))
 rmssecroston.append(rmsse(train, test, predcrost, horizon))
 rmsseses.append(rmsse(train, test, predict_ses, horizon))
 maernn.append(min([mean_absolute_error(test, predict_rnn[0]), mean_absolute_error(test, predict_rnn[1])]))
 maednn.append(mean_absolute_error(test, predict_dnn))
 maecroston.append(mean_absolute_error(test, predcrost))
def exponential_smoothing(y):
    model = SimpleExpSmoothing(y)
    return model
Ejemplo n.º 14
0
model = VARMAX(data, exog=data_exog, order=(1, 1))
model_fit = model.fit(disp=False)
# make prediction
data_exog2 = [[100]]
yhat = model_fit.forecast(exog=data_exog2)
print(yhat)



# SES example
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = SimpleExpSmoothing(data)
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)





# HWES example
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
Ejemplo n.º 15
0
    plt.title('Serie Maglie: training set, validation set, moving average e std')
    plt.ylabel('#Maglie vendute')
    plt.xlabel('Data')
    plt.plot(train, label="training set", color=TSC)
    plt.plot(valid, label="validation set", color =VSC, linestyle = '--')
    plt.plot(rolmean, color=OLC, label='Rolling Mean',  linewidth=3)
    plt.plot(rolstd, color=OLC, label='Rolling Std', linestyle = '--',  linewidth=3)
    plt.legend(loc='best')
    plt.show(block=False)
    plt.plot()
    """
    #%%
    #SIMPLE EXPONENTIAL SMOOTHING...

    # Creiamo il modello usando la classe SimpleExpSmoothing
    modelv1 = SimpleExpSmoothing(train)

    # Facciamo l'adattamento del modello ai dati
    fitted = modelv1.fit()

    # Creiamo le previsioni sullo stesso periodo del validation set
    forecasted = fitted.predict(start="2018-06-11", end="2019-09-29")

    # Calcolo gli intervalli di predizione per Simple Exponential Smoothing.
    # Sommiamo/Sottraiamo alle previsioni all'istante t-esimo il valore di
    # c*sigma dove questo valore rappresenta il 95% della guassiana.

    predint_xminus = ts[pd.date_range(start=ts.index[int(len(ts) * 0.8) + 1],
                                      end=ts.index[int(len(ts)) - 1],
                                      freq='D')]
    predint_xplus = ts[pd.date_range(start=ts.index[int(len(ts) * 0.8) + 1],
Ejemplo n.º 16
0
from util import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.utils import check_array
from statsmodels.tsa.holtwinters import SimpleExpSmoothing

ses = [
    SimpleExpSmoothing(x_train.values[i, :]).fit(smoothing_level=0.6,
                                                 optimized=False).forecast()
    for i in range(len(x_train))
]
plt.plot(ses, label='pred')
plt.plot(y_train.values, label='real')
plt.title('TPCC8131')
plt.legend(loc='upper left')
plt.grid()
plt.show()

# --------------------------------------------------- plot ---------------------------------------------------------
ses_train = [
    SimpleExpSmoothing(x_train.values[i, :]).fit(smoothing_level=0.6,
                                                 optimized=True).forecast()
    for i in range(len(x_train))
]
mape_train = mean_absolute_percentage_error(y_train.values.reshape(1, -1),
                                            ses_train)
plt.plot(ses_train, 'r-', label='predict')
plt.plot(y_train, label='real')
plt.title('TPCC8131_SES_train_mape: {}'.format(mape_train))
plt.legend(loc='upper left')
Ejemplo n.º 17
0
        # print((yhat[i+1]-close_label[i])*(close_label[i+1]-close_label[i]))
        if (yhat[i + 1] - close_label[i]) * (close_label[i + 1] -
                                             close_label[i]) >= 0:
            true_predict = true_predict + 1
    print("Trend accuracy: ", str(true_predict / len(close_label)))

    error = [(x - y) * (x - y) for x, y in zip(yhat, close_label)]
    print("MSE: ", str(mean(error)))
    print("___________________________________________")

    # SES example
    print("SES model")
    from statsmodels.tsa.holtwinters import SimpleExpSmoothing
    from random import random
    # contrived dataset
    model = SimpleExpSmoothing(close_train)
    model_fit = model.fit()
    # make prediction
    yhat = model_fit.predict(len(close_train), len(close_all) - 1)
    true_predict = 0
    for i in range(0, len(yhat) - 1):
        # print((yhat[i+1]-close_label[i])*(close_label[i+1]-close_label[i]))
        if (yhat[i + 1] - close_label[i]) * (close_label[i + 1] -
                                             close_label[i]) >= 0:
            true_predict = true_predict + 1
    print("Trend accuracy: ", str(true_predict / len(close_label)))

    error = [(x - y) * (x - y) for x, y in zip(yhat, close_label)]
    print("MSE: ", str(mean(error)))
    print("___________________________________________")
Ejemplo n.º 18
0
df = df.dropna()

df.index

df.index.freq = 'MS'

df.head()

span = 12
alpha = 2 / (span + 1)

df.rename(columns={'Thousands of Passengers': 'Pass_K'}, inplace=True)
df['EWMA12'] = df['Pass_K'].ewm(alpha=alpha, adjust=False).mean()

model = SimpleExpSmoothing(df['Pass_K'])
fitted_model = model.fit(smoothing_level=alpha, optimized=False)

fitted_model.fittedvalues.shift(-1)

df['SES12'] = fitted_model.fittedvalues.shift(-1)

# DOUBLE EXP SM
df['DES_add_12'] = ExponentialSmoothing(
    df['Pass_K'], trend='add').fit().fittedvalues.shift(-1)

df.iloc[:24].plot(figsize=(12, 5))
df.iloc[-24:].plot(figsize=(12, 5))

df['DES_mul_12'] = ExponentialSmoothing(
    df['Pass_K'], trend='mul').fit().fittedvalues.shift(-1)
airline.dropna(inplace=True)

airline.index = pd.to_datetime(airline.index)

airline.index.freq = 'MS'

from statsmodels.tsa.holtwinters import SimpleExpSmoothing

span = 12
alpha = 2 / (span + 1)

airline['EWMA12'] = airline['Thousands of Passengers'].ewm(
    alpha=alpha, adjust=False).mean()

model = SimpleExpSmoothing(airline['Thousands of Passengers'])

fitted_model = model.fit(smoothing_level=alpha, optimized=False)

#Double Exponential Smoothing Method which includes trend info in model

from statsmodels.tsa.holtwinters import ExponentialSmoothing

#we can use mulitolicative or additive adjustment

airline['DSA_add_12'] = ExponentialSmoothing(
    airline['Thousands of Passengers'], trend='add').fit().fittedvalues.shift(
        -1)  # code add represent additive model

#DSA IS MORE CLOSE TO THE OBSERVED GRAPH LINE dOUBLE eXPONENTIAL SMOOTHING
df_1['trend'] = trend
df_1['cycle'] = cycle

## Seperating trend and cyclic part of the variable and visiualising all three lines in the same graph
ax = df_1[['_Gross_Bookings_','trend','cycle']].plot(figsize=(12,5), title = 'title' ,legend = True)
ax.autoscale(axis='both' , tight = True)
ax.set(xlabel = 'Date' , ylabel = '$')
plt.legend(['Gross Bookings','Trend','Cycle'])
plt.show()


### Predictive model by using Simple Exponential Smoothing  
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
span = 12 
alpha = 2 / (span + 1)

df_1['EWMA12'] = df_1['_Gross_Bookings_'].ewm(alpha = alpha, adjust =  False).mean()
df_1.head()

model = SimpleExpSmoothing(df_1['_Gross_Bookings_'])
fitted_model = model.fit(smoothing_level=alpha,optimized= False)
df_1['ses12'] = fitted_model.fittedvalues.shift(-1)


test_predictions = fitted_model.forecast(1)
test_predictions




Ejemplo n.º 21
0
 def expo01(self):
     x = list(self.df_m['y'])
     m = SimpleExpSmoothing(x).fit(optimized=True)
     r = self.expo(m)
     return r
    plastic["Sales"].rolling(i).mean().plot(label=str(i))
    plt.legend(loc=4)
seasonal_dec=sm.tsa.seasonal_decompose(plastic["Sales"],freq=3)
seasonal_dec.plot()

Train=plastic.head(48)
Test=plastic.tail(12)
Test=Test.set_index(np.arange(1,13))

#MAPE
def MAPE(pred,org):
    temp=np.abs((pred-org))*100/org
    return np.mean(temp)

###Simple exponential Smoothing#######
ses=SimpleExpSmoothing(Train["Sales"]).fit()
ses_pred=ses.predict(start=Test.index[0],end=Test.index[-1])
ses_MAPE=MAPE(ses_pred,Test.Sales)#######26.09

HW=Holt(Train["Sales"]).fit()
HW_pred=HW.predict(start=Test.index[0],end=Test.index[-1])
HW_Mape=MAPE(HW_pred,Test.Sales) #########26.60



HW_exp_add=ExponentialSmoothing(Train["Sales"],trend="add",seasonal="add",seasonal_periods=12,damped=True).fit()
HW_exp_pred=HW_exp_add.predict(start=Test.index[0],end=Test.index[-1])
HW_exp_add_Mape=MAPE(HW_exp_pred,Test.Sales)####25.50

HW_exp_mul_add=ExponentialSmoothing(Train["Sales"],trend="mul",seasonal="add",seasonal_periods=12).fit()
HW_exp_mul_pred=HW_exp_mul_add.predict(start=Test.index[0],end=Test.index[-1])
from matplotlib import style
import math
from statistics import mean

plt.style.use('fivethirtyeight')

df = pd.read_excel("../00Daily/Australia.xlsx", squeeze=True, parse_dates=True)
df = df[["Date", "LocalTransmission"]]
df.set_index("Date", inplace=True)
df.dropna(inplace=True)
##df['Date'] = pd.to_datetime(df['Date'])
LocalTransmission = df['LocalTransmission'].astype('int32')
#print (df.head())
#print (df.index)

result = SimpleExpSmoothing(df).fit()
print(result.summary())
#print(result.params)
predictions = result.predict(start="2020-02-29", end="2020-05-01")
#accuracy = result.score()
print("Predictions: ", predictions, sep='\n')
##accuracy = result.score()
#print (accuracy)

#result.plot_predict(start="2020-03-01", end="2020-05-01")

plt.plot(predictions)
plt.show()

##def mean_forecast_error(y, yhat):
##    return y.sub(yhat).mean()
#################################
# smoothing_level = alpha parameter = learning coefficient -> level
def optimize_ses(train, alphas, step=48):
    for alpha in alphas:
        ses_model = SimpleExpSmoothing(train).fit(smoothing_level=alpha)
        y_pred = ses_model.forecast(step)
        mae = mean_absolute_error(test, y_pred)
        print("alpha:", round(alpha, 2), "mae:", round(mae, 4))


alphas = np.arange(0.01, 1, 0.10)
# step 24 because test size is 24 months, test.shape = (24, 1)
optimize_ses(train, alphas, step=24)
# alpha: 0.11 mae: 82.528

ses_model = SimpleExpSmoothing(train).fit(smoothing_level=0.11)
y_pred = ses_model.forecast(24)


def plot_prediction(y_pred, label):
    train["total_passengers"].plot(legend=True, label="TRAIN")
    test["total_passengers"].plot(legend=True, label="TEST")
    y_pred.plot(legend=True, label="PREDICTION")
    plt.title("Train, Test and Predicted Test Using " + label)
    plt.show()


plot_prediction(y_pred, "Single Exponential Smoothing")


#################################
Ejemplo n.º 25
0
import Constants
import Logging

airlinePassengerVolumeDF = pd.read_html ( Constants.readhtml ( Constants.AIRLINES ).text, index_col='Month',
                                          parse_dates=True )
airlinePassengerVolumeDF = airlinePassengerVolumeDF[0]
airlinePassengerVolumeDF = airlinePassengerVolumeDF.drop ( 'Unnamed: 0', axis=1 )
airlinePassengerVolumeDF = airlinePassengerVolumeDF.dropna ()

span = 12
alpha = 2 / (span + 1)

airlinePassengerVolumeDF['EWMA'] = airlinePassengerVolumeDF['Thousands of Passengers'].ewm ( alpha=alpha,
                                                                                             adjust=False ).mean ()
model = SimpleExpSmoothing ( airlinePassengerVolumeDF['Thousands of Passengers'] )
fitted_model = model.fit ( optimized=False, smoothing_level=alpha )
airlinePassengerVolumeDF['SES12'] = fitted_model.fittedvalues.shift ( -1 )
toJSON = airlinePassengerVolumeDF.head ( 5 ).to_json ()
print ( toJSON )
parsed = json.loads ( toJSON )
print ( parsed )
Logging.test_logger.info ( 'After adding the SES: {} '.format ( parsed ) )

ax = airlinePassengerVolumeDF['EWMA'].plot ( label='EWMA' )
airlinePassengerVolumeDF['SES12'].plot ( label='SES12' )
ax = ax.set ( xlabel=Constants.TIME, ylabel='PassengerVolume ( Thousands)' )
plt.show ()

airlinePassengerVolumeDF['DES12'] = ExponentialSmoothing ( airlinePassengerVolumeDF['Thousands of Passengers'],
                                                           trend='add' ).fit ().fittedvalues.shift ( -1 ).plot (
def optimize_ses(train, alphas, step=48):
    for alpha in alphas:
        ses_model = SimpleExpSmoothing(train).fit(smoothing_level=alpha)
        y_pred = ses_model.forecast(step)
        mae = mean_absolute_error(test, y_pred)
        print("alpha:", round(alpha, 2), "mae:", round(mae, 4))
Ejemplo n.º 27
0
# 2. find if data is stationary by applying Dickey-Fuller test
# 3. if not stationary, take lag-1, lag-2, etc. difference to see if smoothing occurs
# 4. now ready for Stationary ARIMA modeling
# look at link: https://towardsdatascience.com/the-complete-guide-to-time-series-analysis-and-forecasting-70d476bfe775

company = "AAPL"
data = yf.Ticker(company).history('10y').reset_index()

tsdata = data["Close"]

ts_ma = tsdata.rolling(30).mean()
# plt.plot(tsdata)
# plt.plot(ts_ma)
# plt.show()

simp_model = SimpleExpSmoothing(tsdata)
simp_model_fit = simp_model.fit()
# print(simp_model_fit.summary())

double_model = ExponentialSmoothing(tsdata, trend='add')
double_model_fit = double_model.fit(use_boxcox=True)

# print(double_model_fit.summary())
# print(double_model_fit.forecast(10))

# print(double_model_fit.predict(1250,1300))

plt.plot(tsdata)
plt.plot(double_model_fit.fittedvalues)
plt.show()
Ejemplo n.º 28
0
def init_ES_models():
    models = dict()

    models['SES'] = SimpleExpSmoothing()
    models['HWES'] = ExponentialSmoothing()
    return models
def SES(data1):
    from statsmodels.tsa.holtwinters import SimpleExpSmoothing
    model = SimpleExpSmoothing(data1)
    model_fit = model.fit()
    SESresult = model_fit.predict(len(data1), len(data1))
    print('The period and the prediction from the SES Model is:', SESresult)
Ejemplo n.º 30
0
    print (dfoutput)

test_stationarity(AirlineData.Passengers)

# splitting the data into Train and Test data and considering the last 12 months data as 
# Test data and left over data as train data 
Train = AirlineData.head(84)
Test = AirlineData.tail(12)

# Creating a function to calculate the MAPE value for test data 
def MAPE(pred,org):
    temp = np.abs((pred-org))*100/org
    return np.mean(temp)

# Simple Exponential Method
ses_model = SimpleExpSmoothing(Train['Passengers'].astype('double')).fit()
pred_ses = ses_model.predict(start = Test.index[0],end = Test.index[-1])
ses_model_MAPE=MAPE(pred_ses,Test.Passengers) #14.235

# Holt method 
hw_model = Holt(Train["Passengers"].astype('double')).fit()
pred_hw = hw_model.predict(start = Test.index[0],end = Test.index[-1])
hw_model_MAPE=MAPE(pred_hw,Test.Passengers) # 11.840


# Holts winter exponential smoothing with additive seasonality and additive trend
hwe_model_add_add = ExponentialSmoothing(Train["Passengers"].astype('double'),seasonal="add",trend="add",seasonal_periods=12,damped=True).fit()
pred_hwe_add_add = hwe_model_add_add.predict(start = Test.index[0],end = Test.index[-1])
hwe_add_add_MAPE=MAPE(pred_hwe_add_add,Test.Passengers) # 1.61

# Holts winter exponential smoothing with multiplicative seasonality and additive trend

# In[7]:


from statsmodels.tsa.seasonal import seasonal_decompose
seasonal_decompose(train_data['Passengers'], period=12).plot();


# In[8]:


from statsmodels.tsa.holtwinters import SimpleExpSmoothing
span = 12 # The model will consider the last 12 months weighted average for forecasting
alpha = 2/(span+1)
model = SimpleExpSmoothing(train_data['Passengers']).fit(smoothing_level=alpha)
test_predictions = model.forecast(36).rename('SES Forecast')


# In[10]:


#Now Lets Plot the Predictions
train_data['Passengers'].plot(legend=True,label='TRAIN')
test_data['Passengers'].plot(legend=True,label='TEST',figsize=(12,8))
test_predictions.plot(legend=True,label='PREDICTION');


# The SimpleExponentialModel does not consider the trend and seasonality. It will just take the weighted average of past data and forecast that average for all testing data. That’s why you can observe a straight line as the prediction. This model is not much useful for us.

#                                              #### Double Exponential Smoothing
Ejemplo n.º 32
0
    def forecast_prediction(self, weather_filtered, weather_keys):
        '''
            # TODO: Adjust data-time to give valid look forward prediction
        '''
        scaled_data = self.scale_weather_data(weather_filtered)
        index = [i for i in range(len(scaled_data[weather_keys[0]]))]
        shortest_val = np.Inf
        for i in scaled_data.items():
            shortest_val = min(i[1].shape[0], shortest_val)
        scaled_df = pd.DataFrame(
            {
                weather_keys[0]:
                np.transpose(scaled_data[weather_keys[0]])[0][-shortest_val:],
                weather_keys[1]:
                np.transpose(scaled_data[weather_keys[1]])[0][-shortest_val:],
                weather_keys[2]:
                np.transpose(scaled_data[weather_keys[2]])[0][-shortest_val:],
                weather_keys[3]:
                np.transpose(scaled_data[weather_keys[3]])[0][-shortest_val:],
                weather_keys[4]:
                np.transpose(scaled_data[weather_keys[4]])[0][-shortest_val:],
                weather_keys[5]:
                np.transpose(scaled_data[weather_keys[5]])[0][-shortest_val:],
                weather_keys[6]:
                np.transpose(scaled_data[weather_keys[6]])[0][-shortest_val:],
                weather_keys[7]:
                np.transpose(scaled_data[weather_keys[7]])[0][-shortest_val:],
                weather_keys[8]:
                np.transpose(scaled_data[weather_keys[8]])[0][-shortest_val:],
                # following columns not being used
                'peas': [-1] * shortest_val,
                'citrus': [-1] * shortest_val,
                'potato': [-1] * shortest_val,
            },
            index=index)
        ##############################################################################################
        scaled_df['potato_pymc3'] = scaled_df.apply(
            lambda x: self.predict_crop_feasibility(x, pymc3_params)[0],
            axis=1)
        scaled_df['citrus_pymc3'] = scaled_df.apply(
            lambda x: self.predict_crop_feasibility(x, pymc3_params)[1],
            axis=1)
        scaled_df['peas_pymc3'] = scaled_df.apply(
            lambda x: self.predict_crop_feasibility(x, pymc3_params)[2],
            axis=1)
        scaled_df['crop_pymc3'] = scaled_df[[
            'potato_pymc3', 'citrus_pymc3', 'peas_pymc3'
        ]].idxmax(axis=1).replace('_pymc3', '')

        scaled_df['potato_lr'] = scaled_df.apply(
            lambda x: self.predict_crop_feasibility(x, lr_params)[0], axis=1)
        scaled_df['citrus_lr'] = scaled_df.apply(
            lambda x: self.predict_crop_feasibility(x, lr_params)[1], axis=1)
        scaled_df['peas_lr'] = scaled_df.apply(
            lambda x: self.predict_crop_feasibility(x, lr_params)[2], axis=1)
        scaled_df['crop_lr'] = scaled_df[['potato_lr', 'citrus_lr', 'peas_lr'
                                          ]].idxmax(axis=1).replace('_lr', '')

        scaled_df['potato_ridge'] = scaled_df.apply(
            lambda x: self.predict_crop_feasibility(x, ridge_params)[0],
            axis=1)
        scaled_df['citrus_ridge'] = scaled_df.apply(
            lambda x: self.predict_crop_feasibility(x, ridge_params)[1],
            axis=1)
        scaled_df['peas_ridge'] = scaled_df.apply(
            lambda x: self.predict_crop_feasibility(x, ridge_params)[2],
            axis=1)
        scaled_df['crop_ridge'] = scaled_df[[
            'potato_ridge', 'citrus_ridge', 'peas_ridge'
        ]].idxmax(axis=1).replace('_ridge', '')

        ## Everything up to here looks fine
        exp_potato = SimpleExpSmoothing(np.asarray(
            scaled_df['potato_pymc3'])).fit(smoothing_level=0.1,
                                            optimized=False)
        exp_citrus = SimpleExpSmoothing(np.asarray(
            scaled_df['citrus_pymc3'])).fit(smoothing_level=0.1,
                                            optimized=False)
        exp_peas = SimpleExpSmoothing(np.asarray(scaled_df['peas_pymc3'])).fit(
            smoothing_level=0.1, optimized=False)

        month_current = int(date.today().strftime('%m'))

        potato_month3 = exp_potato.predict(month_current, month_current + 3)

        rms1 = sqrt(
            mean_squared_error(scaled_df['potato_pymc3'][:4], potato_month3))
        # print(rms1)
        citrus_month3 = exp_citrus.predict(month_current, month_current + 3)
        rms2 = sqrt(
            mean_squared_error(scaled_df['citrus_pymc3'][:4], citrus_month3))
        # print(rms2)
        peas_month3 = exp_peas.predict(month_current, month_current + 3)
        rms3 = sqrt(
            mean_squared_error(scaled_df['peas_pymc3'][:4], peas_month3))
        # print(rms3)

        potato_month6 = exp_potato.predict(month_current, month_current + 6)
        # rms4 = sqrt(mean_squared_error(scaled_df['potato_pymc3'], potato_month6))
        # print(rms4)
        citrus_month6 = exp_citrus.predict(month_current, month_current + 6)
        # rms5 = sqrt(mean_squared_error(scaled_df['citrus_pymc3'], citrus_month6))
        # print(rms5)
        peas_month6 = exp_peas.predict(month_current, month_current + 6)
        # rms6 = sqrt(mean_squared_error(scaled_df['peas_pymc3'], peas_month6))
        # print(rms6)

        scaled_df['3 months potato'] = potato_month3.mean()
        scaled_df['3 months citrus'] = citrus_month3.mean()
        scaled_df['3 months peas'] = peas_month3.mean()
        scaled_df['6 months potato'] = potato_month6.mean()
        scaled_df['6 months citrus'] = citrus_month6.mean()
        scaled_df['6 months peas'] = peas_month6.mean()

        # print(scaled_df[['3 months potato','3 months citrus', '3 months peas','6 months potato', '6 months citrus', '6 months peas'  ]])

        return scaled_df