コード例 #1
0
def sem(obs, p_mean:bool, boxcox:bool, n_forecast:int):
        '''
           Implement a Simple Exponential Smoothin with Grid Search to find the model with the best
           combination of parameters based on an SSE minimization.
           
           Input:
           :param obs: sequential data for forecasting
           :param p_mean: wether or not to apply penalized_mean
           :param boxcox: wether or not to apply boxcox transformation
           :param n_forecast: number of observations to forecast
          
           Output:
           forecast: Forecast for the next n_forecast observations
           aic_min: AIC of model
           bic: BIC of model
           mse: MSE of model
           sse_min: SSE of model
        '''
        assert type(obs) == pd.core.series.Series, "Data must be of pandas Series type"
        
        if p_mean == True:
            y_prc = pp_transforms().penalized_mean(obs)
        else:
            y_prc = obs

        untransform = False
        if boxcox == True:
            y_prc, lmbd = pp_transforms().boxcox(y_prc)
            untransform = True   

        # Perform a Grid Search to find the best model
        sse_min, sl = 1000000000, 0
        print(obs)
        print(y_prc)
        try:
            for i in [.95,.9,.85,.8,.75,.7,.65,.6,.55,.5,.45,.4,.35,.3,.25,.2,.15,.1,.05]:
                mdl = SimpleExpSmoothing(y_prc).fit(smoothing_level = i, optimized = False)
                sse = np.sum((y_prc.astype(float) - mdl.fittedvalues)**2)
                if sse <= sse_min:
                    sse_min, sl = sse, i
        except Exception as e:
            print("Error while fitting the model","\n",e)
        
        # Best model
        mdl = SimpleExpSmoothing(y_prc).fit(smoothing_level = sl,optimized=False)
        aic, bic = mdl.aic, mdl.bic

        # Untransform Box-Cox Data
        if untransform:
            pred = pp_transforms().boxcox_untransform(mdl.fittedvalues, lmbd)
            forecast = pp_transforms().boxcox_untransform(mdl.forecast(n_forecast), lmbd)
            mse = ((obs.astype(float).values - pred)**2).mean()
        else:
            pred = mdl.fittedvalues
            forecast = mdl.forecast(n_forecast)
            mse = ((obs.astype(float).values - pred)**2).mean()

        return (forecast, aic, bic, mse, sse_min)
コード例 #2
0
ファイル: process.py プロジェクト: 909041282/MingJie_paper
def simpleExpSmoothing(x, y, save_fn):
    pred = []
    for data in x:
        fit = SimpleExpSmoothing(data).fit(smoothing_level=0.6,
                                           optimized=False)
        pred.append(fit.forecast(1))
    save_fn('rate_simpleExpSmoothing.txt', np.array(pred), y)
コード例 #3
0
def forecast_ses(og_df):
    if len(og_df) <= 1:
        result = [0, 0]
    else:
        df = og_df.copy()
        train = aggregate_by_day(df)
        test = train.copy()
        #print('train df before create split')
        #print(train)
        test = test.reindex(create_split(train))
        y_hat_avg = test.copy()
        fit2 = SimpleExpSmoothing(np.asarray(train['Count'])).fit(
            smoothing_level=0.6, optimized=False)
        y_hat_avg['SES'] = fit2.forecast(len(test))
        plt.figure(figsize=(16, 8))
        plt.plot(train['Count'], label='Train')
        plt.plot(y_hat_avg['SES'], label='SES')
        plt.legend(loc='best')
        # plt.show()
        # print(y_hat_avg['SES'].iloc[0])
        # get max y value and index (x)
        date_projected = str(y_hat_avg['SES'].idxmax())
        qty_projected = str(y_hat_avg.loc[y_hat_avg['SES'].idxmax(), 'SES'])
        result = [date_projected, qty_projected]
    return result
