-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforecast.py
executable file
·127 lines (115 loc) · 3.65 KB
/
forecast.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from scipy import signal
import os
from statsmodels.tsa.seasonal import seasonal_decompose
from matplotlib import pyplot
from pandas.plotting import autocorrelation_plot
from statsmodels.tsa.arima_model import ARIMA
from sklearn.metrics import mean_squared_error
plt.rcParams["savefig.directory"] = os.chdir(os.path.dirname('./'))
# Input file paths
flxsPath = './stockData/FLXS_2017_2018.csv'
vfcPath = './stockData/VFC_2017_2018.csv'
flxs = pd.read_csv(flxsPath, index_col = 0) # steel manu.
vfc = pd.read_csv(vfcPath, index_col = 0) # apparel company
# Plotting individual stock prices with the cross correlation value
corr = flxs['High'].corr(vfc['High'])
fig, ax = subplots()
flxs['High'].plot(legend = True,ax=ax, title=corr)
vfc['High'].plot(legend = True,ax=ax)
ax.legend(['$vfc','$flxs'])
plt.show()
# Cross correlation plot
corrSignals = signal.correlate((vfc['High'].values), (flxs['High'].values), mode='same') / 128
plt.clf()
plt.plot(corrSignals)
plt.title("Cross correlation between the two stocks")
plt.show()
# Autocorrelation_plot VFC
autocorrelation_plot(vfc['High'].values)
pyplot.title("vfc autocorrelation_plot")
pyplot.show()
# Autocorrelation_plot FLXS
autocorrelation_plot(flxs['High'].values)
pyplot.title("flxs autocorrelation_plot")
pyplot.show()
# Detrending and plotting $FLXS
X = flxs['High']
diffa = list()
for i in range(1, len(X)):
value = X[i] - X[i - 1]
diffa.append(value)
plt.plot(diffa)
plt.title("Detendring $flxs")
plt.show()
# Detrending and plotting $VFC
X = vfc['High']
diffb = list()
for i in range(1, len(X)):
value = X[i] - X[i - 1]
diffb.append(value)
plt.title("Detendring $vfc")
plt.plot(diffb)
plt.show()
# Analysing individual graphs using seasonal decomposition
result = seasonal_decompose(vfc['High'].values, model='additive', freq=1)
result.plot()
pyplot.title("$vfc characteristics")
pyplot.show()
result = seasonal_decompose(flxs['High'].values, model='additive', freq=1)
result.plot()
pyplot.title("$flxs characteristics")
pyplot.show()
plt.clf()
# ARIMA model for FLXS
model = ARIMA(flxs['High'].values, order=(5,0,0))
model_fit = model.fit(disp=0)
print(model_fit.summary())
residuals = pd.DataFrame(model_fit.resid)
print(residuals.describe())
size = int(len(flxs['High'].values) * 0.66)
train, test = flxs['High'].values[0:size], flxs['High'].values[size:len(flxs['High'].values)]
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)
pyplot.plot(test)
pyplot.plot(predictions, color='red')
pyplot.title("ARIMA predictions $FLXS")
pyplot.show()
# ARIMA model for VFC
model = ARIMA(vfc['High'].values, order=(5,0,0))
model_fit = model.fit(disp=0)
print(model_fit.summary())
print(residuals.describe())
size = int(len(vfc['High'].values) * 0.66)
train, test = vfc['High'].values[0:size], vfc['High'].values[size:len(vfc['High'].values)]
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)
pyplot.plot(test)
pyplot.plot(predictions, color='red')
pyplot.title("ARIMA predictions $VFC")
pyplot.show()
sys.exit(1)