Esempio n. 1
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)
Esempio 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)
def Croston_HW(crop_data, market, time_to_predict, date):
    marketData = marketChoice(crop_data, market)  # nan rows are made
    marketData = croston_prep(marketData, market)
    marketData["Modal"] = pd.to_numeric(marketData["Modal"])
    crostonData = Croston(marketData["Modal"], extra_periods=1, alpha=0.4)
    for ind in crostonData.index:
        if crostonData["Demand"][ind] == 0:
            crostonData["Demand"][ind] = crostonData["Forecast"][ind]
    crostonData = crostonData["Demand"]
    trial = marketChoice(crop_data, market)
    trial = trial[trial['Date'] == date]
    start_date = trial.index[0]
    train_data, test_data = split_test_train(crostonData, time_to_predict,
                                             start_date)
    #   print("train_data: ", train_data)
    #   print("\n")
    #   print("test_data: ", test_data)
    test_data = test_data[0:len(test_data) - 1]
    #   fit1 = ExponentialSmoothing(train_data,
    #                             seasonal_periods=rand,
    #                             trend='multiplicative',
    #                             seasonal='mul',
    #                             damped=True).fit(use_boxcox=True)
    fit1 = SimpleExpSmoothing(train_data).fit(use_boxcox=True)
    frame = fit1.forecast(int(time_to_predict))
    pred = pd.DataFrame
    pred = frame
    res = list(pred[0:int(time_to_predict)])
    return res, test_data
Esempio n. 4
0
def get_forecast(df, month, country="ALL"): 

    df_ts = df.copy() if country=="ALL" else df[df['country']==country].copy()
    if len(df_ts) <= 180: 
        return None
    
    actual = df_ts[df_ts['inv_month'] == month.isoformat()]['value'].sum()

    df_ts = df_ts[['inv_month', 'inv_date', 'value']].groupby(['inv_month', 'inv_date']).sum().reset_index()
    
    df_train = df_ts[df_ts['inv_month'] < month.isoformat()]
    df_train['inv_date'] = pd.to_datetime(df_train['inv_date'])
    df_train.rename(columns={'inv_date':'ds', 'value':'y'}, inplace=True)

    m = Prophet(yearly_seasonality=20)
    m.fit(df_train)
    future = m.make_future_dataframe(periods=60, freq='D')
    df_forecast = m.predict(future)
    df_forecast['inv_month'] = df_forecast['ds'].apply(lambda v: date(v.year, v.month, 1).isoformat())
    forecast = df_forecast[df_forecast['inv_month'] == month.isoformat()]['yhat'].sum()

    exp_model = SimpleExpSmoothing(df_ts[['inv_month', 'inv_date', 'value']].set_index(['inv_month', 'inv_date'])).fit(smoothing_level=0.2, optimized=False)
    exp_forecast = forecast_df = sum(exp_model.forecast(30))
    
    return actual, forecast, exp_forecast
Esempio n. 5
0
def CC_CLS_CL(Dataframe, HNAME_List, Raceday):

    """
    Horse's Competition Level
    Parameter
    ---------
    Matchday : Matchday Dataframe
    HNAME_List : String of List of Horse Names
    Raceday : Date of Race
    Return
    ------
    Dataframe [HNAME, CC_CLS_CL]
    """

    Feature_DF = Dataframe.loc[:,['HNAME','HJRAT']]

    Horse_Comp = []
    #For each horse, get data for last 5 races
    for Horse in Dataframe['HNAME'].tolist():
        Extraction = Extraction_Database("""
                                         Select HNAME, RARID, HJRAT, RESFP from RaceDb
                                         where RARID in (
                                         Select RARID from RaceDb
                                         where RADAT < {Raceday} and HNAME = {Horse}
                                         ORDER BY RARID DESC
                                         LIMIT 5)
                                         """.format(Raceday = Raceday, Horse = "'" + Horse + "'"))

        for RARID, race in Extraction.groupby('RARID'):
            Horse_Rat = race.loc[race.loc[:,'HNAME']==Horse,'HJRAT'].to_list()[0]
            Horse_FP = race.loc[race.loc[:,'HNAME']==Horse,'RESFP'].to_list()[0]
            Comp_Rat = race.nlargest(3, 'HJRAT').loc[:,'HJRAT'].mean()
            Comp_Level = (Comp_Rat - Horse_Rat) / Horse_FP
            Horse_Comp.append([Horse,Comp_Level])
    Horse_Comp = pd.DataFrame(Horse_Comp, columns=['HNAME', 'Comp_Level'])

    #Recency Weighting
    Comp = []
    for name, group in Horse_Comp.groupby('HNAME'):
        Comp_Figure = group.loc[:,'Comp_Level'].dropna().values
        try :
            model = SimpleExpSmoothing(Comp_Figure)
            model = model.fit()
            Comp.append([name, model.forecast()[0]])
        except :
            Comp.append([name,Comp_Figure[0]])
    Comp = pd.DataFrame(Comp, columns=['HNAME','CC_CLS_CL'])

    Feature_DF = Feature_DF.merge(Comp, how='left')
    Feature_DF.loc[:, 'CC_CLS_CL'].fillna(Feature_DF.loc[:, 'CC_CLS_CL'].min(), inplace = True)
    Feature_DF.loc[:, 'CC_CLS_CL'].fillna(0, inplace=True)
    Feature_DF = Feature_DF.loc[:, ['HNAME', 'CC_CLS_CL']]

    return Feature_DF
