Exemplo n.º 1
0
    def test_holt_damp_fit(self):
        # Smoke test for parameter estimation
        fit1 = SimpleExpSmoothing(self.livestock2_livestock).fit()
        mod4 = Holt(self.livestock2_livestock, damped=True)
        fit4 = mod4.fit(damping_slope=0.98)
        mod5 = Holt(self.livestock2_livestock, exponential=True, damped=True)
        fit5 = mod5.fit()
        # We accept the below values as we getting a better SSE than text book
        assert_almost_equal(fit1.params['smoothing_level'], 1.00, 2)
        assert_almost_equal(fit1.params['smoothing_slope'], np.NaN, 2)
        assert_almost_equal(fit1.params['damping_slope'], np.NaN, 2)
        assert_almost_equal(fit1.params['initial_level'], 263.92, 2)
        assert_almost_equal(fit1.params['initial_slope'], np.NaN, 2)
        assert_almost_equal(fit1.sse, 6761.35, 2)  # 6080.26

        assert_almost_equal(fit4.params['smoothing_level'], 0.98, 2)
        assert_almost_equal(fit4.params['smoothing_slope'], 0.00, 2)
        assert_almost_equal(fit4.params['damping_slope'], 0.98, 2)
        assert_almost_equal(fit4.params['initial_level'], 257.36, 2)
        assert_almost_equal(fit4.params['initial_slope'], 6.64, 2)
        assert_almost_equal(fit4.sse, 6036.56, 2)  # 6080.26

        assert_almost_equal(fit5.params['smoothing_level'], 0.97, 2)
        assert_almost_equal(fit5.params['smoothing_slope'], 0.00, 2)
        assert_almost_equal(fit5.params['damping_slope'], 0.98, 2)
        assert_almost_equal(fit5.params['initial_level'], 258.95, 2)
        assert_almost_equal(fit5.params['initial_slope'], 1.04, 2)
        assert_almost_equal(fit5.sse, 6082.00, 2)  # 6100.11
Exemplo n.º 2
0
def holt(train, test, alpha=0.0, beta=0.0, exponential=False, damped=False):

    if (alpha > 0.0 and beta > 0.0):

        if (exponential or damped):
            model = Holt(train, exponential=exponential,
                         damped=damped).fit(smoothing_level=alpha,
                                            smoothing_slope=beta,
                                            optimized=False)
        else:
            model = Holt(train).fit(smoothing_level=alpha,
                                    smoothing_slope=beta,
                                    optimized=False)

        _alpha = '{0:2.1f}'.format(alpha)
        _beta = '{0:2.1f}'.format(beta)

    else:
        if (exponential or damped):
            model = Holt(train, exponential=exponential, damped=damped).fit()
        else:
            model = Holt(train).fit()

        _alpha = model.model.params['smoothing_level']
        _beta = model.model.params['smoothing_slope']

    pred = model.predict(start=test.index[0], end=test.index[-1])

    plt.plot(train.index, train, label='Train')
    plt.plot(test.index, test, label='Test')
    plt.plot(pred.index, pred, label=r'Holt, $\alpha={0}$'.format(_alpha))
    plt.legend(loc='best')
Exemplo n.º 3
0
def holt(alpha=0.2):
    df = pd.read_csv('airline_passengers.csv',
                     index_col='Month',
                     parse_dates=True)
    df['EWMA'] = df['Passengers'].ewm(alpha=alpha, adjust=False).mean()
    df.index.freq = 'MS'

    N_test = 12
    train = df.iloc[:-N_test]
    test = df.iloc[-N_test:]

    train_idx = df.index <= train.index[-1]
    test_idx = df.index > train.index[-1]

    holt = Holt(df['Passengers'])
    res_h = holt.fit()
    df['Holt'] = res_h.fittedvalues
    df[['Passengers', 'Holt']].plot()
    plt.show()

    holt = Holt(train['Passengers'])
    res_h = holt.fit()
    df.loc[train_idx, 'Holt'] = res_h.fittedvalues
    df.loc[test_idx, 'Holt'] = res_h.forecast(N_test)

    df[['Passengers', 'Holt']].plot()
    plt.show()
