0

I am following this tutorial in order to implement forecasting with ARIMA. I am making predictions for the existing dataset (split in training and testing set), and it is working perfectly.

def split_dataframe(df, periodos):

    to_row = int(len(df[:-periodos]))
    training_set = list(df[0:to_row][kafsimo])
    testing_set = list(df[to_row:][kafsimo])  

    return training_set, testing_set 

def differencing_parameter(set_df):
    
    first_diff = adfuller(set_df)
    second_diff = adfuller(set_df)

    if ((first_diff[1] > 0.05) and (second_diff[1] > 0.05)): 
        d = 2
    elif((first_diff[1] > 0.05) and (second_diff[1] <= 0.05)):
        d =1 
    else: 
        d = 0 
        
    return d 

def previous_prediction(df, periodos):

    training_set, testing_set = split_dataframe(df, periodos)
    model_existing_predictions = []

    for i in range(periodos):
        d = differencing_parameter(training_set)    
        model = ARIMA(training_set, order=(1, 2, d))
        model_fit = model.fit()
        output = model_fit.forecast()
        yhat = output[0]
        model_existing_predictions.append(yhat)
        actual_test_value = testing_set[i]
        training_set.append(actual_test_value)

    return model_existing_predictions, training_set  

and I am taking this kind of results:

enter image description here

When I try to forecast though, the results I am getting are a straight line at the plot:

def future_prediction(df, periodos):

    model_future_predictions = []
    set_df = list(df[kafsimo])

    for i in range(periodos):
        d = differencing_parameter(set_df)    
        model = ARIMA(set_df, order=(1, 2, d))
        model_fit = model.fit()
        output = model_fit.forecast()
        yhat = output[0]
        model_future_predictions.append(yhat)
        set_df.append(model_future_predictions[i])

    return model_future_predictions, set_df  

and taking this plot:

enter image description here

Why? Thank you in advance.

mariniou77
  • 27
  • 5

2 Answers2

0

check:

ARIMA model producing a straight line prediction

In short you are using only historic prices to predict n steps into the future. Your model learned that the best prediction for future prices is the price at n-1 + overall trend. The reason for this is because prices especially over shorter time frames move almost at random if your only prediction data is historic prices. Your model can't figure out a more accurate way of prediction.

Niko
  • 144
  • 8
0
d = differencing_parameter(set_df)    
model = ARIMA(set_df, order=(1, 2, d))

actually, you did not it in the right position. the differencing parameter should be in the middle:

 d = differencing_parameter(set_df)    
 model = ARIMA(set_df, order=(1,d,2))