-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexample_usage.py
246 lines (213 loc) · 6.91 KB
/
example_usage.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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 7 23:29:32 2022
@author: Maciej Rosoł
contact: [email protected], [email protected]
"""
#%%
import os
# os.chdir(os.path.dirname(__file__))
import numpy as np
import nonlincausality as nlc
import matplotlib.pyplot as plt
import copy
from nonlincausality.utils import prepare_data_for_prediction, calculate_pred_and_errors
from sklearn.svm import SVR
#%% Data generation Y->X
np.random.seed(10)
y = (
np.cos(np.linspace(0, 20, 10_100))
+ np.sin(np.linspace(0, 3, 10_100))
- 0.2 * np.random.random(10_100)
)
np.random.seed(20)
x = 2 * y ** 3 - 5 * y ** 2 + 0.3 * y + 2 - 0.05 * np.random.random(10_100)
data = np.vstack([x[:-100], y[100:]]).T
plt.figure()
plt.plot(data[:, 0], label="X")
plt.plot(data[:, 1], label="Y")
plt.xlabel("Number of sample")
plt.ylabel("Signals [AU]")
plt.legend()
#%% Test in case of presence of the causality
lags = [50, 150]
data_train = data[:6000, :]
data_val = data[6000:8000, :]
data_test = data[8000:, :]
results = nlc.nonlincausalityNN(
x=data_train,
maxlag=lags,
NN_config=['d','dr','d','dr'],
NN_neurons=[100,0.05,100,0.05],
x_test=data_test,
run=3,
epochs_num=[50, 50],
learning_rate=[0.0001, 0.00001],
batch_size_num=32,
x_val=data_val,
reg_alpha=None,
callbacks=None,
verbose=True,
plot=True,
)
#%% Example of obtaining the results
for lag in lags:
best_model_X = results[lag].best_model_X
best_model_XY = results[lag].best_model_XY
p_value = results[lag].p_value
test_statistic = results[lag]._test_statistic
best_history_X = results[lag].best_history_X
best_history_XY = results[lag].best_history_XY
nlc.plot_history_loss(best_history_X, best_history_XY)
plt.title("Lag = %d" % lag)
best_errors_X = results[lag].best_errors_X
best_errors_XY = results[lag].best_errors_XY
cohens_d = np.abs(
(np.mean(np.abs(best_errors_X)) - np.mean(np.abs(best_errors_XY)))
/ np.std([best_errors_X, best_errors_XY])
)
print("For lag = %d Cohen's d = %0.3f" % (lag, cohens_d))
print(f"Test statistic = {test_statistic} p-value = {p_value}")
# Using models for prediction
data_X, data_XY = prepare_data_for_prediction(data_test, lag)
X_pred_X = best_model_X.predict(data_X)
X_pred_XY = best_model_XY.predict(data_XY)
# Plot of true X vs X predicted
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].plot(data_test[lag:, 0], X_pred_X, "o")
ax[0].set_xlabel("X test values")
ax[0].set_ylabel("Predicted X values")
ax[0].set_title("Model based on X")
ax[1].plot(data_test[lag:, 0], X_pred_XY, "o")
ax[1].set_xlabel("X test values")
ax[1].set_ylabel("Predicted X values")
ax[1].set_title("Model based on X and Y")
plt.suptitle("Lag = %d" % lag)
# Another way of obtaining predicted values (and errors)
X_pred_X, X_pred_XY, error_X, error_XY = calculate_pred_and_errors(
data_test[lag:, 0],
data_X,
data_XY,
best_model_X,
best_model_XY
)
# Plot of X predicted vs prediction error
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].plot(X_pred_X, error_X, "o")
ax[0].set_xlabel("Predicted X values")
ax[0].set_ylabel("Prediction errors")
ax[0].set_title("Model based on X")
ax[1].plot(X_pred_XY, error_XY, "o")
ax[1].set_xlabel("Predicted X values")
ax[1].set_ylabel("Prediction errors")
ax[1].set_title("Model based on X and Y")
plt.suptitle("Lag = %d" % lag)
#%% Test in case of absence of the causality
np.random.seed(30)
data_noise = np.vstack([x[:-100], np.random.random(10_000)]).T
lags = [50, 150]
data_noise_train = data_noise[:6000, :]
data_noise_val = data_noise[6000:8000, :]
data_noise_test = data_noise[8000:, :]
results = nlc.nonlincausalityNN(
x=data_noise_train,
maxlag=lags,
NN_config=['d','dr','d','dr'],
NN_neurons=[100,0.05,100,0.05],
x_test=data_noise_test,
run=3,
epochs_num=[50, 50],
learning_rate=[0.001, 0.0001],
batch_size_num=32,
x_val=data_noise_val,
reg_alpha=None,
callbacks=None,
verbose=True,
plot=True,
)
#%% Example of obtaining the results
for lag in lags:
best_model_X_lag50 = results[lag].best_model_X
best_model_XY_lag50 = results[lag].best_model_XY
p_value = results[lag].p_value
test_statistic = results[lag].test_statistic
best_history_X = results[lag].best_history_X
best_history_XY = results[lag].best_history_XY
nlc.plot_history_loss(best_history_X, best_history_XY)
plt.title("Lag = %d" % lag)
best_errors_X = results[lag].best_errors_X
best_errors_XY = results[lag].best_errors_XY
cohens_d = np.abs(
(np.mean(np.abs(best_errors_X)) - np.mean(np.abs(best_errors_XY)))
/ np.std([best_errors_X, best_errors_XY])
)
print("For lag = %d Cohen's d = %0.3f" % (lag, cohens_d))
print(f"test statistic = {test_statistic} p-value = {p_value}")
#%% Example of the measure of the causality change over time
data_test_measure = copy.copy(data_test)
np.random.seed(30)
data_test_measure[:1000, 1] = np.random.random(1000)
plt.figure()
plt.plot(data_test_measure[:, 0], label="X")
plt.plot(data_test_measure[:, 1], label="Y")
plt.xlabel("Number of sample")
plt.ylabel("Signals [AU]")
plt.legend()
results = nlc.nonlincausalitymeasureNN(
x=data_train,
maxlag=lags,
window=100,
step=1,
NN_config=['d','dr','d','dr'],
NN_neurons=[100,0.05,100,0.05],
x_test=data_test_measure,
run=3,
epochs_num=[50,50],
learning_rate=[0.0001, 0.00001],
batch_size_num=32,
x_val=data_val,
verbose=True,
plot=True,
)
#%% Example of usage for conditional analysis
np.random.seed(30)
z = np.random.random([10_000, 2])
z_train = z[:6000, :]
z_val = z[6000:8000, :]
z_test = z[8000:, :]
results_conditional = nlc.nonlincausalityNN(
x=data_train,
maxlag=lags,
NN_config=['d','dr','d','dr'],
NN_neurons=[100,0.05,100,0.05],
x_test=data_test,
run=1,
z=z_train,
z_test=z_test,
epochs_num=[50, 50],
learning_rate=[0.0001, 0.00001],
batch_size_num=32,
x_val=data_val,
z_val=z_val,
reg_alpha=None,
callbacks=None,
verbose=True,
plot=True,
)
# %% Exaple of the usage the package with Scikit-learn model
parametres = {
'kernel':['poly', 'rbf'],
'C':[0.01,0.1,1],
'epsilon':[0.01,0.1,1.]
}
results_skl = nlc.nonlincausality_sklearn(
x=data_train,
sklearn_model=SVR,
maxlag=lags,
params=parametres,
x_test=data_test,
x_val=data_val,
plot=True)
#%% Example of usage other functions for causality analysis
# ARIMA/ARIMAX models
results_ARIMA = nlc.nonlincausalityARIMA(x=data_train[::10], maxlag=[5,15], x_test=data_test[::10])