Exemplo n.º 4
0
 def test_holt(self):
     fit1 = Holt(self.air_ausair).fit(smoothing_level=0.8,
                                      smoothing_slope=0.2,
                                      optimized=False)
     fit2 = Holt(self.air_ausair, exponential=True).fit(smoothing_level=0.8,
                                                        smoothing_slope=0.2,
                                                        optimized=False)
     fit3 = Holt(self.air_ausair, damped=True).fit(smoothing_level=0.8,
                                                   smoothing_slope=0.2)
     assert_almost_equal(fit1.forecast(5),
                         [43.76, 45.59, 47.43, 49.27, 51.10], 2)
     assert_almost_equal(fit1.slope, [
         3.617628, 3.59006512, 3.33438212, 3.23657639, 2.69263502,
         2.46388914, 2.2229097, 1.95959226, 1.47054601, 1.3604894,
         1.28045881, 1.20355193, 1.88267152, 2.09564416, 1.83655482
     ], 4)
     assert_almost_equal(fit1.fittedfcast, [
         21.8601, 22.032368, 25.48461872, 27.54058587, 30.28813356,
         30.26106173, 31.58122149, 32.599234, 33.24223906, 32.26755382,
         33.07776017, 33.95806605, 34.77708354, 40.05535303, 43.21586036,
         43.75696849
     ], 4)
     assert_almost_equal(fit2.forecast(5),
                         [44.60, 47.24, 50.04, 53.01, 56.15], 2)
     assert_almost_equal(fit3.forecast(5),
                         [42.85, 43.81, 44.66, 45.41, 46.06], 2)
Exemplo n.º 5
0
    def model(self,
              column_name,
              df,
              apply_smoothing,
              smoothing_level=None,
              smoothing_slope=None):
        """
        performs predictions using the double exponential smoothing without damping 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 Holt model
        :input smoothing_slope       : int, default=None, b parameter in Holt model
        
        :returns df                  : dataframe, weekly-level, with predictions
        :returns params              : dictionary, default=None, placeholder for saving the best hyperparameters chosen by the model, if not provided as arguments to this method

        """

        m = self.prediction_period

        if apply_smoothing == True:
            fit1 = Holt(df["train"][:-m],
                        damped=False).fit(smoothing_level=smoothing_level,
                                          smoothing_slope=smoothing_slope,
                                          optimized=True)
            params = None
        elif apply_smoothing == False:
            fit1 = Holt(df["train"][:-m], damped=False).fit(optimized=True)
            params = fit1.params

            if params['smoothing_slope'] == 0:
                print('Smoothing Slope is 0')
                fit1 = Holt(df["train"][:-m],
                            damped=True).fit(smoothing_slope=0.1,
                                             optimized=True)
                params = fit1.params
                print('Model is refitted with smoothing slope fixed at 0.1')

            print('====================')
            print(params)
            print('====================')

        df[column_name] = np.nan
        #y_fit = fit1.fittedvalues
        y_fore = fit1.forecast(m)
        #y_fore = fit1.predict(df.shape[0]-m)
        #df[column_name][:-1] = y_fit
        df[column_name][:-m] = df['train'].iloc[:-m]
        df[column_name][-m:] = y_fore
        return df
def doHoltsLinear(train_set, test_set, predict_set):
    print('>Holts Linear')

    try:

        # copy test dataframe dates
        y_hat_avg = pd.DataFrame(index=test_set.index.copy())

        # fit model
        fit1 = Holt(np.asarray(train_set['Sales'])).fit(smoothing_level=0.3,
                                                        smoothing_slope=0.1)

        # predict test dataframe
        y_hat_avg['Sales'] = fit1.forecast(len(test_set))

        # calculate error
        rms = sqrt(mean_squared_error(test_set.Sales, y_hat_avg.Sales))

        # create final predict dataframe
        predict_set['FutureValue'] = fit1.forecast(len(predict_set))

        # plot chart
        #plotChart(train_set, test_set, y_hat_avg, 'Holt_linear', 'Sales')

    except:
        rms = 999999999

    # return dataframes: error and prediction
    return (rms, predict_set)
Exemplo n.º 7
0
def halt(data):
    """ halt(二次指数平滑)预测算法, 用于有明显趋势的数据 """

    data_src=pd.Series(data)
    #fit=Holt(data, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2)
    fit=Holt(data, damped=False).fit(smoothing_level=0.8, smoothing_slope=0.2)
    return fit
Exemplo n.º 8
0
 def Holt(self):
     # model = Holt(self._train_data,damped=True)
     with warnings.catch_warnings():
         warnings.filterwarnings("ignore")
         model = Holt(self._train_data.values)
         model_fit = model.fit()
         output = model_fit.forecast(self._h)
     # print(output)
     return output