Esempio n. 6
0
def model_exp_smooth(data3,train_X,train_y,col_name):
    # create class
    model = SimpleExpSmoothing(data3).fit(smoothing_level=0.3,optimized=False)
    # fit model
    for i in range(5):
        model_fit = model.forecast(1)
        model_fit.columns=[col_name]
        data3=pd.concat([data3,model_fit])
        data3.iloc[-1,0]=data3.iloc[-1,-1]
        data3.drop(data3.columns[-1],inplace=True,axis=1)
        data3.columns=[col_name]
        model = SimpleExpSmoothing(data3).fit(smoothing_level=0.2, optimized=True)
    return model,data3
def theta(data, forecast_length):
    theta0 = 0
    theta2 = 2

    def b_theta(theta, series):
        n = len(series)
        const = (6 * (1 - theta)) / ((n ** 2) - 1)
        tmp1 = 2 * np.array([(t + 1) * x_t for t, x_t in enumerate(
            series)]).mean()
        tmp2 = (n + 1) * series.mean()

        return const * (tmp1 - tmp2)

    def a_theta(theta, series):
        term1 = (1 - theta) * series.mean()
        term2 = b_theta(theta, series) * (len(series) - 1) / 2

        return term1 - term2

    a_theta0 = a_theta(theta0, data)
    a_theta2 = a_theta(theta2, data)
    b_theta0 = b_theta(theta0, data)
    b_theta2 = b_theta(theta2, data)

    # Calculate theta0 and theta2 lines for the train data
    theta0_fitted = pd.Series(
        [a_theta0 + (b_theta0 * t) for t in range(len(data))]
    )
    theta2_fitted = pd.Series(
        [a_theta2 + (b_theta2 * t) + theta2 * data[t]
         for t in range(len(data))]
    )

    # Predict theta0 and theta2 lines for the test data
    theta0_predicted = pd.Series(
        [a_theta0 + b_theta0 * (len(data) + h)
         for h in range(forecast_length)]
    )

    # Predict theta0 and theta2 lines for the test data
    theta2_model_fitted = SimpleExpSmoothing(theta2_fitted).fit()
    theta2_predicted = theta2_model_fitted.forecast(forecast_length)

    theta0 = theta0_fitted.append(theta0_predicted).reset_index(drop=True)
    theta2 = theta2_fitted.append(theta2_predicted).reset_index(drop=True)

    params = theta2_model_fitted.params
    params['initial_seasons'] = params['initial_seasons'].tolist()
    # Take the arithmetic average of the two theta lines to get the forecast
    return (theta0 + theta2) / 2, theta2_model_fitted.params
Esempio n. 8
0
def common_qty(user_id, product_id):
    user_history = df.loc[df['Ship-to nu'] == user_id]
    matching_df = user_history.loc[user_history['Material'] == product_id]
    qty_history = list(matching_df['HL delivered'])
    index = range(len(qty_history))
    qty_data = pd.Series(qty_history, index)
    if (qty_data.shape[0] < 3):
        qty = np.mean(qty_history)
    else:
        fit = SimpleExpSmoothing(qty_data).fit()
        qty = float(fit.forecast(1))
    if qty > 0.0:
        return round(qty, 2)
    else:
        return float('NaN')
Esempio n. 9
0
def get_exp_forecast(df, country="ALL"):
    df_ts = df.copy() if country == "ALL" else df[df['country'] ==
                                                  country].copy()
    if len(df_ts) <= 180:
        return None

    df_ts['inv_date'] = pd.to_datetime(df_ts['inv_date'])
    model = SimpleExpSmoothing(
        df_ts[['inv_month', 'inv_date',
               'value']].set_index(['inv_month',
                                    'inv_date'])).fit(smoothing_level=0.2,
                                                      optimized=False)

    forecast_df = model.forecast(30).rename(r'value')
    return sum(forecast_df)
Esempio n. 10
0
def OD_PR_LPAVG(Dataframe, HNAME_List, Raceday):
    """
    Average Log Odds implied Probability
    Parameter
    ---------
    Matchday : Matchday Dataframe
    HNAME_List : String of List of Horse Names
    Raceday : Date of Race
    Return
    ------
    Dataframe [HNAME, OD_PR_LPAVG]
    """

    Feature_DF = Dataframe.loc[:, ['HNAME', 'RARID']]

    Extraction = Extraction_Database("""
                                     Select HNAME, RARID, RESFO from RaceDb
                                     where RADAT < {Raceday} and HNAME in {HNAME_List}
                                     """.format(Raceday=Raceday,
                                                HNAME_List=HNAME_List))

    Odds = []
    for name, group in Extraction.groupby('HNAME'):
        Probi = group.loc[:, 'RESFO'].map(lambda x: np.log(
            (1 - 0.175) / x)).dropna().values
        if len(Probi) > 1:
            model = SimpleExpSmoothing(Probi)
            model = model.fit()
            Odds.append([name, model.forecast()[0]])
        elif len(Probi) == 1:
            Odds.append([name, Probi[0]])
        else:
            Odds.append([name, 0])
    Odds = pd.DataFrame(Odds, columns=['HNAME', 'OD_PR_LPAVG'])

    Feature_DF = Feature_DF.merge(Odds, how='left')
    Feature_DF.loc[:,
                   'OD_PR_LPAVG'].fillna(Feature_DF.loc[:,
                                                        'OD_PR_LPAVG'].min(),
                                         inplace=True)
    Feature_DF.loc[:, 'OD_PR_LPAVG'].fillna(0, inplace=True)
    Feature_DF = Feature_DF.loc[:, ['HNAME', 'OD_PR_LPAVG']]

    return Feature_DF
Esempio n. 11
0
def simpleExponentialSmoothing(df,
                               no_predictions=7,
                               debug=False,
                               visualize=False):
    train_data, test_data = df[1:int(len(df) - no_predictions
                                     )], df[int(len(df) - no_predictions):]
    fit1 = SimpleExpSmoothing(np.asarray(train_data)).fit(smoothing_level=0.85,
                                                          optimized=False)
    if (debug):
        print(fit1.summary())
    predictions = fit1.forecast(no_predictions * 2)
    if (visualize):
        plt.plot(list(test_data), color='blue', label='testing data')
        plt.plot(list(predictions), color='red', label='prediction')
        plt.legend(loc='upper left', fontsize=8)
        plt.show()
    error = np.sqrt(mean_squared_error(test_data,
                                       predictions[:no_predictions]))
    return (predictions[-no_predictions:], error, fit1)
Esempio n. 12
0
def CC_BWEI_D(Dataframe, HNAME_List, Raceday):

    """
    Change in Bodyweight of Horse
    Parameter
    ---------
    Matchday : Matchday Dataframe
    HNAME_List : String of List of Horse Names
    Raceday : Date of Race
    Return
    ------
    Dataframe [HNAME, CC_BWEI_D]
    """

    Feature_DF = Dataframe.loc[:,['HNAME','RARID']]

    Extraction = Extraction_Database("""
                                     Select HNAME, RARID, HBWEI from RaceDb
                                     where RADAT < {Raceday} and HNAME in {HNAME_List}
                                     """.format(Raceday = Raceday, HNAME_List = HNAME_List))

    HBWEI = []
    for name, group in Extraction.groupby('HNAME'):
        Weight = (group.loc[:,'HBWEI'].diff() / group.loc[:,'HBWEI']).dropna().values
        if len(Weight) >1:
            model = SimpleExpSmoothing(Weight)
            model = model.fit()
            HBWEI.append([name, model.forecast()[0]])
        elif len(Weight) == 1:
            HBWEI.append([name,Weight[0]])
        else :
            HBWEI.append([name,0])
    HBWEI = pd.DataFrame(HBWEI, columns=['HNAME','CC_BWEI_D'])

    Feature_DF = Feature_DF.merge(HBWEI, how='left')
    Feature_DF.loc[:,'CC_BWEI_D'] = Feature_DF.loc[:,'CC_BWEI_D'].abs()
    Feature_DF.loc[:,'CC_BWEI_D'].fillna(Feature_DF.loc[:,'CC_BWEI_D'].max(), inplace = True)
    Feature_DF.loc[:,'CC_BWEI_D'].fillna(0, inplace = True)
    Feature_DF = Feature_DF.loc[:,['HNAME','CC_BWEI_D']]

    return Feature_DF
Esempio n. 13
0
def CC_CLS_D(Dataframe, HNAME_List, Raceday):

    """
    Change in HKJC Rating
    Parameter
    ---------
    Matchday : Matchday Dataframe
    HNAME_List : String of List of Horse Names
    Raceday : Date of Race
    Return
    ------
    Dataframe [HNAME, CC_CLS_D]
    """

    Feature_DF = Dataframe.loc[:,['HNAME','RARID']]

    Extraction = Extraction_Database("""
                                     Select HNAME, RARID, HJRAT from RaceDb
                                     where RADAT < {Raceday} and HNAME in {HNAME_List}
                                     """.format(Raceday = Raceday, HNAME_List = HNAME_List))

    JRat = []
    for name, group in Extraction.groupby('HNAME'):
        Rating = (group.loc[:,'HJRAT'].diff() / group.loc[:,'HJRAT']).dropna().values
        if len(Rating) >1:
            model = SimpleExpSmoothing(Rating)
            model = model.fit()
            JRat.append([name, model.forecast()[0]])
        elif len(Rating) == 1:
            JRat.append([name,Rating[0]])
        else :
            JRat.append([name,0])
    JRat = pd.DataFrame(JRat, columns=['HNAME','CC_CLS_D'])

    Feature_DF = Feature_DF.merge(JRat, how='left')
    Feature_DF.loc[:,'CC_CLS_D'].fillna(Feature_DF.loc[:,'CC_CLS_D'].min(), inplace = True)
    Feature_DF.loc[:,'CC_CLS_D'].fillna(0, inplace = True)
    Feature_DF = Feature_DF.loc[:,['HNAME','CC_CLS_D']]

    return Feature_DF
Esempio n. 14
0
def calculate_time_serie(data, time_serie_type, trend_seasonal, period,
                         forecast):

    if time_serie_type == 'simpsmoothing':
        data_simp_exp = SimpleExpSmoothing(data).fit()
        proyeccion = data_simp_exp.forecast(int(forecast))
        return data_simp_exp.fittedvalues, proyeccion
    elif time_serie_type == 'holt':
        data_holt = Holt(data).fit()
        proyeccion = data_holt.forecast(int(forecast))
        return data_holt.fittedvalues, proyeccion
    elif time_serie_type == 'holt_winters':
        print(trend_seasonal)
        if trend_seasonal == 'add':
            print('periodo', period)
            data_holtwinters = ExponentialSmoothing(
                data, trend='add', seasonal='add',
                seasonal_periods=period).fit(use_boxcox=True)
            print(data_holtwinters.fittedvalues)
        elif trend_seasonal == 'mult':
            data_holtwinters = ExponentialSmoothing(
                data, trend='mul', seasonal='mul',
                seasonal_periods=period).fit(use_boxcox=True)
        proyeccion = data_holtwinters.forecast(int(forecast))

        return data_holtwinters.fittedvalues, proyeccion
    elif time_serie_type == 'arima':
        arima = pmdarima.auto_arima(data,
                                    seasonal=False,
                                    error_action='ignore',
                                    suppress_warnings=True)
        proyeccion, int_conf = arima.predict(n_periods=int(forecast),
                                             return_conf_int=True)
        prediccion = arima.predict_in_sample()
        print('pro', proyeccion)
        print('pre', prediccion)
        return prediccion, proyeccion
