-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicators.py
371 lines (314 loc) · 14.1 KB
/
indicators.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import arabic_reshaper
import itertools
import math
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import seaborn as sns
import sys
import warnings
from bidi.algorithm import get_display
from matplotlib import rc
from matplotlib.backends import backend_gtk3
from iran_stock import get_iran_stock_network
from settings import OUTPUT_DIR, XI_PATH
warnings.filterwarnings('ignore', module=backend_gtk3.__name__)
# Algorithm Settings
RSI_PERIOD = 7
CV_DAYS = 14
TEST_DAYS = 21
FOURIER_HARMONICS = 10
SINDY_ITERATIONS = 10
CANDIDATE_LAMBDAS_RSI = [10 ** i for i in range(-9, -1)] # empirical
CANDIDATE_LAMBDAS_SRSI = list(np.arange(0.001, 0.011, 0.001)) # empirical
def _simple_moving_average(x, n):
s = np.zeros(x.shape)
s[:n - 1] = x[:n - 1] # this is automatically deep copy
for i in range(n - 1, s.shape[0]):
s[i] = np.mean(x[i - (n - 1):i + 1], axis=0)
return s
def _exponential_moving_average(x, n):
alpha = 1 / n
s = np.zeros(x.shape)
s[0] = x[0] # this is automatically deep copy
for i in range(1, s.shape[0]):
s[i] = x[i] * alpha + s[i - 1] * (1 - alpha)
return s
def _get_iran_stock_indicators():
iran_stock_network = get_iran_stock_network()
x = iran_stock_network.x
u = x[1:] - x[:x.shape[0] - 1]
u = u.clip(min=0)
d = x[:x.shape[0] - 1] - x[1:]
d = d.clip(min=0)
rs = np.nan_to_num(_exponential_moving_average(u, RSI_PERIOD) / _exponential_moving_average(d, RSI_PERIOD))
rsi = 100 - (100 / (1 + rs))
srsi = np.zeros(rsi.shape)
for i in range(RSI_PERIOD - 1, srsi.shape[0]):
min_rsi = rsi[i - RSI_PERIOD + 1:i + 1].min(axis=0)
max_rsi = rsi[i - RSI_PERIOD + 1:i + 1].max(axis=0)
srsi[i] = (rsi[i] - min_rsi) / (max_rsi - min_rsi)
srsi = np.nan_to_num(np.delete(srsi, list(range(RSI_PERIOD - 1)), 0))
return rsi, srsi, iran_stock_network.node_labels
def _normalize_x(x):
normalized_columns = []
normalization_parameters = []
for column_index in range(x.shape[1]):
column = x[:, column_index]
std = max(10 ** -9, np.std(column)) # to avoid division by zero
mean = np.mean(column)
normalized_column = (column - mean) / std
normalized_columns.append(normalized_column)
normalization_parameters.append((mean, std))
normalized_x = np.column_stack(normalized_columns)
return normalized_x, normalization_parameters
def _revert_x(normalized_x, normalization_parameters):
reverted_columns = []
for column_index in range(normalized_x.shape[1]):
column = normalized_x[:, column_index]
mean, std = normalization_parameters[column_index]
reverted_column = column * std + mean
reverted_columns.append(reverted_column)
reverted_x = np.column_stack(reverted_columns)
return reverted_x
def _get_detrended_x(x):
columns = []
detrending_parameters = []
for node_index in range(x.shape[1]):
x_i = x[:, node_index]
t = np.arange(0, x_i.shape[0])
linear_trend = np.polyfit(t, x_i, 1)[0]
detrending_parameters.append(linear_trend)
column = x_i - linear_trend * t
columns.append(column)
detrended_x = np.column_stack(columns)
return detrended_x, detrending_parameters
def _fourier_extrapolation(x, prediction_time_frames):
detrended_x, detrending_parameters = _get_detrended_x(x)
columns = []
for node_index in range(detrended_x.shape[1]):
x_i = detrended_x[:, node_index]
x_i_frequency_domain = np.fft.fft(x_i)
time_frames = x_i.size
frequencies = np.fft.fftfreq(time_frames)
amplitudes = np.absolute(x_i_frequency_domain) / time_frames
phases = np.angle(x_i_frequency_domain)
amplitude_threshold = np.copy(amplitudes)
amplitude_threshold.sort()
amplitude_threshold = amplitude_threshold[-FOURIER_HARMONICS]
t = np.arange(0, time_frames + prediction_time_frames)
extrapolated_x_i = np.zeros(t.size)
for i in range(time_frames):
amplitude = amplitudes[i]
if amplitude >= amplitude_threshold:
frequency = frequencies[i]
phase = phases[i]
extrapolated_x_i += amplitude * np.cos(2 * np.pi * frequency * t + phase)
columns.append(extrapolated_x_i + detrending_parameters[node_index] * t)
extrapolated_x = np.column_stack(columns)
return extrapolated_x
def _get_x_dot(x):
x_dot = (x[1:] - x[:len(x) - 1])
return x_dot
def _get_theta(x): # empirical
time_frames = x.shape[0] - 1
x_vectors = [x[:time_frames, i] for i in range(x.shape[1])]
column_list = [np.ones(time_frames)] + x_vectors
for subset in itertools.combinations(x_vectors, 2):
column_list.append(subset[0] / (1 + np.abs(subset[1])))
column_list.append(subset[1] / (1 + np.abs(subset[0])))
theta = np.column_stack(column_list)
return theta
def _least_squares(x_dot, theta):
return np.linalg.lstsq(theta, x_dot, rcond=None)[0].T
def _single_node_sindy(x_dot_i, theta, candidate_lambda):
xi_i = np.linalg.lstsq(theta, x_dot_i, rcond=None)[0]
for j in range(SINDY_ITERATIONS):
small_indices = np.flatnonzero(np.absolute(xi_i) < candidate_lambda)
big_indices = np.flatnonzero(np.absolute(xi_i) >= candidate_lambda)
xi_i[small_indices] = 0
xi_i[big_indices] = np.linalg.lstsq(theta[:, big_indices], x_dot_i, rcond=None)[0]
return xi_i
def _optimum_sindy(x_dot, theta, candidate_lambdas):
cv_index = x_dot.shape[0] - CV_DAYS
x_dot_train = x_dot[:cv_index]
x_dot_cv = x_dot[cv_index:]
theta_train = theta[:cv_index]
theta_cv = theta[cv_index:]
xi = np.zeros((x_dot_train.shape[1], theta_train.shape[1]))
for i in range(x_dot_train.shape[1]):
# progress bar
sys.stdout.write('\rNode [%d/%d]' % (i + 1, x_dot_train.shape[1]))
sys.stdout.flush()
least_cost = sys.maxsize
best_xi_i = None
x_dot_i = x_dot_train[:, i]
x_dot_cv_i = x_dot_cv[:, i]
for candidate_lambda in candidate_lambdas:
xi_i = _single_node_sindy(x_dot_i, theta_train, candidate_lambda)
complexity = math.log(1 + np.count_nonzero(xi_i))
x_dot_hat_i = np.matmul(theta_cv, xi_i.T)
mse_cv = np.square(x_dot_cv_i - x_dot_hat_i).mean()
if complexity: # zero would mean no statements
cost = mse_cv * complexity
if cost < least_cost:
least_cost = cost
best_xi_i = xi_i
xi[i] = best_xi_i
print() # newline
return xi
def thresholding_alg(y, lag, threshold, influence):
"""
slightly modified the code from:
https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data/43512887#43512887
"""
signals = np.zeros(len(y))
filtered_y = np.array(y)
avg_filter = np.zeros(len(y))
std_filter = np.zeros(len(y))
avg_filter[lag - 1] = np.mean(y[0:lag])
std_filter[lag - 1] = np.std(y[0:lag])
for i in range(lag, len(y)):
if abs(y[i] - avg_filter[i - 1]) > threshold * std_filter[i - 1]:
if y[i] > avg_filter[i - 1]:
signals[i] = 1
else:
signals[i] = -1
filtered_y[i] = influence * y[i] + (1 - influence) * filtered_y[i - 1]
avg_filter[i] = np.mean(filtered_y[(i - lag + 1):i + 1])
std_filter[i] = np.std(filtered_y[(i - lag + 1):i + 1])
else:
signals[i] = 0
filtered_y[i] = y[i]
avg_filter[i] = np.mean(filtered_y[(i - lag + 1):i + 1])
std_filter[i] = np.std(filtered_y[(i - lag + 1):i + 1])
return signals
def _draw_time_series(
indicator,
indicator_name,
indicator_hat_fourier,
indicator_hat_lstsq,
indicator_hat_sindy,
node_labels,
test_index):
for node_id in range(indicator.shape[1]):
# Time Series plot
data_frame = pd.DataFrame({
'index': np.arange(indicator.shape[0]),
indicator_name: indicator[:, node_id],
'Fourier': indicator_hat_fourier[:, node_id],
# 'Least_Squares': indicator_hat_lstsq[:, node_id],
'SINDy': indicator_hat_sindy[:, node_id],
})
melted_data_frame = pd.melt(
data_frame,
id_vars=['index'],
value_vars=[
indicator_name,
'Fourier',
# 'Least-Squares',
'SINDy',
]
)
rc('font', weight=600)
plt.subplots(figsize=(20, 10))
ax = sns.lineplot(x='index', y='value', hue='variable', style='variable', data=melted_data_frame, linewidth=4)
node_instrument_id, node_name = node_labels[node_id].split('_')
ax.set_title(get_display(arabic_reshaper.reshape(node_name)), fontsize=28, fontweight=500)
ax.set_xlabel(get_display(arabic_reshaper.reshape('روزها')), fontsize=20, fontweight=500)
ax.set_ylabel(indicator_name, fontsize=20, fontweight=500)
for axis in ['top', 'bottom', 'left', 'right']:
ax.spines[axis].set_linewidth(3)
ax.tick_params(width=3, length=10, labelsize=16)
plt.savefig(os.path.join(OUTPUT_DIR, 'node_%d_%s_%s.png' % (node_id, node_instrument_id, indicator_name)))
plt.close('all')
# Signal Plot
lag = 5
threshold = 1
influence = 1
indicator_signal = thresholding_alg(indicator[:, node_id], lag, threshold, influence)[test_index:]
indicator_hat_sindy_signal = \
thresholding_alg(indicator_hat_sindy[:, node_id], lag, threshold, influence)[test_index:]
data_frame = pd.DataFrame({
'index': np.arange(len(indicator_signal)),
'%s' % indicator_name + get_display(arabic_reshaper.reshape('قلههای')): indicator_signal,
'SINDy' + get_display(arabic_reshaper.reshape('قلههای')): indicator_hat_sindy_signal,
})
melted_data_frame = pd.melt(
data_frame,
id_vars=['index'],
value_vars=[
'%s' % indicator_name + get_display(arabic_reshaper.reshape('قلههای')),
'SINDy' + get_display(arabic_reshaper.reshape('قلههای')),
]
)
rc('font', weight=600)
plt.subplots(figsize=(20, 10))
ax = sns.lineplot(x='index', y='value', hue='variable', style='variable', data=melted_data_frame, linewidth=4)
node_instrument_id, node_name = node_labels[node_id].split('_')
ax.set_title(get_display(arabic_reshaper.reshape(node_name)), fontsize=28, fontweight=500)
ax.set_xlabel(get_display(arabic_reshaper.reshape('روزهای پیشبینی شده')), fontsize=20, fontweight=500)
ax.set_ylabel(get_display(arabic_reshaper.reshape('قلههای تشخیص داده شده')), fontsize=20, fontweight=500)
for axis in ['top', 'bottom', 'left', 'right']:
ax.spines[axis].set_linewidth(3)
ax.tick_params(width=3, length=10, labelsize=16)
plt.savefig(os.path.join(OUTPUT_DIR, 'node_%d_%s_%s_peaks.png' % (node_id, node_instrument_id, indicator_name)))
plt.close('all')
def _mean_absolute_error(test_data, prediction_data):
return np.abs(test_data - prediction_data).mean()
def _create_indicator_time_series(indicator, indicator_name, node_labels, candidate_lambdas_indicator):
normalized_indicator, normalization_parameters = _normalize_x(indicator)
entire_x_dot = _get_x_dot(normalized_indicator)
entire_theta = _get_theta(normalized_indicator)
test_index = entire_x_dot.shape[0] - TEST_DAYS
x_dot_train = entire_x_dot[:test_index]
theta_train = entire_theta[:test_index]
print('Calculating fourier predictions...')
indicator_hat_fourier = _revert_x(
_fourier_extrapolation(normalized_indicator[:test_index], normalized_indicator.shape[0] - test_index),
normalization_parameters
)
indicator_hat_fourier[:test_index] = np.nan # to avoid drawing
print('Creating lstsq predictions...')
xi_lstsq = _least_squares(x_dot_train, theta_train)
normalized_indicator_hat_lstsq = np.copy(normalized_indicator)
for time_frame in range(test_index, indicator.shape[0]):
theta_hat_lstsq = _get_theta(normalized_indicator_hat_lstsq[time_frame - 1:time_frame + 1])
x_dot_hat_lstsq = np.matmul(theta_hat_lstsq, xi_lstsq.T)
normalized_indicator_hat_lstsq[time_frame] = normalized_indicator_hat_lstsq[time_frame - 1] + x_dot_hat_lstsq
indicator_hat_lstsq = _revert_x(normalized_indicator_hat_lstsq, normalization_parameters)
indicator_hat_lstsq[:test_index] = np.nan # to avoid drawing
print('MAE', _mean_absolute_error(
normalized_indicator_hat_lstsq[test_index:],
normalized_indicator[test_index:]
))
print('Creating SINDy predictions...')
if os.path.exists(XI_PATH):
xi_sindy = np.load(XI_PATH, allow_pickle=True)
else:
xi_sindy = _optimum_sindy(x_dot_train, theta_train, candidate_lambdas_indicator)
np.save(XI_PATH, xi_sindy)
normalized_indicator_hat_sindy = np.copy(normalized_indicator)
for time_frame in range(test_index, indicator.shape[0]):
theta_hat_sindy = _get_theta(normalized_indicator_hat_sindy[time_frame - 1:time_frame + 1])
x_dot_hat_sindy = np.matmul(theta_hat_sindy, xi_sindy.T)
normalized_indicator_hat_sindy[time_frame] = normalized_indicator_hat_sindy[time_frame - 1] + x_dot_hat_sindy
indicator_hat_sindy = _revert_x(normalized_indicator_hat_sindy, normalization_parameters)
indicator_hat_sindy[:test_index] = np.nan # to avoid drawing
print('Drawing time series...')
_draw_time_series(
indicator,
indicator_name,
indicator_hat_fourier,
indicator_hat_lstsq,
indicator_hat_sindy,
node_labels,
test_index
)
def run():
rsi, srsi, node_labels = _get_iran_stock_indicators()
_create_indicator_time_series(rsi, 'RSI', node_labels, CANDIDATE_LAMBDAS_RSI)
# _create_indicator_time_series(srsi, 'SRSI', node_labels, CANDIDATE_LAMBDAS_SRSI)
if __name__ == '__main__':
run()