-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvol.py
111 lines (83 loc) · 2.59 KB
/
vol.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
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
class NeuralNetwork(nn.Module):
def __init__(self, inputs, outputs):
super(NeuralNetwork, self).__init__()
self.layer = nn.Linear(inputs, 100)
self.layer2 = nn.Linear(100, outputs)
self.relu = nn.ReLU()
def forward(self, x):
x = self.layer(x)
x = self.relu(x)
x = self.layer2(x)
return x
class Normalize:
def __init__(self):
self.minx = None
self.maxx = None
def normalize(self, x):
x = np.array(x)
self.minx = min(x)
self.maxx = max(x)
return (x - self.minx)/(self.maxx - self.minx)
def denormalize(self, x):
return x*(self.maxx - self.minx) + self.minx
def BuildDataSet(pct):
window = 150
result = []
for i in range(window, len(pct)):
hold = pct[i-window:i]
result.append(np.std(hold))
return result
def BuildTrainTest(vol):
window = 100
output = 30
inputs = []
outputs = []
for i in range(window, len(vol)-output+1):
inp_ = vol[i-window:i]
out_ = vol[i:i+output]
inputs.append(inp_)
outputs.append(out_)
IN = [torch.tensor(i, dtype=torch.float32) for i in inputs]
OUT = [torch.tensor(i, dtype=torch.float32) for i in outputs]
TEST = [torch.tensor(vol[-window:], dtype=torch.float32)]
return torch.stack(IN), torch.stack(OUT), torch.stack(TEST)
normal = Normalize()
model = NeuralNetwork(100, 30)
data = pd.read_csv('BLK.csv')[::-1]
close = data['adjClose'].values
delta = close[1:]/close[:-1] - 1.0
Volatility = BuildDataSet(delta)
nVol = normal.normalize(Volatility)
TIN, TOUT, TTEST = BuildTrainTest(nVol)
learning_rate = 0.001
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
criterion = nn.MSELoss()
epochs = 500
for epoch in range(epochs):
output = model(TIN)
loss = criterion(output, TOUT)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(epoch, loss.item())
with torch.no_grad():
testout = model(TTEST)
nvolatility = testout.numpy()[0]
predicted_volatility = normal.denormalize(nvolatility)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title("Volatility Forecast for BlackRock")
ax.set_xlabel("Time")
ax.set_ylabel("Volatility")
kVolatility = Volatility[-100:]
ux = list(range(len(kVolatility)))
px = list(range(len(kVolatility), len(kVolatility)+len(predicted_volatility)))
ax.plot(ux, kVolatility, color='red')
ax.plot(px, predicted_volatility, color='limegreen')
plt.show()