コード例 #4
0
def ses1(input_df, kunag, matnr, n, l, alpha):
    index = str(kunag) + "-" + str(matnr)
    dfw = n_series(df, kunag, matnr)
    test1 = test(df, kunag, matnr)
    mae = []
    for i in range(0, l):
        k = n - i
        lst = []
        train1, cv1 = train_cv_split(df, kunag, matnr, k, l)
        y_hat_avg = cv1.copy()
        for j in range(k, l, -1):
            train, cv = train_cv_split(df, kunag, matnr, j, l)
            dd = np.asarray(train["quantity"])
            fit2 = SimpleExpSmoothing(np.asarray(train['quantity'])).fit(
                smoothing_level=alpha, optimized=False)
            y_hat_avg['SES'] = fit2.forecast(len(cv1))
            pred = y_hat_avg['SES']
            lst.append(pred.iloc[-1])
        pd.DataFrame(lst)
        y_hat_avg['pred_column'] = lst
        rms = sqrt(mean_squared_error(cv1.quantity, y_hat_avg.pred_column))
        mae1 = mean_absolute_error(cv1.quantity, y_hat_avg.pred_column)
        mae.append(mae1)
        del y_hat_avg['SES']
        l = l - 1
    return mae
コード例 #5
0
def ses(input_df, kunag, matnr, n, alpha):
    i = 0
    lst = []
    test1 = train_test_split(df, kunag, matnr, n)[1]
    y_hat_avg = test1.copy()
    for i in range(n, 0, -1):
        train, test = train_test_split(df, kunag, matnr, i)
        dd = np.asarray(train["quantity"])
        fit2 = SimpleExpSmoothing(np.asarray(train['quantity'])).fit(
            smoothing_level=alpha, optimized=False)
        y_hat_avg['SES'] = fit2.forecast(len(test1))
        pred = y_hat_avg['SES']
        lst.append(pred.iloc[-1])

    pd.DataFrame(lst)
    y_hat_avg['pred_column'] = lst
    plt.figure(figsize=(12, 8))
    plt.plot(train.set_index("date")['quantity'], label='Train', marker='.')
    plt.plot(test1.set_index("date")['quantity'], label='Test', marker='.')
    plt.plot(y_hat_avg.set_index("date")['pred_column'],
             label='SES',
             marker='.')
    plt.legend(loc='best')
    plt.title("SES")
    plt.show()
    rms = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_column))
    mae = mean_absolute_error(test1.quantity, y_hat_avg.pred_column)
    del y_hat_avg['SES']
    return y_hat_avg, rms, mae
コード例 #6
0
def simple_exponential_smoothing_statsmodels():
    N, t, alpha, x0 = 200, 160, 0.5, 20
    realisations = pd.Series(sample_gaussian_process(20, 5, N), range(N))
    mod = SimpleExpSmoothing(realisations[:t+1]).fit(smoothing_level=alpha, initial_level=x0, optimized=False)
    forecasts = mod.forecast(N-(t+1)).rename(r'$\alpha=0.5$')
    plot(realisations, pd.Series(np.nan, range(t+1)).append(forecasts), alpha)
    py.show()
コード例 #7
0
def SES_find_smoothng_level(df):
    train = df[0:20]
    test = df[20:]
    num = 0
    rms = 0.0

    for id_idx in range(1, 25, 1):
        if train['id' + str(id_idx)].sum() > 25:
            fit = SimpleExpSmoothing(train['id' + str(id_idx)]).fit(
                smoothing_level=0.1, optimized=False)
            fcast = fit.forecast(len(test))

            plt.plot(train['id' + str(id_idx)],
                     marker='o',
                     label='train_id' + str(id_idx))
            plt.plot(test['id' + str(id_idx)],
                     marker='o',
                     label='test_id' + str(id_idx))
            plt.plot(fcast, marker='o', label='SES' + str(id_idx))
            plt.legend(loc='best')

            rms = rms + math.sqrt(
                mean_squared_error(test['id' + str(id_idx)], fcast))
            num = num + 1

    plt.show()
    mean_error = rms / num
    #print(mean_error)
    return 0.1
コード例 #8
0
ファイル: Model_ses.py プロジェクト: ajgithub5/demand-
    def model(self, column_name, df, apply_smoothing, smoothing_level=None):
        """
        performs predictions using the simple exponential smoothing model approach
        
        :input column_name           : str, name of column to hold the predicted values
        :input df                    : dataframe, weekly-level data
        :input apply_smoothing       : bool, indicates whether to factor-in smoothing parameters in the Holt model
        :input smoothing_level       : int, default=None, l parameter in Simple Exponential Smoothing model
        
        
        :returns df                  : dataframe, weekly-level, with predictions
        """

        m = self.prediction_period

        if apply_smoothing == True:
            fit1 = SimpleExpSmoothing(df["train"][:-m]).fit(
                smoothing_level=smoothing_level, optimized=True)
            params = None
        elif apply_smoothing == False:
            fit1 = SimpleExpSmoothing(df["train"][:-m]).fit(optimized=True)
            params = fit1.params
        y_fit = fit1.fittedvalues
        y_fore = fit1.forecast(m)
        df[column_name] = np.nan
        #df[column_name][:-1] = y_fit
        df[column_name][:-m] = df['train'].iloc[:-m]
        df[column_name][-m:] = y_fore

        #df[column_name].iloc[-1:] = list(y_pred)[-1]
        return df