Exemplo n.º 9
0
    def __init__(self, data_in, num_prediction_periods):
        self.__history = data_in
        self.__num_prediction_periods = num_prediction_periods

        y = np.array(data_in).reshape(-1, 1)
        y = np.clip(y, 0.00001, None)  # HoltWinters doesn't like zeros

        self.__model = Holt(y, exponential=True, damped=True)
        self.__results = self.__model.fit(optimized=True)
Exemplo n.º 10
0
 def update(self):
     begin = max(0, self.index - self.window)
     data = self.arrivals[begin:self.index]
     # fit model
     model = Holt(data)
     model_fit = model.fit()
     self.model = model_fit
     # make prediction
     self.prediction = model_fit.predict(len(data), len(data))[0]
Exemplo n.º 11
0
 def daily_trend_pred(self, now, delta=pd.Timedelta(3, "W")):
     train_time = pd.date_range(start=now - delta,
                                end=now - pd.Timedelta(1, "H"),
                                freq="H")
     model_series = self.trend[train_time]
     h_model = Holt(model_series, exponential=True, damped_trend=True)
     #Fit
     fitted_h = h_model.fit(optimized=True)
     #Predict tomorrow
     pred = fitted_h.predict(start=now, end=now + pd.Timedelta(23, "H"))
     return pred
Exemplo n.º 12
0
def update_graph(selected_dropdown1, selected_dropdown2):
    df = values["Gold"]

    trace1 = []
    trace1.append(
        go.Scatter(x=values.date,
                   y=df,
                   mode='lines',
                   opacity=0.6,
                   name='Actual values',
                   textposition='bottom center'))

    if selected_dropdown1 != None and selected_dropdown2 != None:
        alpha = float(selected_dropdown1)
        beta = float(selected_dropdown2)

        model = Holt(np.asarray(np.asarray(df)))
        fit = model.fit(smoothing_level=alpha, smoothing_slope=beta)
        result = fit.fittedvalues

        name = 'Exponential Smoothing ' + '{0:.2f}'.format(
            alpha) + ',' + '{0:.2f}'.format(beta)
        trace1.append(
            go.Scatter(x=values.date,
                       y=result,
                       mode='lines',
                       opacity=0.6,
                       name=name,
                       textposition='bottom center'))

    title = ""
    traces = [trace1]
    data = [val for sublist in traces for val in sublist]
    figure = {
        'data':
        data,
        'layout':
        go.Layout(colorway=[
            "#5E0DAC", '#FF4F00', '#375CB1', '#FF7400', '#FFF400', '#FF0056'
        ],
                  height=600,
                  title=title,
                  xaxis={
                      "title": "Date",
                      'rangeslider': {
                          'visible': True
                      },
                  },
                  yaxis={"title": "Price (USD)"},
                  paper_bgcolor='rgba(0,0,0,0)',
                  plot_bgcolor='rgba(0,0,0,0)')
    }

    return figure
Exemplo n.º 13
0
    def hw(self, data, pre_len=7):
        m = Holt(data).fit()
        out = m.predict(pre_len)[-pre_len:].tolist()

        self._get_model({
            'model_name': 'holt_winter',
            'model': m,
            'pred': out,
            'org_data': data,
            'pre_len': pre_len
        })
        return out
    def double_exp_moving_average_forecast(self):

        parameters = [[0.1, 0.3], [0.2, 0.8], [0.6, 0.6]]
        self.create_fig_with_data()
        for p, s in parameters:
            fit1 = Holt(self.data_of_shares.Average).fit(smoothing_level=p,
                                                         smoothing_slope=s)
            y_hat = fit1.fittedvalues
            plt.plot(y_hat[0:self.amount_of_test_data],
                     label=f'Double exp smoothing level={p}, slope={s}')
        plt.legend(loc='best')
        plt.savefig("static/results/double_exp_moving_forecast")
Exemplo n.º 15
0
def forecast(data, train_hours, test_hours):
    # Split data into training and test data
    train = data.iloc[25:train_hours]

    # Create the Holt Model
    model = Holt(np.asarray(train['seasonally differenced']))
    model._index = pd.to_datetime(train.index)

    # Fit the model, and forecast
    fit = model.fit()
    pred = fit.forecast(test_hours)
    data['holt'] = 0
    data['holt'][25:] = list(fit.fittedvalues) + list(pred)