# 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")


#################################
# Double Exponential Smoothing
Esempio n. 16
0
                 progress=False)

#aggreagte to monthly frequency

goog = df.resample('M').last() \
    .rename(columns={'Adj Close': 'adj_close'}).adj_close
#create split
train_indices = goog.index.year < 2018
goog_train = goog[train_indices]
goog_test = goog[~train_indices]

test_length = len(goog_test)

#fit three SES models
ses_1 = SimpleExpSmoothing(goog_train).fit(smoothing_level=0.2)
ses_forecast_1 = ses_1.forecast(test_length)

ses_2 = SimpleExpSmoothing(goog_train).fit(smoothing_level=0.5)
ses_forecast_2 = ses_2.forecast(test_length)

ses_3 = SimpleExpSmoothing(goog_train).fit()
alpha = ses_3.model.params['smoothing_level']
ses_forecast_3 = ses_3.forecast(test_length)

#plotting
goog.plot(color=COLORS[0],
          title='Simple Exponential Smoothin',
          label='Actual',
          legend=True)

ses_forecast_1.plot(c=COLORS[1], legend=True, label=r'$\alpha=0.2$')
#Linear Model with Multiplicative seasonality can be used for max accuracy

# Data based approaches
from statsmodels.graphics import tsaplots
from statsmodels.tsa.holtwinters import SimpleExpSmoothing, Holt, ExponentialSmoothing

tsaplots.plot_acf(airlines.Passengers, lags=20)
tsaplots.plot_pacf(airlines.Passengers, lags=20)

# as the data has trend and seasonality, Holt Winters method should be chosen

# --> Simple Exponential smoothing
len(test)
ses_model = SimpleExpSmoothing(train.Passengers).fit()
ses_fcast = ses_model.forecast(24)

airlines.Passengers.plot(label='Original', legend=True)
ses_fcast.plot(label='Predicted', legend=True)
ses_model.fittedvalues.plot(label='Fitted', legend=True)


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


ses_mape = MAPE(test.Passengers, ses_fcast)
# --> Holts smoothing
holt_model_lin = Holt(train.Passengers).fit()
holt_fcast_lin = holt_model_lin.forecast(24)
def run_method():
    # config
    plt.style.use('bmh')
    sns.set_style("whitegrid")
    plt.rc('xtick', labelsize=15)
    plt.rc('ytick', labelsize=15)
    warnings.filterwarnings("ignore")
    pd.set_option('max_colwidth', 100)
    pd.set_option('display.max_rows', 500)
    pd.set_option('display.max_columns', 500)
    color_pal = plt.rcParams['axes.prop_cycle'].by_key()['color']
    color_cycle = cycle(plt.rcParams['axes.prop_cycle'].by_key()['color'])

    # 导入数据集:
    data = pd.read_csv(str(proj_root_dir / 'data/data_for_tsa.csv'))
    data['date'] = pd.to_datetime(data['date'])
    print(data.head())

    train = data[data['date'] <= '2016-03-27']
    test = data[(data['date'] > '2016-03-27') & (data['date'] <= '2016-04-24')]

    # plot data
    fig, ax = plt.subplots(figsize=(25, 5))
    train.plot(x='date', y='demand', label='Train', ax=ax)
    test.plot(x='date', y='demand', label='Test', ax=ax)

    predictions = pd.DataFrame()
    predictions['date'] = test['date']
    stats = pd.DataFrame(columns=['Model Name', 'Execution Time', 'RMSE'])

    # 开始调用具体方法
    fig, ax = plt.subplots(figsize=(15, 3))
    plot_acf(data['demand'].tolist(), lags=60, ax=ax)

    t0 = time.time()
    model_name = 'Simple Exponential Smoothing'
    span = 7
    alpha = 2 / (span + 1)
    # train
    simpleExpSmooth_model = SimpleExpSmoothing(train['demand']).fit(
        smoothing_level=alpha, optimized=False)
    t1 = time.time() - t0
    # predict
    predictions[model_name] = simpleExpSmooth_model.forecast(28).values
    # visualize
    fig, ax = plt.subplots(figsize=(25, 4))
    train[-28:].plot(x='date', y='demand', label='Train', ax=ax)
    test.plot(x='date', y='demand', label='Test', ax=ax)
    predictions.plot(x='date', y=model_name, label=model_name, ax=ax)
    # evaluate
    score = np.sqrt(
        mean_squared_error(predictions[model_name].values, test['demand']))
    print('RMSE for {}: {:.4f}'.format(model_name, score))

    stats = stats.append(
        {
            'Model Name': model_name,
            'Execution Time': t1,
            'RMSE': score
        },
        ignore_index=True)

    print("stats: %s" % (stats, ))
    plt.show()