コード例 #9
0
def gen_plot_forecast():
    es_conn = fetchData.elasticSearch(
        url="https://*****:*****@kf6-stage.ikit.org/es/_search")
    df = es_conn.get_nginx_reliability(interval='1h')
    df = df.sort_values('buckets', ascending=True)
    data_in_window = df.tail(1000)
    rel_data = data_in_window["reliability"].to_numpy()
    date_data = data_in_window["buckets"].to_numpy()
    last_bucket = parse(data_in_window["buckets"].iloc[-1])
    if np.isnan(np.sum(rel_data)):
        print("NaN in data")
        exit(1)
    simple_exp_model = SimpleExpSmoothing(rel_data).fit(smoothing_level=.5)
    predicted_data = np.average(simple_exp_model.forecast(5))
    fitted_values = simple_exp_model.fittedvalues
    for idx, val in enumerate(fitted_values):
        if val < 0.5:
            fitted_values[idx] = 0.5
    fig = go.Figure()
    fig.add_trace(
        go.Scatter(x=date_data,
                   y=rel_data,
                   mode='lines',
                   name="Observed Reliability"))
    fig.add_trace(
        go.Scatter(x=date_data,
                   y=fitted_values,
                   mode='lines',
                   name="Predicted Reliability"))
    print(datetime.datetime.now())
    sys.stdout.flush()
    tm = datetime.datetime.now()
    return fig, predicted_data, last_bucket, tm
コード例 #10
0
def simple_exponential_smoothing(input_df, kunag, matnr, alpha=0.6):

    df = input_df.copy()
    df = remove_negative_rows(df)
    df_series = individual_series(df, kunag, matnr)
    df_series = data_transformation.get_weekly_aggregate(df_series)
    df_series["date"] = df_series["dt_week"].map(str)
    df_series["date"] = df_series["date"].apply(lambda x: x.replace("-", ""))
    df_series["prediction"] = df_series["quantity"]
    df_series_train, df_series_test = splitter(df_series)
    k = 0
    for index, row in df_series_test.iterrows():
        fit2 = SimpleExpSmoothing(np.asarray(df_series_train["quantity"])).fit(
            smoothing_level=alpha, optimized=False)
        row["prediction"] = fit2.forecast(1)
        df_series_train = pd.concat([df_series_train,
                                     pd.DataFrame(row).T
                                     ]).reset_index(drop=True)
        if k == 0:
            test_index = df_series_train.shape[0] - 1
            k = 1
    output_df = df_series_train
    test_df = df_series_train.iloc[test_index:]
    # print("mean squared error is :",mean_squared_error(output_df["quantity"], output_df["prediction"]))
    return output_df, mean_squared_error(test_df["quantity"],
                                         test_df["prediction"])
コード例 #11
0
def get_gravity_from_acc(acc):
    """
    Get gravity from acc data by using low pass filter

    Parameters
    ----------
    acc : numpy array
        [time, x, y, z]

    Returns
    --------
    gravity : 1-D array
        The gravity component in 3 axis
    """
    if debug:
        print('get gravity component...')
    gravity_component = [0.0] * 3

    # use low pass filter (exponential)
    # https://developer.android.com/guide/topics/sensors/sensors_motion
    # https://medium.com/datadriveninvestor/how-to-build-exponential-smoothing-models-using-python-simple-exponential-smoothing-holt-and-da371189e1a1
    for i in range(3):
        # TODO: we don't need to use all rows to get the component
        top_rows = min(len(acc), 1000)
        acc_x = acc[:top_rows, i + 1]
        fit_x = SimpleExpSmoothing(acc_x).fit()
        fcast_x = fit_x.forecast(1)
        gravity_component[i] = fcast_x[0]

    if debug:
        print_floats(*gravity_component, description="Gravity component:")

    return gravity_component