def forecast(data, train_hours, test_hours):
    # Train on the first 6 days to predict the 7th day
    train = data.iloc[25:train_hours]

    # Create the SES Model
    model = Holt(np.asarray(train['seasonally differenced']), damped=True)
    model._index = pd.to_datetime(train.index)

    # Fit the model, and forecast
    fit = model.fit()
    pred = fit.forecast(test_hours)
    data['holtDamped'] = 0
    data['holtDamped'][25:] = list(fit.fittedvalues) + list(pred)
Exemplo n.º 17
0
def holt_package(history_data):
    result = Holt(history_data).fit(smoothing_level=0.93,
                                    smoothing_slope=0.35,
                                    optimized=False)
    T = len(history_data)
    forecast_data = [0] * T
    PE = [0] * T

    forecast_data = result.fittedvalues
    forecast_data_last = result.fcastvalues
    forecast_data = np.append(forecast_data, forecast_data_last)
    for i in range(T):
        PE[i] = abs(forecast_data[i] - history_data[i]) / history_data[i]
    MPE = sum(PE) / T
    print(result.summary)
    return forecast_data_last, forecast_data, MPE
Exemplo n.º 18
0
def trend(tr_dict):
    """
    version of holt calculation, which fit and predict for each new sample

    :param tr_dict:
    :return:
    """
    trends_dict = OrderedDict()

    for k, v in tr_dict.items():
        pred_des = [v[0], v[1]]
        for i in range(2, len(v)):
            des = Holt(v[:i]).fit(optimized=True)
            pred_des.append(des.forecast(2)[0])
        trends_dict[k] = np.array(pred_des)
    return trends_dict
Exemplo n.º 19
0
def Holtmethod(paramsList=['pollution.csv', '0.93','pm', 'humidity', 'date'], specialParams=['0.3','0.1']):

    path = paramsList[0]
    trainRows = float(paramsList[1])
    saveto = 'result.csv'
    df = pd.read_csv(path, usecols=paramsList[2:])
    allRows = df.shape[0]
    smoothing_level = specialParams[0]
    smoothing_slope = specialParams[1]

    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 = Holt(np.asarray(train[paramsList[i]])).fit(smoothing_level=float(smoothing_level), smoothing_slope=float(smoothing_slope))
        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')
Exemplo n.º 20
0
def pop_sim(init_data, num_increments):

    data = init_data

    for key, county in init_data.items():
        population = pd.Series(county)

        # https://www.statsmodels.org/stable/examples/notebooks/generated/exponential_smoothing.html#Holt's-Method
        fit1 = Holt(np.asarray(population)).fit(smoothing_level=0.7,
                                                smoothing_slope=0.3)
        future_pop = fit1.forecast(num_increments)

        last_inc = int(max(data[key].keys()))
        for inc, value in zip(range(num_increments), future_pop):
            # round negative population values to 0
            data[key][str(last_inc + 1 + inc)] = value if value > 0 else 0

    return data
def forecast(data, train_hours, test_hours, in_place=True):
    train = data.iloc[25:train_hours]
    model = Holt(np.asarray(train['seasonally adjusted']), damped=True)
    fit = model.fit()
    pred = fit.forecast(test_hours)

    fcst = pd.concat(
        [pd.Series([0] * 25), data['seasonal indices'][25:].multiply(
                list(fit.fittedvalues) + list(pred)
            )
         ]
    )

    if in_place:
        data['holtDamped adjusted'] = fcst
    else:
        data['holtDamped adjusted'] = fcst
        return fcst
Exemplo n.º 22
0
def pipeline(attrs: dict, results: ResultContainer):
    data = pd.read_csv(attrs["data_path"],
                       header=0,
                       index_col=0,
                       parse_dates=[0])

    cpu_usage_data = data[["cpu_usage"]]

    train_data_percentage = attrs["train_data_percentage"]
    train_data_count = int((len(cpu_usage_data) * train_data_percentage) / 100)

    train_data, test_data = cpu_usage_data[:train_data_count], cpu_usage_data[
        train_data_count:]

    model = Holt(train_data).fit(smoothing_level=attrs["smoothing_level"],
                                 smoothing_slope=attrs["smoothing_slope"],
                                 damping_slope=attrs["damping_slope"],
                                 optimized=attrs["optimized"])

    predictions = model.predict(start=train_data_count,
                                end=len(cpu_usage_data) - 1)

    figure_train = figure(figsize=(10, 6))
    plot(train_data[:200])
    plot(model.fittedvalues[:200])

    figure_predictions = figure(figsize=(10, 6))
    plot(predictions[:200])

    figure_result = figure(figsize=(10, 6))
    plot(test_data[:200])
    plot(predictions[:200])

    mse = mean_squared_error(test_data, predictions)
    rmse = sqrt(mse)

    results.add_model_result("mse", mse)
    results.add_model_result("rmse", rmse)

    results.set_slack_main_figure(image_label="final-result",
                                  figure=figure_result)
    results.add_figure(image_label="train-plot", figure=figure_train)
    results.add_figure(image_label="predictions-plot",
                       figure=figure_predictions)
Exemplo n.º 23
0
def valueForecast(file):
    """
    电费预测
    :param data: 电量数据
    格式为:用户 日期 使用电量值
    :return: 预测电量值
    """
    logging.debug('开始运行')
    data = pd.read_excel(file)
    if data.shape[0] == 0:
        raise ValueError('相关性原始数据不存在')
    data.iloc[:, 0] = data.iloc[:, 0].astype(str)
    users = set(data.iloc[:, 0].values)

    # 用电量预测
    result_pre = pd.DataFrame(columns=[
        'DATA_DATE', 'DATA_DATE1', 'DATA_DATE2', 'DATA_DATE3', 'DATA_DATE4',
        'DATA_DATE5'
    ])
    for user in users:
        subdata = data.loc[data.iloc[:, 0] == user]
        df_index = pd.MultiIndex.from_frame(subdata.iloc[:, 1:2])
        df = pd.DataFrame(np.array(subdata.iloc[:, -1]).reshape(1, -1),
                          columns=df_index)
        df.dropna(axis=1, inplace=True)
        df_values = df.values.flatten()
        model = Holt(
            endog=df_values,
            initialization_method='estimated',
        ).fit()
        pre = model.forecast(steps=5)
        print(f'数据的预测 {pre}')
        res2 = pd.DataFrame(pre).T
        res2.columns = [
            'DATA_DATE1', 'DATA_DATE2', 'DATA_DATE3', 'DATA_DATE4',
            'DATA_DATE5'
        ]
        res2['DATA_DATE'] = datetime.date.today()
        res2['USRE'] = user
        print(f'RES2 {res2}')
        result_pre = result_pre.append(res2, ignore_index=True)
    print(result_pre)
    return result_pre
Exemplo n.º 24
0
def get_trend(tr_dict, return_fitted=False):
    """
    method of trend calculation, which fit holt
    which returns also g_trend (i.e. the distance between y_holt - y_trajectory)

    :param tr_dict:
    :param return_fitted:
    :return:
    """
    trend_dict = OrderedDict()
    res_dict = None
    if return_fitted:
        res_dict = OrderedDict()
    for patient, values in tr_dict.items():
        fitted = Holt(values, damped_trend=True).fit(optimized=True, smoothing_level=0.7)  # type: HoltWintersResults
        trend_dict[patient] = fitted.predict(start=0, end=len(values)-1)
        if return_fitted:
            res_dict[patient] = fitted.summary()

    return trend_dict, res_dict
Exemplo n.º 25
0
 def __init__(self,
              ts,
              Mode,
              Damped=False,
              Trend=None,
              Seasonal=None,
              Seasonal_periods=None):
     self.mode = Mode
     self.ts = ts
     if Mode == 'Simple':
         self.exp_smoothing = SimpleExpSmoothing(ts)
     elif Mode == 'Holt':
         self.exp_smoothing = Holt(ts, damped=Damped)
     elif Mode == 'Holt-Winter':
         self.exp_smoothing = ExponentialSmoothing(
             ts,
             damped=Damped,
             trend=Trend,
             seasonal=Seasonal,
             seasonal_periods=Seasonal_periods)
    def double_exp_moving_average_errors(self):

        parameters = [[0.1, 0.3], [0.2, 0.8], [0.6, 0.6]]
        self.create_fig()
        print("Double Exponential Average Method: ")
        for p, s in parameters:
            fit1 = Holt(self.data_of_shares.Average).fit(smoothing_level=p,
                                                         smoothing_slope=s)
            y_hat = fit1.fittedvalues
            mse = round(
                mean_squared_error(self.test_data.Average,
                                   y_hat[0:self.amount_of_test_data]), 6)
            mae = round(
                mean_absolute_error(self.test_data.Average,
                                    y_hat[0:self.amount_of_test_data]), 6)
            residual = self.test_data.Average - y_hat[0:self.
                                                      amount_of_test_data]
            plt.plot(residual, label=f'Residual for level={p}, slope={s}')
            print(f"MSE for level={p}, slope={s}: {mse}")
            print(f"MAE for level={p}, slope={s}: {mae}")

        plt.legend(loc='best')
        plt.savefig("static/results/double_exp_moving_errors")
