-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARIMA_Python_APPLE_Prediction.py
97 lines (71 loc) · 2.43 KB
/
ARIMA_Python_APPLE_Prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 29 12:34:05 2020
@author: Robert_Hennings
"""
#Als Erweiterung der Moving Averages folgt nun ein ARIMA Modell
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from pandas import read_csv
from pandas import datetime
from pandas.plotting import autocorrelation_plot
df = pd.read_csv('/Users/Robert_Hennings/Dokumente/IT Weiterbildung/Python Data Analysis /AbbeV/Meine Projekte/AAPL.csv')
del df['Open']
del df['High']
del df['Low']
del df['Close']
del df['Volume']
print(df)
def parser(x):
return df['Date']
series = df['Adj Close']
series.plot()
plt.show()
#Die Saleszahlen 36 an der Zahl sind nun nochmal im eigenen Dataframe mit den angegebene Datetimes welche auf den Start 1900 normiert wurden
#Wie im Plot zu sehen ist, gibt es einen deutlichen Aufwärtstrend zu beobachten
#Dies zeigt aber auch dass der Datensatz nicht stationär ist , er muss differenziert werden um ihn stationär zu machen mindestents mit einer Differenzierungsorder von 1
#Außerdem kann der Datensatz auf eine Autocorrelation hin beobachtet werden
autocorrelation_plot(series)
plt.show()
#Zu sehen ist eine positive Korrelation bei den ersten 10-12 Lags ab dann ca 0 bzw. eine negative, die positive Korrelation ist vermutlich für die ersten 5 Lags tatsächlich signifikant
#Die ersten 5 Lags bieten also daher einen guten Startpunkt für das ARIMA Modell
from pandas import datetime
from pandas import DataFrame
from statsmodels.tsa.arima_model import ARIMA
from matplotlib import pyplot
# fit model
model = ARIMA(series, order=(5,1,0))
model_fit = model.fit(disp=0)
print(model_fit.summary())
# plot residual errors
residuals = DataFrame(model_fit.resid)
residuals.plot()
pyplot.show()
residuals.plot(kind='kde')
pyplot.show()
print(residuals.describe())
from sklearn.metrics import mean_squared_error
X = series.values
size = int(len(X) * 0.66)
train, test = X[0:size], X[size:len(X)]
history = [x for x in train]
predictions = list()
for t in range(len(test)):
model = ARIMA(history, order=(5,1,0))
model_fit = model.fit(disp=0)
output = model_fit.forecast()
yhat = output[0]
predictions.append(yhat)
obs = test[t]
history.append(obs)
print('predicted=%f, expected=%f' % (yhat, obs))
error = mean_squared_error(test, predictions)
print('Test MSE: %.3f' % error)
# plot
plt.plot(test)
plt.plot(predictions, color='red')
plt.show()
plt.plot(test,predictions)
plt.show()