-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFBackGEmanager.py
More file actions
346 lines (296 loc) · 11.1 KB
/
FBackGEmanager.py
File metadata and controls
346 lines (296 loc) · 11.1 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
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
import numpy as np
from FBackGainsEstimator import *
class FBackGEmanager:
"""
GE=Gains Estimator. The goal of this class is to create an
FBackGainsEstimator for n=1,2,3, ..., n_max-1. These are stored in the
dictionary self.n_to_estimator.
Attributes
----------
graph: FBackGraph
hidden_nds: list[str]
same meaning as in FBackGainsEstimator
mean_alpha_mat: np.array
average over time-slices of alpha_mat
mean_beta_mat: np.array
average over time-slices of beta_mat
n_max: int
>=1
n_to_estimator: dict[int, FBackGainsEstimator]
solve_symbolically: bool
same meaning as in FBackGainsEstimator
std_of_alpha_mat: np.array
standard deviation over time-slices of alpha_mat
std_of_beat_mat: np.array
standard deviation over time-slices of beta_mat
"""
def __init__(self, n_max,
graph,
path,
solve_symbolically=False,
hidden_nds=None,
delta=True):
"""
Constructor
Parameters
----------
n_max: int
graph: FBackGraph
path: str
path to csv file with data
solve_symbolically: bool
hidden_nds: list[str]
delta: bool
"""
self.n_max = n_max
self.graph = graph
self.solve_symbolically = solve_symbolically
if hidden_nds is None:
self.hidden_nds = []
else:
assert set(hidden_nds).issubset(graph.ord_nodes)
self.hidden_nds = hidden_nds
df = pd.read_csv(path)
columns = FBackRandomDataMaker.get_columns(n_max, graph)
assert set(df.columns) == set(columns)
# put columns in same order as graph.ord_nodes
df = df[columns]
dim = self.graph.num_nds
self.n_to_estimator = {}
for time in range(1, self.n_max):
slice_n = columns[(time-1)*dim: time*dim]
slice_n_plus_one = columns[time * dim: (time+1) * dim]
two_slices = slice_n + slice_n_plus_one
df_two_slices = df[two_slices]
self.n_to_estimator[time] = FBackGainsEstimator(
time,
graph,
df_two_slices,
solve_symbolically,
hidden_nds,
delta
)
self.n_to_estimator[time].calculate_gains()
self.mean_alpha_mat = None
self.std_of_alpha_mat = None
self.mean_beta_mat = None
self.std_of_beta_mat = None
def print_greek_lists(self, name, true_greek_mat=None, verbose=False):
"""
This method prints the alpha_list (or the beta_list) of
self.n_to_estimator[n], for n=1,2,3,..., n_max-1, by calling
latexify.print_list_sb() for each n.
Parameters
----------
name: str
either "alpha" or "beta"
true_greek_mat: np.array
verbose: bool
Returns
-------
sp.Symbol
"""
assert name in ["alpha", "beta"]
str0 = r"\begin{array}{l}" + "\n"
for time in range(1, self.n_max):
str0 += r"\text{******** time=" + str(time) + r"}\\" + "\n"
gest = self.n_to_estimator[time]
if name == "alpha":
mat = gest.alpha_mat_estimate
else:
mat = gest.beta_mat_estimate
eq_list = create_eq_list_from_matrix(mat, name, self.graph,
time=None)
comments = gest.get_greek_list_comments(
name, eq_list, true_greek_mat=true_greek_mat)
x = print_list_sb(eq_list,
self.graph,
comment_list=comments)
str0 += str(x) + "\n" + r"\\" + "\n"
str0 = str0[:-3]
str0 += r"\end{array}"
if verbose:
print(str0)
return sp.Symbol(str0)
def print_mean_greek_list(self, name, true_greek_mat=None,
verbose=False):
"""
This method prints the average of the alpha_mat (or the
beta_mat) of self.n_to_estimator[n] for n=1,2,3,..., n_max-1.
Parameters
----------
name: str
either "alpha" or "beta"
true_greek_mat: np.array
either "true_alpha_mat" or "true_beta_mat"
verbose: bool
Returns
-------
sp.Symbol
"""
assert name in ["alpha", "beta"]
if name == "alpha":
self.mean_alpha_mat = np.mean([self.n_to_estimator[
time].alpha_mat_estimate for
time in range(1, self.n_max)], axis=0)
mat = sp.Matrix(self.mean_alpha_mat)
else:
self.mean_beta_mat = np.mean([self.n_to_estimator[
time].beta_mat_estimate for
time in range(1, self.n_max)], axis=0)
mat = sp.Matrix(self.mean_beta_mat)
eq_list = create_eq_list_from_matrix(mat, name, self.graph,
time=None)
comments = self.n_to_estimator[1].get_greek_list_comments(
name, eq_list, true_greek_mat=true_greek_mat)
return print_list_sb(eq_list, self.graph,
comment_list=comments, verbose=verbose,
prefix_str="mean of")
def print_std_of_greek_list(self, name, verbose=False):
"""
This method prints the standard deviation of the alpha_mat ( or the
beta_mat) of self.n_to_estimator[n] for n=1,2,3, ..., n_max-1.
Parameters
----------
name: str
either "alpha" or "beta"
verbose: bool
Returns
-------
sp.Symbol
"""
assert name in ["alpha", "beta"]
if name == "alpha":
self.std_of_alpha_mat = np.std([self.n_to_estimator[
time].alpha_mat_estimate for
time in range(1, self.n_max)], axis=0)
mat = sp.Matrix(self.std_of_alpha_mat)
else:
self.std_of_beta_mat = np.std([self.n_to_estimator[
time].beta_mat_estimate for
time in range(1, self.n_max)], axis=0)
mat = sp.Matrix(self.std_of_beta_mat)
eq_list = create_eq_list_from_matrix(mat, name, self.graph,
time=None)
return print_list_sb(eq_list, self.graph,
comment_list=None, verbose=verbose,
prefix_str="std of ")
def print_alpha_lists(self, true_alpha_mat=None, verbose=False):
"""
This method prints the alpha_list of FBackGainsEstimator[n] for n=1,
2,3, ... n_max-1 by calling self.print_greek_lists().
Parameters
----------
true_alpha_mat: np.array
verbose: bool
Returns
-------
sp.Symbol
"""
return self.print_greek_lists("alpha",
true_greek_mat=true_alpha_mat,
verbose=verbose)
def print_mean_alpha_list(self, true_alpha_mat=None, verbose=False):
"""
This method prints the average of the alpha_list of
FBackGainsEstimator[n] for n=1, 2,3, ... n_max-1. It does this by
calling self.print_mean_greek_list().
Parameters
----------
true_alpha_mat: np.array
verbose: bool
Returns
-------
sp.Symbol
"""
return self.print_mean_greek_list("alpha",
true_greek_mat=true_alpha_mat,
verbose=verbose)
def print_std_of_alpha_list(self, verbose=False):
"""
This method prints the standard deviation of the alpha_list of
FBackGainsEstimator[n] for n=1, 2,3, ... n_max-1. It does this by
calling self.print_std_of_greek_list().
Parameters
----------
verbose: bool
Returns
-------
sp.Symbol
"""
return self.print_std_of_greek_list("alpha", verbose=verbose)
def print_beta_lists(self, true_beta_mat=None, verbose=False):
"""
This method prints the beta_list of FBackGainsEstimator[n] for n=1,
2,3, ... n_max-1 by calling self.print_greek_lists().
Parameters
----------
true_beta_mat: np.array
verbose: bool
Returns
-------
sp.Symbol
"""
return self.print_greek_lists("beta",
true_greek_mat=true_beta_mat,
verbose=verbose)
def print_mean_beta_list(self, true_beta_mat=None, verbose=False):
"""
This method prints the average of the beta_list of
FBackGainsEstimator[n] for n=1, 2,3, ... n_max-1. It does this by
calling self.print_mean_greek_list().
Parameters
----------
true_beta_mat: np.array
verbose: bool
Returns
-------
sp.Symbol
"""
return self.print_mean_greek_list("beta",
true_greek_mat=true_beta_mat,
verbose=verbose)
def print_std_of_beta_list(self, verbose=False):
"""
This method prints the standard deviation of the beta_list of
FBackGainsEstimator[n] for n=1, 2,3, ... n_max-1. It does this by
calling self.print_std_of_greek_list().
Parameters
----------
verbose: bool
Returns
-------
sp.Symbol
"""
return self.print_std_of_greek_list("beta", verbose=verbose)
if __name__ == "__main__":
def main():
path = 'dot_atlas/fback-2node.dot'
graph = FBackGraph(path)
dim = graph.num_nds
mean_eps = [0]*dim
sig_eps = [10] * dim
n_max = 4
alpha_bound = 10
beta_bound = 1
alpha_mat, beta_mat = \
FBackRandomDataMaker.generate_random_alpha_and_beta_mats(
graph, alpha_bound=alpha_bound, beta_bound=beta_bound)
dmaker = FBackRandomDataMaker(n_max, graph,
alpha_mat=alpha_mat,
beta_mat=beta_mat,
mean_eps=mean_eps,
sig_eps=sig_eps)
num_rows = 100
data_path = "test_data.csv"
dmaker.write_dataset_csv(num_rows, data_path)
df = pd.read_csv(data_path)
print(df)
mger = FBackGEmanager(n_max, graph, data_path)
mger.print_alpha_lists(true_alpha_mat=dmaker.alpha_mat, verbose=True)
mger.print_mean_alpha_list(true_alpha_mat=dmaker.alpha_mat,
verbose=True)
mger.print_beta_lists(true_beta_mat=dmaker.beta_mat, verbose=True)
mger.print_mean_beta_list(true_beta_mat=dmaker.beta_mat,
verbose=True)
main()