コード例 #12
0
def simple_smoothing(data,company):
    fit1 = SimpleExpSmoothing(data).fit(smoothing_level=0.1, optimized=False)
    fcast1 = fit1.forecast(1).rename(r'$\alpha=0.1$')
    print(fit1['fittedvalues'])
    fcast1.plot(marker='o', color='blue', legend=True)
    fit1.fittedvalues.plot(marker='o', color='blue')
    pyplot.title("Trade War Impact - "+company)
    pyplot.show()
コード例 #13
0
 def simpleExponentialSmoothing(self, train, test, trend):
     
     if trend == 'no trend':
         sm = SimpleExpSmoothing(train).fit()
         sm_pred = sm.forecast(len(test))
         rmse_sm = rootMeanSquaredError(test, sm_pred)
         if rmse_sm < self.rmse:
             self.rmse = rmse_sm
             self.__model__ = 'simpleExponentialSmoothing'
コード例 #14
0
def plot_all_graphs(data,company):
    i=0
    for df in data:
        fit1 = SimpleExpSmoothing(df['sentiment']).fit(smoothing_level=1, optimized=False)
        fcast1 = fit1.forecast(1).rename(r'$\alpha=0.1$')
        fcast1.plot(marker='o', color='blue', legend=True)
        fit1.fittedvalues.plot(marker='o', color='blue')
        pyplot.title("Trade War Impact - " + company[i])
        i=i+1
        pyplot.show()
コード例 #15
0
        def fit_model(self, n_predict):

                fit = SimpleExpSmoothing(self.train).fit()
                forecast = fit.forecast(n_predict)

                ds = self.ds_test
                
                self.forecast = pd.DataFrame({"ds": ds, "yhat": forecast})

                return self.forecast
コード例 #16
0
def sess(i, fc_periods, df):
    df = df
    saledata = np.asarray(df.iloc[0:, 0])
    fit1 = SimpleExpSmoothing(saledata).fit(smoothing_level=0.2,
                                            optimized=True)
    suav = fit1.fittedvalues
    df['pronostico'] = suav
    nombre = list(df.columns.values.tolist())
    fcast1 = fit1.forecast(fc_periods)
    return (fcast1)
コード例 #17
0
def SES(paramsList=['pollution.csv', '0.93','pm', 'humidity', 'date'],specialParams=['0.6']):

    '''
    1.时间序列比较平稳时,选择较小的α值,0.05-0.20。

            2.时间序列有波动,但长期趋势没大的变化,可选稍大的α值,0.10-0.40。

            3.时间序列波动很大,长期趋势变化大有明显的上升或下降趋势时,宜选较大的α值,0.60-0.80。

            4.当时间序列是上升或下降序列,满足加性模型,α取较大值,0.60-1。
    '''
    path = paramsList[0]
    trainRows = float(paramsList[1])
    saveto = 'result.csv'
    df = pd.read_csv(path, usecols=paramsList[2:])
    allRows = df.shape[0]
    es = specialParams[0]

    train = df[0:int(allRows*trainRows)]
    test = df[int(allRows*trainRows)+1:]

    df['Timestamp'] = pd.to_datetime(df[paramsList[-1]], format='%Y/%m/%d %H:%M')
    df.index = df['Timestamp']
    df = df.resample('D').mean()

    train['Timestamp'] = pd.to_datetime(train[paramsList[-1]], format='%Y/%m/%d %H:%M')
    train.index = train['Timestamp']
    train = train.resample('D').mean()

    test['Timestamp'] = pd.to_datetime(test[paramsList[-1]], format='%Y/%m/%d %H:%M')
    test.index = test['Timestamp']
    test = test.resample('D').mean()

    y_hat = test.copy()
    nullArray = train.copy()
    nullArray['time'] = train.index
    # 以上可通用----------------------------

    for i in range(2,len(paramsList)-1):
        fit = SimpleExpSmoothing(np.asarray(train[paramsList[i]])).fit(smoothing_level=float(es), optimized=False)
        y_hat[paramsList[i]] = fit.forecast(len(test))
        y_hat[paramsList[i]] = round(y_hat[paramsList[i]],2)

        rms = sqrt(mean_squared_error(test[paramsList[i]], y_hat[paramsList[i]]))
        print(rms)

    y_hat['time'] = test.index
    yhat_naive = np.array(y_hat)
    nArray = np.array(nullArray)
    newArray = np.concatenate((nArray,yhat_naive),axis=0)
    s = pd.DataFrame(newArray, columns=paramsList[2:])
    for i in range(2,len(paramsList)-1):
        s[paramsList[i]][0:int(len(s)*trainRows)] = ""
    s.to_csv(saveto,index=False,header=True,float_format='%.2f')
