import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.arima_model import ARMA
import warnings
from itertools import product
from datetime import datetime
warnings.filterwarnings('ignore')
df=pd.read_csv("E:/数据学习网站/bitcoin-master/bitcoin_2012-01-01_to_2018-10-31.csv")
df.Timestamp=pd.to_datetime(df.Timestamp)#时间转换
# print(df.Timestamp)
df.index=df.Timestamp#添加标签
df_month=df.resample("M").mean()
df_Q=df.resample("Q-DEC").mean()
df_year=df.resample('A-DEC').mean()
# print(df_month.Weighted_Price)
#设置pq取值
ps=range(0,3)
qs=range(0,3)
parameters=product(ps,qs)#变成2元组合
parameters_list=list(parameters)
results=[]
best_aic=float('inf')#最好的aic无限大
for param in parameters_list:
try:
model=ARMA(df_month.Weighted_Price,order=(param[0],param[1])).fit()
except ValueError:
print("参数错误:",param)
continue
aic=model.aic
if aic<best_aic:
best_model=model
best_aic=aic
best_param=param
results.append([param,model.aic])
print("-"*50)
# print(results)
results_table=pd.DataFrame(results)
results_table.columns=['paramter','aic']
# print(results_table)
# print(best_model.summary())#输出最优模型
# print(df_month[['Weighted_Price']])
df_month2=df_month[['Weighted_Price']]
date_list=[datetime(2018,10,31),datetime(2018,11,30),datetime(2018,12,31),datetime(2019,1,31),datetime(2019,2,28),
datetime(2019,3,31),datetime(2019,4,30),datetime(2019,5,31)]
future=pd.DataFrame(index=date_list,columns=df_month2.columns)
df_month2=pd.concat([df_month2,future])
predict_y=best_model.predict(datetime(2012,1,31),datetime(2019,5,31))
# print(predict_y)
df_month2["forcast"]=predict_y
print(df_month2)
#图片制作
plt.figure(figsize=(20,8))#预测图大小
plt.rcParams['font.sans-serif']=['SimHei']#添加中文
plt.plot(df_month.Weighted_Price,label="实际金额")
plt.plot(predict_y,"--",label="预测线")
plt.legend()
# predict_y.plot(ls="--",label="预测线")
# plt.rcParams['font.sans-serif']=['SimHei']
plt.title("比特币走势图")
plt.xlabel("时间")
plt.ylabel("美金")
plt.show()
预测结果:
Weighted_Price forcast
2011-12-31 4.471603 NaN
2012-01-31 6.208550 109.106043
2012-02-29 5.252527 136.162334
2012-03-31 4.962021 126.230568
2012-04-30 4.976547 128.403161
2012-05-31 5.041348 127.828475
2012-06-30 5.971145 128.059187
2012-07-31 7.795287 129.106723
2012-08-31 10.917099 131.002441
2012-09-30 11.410971 134.219142
2012-10-31 11.596922 133.945968
2012-11-30 11.325439 134.241091
2012-12-31 13.154020 133.838025
2013-01-31 15.321195 136.127982
2013-02-28 25.815375 138.099894
2013-03-31 57.225956 150.092965
2013-04-30 127.824670 184.356852
2013-05-31 117.865537 259.409210
2013-06-30 105.460345 227.402675
2013-07-31 85.431863 221.182298
2013-08-31 103.470034 198.951886
2013-09-30 124.845164 226.435711
2013-10-31 152.767668 244.571766
2013-11-30 528.338932 273.026569
2013-12-31 794.140758 713.533461
2014-01-31 818.988309 912.581956
2014-02-28 663.865791 888.861114
2014-03-31 594.519243 710.127095
2014-04-30 462.273106 675.304432
2014-05-31 485.755688 526.843433
... ... ...
2017-01-31 908.865314 914.317236
2017-02-28 1059.621128 995.638194
2017-03-31 1131.982846 1153.717960
2017-04-30 1208.301280 1197.676216
2017-05-31 1877.870030 1276.954142
2017-06-30 2619.212599 2054.635659
2017-07-31 2494.463304 2730.701335
2017-08-31 3828.175246 2400.580982
2017-09-30 4084.504436 4080.494929
2017-10-31 5284.178124 3935.928479
2017-11-30 7755.883566 5406.155283
2017-12-31 14840.260446 7961.224695
2018-01-31 12893.209157 15729.320363
2018-02-28 9389.578296 11323.278878
2018-03-31 9025.345023 8324.059359
2018-04-30 7983.607544 8693.607945
2018-05-31 8438.807228 7351.509945
2018-06-30 6784.289128 8254.505563
2018-07-31 7097.150293 6038.201407
2018-08-31 6689.298819 7005.750702
2018-09-30 6588.825363 6259.676232
2018-10-31 6415.047708 6339.828917
2018-10-31 NaN 6339.828917
2018-11-30 NaN 6110.984600
2018-12-31 NaN 5809.530918
2019-01-31 NaN 5530.658992
2019-02-28 NaN 5272.677232
2019-03-31 NaN 5034.020766
2019-04-30 NaN 4813.241945
2019-05-31 NaN 4609.001564
[91 rows x 2 columns]
图片: