# 预测分析1：根据一年的历史数据预测后十年的数据趋势

import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.graphics.api import qqplot

#读取数据，进行处理
dta = [10930,10318,10595,10972,7706,6756,9092,10551,9722,10913,11151,8186,6422,6337,11649,11652,10310,12043,7937,6476,9662,9570,9981,9331,9449,6773,6304,9355, 10477,10148,10395,11261,8713,7299,10424,10795,11069,11602,11427,9095,7707,10767,12136,12812,12006,12528,10329,7818,11719,11683,12603,11495,13670,11337,10232, 13261,13230,15535,16837,19598,14823,11622,19391,18177,19994,14723,15694,13248, 9543,12872,13101,15053,12619,13749,10228,9725,14729,12518,14564,15085,14722, 11999,9390,13481,14795,15845,15271,14686,11054,10395]
dta = np.array(dta,dtype=np.float)
dta = pd.Series(dta)

#对数据进行绘图，观测是否是平稳时间序列
dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1927','2016'))
# dta.plot(figsize=(12,8))
# plt.show()

# fig = plt.figure(figsize=(12,8))
# diff1 = dta.diff(1)
# diff1.plot(ax=ax1)
# plt.show()

#选择合适的p,q
diff1 = dta.diff(1)
fig = plt.figure(figsize=(12,8))
fig = sm.graphics.tsa.plot_acf(dta,lags=40,ax=ax1)
fig = sm.graphics.tsa.plot_acf(dta,lags=40,ax=ax2)
# plt.show()

#获取最佳模型
arma_mod70 = sm.tsa.ARMA(dta,(7,0)).fit()
print(arma_mod70.aic,arma_mod70.bic,arma_mod70.hqic)
arma_mod30 = sm.tsa.ARMA(dta,(0,1)).fit()
print(arma_mod30.aic,arma_mod30.bic,arma_mod30.hqic)
arma_mod71 = sm.tsa.ARMA(dta,(7,1)).fit()
print(arma_mod71.aic,arma_mod71.bic,arma_mod71.hqic)
arma_mod80 = sm.tsa.ARMA(dta,(8,0)).fit()
print(arma_mod80.aic,arma_mod80.bic,arma_mod80.hqic)

#模型检验
resid = arma_mod80.resid
fig = plt.figure(figsize=(12,8))
fig = sm.graphics.tsa.plot_acf(dta,lags=40,ax=ax1)
fig = sm.graphics.tsa.plot_acf(dta,lags=40,ax=ax2)
plt.show()

print(sm.stats.durbin_watson(arma_mod80.resid.values))

fig = plt.figure(figsize=(12,8))
fig = qqplot(resid,line='q',ax=ax,fit=True)
plt.show()

r,q,p = sm.tsa.acf(resid.values.squeeze(),qstat=True)
data = np.c_[range(1,41),r[1:],q,p]
table = pd.DataFrame(data,columns=['lag','AC','Q','Prob(>Q)'])
print(table.set_index('lag'))

#模型预测
predict_sunspots = arma_mod80.predict('2016','2026',dynamic=True)
print(predict_sunspots)
fig,ax = plt.subplots(figsize=(12,8))
ax = dta.ix['1927':].plot(ax=ax)
fig = arma_mod80.plot_predict('2016','2026',dynamic=True,ax=ax,plot_insample=False)
plt.show()



09-17 9万+

04-02 7252

01-08 7245

07-20 1835

05-07 4747

07-21 9万+

03-30 2611

01-30 3624

06-08 1万+

01-13 2222

03-12 1445

12-21 144

03-26 7万+

04-25

11-10

11-12

01-02 2169

11-15 4236

04-26 216

09-26 607

02-19 9268

11-08 1万+

01-24 3561

03-07 670

04-06 650

01-27 4447

11-14 2212

03-30 195

09-17 1660

09-04 976

05-15 55

11-05 2159

03-07 2449

01-25 3万+

05-09 848

01-03 6210

01-25 855

11-26 570

04-27 4131

05-09 47

07-25 810

11-19 920

09-05 2137

06-10 5743

02-26 2901

03-19 3万+

07-22 1071

01-30 4410

04-10 2601

05-15 66

03-26 3861

03-11 2万+

04-30 2万+

12-18 1462

03-08 2万+

03-17 2万+

08-03 198

03-20 3万+

01-11 276

05-06 3万+

03-22 4万+

12-27 1万+

06-02 2万+

03-27 2万+

06-25 4982

02-27 7万+

03-16 9万+

12-04 3295

11-30 118

01-04 158

07-22 9317

03-08 1万+

09-11 761

06-08 1万+

10-18 3万+

12-06 7260

04-21 6541

05-14 1万+

#### 用python打开电脑摄像头，并把图像传回qq邮箱【Pyinstaller打包】

©️2019 CSDN 皮肤主题: 鲸 设计师: meimeiellie