コード例 #18
0
def ewma(data, col, train, test, frequency):
    y_hat_avg = test.copy()
    fit2 = SimpleExpSmoothing(np.asarray(train[col])).fit(smoothing_level=0.6,optimized=False)
    y_hat_avg['SES'] = fit2.forecast(len(test))
    print('Rmse= ', rmse(test[col], y_hat_avg['SES']))
    plt.figure(figsize=(16,8))
    plt.plot(train[col], label='Train')
    plt.plot(test[col], label='Test')
    plt.plot(y_hat_avg['SES'], label='Exponential Smoothing')
    plt.legend(loc='best')
    plt.savefig(frequency+'ses.png')
コード例 #19
0
 def seasonal_prediction(self):
     from statsmodels.tsa.api import SimpleExpSmoothing
     y_hat_avg = self.test_y.copy()
     fit2 = SimpleExpSmoothing(np.asarray(self.train_y['Count'])).fit(smoothing_level=0.6, optimized=False)
     y_hat_avg['SES'] = fit2.forecast(len(self.test_y))
     plt.figure(figsize=(16, 8))
     plt.plot(self.train_y['Count'], label='Train')
     plt.plot(self.test_y['Count'], label='Test')
     plt.plot(y_hat_avg['SES'], label='SES')
     plt.legend(loc='best')
     plt.show()
コード例 #20
0
def sesm(i):
    df = i
    train = np.asarray(df.iloc[:(round(len(df) * .85)), 0])
    hell = df.iloc[(round(len(df) * .85)):, 0]
    fit1 = SimpleExpSmoothing(train).fit(smoothing_level=0.2, optimized=True)
    suav = fit1.fittedvalues
    fcast1 = fit1.forecast(len(hell))
    sreal = (sum(hell))
    spred = (sum(fcast1))
    mape = calculomape(sreal, spred)
    return (mape)
コード例 #21
0
def fit_SES(params, df):
    data = df.carbon_monoxide[:-1]
    if 'a' in params:
        a = params['a']
        fit = SimpleExpSmoothing(
            data,
            initialization_method="estimated").fit(smoothing_level=float(a))
    else:
        fit = SimpleExpSmoothing(data, initialization_method="estimated").fit()
    fcast = fit.forecast(1)

    return fcast[0], fit.model.params['smoothing_level']
コード例 #22
0
def ses(y, y_to_train,y_to_test,smoothing_level,predict_date):
    y.plot(marker='o', color='black', legend=True, figsize=(14, 7))
    
    fit1 = SimpleExpSmoothing(y_to_train).fit(smoothing_level=smoothing_level,optimized=False)
    fcast1 = fit1.forecast(predict_date).rename(r'$\alpha={}$'.format(smoothing_level))
    # specific smoothing level
    fcast1.plot(marker='.', color='blue', legend=True)
    fit1.fittedvalues.plot(marker='.',  color='blue')
    mse1 = ((fcast1 - y_to_test) ** 2).mean()
    print('The Root Mean Squared Error of our forecasts with smoothing level of {} is {}'.format(smoothing_level,round(np.sqrt(mse1), 2)))
    
    ## auto optimization
    fit2 = SimpleExpSmoothing(y_to_train).fit()
    fcast2 = fit2.forecast(predict_date).rename(r'$\alpha=%s$'%fit2.model.params['smoothing_level'])
    # plot
    fcast2.plot(marker='.', color='green', legend=True)
    fit2.fittedvalues.plot(marker='.', color='green')
    
    mse2 = ((fcast2 - y_to_test) ** 2).mean()
    print('The Root Mean Squared Error of our forecasts with auto optimization is {}'.format(round(np.sqrt(mse2), 2)))
    
    plt.show()
