Created
January 23, 2017 19:05
-
-
Save kenluck2001/ece4a3549e14b7664d176bac26f97c17 to your computer and use it in GitHub Desktop.
improving pyflux for online prediction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pyflux as pf | |
import pandas as pd | |
class TimeSeriesPrediction: | |
'Common base for time series prediction' | |
iterations=500 | |
updateFlag=False | |
def __init__(self, data, p=1, q=1): | |
''' | |
Constructor | |
''' | |
self.model = pf.EGARCHM(data,p,q) | |
x = self.model.fit('BBVI', record_elbo=True, iterations=self.iterations, map_start=False) | |
self.updatedmodel = None | |
def update(self, data, p=1, q=1): | |
''' | |
update the model | |
''' | |
self.updatedmodel = pf.EGARCHM(data,p,q) | |
self.updatedmodel.latent_variables = self.model.latent_variables | |
x = self.updatedmodel.fit('BBVI', record_elbo=True, iterations=self.iterations, map_start=False) | |
self.updateFlag=True | |
def predict (self, h): | |
''' | |
predict the data | |
''' | |
if self.updateFlag: | |
print self.updateFlag | |
self.updateFlag=False | |
return self.updatedmodel.predict(h) | |
return self.model.predict(h) | |
if __name__ == "__main__": | |
data = pd.read_csv('https://vincentarelbundock.github.io/Rdatasets/csv/datasets/sunspot.year.csv') | |
h = 2 | |
data.index = data['time'].values | |
tsData = TimeSeriesPrediction(data) | |
print tsData.predict (h) | |
tsData.update(data) | |
print tsData.predict (h) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment