timeseries.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import print_function
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import pandas as pd
  5. import math
  6. from scipy import stats
  7. from statsmodels.graphics.api import qqplot
  8. #from keras.models import Sequential
  9. #from keras.layers import Dense, Activation, Dropout
  10. #from keras.layers.recurrent import LSTM
  11. import statsmodels.api as sm
  12. def predict(rawdata):
  13. np.random.seed(1234)
  14. i = 0
  15. while rawdata[i] == 0:
  16. i += 1
  17. indexes = []
  18. year = 1995
  19. print(rawdata)
  20. for i in range(len(rawdata)):
  21. indexes[i] = str(year)
  22. usefuldata = rawdata[i:len(rawdata)-1]
  23. print(usefuldata)
  24. dta = pd.Series(usefuldata)
  25. dta = dta.astype(float)
  26. print(dta)
  27. dta.plot(figsize=(12,8))
  28. fig = plt.figure(figsize=(12,8))
  29. ax1 = fig.add_subplot(211)
  30. fig = sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=20, ax=ax1)
  31. ax2 = fig.add_subplot(212)
  32. fig = sm.graphics.tsa.plot_pacf(dta, lags=20, ax=ax2)
  33. arma_mod20 = sm.tsa.ARMA(dta, (2,0)).fit()
  34. print(arma_mod20.params)
  35. arma_mod30 = sm.tsa.ARMA(dta, (3,0)).fit()
  36. print(arma_mod20.aic, arma_mod20.bic, arma_mod20.hqic)
  37. print(arma_mod30.params)
  38. print(arma_mod30.aic, arma_mod30.bic, arma_mod30.hqic)
  39. sm.stats.durbin_watson(arma_mod30.resid.values)
  40. # fig = plt.figure(figsize=(12,8))
  41. # ax = fig.add_subplot(111)
  42. # ax = arma_mod20.resid.plot(ax=ax);
  43. # fig = plt.figure(figsize=(12,8))
  44. # ax = fig.add_subplot(111)
  45. # ax = arma_mod30.resid.plot(ax=ax);
  46. resid = arma_mod30.resid
  47. # fig = plt.figure(figsize=(12,8))
  48. # ax = fig.add_subplot(111)
  49. # fig = qqplot(resid, line='q', ax=ax, fit=True)
  50. print('this is the data indexes ' + str(dta.ix[0]))
  51. fig, ax = plt.subplots(figsize=(12, 8))
  52. ax = dta.ix['0':].plot(ax=ax)
  53. fig = arma_mod30.plot_predict('29', '30', dynamic=True, ax=ax, plot_insample=False)
  54. plt.show()