RJT1990 / pyflux

Open source time series library for Python
BSD 3-Clause "New" or "Revised" License
2.11k stars 240 forks source link

Manipulating data of the fitted model #23

Closed mina-nik closed 8 years ago

mina-nik commented 8 years ago

I want to fit a GARCH model on the first 70% of my time series and then test it for predicting the remained 30%. At each step I want to predict five steps ahead. Is it possible to shift and replace the model.data with some part of test data and then use this model for predicting future? My question is: is there any parameter that should be changed when I change and shift the model's training data to test the fitted model for the test data?

RJT1990 commented 8 years ago

Hey there. There is predict_is, which does the out of sample prediction -> but only does one step ahead out of sample predictions.

I think the way to achieve what you want is to loop over the length of the data with new GARCH instances defined on different parts of the data, and use predict(h=5) at each stage and take the last element of the outputted dataframe?

In terms of the parameters, ideally you'd want to refit the model with the latest data at each stage to exploit the new information.

If the above is not clear, let me know, and I'd be happy to go into more detail for you.

Thanks. Ross

mina-nik commented 8 years ago

Hi Ross Thanks a lot. My code is as follows:

def garch_model_fitting(signal,target,N):  
 for p in range(1,3):
     for q in range(1,3):
         if(p == 1 and q == 1):
            garch_mod_B = pf.GARCH(data=signal[signal.index[0]:signal.index[N]],p=1,q=1,target=target)
            x = garch_mod_B.fit()
            aic_B = x.aic
         else:
             garch_mod = pf.GARCH(data=signal[signal.index[0]:signal.index[N]],p=p,q=q,target=target)
             x = garch_mod.fit()
             aic = x.aic
             if(aic < aic_B):
                 garch_mod_B = garch_mod
                 aic_B = aic

return garch_mod_B 

 def garch_model_predicting(model,signal,N,nxt = 5):
    print('prediciting is started')
    lastindx = signal.shape[0]
    test_data = signal[signal.index[N]:signal.index[lastindx-1]]
    rows = len(test_data)-(nxt)
    prediced_values =  np.zeros([rows,nxt])
    real_values =  np.zeros([rows,nxt])

    s = N+1
    for i in range(len(test_data)-(nxt)):
         real_values[i] = signal[signal.index[s+i]:signal.index[s+i+nxt-1]]

   predicted = model.predict(nxt).values#model.predict(start,end,dynamic=True)
   prediced_values[0,:] = predicted[0:nxt].reshape([nxt])
   n = model.data.shape[0]
   for i in range(len(test_data)-(nxt)-1):
       for j in range(2,n):
            model.data[j-1] = model.data[j]
       model.data[n-1] = signal[signal.index[n+i]]
       predicted = model.predict(nxt).values
       prediced_values[i+1,:] = predicted[0:nxt].reshape([nxt])

return real_values,prediced_values

Here N is length of the whole data multiplied by 0.7 (I used 70% of first values for training). I used the function _garch_modelfitting for fitting the model. Then in _garch_modelpredicting function I want to test the model for the remained test data (30% left). In the first for loop (in this function), I shift the model.data one to the left and put one of the new test data and predict five steps ahead and then I repeat this untill the end of the test data. Is it right?

RJT1990 commented 8 years ago

Hey Minina - sorry for late response. It looks good to me, I checked at the weekend. The only thing you might want to think about is whether you want to move 5 steps ahead each time (then predict 5 steps ahead?) - but I guess it depends on what you want to achieve?

RJT1990 commented 8 years ago

Now closing this issue - let me know if you have any further problems