Esempio n. 19
0
# %%
from statsmodels.tsa.holtwinters import SimpleExpSmoothing

airpassengers_train = airpassengers_series[:-24]
airpassengers_test = airpassengers_series[-24:]

airpassengers_log_train = airpassengers_log[:-24]
airpassengers_log_test = airpassengers_log[-24:]

airpassengers_diff_train = airpassengers_diff[:-24]
airpassengers_diff_test = airpassengers_diff[-24:]

ses = SimpleExpSmoothing(airpassengers_diff_train[1:])
ses = ses.fit()

ses_forecast = ses.forecast(24)


# %%
plt.plot(airpassengers_diff_train)
plt.plot(ses_forecast)
plt.title('forecast for next 24 month')

# %% [markdown]
# Inverse differencing

# %%
ses_forecast[0] = ses_forecast[0] + airpassengers_log_train[-1]
ses_forecast_inv_diff = ses_forecast.cumsum()

# %% [markdown]
Esempio n. 20
0
#Plotting acf & pacf - residual
from statsmodels.graphics.tsaplots import plot_acf
from statsmodels.graphics.tsaplots import plot_pacf
plot_acf(rain_ses_res,
         lags=20,
         title='rain - Residual given by the model - Autocorrelation')
plot_pacf(rain_ses_res,
          lags=20,
          title='rain - Residual given by the model - Partial Autocorrelation')

#Squaring residuals/ errors
rain_ses_se = pow(rain_ses_res, 2)
rain_ses_se.head()

#average/mean of squared residuals/ errors
rain_ses_mse = (rain_ses_se.sum()) / len(rain_ses_se)
print(rain_ses_mse)  #17.584652052034816

#Root of average/mean of squared residuals/ errors
rain_ses_rmse = sqrt(rain_ses_mse)
print(rain_ses_rmse)  #4.193405781943219

#forecasting next 8 periods
rain_pred = rain_ses.forecast(steps=8)
print(rain_pred)

#Plot Actual & Forecast
plt.plot(rain)
plt.plot(rain_pred)
plt.legend(['Actual', 'Forecast'], bbox_to_anchor=(1, 1), loc=2)
plt.show()
Esempio n. 21
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(
# 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
#                                              
Esempio n. 23
0
# In[13]:


data1=data.astype('float64')


# In[14]:


ses = SimpleExpSmoothing(data).fit()


# In[15]:


data2=ses.forecast(12)


# In[49]:


data2


# In[16]:


ses.sse


# In[17]:
Esempio n. 24
0

# %%
from statsmodels.tsa.holtwinters import SimpleExpSmoothing


# %%
modelo_ajustado = SimpleExpSmoothing(carbonico_treino).fit(smoothing_level=0.5)


# %%
carbonico_teste.shape


# %%
modelo_previsto = modelo_ajustado.forecast(57)


# %%
plt.plot(carbonico_treino)
plt.plot(carbonico_treino.index, modelo_ajustado.fittedvalues.values)
plt.plot(carbonico_teste,'g')
plt.plot(carbonico_teste.index, modelo_previsto, 'r.')


# %%
nasc_pred = nasc_plot.set_index('data')


# %%
nasc_treino = nasc_pred['1959-01-01':'1959-12-01'].astype('double')
Esempio n. 25
0
  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))
  maeses.append(mean_absolute_error(test, predict_ses))

