-
Notifications
You must be signed in to change notification settings - Fork 2
/
FBackGainsEstimator.py
404 lines (346 loc) · 13.9 KB
/
FBackGainsEstimator.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import pandas as pd
import numpy as np
from copy import deepcopy
from itertools import product
import sympy as sp
from FBackGraph import *
from FBackRandomDataMaker import *
from GainsEstimator import *
from FBackGainsCalculator import *
class FBackGainsEstimator(GainsEstimator):
"""
The goal of this class is to estimate the inslice gain \alpha_{i|j} or
the feedback gain \beta_{i|j} for each arrow x_j->x_i in a linear SCM
with feedback loops. The estimation algorithm requires as input a file
which contains a dataset with, for each time n=1,2, \dots, n_{max},
the node names (plus string [ n]) as column labels, and with node
values, at time n, as rows.
The input dataset column labels must include ALL node names, and nothing
else, but these column labels need not be in topological order (as they
are in self.ord_nodes).
A list of hidden nodes is an argument of the class constructor with None
as default value. Columns of the input dataset corresponding to hidden
nodes will be ignored. Hence, these column entries can be any number.
Correlations <x_i, x_j> where x_i or x_j is a hidden node will be
expressed symbolically (sb); otherwise, they will be expressed
numerically (nm).
Attributes
----------
beta_cum_err: float
Same as for alpha. See alpha explanation in parent class GainsEstimator
beta_list: list[sp.Eq]
Same as for alpha. See alpha explanation in parent class GainsEstimator
beta_mat: sp.Matrix
Same as for alpha. See alpha explanation in parent class GainsEstimator
beta_mat_estimate: np.array
Same as for alpha. See alpha explanation in parent class GainsEstimator
cov_mat_list: list[sp.Matrix, sp.Matrix, sp.Matrix]
[cov_mat0, cov2times, cov_mat1] where cov_mat0=covariance matrix at
time n, cov2times=the 2-times covariance matrix between times n and
n+1, and cov_mat1=covariance matrix at time n+1. This is an internal
variable.
delta: bool
see explanation in docstring for class FBackGainsCalculator
time: None or str or int
"""
def __init__(self,
time,
graph,
df,
solve_symbolically=False,
hidden_nds=None,
delta=True):
"""
Constructor
Parameters
----------
time: None or str or int
graph: FBackGraph
df: pd.Dataframe
solve_symbolically: bool
solve_symbolically=True if linsolve() is called using a fully
symbolic covariance matrix, and then the numeric values of the
covariance matrix are substituted in the solution.
solve_symbolically=False if linsolve() is called using a hybrid
covariance matrix, partly symbolic, partly numeric.
hidden_nds: None or list[str]
delta: bool
"""
GainsEstimator.__init__(self, graph, path=None,
solve_symbolically=solve_symbolically,
hidden_nds=hidden_nds)
self.time = time
self.delta = delta
dim = graph.num_nds
# alpha version of the following already defined
# by parent method
self.beta_mat = None
self.beta_mat_estimate = np.zeros((dim, dim))
self.beta_cum_err = 0
self.beta_list = None
self.cov_mat_list = None
self.set_cov_mat(df)
self.calculate_gains()
self.fix_alpha_list()
self.fix_beta_list()
def set_cov_mat(self, df):
"""
This method sets the values of the 3 sp.Matrices in
self.cov_mat_list = [cov_mat0, cov2times, cov_mat1]. Entries of
these matrix that have hidden nodes in their indices, are symbolic.
All other entries are numeric.
Parameters
----------
df: pd.Dataframe
Returns
-------
None
"""
dim = self.graph.num_nds
columns = df.columns
assert len(columns) == 2*dim
cov_mat_nm = df.cov().to_numpy()
cov_mat_list_nm = [
cov_mat_nm[np.ix_(range(dim), range(dim))],
cov_mat_nm[np.ix_(range(dim), range(dim, 2 * dim))],
cov_mat_nm[np.ix_(range(dim, 2 * dim), range(dim, 2 * dim))]]
cov_mat0 = cov_sb_mat(dim, time=self.time)
cov2times = cov2times_sb_mat(dim, time=self.time)
cov_mat1 = cov_sb_mat(dim, time=self.time + 1)
self.cov_mat_list = [cov_mat0, cov2times, cov_mat1]
for row, col in product(range(dim), range(dim)):
row_nd = self.graph.ord_nodes[row]
col_nd = self.graph.ord_nodes[col]
symbolic = (row_nd in self.hidden_nds) or \
col_nd in self.hidden_nds
if not symbolic:
for i in range(3):
self.cov_mat_list[i][row, col] = \
cov_mat_list_nm[i][row, col]
def calculate_gains(self):
"""
This method creates an instance of FBackGainsCalculator and asks it
to fill self.alpha_list and self.beta_list.
Returns
-------
None
"""
dim = self.graph.num_nds
calc = FBackGainsCalculator(self.graph, delta=self.delta)
if self.solve_symbolically:
cov_mat0 = cov_sb_mat(dim, time=self.time)
cov2times = cov2times_sb_mat(dim, time=self.time)
cov_mat1 = cov_sb_mat(dim, time=self.time + 1)
cov_mat_list_in = [cov_mat0, cov2times, cov_mat1]
else:
cov_mat_list_in = self.cov_mat_list
calc.calculate_gains(cov_mat_list_in=cov_mat_list_in,
mat_K=None,
time=self.time)
self.alpha_list = calc.alpha_list
self.beta_list = calc.beta_list
def fix_greek_list(self, name,
greek_list, greek_mat_estimate, greek_cum_err):
"""
This method modifies the list "greek_list" (which must be either an
"alpha_list" or a "beta_list"). For "greek_list": (1) it changes the
constraint items (2) it inserts numerical values if
solve_symbolically=True. It also calculates from "greek_list",
the numpy matrix "greek_mat_estimate" and the float "greek_cum_err".
Parameters
----------
name: str
either "alpha" or "beta"
greek_list: list[sp.Eq]
either "alpha_list" or "beta_list"
greek_mat_estimate: np.array
either "alpha_mat_estimate" or "beta_mat_estimate"
greek_cum_err: float
either "alpha_cum_err" or "beta_cum_err"
Returns
-------
None
"""
dim = self.graph.num_nds
len0 = len(name)
for i in range(len(greek_list)):
eq = greek_list[i]
str0 = str(eq.args[0])
# last 2 terms in split("_")
if str0[0:len0] != name:
# print("hhgd", str0.split("_")[-2:])
row_str, col_str = str0.split("_")[-2:]
str1 = "err" + "_" + row_str + "_" + col_str
eq = sp.Eq(sp.Symbol(str1),
eq.args[0] - eq.args[1])
for row, col in product(range(dim), range(dim)):
row_nd = self.graph.ord_nodes[row]
col_nd = self.graph.ord_nodes[col]
symbolic = (row_nd in self.hidden_nds) or\
(col_nd in self.hidden_nds)
if not symbolic:
sb_str = sb_cov_str(row, col, time=self.time)
eq = eq.subs(sb_str, self.cov_mat_list[0][row, col])
sb_str = sb_cov2times_str(row, col, time=self.time)
eq = eq.subs(sb_str, self.cov_mat_list[1][row, col])
sb_str = sb_cov_str(row, col, time=self.time+1)
eq = eq.subs(sb_str, self.cov_mat_list[2][row, col])
greek_list[i] = eq
str1 = str(eq.args[1])
try:
xx = float(str1)
except ValueError:
xx = np.nan
if str0[0:len0] == name:
row_str, col_str = str0[len0+1:len(str0)].split("_L_")
row, col = int(row_str), int(col_str)
greek_mat_estimate[row, col] = xx
else:
greek_cum_err += abs(xx)
@staticmethod
def get_greek_list_comments(name,
greek_list,
true_greek_mat):
"""
This method returns a list[str] of the same length as "greek_list".
The returned list will be used as comments, to be printed to the
right of each entry, when the entries of "greek_list" are printed.
Parameters
----------
name: str
either "alpha" or "beta"
greek_list: list[sp.Eq]
either "alpha_list" or "beta_list"
true_greek_mat: np.array
the alpha (or beta) matrix used to calculate the synthetic data.
Returns
-------
list[str]
"""
comments = []
len0 = len(name)
for i in range(len(greek_list)):
str0 = str(greek_list[i].args[0])
if str0[0:len0] == name:
row_str, col_str = str0[len0 + 1:].split("_L_")
row, col = int(row_str), int(col_str)
comments.append("(true= " +
("%.6f" % true_greek_mat[row, col]) + ")")
else:
comments.append("")
return comments
def fix_alpha_list(self):
"""
This method modifies self.alpha_list by calling self.fix_greek_list()
Returns
-------
None
"""
self.fix_greek_list("alpha",
self.alpha_list,
self.alpha_mat_estimate,
self.alpha_cum_err)
def fix_beta_list(self):
"""
This method modifies self.beta_list by calling self.fix_greek_list()
Returns
-------
None
"""
self.fix_greek_list("beta",
self.beta_list,
self.beta_mat_estimate,
self.beta_cum_err)
def get_alpha_list_comments(self, true_alpha_mat):
"""
This method returns a list of comments about self.alpha_list. To do
this, it calls self.get_greek_list_comments().
Parameters
----------
true_alpha_mat: np.array
Returns
-------
list[str]
"""
return FBackGainsEstimator.get_greek_list_comments("alpha",
self.alpha_list,
true_alpha_mat)
def get_beta_list_comments(self, true_beta_mat):
"""
This method returns a list of comments about self.beta_list. To do
this, it calls self.get_greek_list_comments().
Parameters
----------
true_beta_mat: np.array
Returns
-------
list[str]
"""
return FBackGainsEstimator.get_greek_list_comments("beta",
self.beta_list,
true_beta_mat)
def print_alpha_list(self, true_alpha_mat=None, verbose=False):
"""
This method prints the info in self.alpha_list. It does this by
calling latexify.print_list_sb()
Parameters
----------
true_alpha_mat: np.array
verbose: bool
Returns
-------
sp.Symbol
"""
comments = self.get_alpha_list_comments(true_alpha_mat)
return print_list_sb(self.alpha_list, self.graph,
verbose=verbose, time=self.time,
comment_list=comments, rounded=True)
def print_beta_list(self, true_beta_mat=None, verbose=False):
"""
This method prints the info in self.beta_list. It does this by
calling latexify.print_list_sb()
Parameters
----------
true_beta_mat: np.array
verbose: bool
Returns
-------
sp.Symbol
"""
comments = self.get_beta_list_comments(true_beta_mat)
return print_list_sb(self.beta_list, self.graph,
verbose=verbose, time=self.time,
comment_list=comments, rounded=True)
if __name__ == "__main__":
def main():
path = 'dot_atlas/fback-2node.dot'
graph = FBackGraph(path)
dim = graph.num_nds
mean_eps = [0]*dim
sig_eps = [.001] * dim
n_max = 2
alpha_bound = 10
beta_bound = 1
dmaker = FBackRandomDataMaker(n_max, graph,
mean_eps=mean_eps,
sig_eps=sig_eps,
alpha_bound=alpha_bound,
beta_bound=beta_bound)
num_rows = 100
data_path = "test_data.csv"
dmaker.write_dataset_csv(num_rows, data_path)
df = pd.read_csv(data_path)
for solve_symbolically in [False, True]:
print("************** solve_symbolically=", solve_symbolically)
time = 1
gest = FBackGainsEstimator(time, graph, df,
solve_symbolically=solve_symbolically)
gest.print_alpha_list(true_alpha_mat=dmaker.alpha_mat,
verbose=True)
print("alpha_mat_estimate=\n", gest.alpha_mat_estimate)
print("alpha_cum_err=", gest.alpha_cum_err)
gest.print_beta_list(true_beta_mat=dmaker.beta_mat,
verbose=True)
print("beta_mat_estimate=\n", gest.beta_mat_estimate)
print("beta_cum_err=", gest.beta_cum_err)
main()