コード例 #23
0
def SES():
    #Simple Exponential Smoothing
    fit1 = SimpleExpSmoothing(df.sales_result).fit(smoothing_level=0.2,optimized=False)
    fcast1 = fit1.forecast(12).rename(r'$\alpha=0.2$')
    # plot
    fcast1.plot(marker='o', color='blue', legend=True)
    fit1.fittedvalues.plot(marker='o',  color='blue')

    fit2 = SimpleExpSmoothing(df.sales_result).fit(smoothing_level=0.6,optimized=False)
    fcast2 = fit2.forecast(12).rename(r'$\alpha=0.6$')
    # plot
    fcast2.plot(marker='o', color='red', legend=True)
    fit2.fittedvalues.plot(marker='o', color='red')


    fit3 = SimpleExpSmoothing(df.sales_result).fit()
    fcast3 = fit3.forecast(12).rename(r'$\alpha=%s$'%fit3.model.params['smoothing_level'])
    # plot
    fcast3.plot(marker='o', color='green', legend=True)
    fit3.fittedvalues.plot(marker='o', color='green')

    plt.show()
コード例 #24
0
def run_expo_forecast_week(df, min_date_dict, last_period_available, period,
                           alpha):
    # Function that creates a naive Forecast for all GEUs

    # List of GEUs
    geus = df['geu'].unique()

    # We check which period we are going to forecast
    if period == 'month_str':
        last_period_year = '12'
    elif period == 'semester':
        last_period_year = '02'
    else:
        last_period_year = '04'

    # If the last available period is the last period of an existing year, we change the year
    last_period_string = str(last_period_available)
    if last_period_string[-2:] == last_period_year:
        fcst_yr = last_period_string[:4] + "01"
        forecasted_period = int(fcst_yr)
        forecasted_period += 100
    else:
        forecasted_period = last_period_available + 1

    # Create output sku
    df_expo_week = pd.DataFrame()

    for geu in geus:
        # Filter result
        df_geu = df[(df['geu'] == geu)
                    & (df['period'] <= last_period_available)].reset_index(
                        drop=True)

        # If this GEU had a previous sale and there is more than two weeks of data, we add it to the output dataframe
        if last_period_available > min_date_dict[geu] and len(df_geu) >= 2:
            # Create and fit model
            model = SimpleExpSmoothing(df_geu['demand'].values).fit(
                optimized=False, smoothing_level=alpha)

            # Create output df
            df_forecast_geu = pd.DataFrame({
                'geu': [geu],
                'period': [forecasted_period],
                f'expo_{alpha}_forecast':
                list(model.forecast(1))
            })
            df_expo_week = pd.concat([df_expo_week, df_forecast_geu],
                                     ignore_index=True)

    return df_expo_week
コード例 #25
0
    def simple_exponential_smoothing(self, values, law=6):
        """
		Simple Exponential Smoothing function implementation
		Arguments:
			values: Tuple of X: Independent variable and
							 Y: Dependent variable 
		Returns:
			Predictions for law values in the future. 
			Default: Predict 6 points in the future.
		"""
        Y_Train = [x[1] for x in values]
        model = SimpleExpSmoothing(Y_Train).fit()
        predictions = model.forecast(law)
        return predictions
コード例 #26
0
def simple_expo_smoothing(train, test, value):
    # Simple Exponential Smoothing
    y_hat_avg = test.copy()
    alphas = np.linspace(0, 1, 101)
    #try to find the lowest error with all possible alphas with two decimal
    error = 100000000000
    for alpha in alphas:
        fit2 = SimpleExpSmoothing(np.asarray(train[value])).fit(
            smoothing_level=alpha, optimized=False)
        y_hat_avg['SES'] = fit2.forecast(len(test))
        mape = mean_abs_percentage_error(test[value], y_hat_avg.SES)
        if mape < error:
            error = mape
            optimal_alpha = alpha
    return error, optimal_alpha
コード例 #27
0
def simpleexponeential(train,test):
     #Smoothing_level is alpha value   
    fitdata= SimpleExpSmoothing(train).fit(smoothing_level=0.5,optimized=False)
    fcast=fitdata.forecast(len(test))
    plt.figure(figsize=(18,8))
    plt.plot(train,label='train data',color='black')
    plt.plot(test,label='test data',color='green')
    plt.plot(fcast,label='forecast',color='red')
    plt.legend(loc='best')
    plt.title('Load Forecast using Simple Exponential Method',fontsize=15)
    plt.xlabel('day----->')
    plt.ylabel('Consumption in Mwh')
    plt.show()
    
    print("Verification of Simple Exponential Forecasting Model")
    modelverification(fitdata,fcast,test)
    return(fitdata)
