forked from IanMadlenya/MRAE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneuralNetwork_utils.py
More file actions
153 lines (117 loc) · 4.38 KB
/
neuralNetwork_utils.py
File metadata and controls
153 lines (117 loc) · 4.38 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# coding: utf-8
# # Utils
# In[1]:
from bokeh.palettes import Spectral7
from bokeh.plotting import figure, output_file, show, save, reset_output
from bokeh.io import gridplot, output_notebook, output_file
import numpy as np
import pandas as pd
# ## Input data frame setup
# ### Load data
# In[2]:
# Concatenate interest variables for each stock in a single data frame
# E.g.:
# stock1|stock2|...|stockn
# return_stock1_date1|return_stock2_date1|...|Return_stockn_date1
# return_stock1_date2|return_stock2_date2|...|Return_stockn_date2
def buildInput(stocks, feature):
initKey = list(stocks.keys())[0]
inputDataFrame = stocks[initKey][feature].to_frame(name=initKey)
for i, stock in enumerate(stocks):
if stock != initKey:
inputDataFrame = pd.concat([inputDataFrame, stocks[stock][feature].to_frame(stock)], axis=1)
return(inputDataFrame)
# In[3]:
def getInputs(scalingFactor,
stocksFile):
inputNN = pd.read_csv(filepath_or_buffer =stocksFile,
sep=';',
header=0,
index_col='Date',
parse_dates=True) * scalingFactor
return(inputNN)
# In[4]:
def splitData(inputData, trainUB, validationLB, validationUB, testLB):
inputData_train = inputData.loc[:trainUB]
inputData_validation = inputData.loc[validationLB:validationUB]
inputData_test = inputData.loc[testLB:]
return(inputData_train, inputData_validation, inputData_test)
# ### Noise data
#
# In case of denoising autoencoder
# In[5]:
def noiseTrain(train, sigma):
res = train.copy()
res += sigma * np.random.normal(loc=0.0, scale=1.0, size=res.shape)
return(res)
def noiseTrain2(train, p=0.25):
res = train.copy()
tmp = np.random.uniform(0, 1, res.shape)
tmp[tmp < p] = 0
tmp[tmp > p] = 1
return(np.multiply(res, tmp))
# ## Plot network results
# In[1]:
def plotPreds(prediction, test, outputDir, parametersSet):
reset_output()
stocks = test.columns.values
dataTest = test.reset_index()
output_file(outputDir + '_'.join(parametersSet) + '_predPerf.html')
colors_list = ['green', 'red']
grid = []
subGrid = []
for i, stock in enumerate(sorted(stocks)):
if i % 3 == 0 and i != 0:
grid.append(subGrid)
subGrid = []
legends_list = [stock, 'reconstruction']
xs = [dataTest['Date'], dataTest['Date']]
ys = [dataTest[stock], prediction[stock]]
p = figure(x_axis_type="datetime",
y_axis_label = "Log-return")
for (colr, leg, x, y ) in zip(colors_list, legends_list, xs, ys):
p.line(x, y, color=colr, legend=leg)
subGrid.append(p)
p = gridplot(grid)
save(p)
return True
def plotError(history, outputDir, parametersSet):
reset_output()
output_file(outputDir + '_'.join(parametersSet) + '_loss.html')
colors_list = ['green', 'red']
p = figure(x_axis_label='iteration',
y_axis_label='average loss',plot_width=350, plot_height=350)
p.xaxis.axis_label_text_font_size = "12pt"
p.yaxis.axis_label_text_font_size = "12pt"
legends_list = ['validation loss', 'training loss']
xs = [history.epoch, history.epoch]
ys = [history.history['loss'], history.history['val_loss']]
for (colr, leg, x, y ) in zip(colors_list, legends_list, xs, ys):
p.line(x, y, color=colr, legend=leg)
save(p)
return True
def plotResiduals(residuals, outputDir, parametersSet, who):
reset_output()
stocks = residuals.columns.values
res = residuals.reset_index()
output_file(outputDir + '_'.join(parametersSet) + '_residuals_' + who + '.html')
grid = []
subGrid = []
for i, stock in enumerate(sorted(stocks)):
if i % 3 == 0 and i != 0:
grid.append(subGrid)
subGrid = []
p1 = figure(title=stock + ' ' + who + ' residuals', background_fill_color="#E8DDCB", x_axis_label='r - r_hat')
p1.yaxis.visible = None
p1.legend.location = "top_left"
hist, edges = np.histogram(res[stock], density=True, bins=25)
p1.quad(top=hist,
bottom=0,
left=edges[:-1],
right=edges[1:],
fill_color="#036564",
line_color="#033649")
subGrid.append(p1)
p = gridplot(grid)
save(p)
return True