print("rmssernn: " + str(sum(rmssernn)/50))
print("rmssednn: " + str(sum(rmssednn)/50))
print("rmssecroston: " + str(sum(rmssecroston)/50))
print("rmsseses: " + str(sum(rmsseses)/50))
print("maernn: " + str(sum(maernn)/50))
print("maednn: " + str(sum(maednn)/50))
Esempio n. 26
0
    def fit(self, x, horizon):
        fit = SES(x, initialization_method='estimated').fit()
        # print('SES', fit.forecast(horizon))

        return fit.forecast(horizon)
Esempio n. 27
0

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()

plt.plot(EXP_results)
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))
Esempio n. 29
0
warnings.filterwarnings("ignore")
plt.style.use('fivethirtyeight')
import pandas as pd
pd.set_option("display.max_columns", 20)
pd.set_option("display.max_rows", 100)
import seaborn as sns
sns.set()
from old.trailer_forecast_load import subtype_result_month

# Initialize local variable for time series
trailer_series = subtype_result_month['Trailer']
# print(trailer_series)

fit1 = SimpleExpSmoothing(trailer_series).fit(smoothing_level=0.2,
                                              optimized=False)
fcast1 = fit1.forecast(12).rename(r'$\alpha=0.2$')
fit2 = SimpleExpSmoothing(trailer_series).fit(smoothing_level=0.1,
                                              optimized=False)
fcast2 = fit2.forecast(12).rename(r'$\alpha=0.1$')
fit3 = SimpleExpSmoothing(trailer_series).fit()
fcast3 = fit3.forecast(12).rename(r'$\alpha=%s$' %
                                  fit3.model.params['smoothing_level'])

# print(fcast2)
#print(fit2.fittedvalues)

ax = trailer_series.plot(marker='o', color='black', figsize=(18, 8))
fcast1.plot(marker='o', ax=ax, color='blue', legend=True)
fit1.fittedvalues.plot(marker='o', ax=ax, color='blue')
fcast2.plot(marker='o', ax=ax, color='red', legend=True)
min(rmse_dict.values())
#Linear model with additive seasonality fits best as it has least rmse

#Data riven Techniques
from statsmodels.graphics import tsaplots
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
from statsmodels.tsa.holtwinters import Holt
from statsmodels.tsa.holtwinters import ExponentialSmoothing

tsaplots.plot_acf(plastic.Sales, lags=20)
tsaplots.plot_pacf(plastic.Sales, lags=20)
#Holt Winters technique to be used as seasonality is present

#Simple Exponential Smoothing
ses_model1 = SimpleExpSmoothing(train.Sales).fit()
fcast1 = ses_model1.forecast(15)

ses_model2 = SimpleExpSmoothing(train.Sales).fit(smoothing_level=0.8)
fcast2 = ses_model2.forecast(15)

ses_model3 = SimpleExpSmoothing(train.Sales).fit(smoothing_level=0.6)
fcast3 = ses_model3.forecast(15)

ses_model1.fittedvalues.plot()
fcast1.plot(color='blue',
            legend=True,
            label='alpha =%s' % ses_model1.model.params['smoothing_level'])
ses_model2.fittedvalues.plot()
fcast2.plot(color='red', legend=True, label='alpha = 0.8')
ses_model3.fittedvalues.plot()
fcast3.plot(color='yellow', legend=True, label='alpha = 0.6')
Esempio n. 31
0
Model:             SimpleExpSmoothing   SSE                            280.315
Optimized:                       True   AIC                             90.007
Trend:                           None   BIC                             96.255
Seasonal:                        None   AICC                            90.253
Seasonal Periods:                None   Date:                 Fri, 26 Mar 2021
Box-Cox:                        False   Time:                         00:21:27
Box-Cox Coeff.:                  None                                         
==============================================================================
                       coeff                 code              optimized      
------------------------------------------------------------------------------
smoothing_level            0.4379774                alpha                 True
initial_level              25.611519                  l.0                 True
------------------------------------------------------------------------------'''

#forecasting/ predicting next 19 periods
births_pred = births_ses.forecast(steps=19)
print(births_pred)

#Plot actual and forecast
plt.plot(births)
plt.plot(births_pred)
plt.legend(['Actual', 'Forecast - SimpleExpSmoothing'],
           bbox_to_anchor=(1, 1),
           loc=2)
plt.show()

#Model with double exponential smoothing
from statsmodels.tsa.holtwinters import Holt
births_holt = Holt(births).fit()
births_holt.summary()
'''                              Holt Model Results