コード例 #28
0
def expo_forecast(mat_object, period, first_date, warehouse, soft=False):
    """
    Linear forecast for material demand. 'soft' parameter is used for deleting outliers.
    :param mat_object:
    :param period:
    :param first_date:
    :param warehouse:
    :param soft:
    :return:
    """
    ALPHA = 0.7

    periods = list_of_periods(period, first_date)
    dates = [p.first_date for p in periods]
    values = [mat_object.get_demand(p, warehouse) for p in periods]

    dates.reverse()
    values.reverse()

    if len(values) == 1:
        return values[0]

    if soft:
        non_outlier_list = deleting_outliers(values)
        filtered_dates = []
        filtered_values = []
        for i in range(len(values)):
            if values[i] in non_outlier_list:
                filtered_dates.append(dates[i])
                filtered_values.append(values[i])
        x_df = pd.to_datetime(filtered_dates)
        y = np.array(filtered_values)
    else:
        x_df = pd.to_datetime(dates)
        y = np.array(values)
    x_df = x_df.map(dt.datetime.toordinal)
    x = np.array(x_df).reshape((-1, 1))

    try:
        model = SimpleExpSmoothing(y).fit(optimized=False, smoothing_level=ALPHA)
    except:
        print(x, mat_object, period, first_date, warehouse)
        return

    return model.forecast(1)[0]
コード例 #29
0
def simple_exponential_smoothing(train, test, column, se):
#simple exponential smoothing technique where se is the smoothing level    
    y_hat_avg = test.copy()
    rmse_se = float("inf")
    start = time.time()
    for p in ranger(0,se,0.01):
        model_ses = SimpleExpSmoothing(np.asarray(train[column])).fit(smoothing_level=0.1,optimized=False)
        y_hat_avg['simple_exponential_smoothing']= model_ses.forecast(len(test))
        y_hat_avg['transformed_values'] = inverse_transform(y_hat_avg,'simple_exponential_smoothing')
        rms = sqrt(mean_squared_error(y_hat_avg[column], y_hat_avg['transformed_values']))
        if rms < rmse_se:
            rmse_se = rms
    end = time.time()            
    time_se = end - start
    print('\n Total RMSE of the simple exponential smoothing model : %.3f ' % rmse_se) 
    result.loc[len(result)] = [column, 'Simple exponential smooting', rmse_se, time_se] 
    #test_plt((20,10), test, predictions, column, 'Simple exponential smoothing')
    result_plots['simple_exponential_smoothing'] = y_hat_avg['transformed_values']  
コード例 #30
0
def ses(input_df, kunag, matnr, n, sl):
    i = 0
    lst = []
    test1 = train_test_split(df, kunag, matnr, n)[1]
    y_hat_avg = test1.copy()
    for i in range(n, 0, -1):
        train, test = train_test_split(df, kunag, matnr, i)
        dd = np.asarray(train["quantity"])
        fit2 = SimpleExpSmoothing(np.asarray(train['quantity'])).fit(
            smoothing_level=sl, optimized=False)
        y_hat_avg['SES'] = fit2.forecast(len(test1))
        pred = y_hat_avg['SES']
        lst.append(pred.iloc[-1])

    pd.DataFrame(lst)
    y_hat_avg['pred_column'] = lst
    rms = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_column))
    mae = mean_absolute_error(test1.quantity, y_hat_avg.pred_column)
    del y_hat_avg['SES']
    return y_hat_avg, rms, mae
コード例 #31
0
ax = oildata.plot()
ax.set_xlabel("Year")
ax.set_ylabel("Oil (millions of tonnes)")
plt.show()
print("Figure 7.1: Oil production in Saudi Arabia from 1996 to 2007.")

# Here we run three variants of simple exponential smoothing:
# 1. In ```fit1``` we do not use the auto optimization but instead choose
# to explicitly provide the model with the $\alpha=0.2$ parameter
# 2. In ```fit2``` as above we choose an $\alpha=0.6$
# 3. In ```fit3``` we allow statsmodels to automatically find an optimized
# $\alpha$ value for us. This is the recommended approach.

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

ax = oildata.plot(marker='o', color='black', figsize=(12, 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)

fit2.fittedvalues.plot(marker='o', ax=ax, color='red')
fcast3.plot(marker='o', ax=ax, color='green', legend=True)
fit3.fittedvalues.plot(marker='o', ax=ax, color='green')
plt.show()