Exemplo n.º 27
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
Exemplo n.º 28
0
def exponential_smoothing(ts, label=None):
	"""Exponential smoothing methods assign exponentially 
	decreasing weights for past observations.
	"""
	from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
	locs, labels = plt.xticks()

	model = SimpleExpSmoothing(np.asarray(ts))
	#  auto optimization 
	fit1 = model.fit()
	pred1 = fit1.forecast(2)
	fit2 = model.fit(smoothing_level=.25)
	pred2 = fit2.forecast(2)
	fit3 = model.fit(smoothing_level=.5)
	pred3 = fit3.forecast(2)
	plot_exponential_smoothing(ts, fit1, fit2, fit3, pred1, pred2, pred3, label, "Simple Exponential Smoothing",figures_dir)

	model = ExponentialSmoothing(np.asarray(ts), trend='add')
	#  auto optimization 
	fit1 = model.fit()
	pred1 = fit1.forecast(2)
	fit2 = model.fit(smoothing_level=.9)
	pred2 = fit2.forecast(2)
	fit3 = model.fit(smoothing_level=.5)
	pred3 = fit3.forecast(2)
	plot_exponential_smoothing(ts, fit1, fit2, fit3, pred1, pred2, pred3, label, "Exponential Smoothing",figures_dir)

	model = Holt(np.asarray(ts))
	#  auto optimization 
	fit1 = model.fit(smoothing_level=.3, smoothing_slope=.05)
	pred1 = fit1.forecast(2)
	fit2 = model.fit(optimized=True)
	pred2 = fit2.forecast(2)
	fit3 = model.fit(smoothing_level=.3, smoothing_slope=.2)
	pred3 = fit3.forecast(2)
	plot_exponential_smoothing(ts, fit1, fit2, fit3, pred1, pred2, pred3, label, "Holt Exponential Smoothing",figures_dir)
	return fit3
Exemplo n.º 29
0
    def predict_with_exp(self, dataset, code='PT', y='Total_Cases', days=5):
        tmp = dataset[[y+code]]
        history = [x for x in tmp.values]
        news = []
        data_mod = tmp.reset_index()
        data_mod.columns = ["Date",y+code]
        for i in range(days):
            model = Holt(history[i:])
            model_fit = model.fit(smoothing_level=self.level)
            output = model_fit.forecast()
            yhat = output[0]
            history.append(np.array([round(yhat)]))
            
            xn = datetime.datetime.strptime(data_mod.iloc[-1]['Date'], '%m/%d/%y') \
                + datetime.timedelta(days=i+1)
            news.append(
                pd.Series([xn.strftime("%m/%d/%y"), round(yhat)], index=data_mod.columns)
            )
            
        data_mod = pd.DataFrame(news)
        data_mod.set_index('Date', inplace=True, drop=True)
        data_mod.columns = ["EXPO"+code]

        return data_mod

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


ses_mape_dict = {
    'ses_model1': MAPE(fcast1, test.Sales),
    'ses_model2': MAPE(fcast2, test.Sales),
    'ses_model3': MAPE(fcast3, test.Sales)
}

#Holts Exponential Smoothing
holt_model1 = Holt(train.Sales).fit(smoothing_level=0.8,
                                    smoothing_slope=0.2,
                                    optimized=False)
fcast1 = holt_model1.forecast(15)

holt_model2 = Holt(train.Sales, exponential=True).fit(smoothing_level=0.8,
                                                      smoothing_slope=0.2,
                                                      optimized=False)
fcast2 = holt_model2.forecast(15)

holt_model3 = Holt(train.Sales, damped=True).fit(smoothing_level=0.8,
                                                 smoothing_slope=0.2)
fcast3 = holt_model3.forecast(15)

holt_model1.fittedvalues.plot()
fcast1.plot(color='red', legend=True, label='Linear Trend')
holt_model2.fittedvalues.plot()