-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtmiic.cpp
314 lines (304 loc) · 12.5 KB
/
tmiic.cpp
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
//*****************************************************************************
// Filename : tmiic.cpp Creation date: 07 may 2020
//
// Description: Store functions for temporal mode of miic (tmiic)
//
// Author : Franck SIMON
//*****************************************************************************
//=============================================================================
// INCLUDE SECTION
//=============================================================================
#include "orientation.h"
#include "tmiic.h"
//=============================================================================
// CONSTANTS
//=============================================================================
//=============================================================================
// NAMESPACES
//=============================================================================
namespace tmiic {
using std::string;
using namespace miic::structure;
using namespace miic::utility;
//-----------------------------------------------------------------------------
// getListLaggedEdges
//-----------------------------------------------------------------------------
// Description: in temporal mode, find the past lagged counterparts of an edge
// assuming stationarity
//
// Params:
// - Environment&: the environment structure
// - int: edge's first node index
// - int: edge's second node index
// Return:
// - std::vector< std::pair<int>, <int> > : list of lagged edges
//--------------------------------------------------------------------------------
std::vector< std::pair<int, int> > getListLaggedEdges
(Environment& environment, int node1_pos, int node2_pos) {
std::vector< std::pair<int, int> > list_ret;
if ( (node1_pos >= environment.n_nodes_not_lagged)
&& (node2_pos >= environment.n_nodes_not_lagged) )
//
// The edge is duplicated, this function deals only with the original ones
//
return (list_ret);
//
// If one of the variables is not lagged, the lag is not constant
//
int sav_lag = environment.nodes_lags[node1_pos] - environment.nodes_lags[node2_pos];
bool same_lag_needed = true;
if ( (node1_pos < environment.n_nodes_not_lagged)
&& (environment.list_n_layers[node1_pos] <= 1) )
same_lag_needed = false;
else if ( (node2_pos < environment.n_nodes_not_lagged)
&& (environment.list_n_layers[node2_pos] <= 1) )
same_lag_needed = false;
//
// Look for the same edge lagged over all layers of history
//
while (true) {
//
// We shift the nodes positions using pre-computed vector nodes_shifts
//
int node1_shift = environment.nodes_shifts[node1_pos];
int node2_shift = environment.nodes_shifts[node2_pos];
if ( (node1_shift <= 0) && (node2_shift <= 0) )
break;
node1_pos += node1_shift;
node2_pos += node2_shift;
//
// Ensure if both variable are lagged than we keep the same lag when duplicating
//
bool same_lag_impossible = false;
if (same_lag_needed) {
int new_lag = environment.nodes_lags[node1_pos] - environment.nodes_lags[node2_pos];
while (sav_lag != new_lag) {
if (sav_lag < new_lag) {
int node2_shift = environment.nodes_shifts[node2_pos];
if (node2_shift <= 0) {
same_lag_impossible = true;
break;
}
node2_pos += node2_shift;
}
else { // sav_lag > new_lag
int node1_shift = environment.nodes_shifts[node1_pos];
if (node1_shift <= 0) {
same_lag_impossible = true;
break;
}
node1_pos += node1_shift;
}
new_lag = environment.nodes_lags[node1_pos] - environment.nodes_lags[node2_pos];
}
}
if (same_lag_impossible)
break;
//
// A lagged edge has been found
//
list_ret.push_back (std::make_pair (node1_pos, node2_pos) );
}
return (list_ret);
}
//-----------------------------------------------------------------------------
// repeatEdgesOverHistory
//-----------------------------------------------------------------------------
// Description: Duplicates edges over the temporal graph
//
// Detail: For consistency and the orientation step when latent variable
// discovery is enabled, we duplicate the edges over the history, assuming
// stationarity, to improve the consistency assessment and the orientations
//
// Params:
// - Environment&: the environment structure
//-----------------------------------------------------------------------------
void repeatEdgesOverHistory (Environment& environment) {
//
// We iterate over computed edges to duplicate (if needed)
// the edges over the history
//
auto& edge_list = environment.connected_list;
std::vector<EdgeID>::size_type size = edge_list.size();
for (std::vector<EdgeID>::size_type i = 0; i < size; ++i) {
const Edge& edge_orig = environment.edges (edge_list[i].X, edge_list[i].Y);
std::vector< std::pair<int, int> > list_lagged = getListLaggedEdges
(environment, edge_list[i].X, edge_list[i].Y);
for (auto const& it_lagged : list_lagged) {
//
// Duplicate the edge info into environment.edges array
//
Edge& edge_to_modif = environment.edges (it_lagged.first, it_lagged.second);
edge_to_modif.status = edge_orig.status;
edge_to_modif.status_init = edge_orig.status_init;
edge_to_modif.status_prev = edge_orig.status_prev;
edge_to_modif.proba_head = edge_orig.proba_head;
Edge& edge_to_modif_inverse = environment.edges (it_lagged.second, it_lagged.first);
edge_to_modif_inverse.status = edge_orig.status;
edge_to_modif_inverse.status_init = edge_orig.status_init;
edge_to_modif_inverse.status_prev = edge_orig.status_prev;
edge_to_modif_inverse.proba_head = edge_orig.proba_head;
//
// Add the nodes into connected_list
//
environment.connected_list.emplace_back (it_lagged.first, it_lagged.second,
environment.edges (it_lagged.first, it_lagged.second) );
}
}
}
//-----------------------------------------------------------------------------
// completeOrientationUsingTime
//-----------------------------------------------------------------------------
// Description: Complete the orientations with the orientation of temporal
// edges that were not previously oriented
//
// Detail: completeOrientationUsingTime will look in the list of connected
// edges for the ones that have not been oriented using the unshielded triples,
// that are lagged and have a node on the layer 0.
// Edges matching these criteria will be oriented using time from the oldest
// node to the newest.
//
// N.B.: Edges not having a node on the layer 0 are edges "past only" and are a
// consequence of duplicating edges over history to maximize the number of
// unshielded triples, There is no interest to orient "past only" edges
// using time as they will be removed at the end of the orientation step.
//
// Params:
// - Environment&: the environment structure
// - std::vector<Triple>& : list of unshielded triples
//--------------------------------------------------------------------------------
void completeOrientationUsingTime (Environment& environment,
const std::vector<Triple>& triples)
{
// Tail probability to use for lagged edges differs if latent variables are
// authorized or not:
// - 0 if no latent var, we are sure that the oldest node is the cause
// - 0.5 with latent var as we can not be sure that the oldest node is the cause
//
double tail_proba = 0;
if (environment.latent_orientation)
tail_proba = 0.5;
//
// Loop over edges to find edges that were not considered when orienting with
// open triples but can be oriented using time
//
const auto& edge_list = environment.connected_list;
for (auto iter0 = begin(edge_list); iter0 != end(edge_list); ++iter0)
{
int posX = iter0->X, posY = iter0->Y;
//
// If edges has no node on the layer 0, it is a duplicated one => skip it
//
if ( ! ( (posX < environment.n_nodes_not_lagged)
|| (posY < environment.n_nodes_not_lagged) ) )
continue;
//
// If the edge is contemporaneous, no information from time => skip it
//
if (environment.nodes_lags[posX] == environment.nodes_lags[posY])
continue;
//
// If edge is in triple, head/tail probas have already been computed
// and applied in adjacency matrix
//
bool is_in_triple = false;
for (unsigned int i = 0; i < triples.size() ; i++)
if ( ( (triples[i][0] == posX) && (triples[i][1] == posY) )
|| ( (triples[i][0] == posY) && (triples[i][1] == posX) )
|| ( (triples[i][1] == posX) && (triples[i][2] == posY) )
|| ( (triples[i][1] == posY) && (triples[i][2] == posX) ) )
{
is_in_triple = true;
break;
}
if (is_in_triple)
continue;
//
// The edge was not in open triples, has a node on layer 0, and is not
// contemporaneous => we can and need to orient it using time
// As time goes from past to present: edge orientation is max lag -> min lag
//
if (environment.nodes_lags[posX] > environment.nodes_lags[posY])
{
if (environment.is_contextual[posX])
miic::reconstruction::updateAdj(environment, posX, posY, 0, 1);
else
miic::reconstruction::updateAdj(environment, posX, posY, tail_proba, 1);
}
else
{
if (environment.is_contextual[posY])
miic::reconstruction::updateAdj(environment, posX, posY, 1, 0);
else
miic::reconstruction::updateAdj(environment, posX, posY, 1, tail_proba);
}
}
}
//-----------------------------------------------------------------------------
// dropPastEdges
//-----------------------------------------------------------------------------
// Description:
// Drop past edges (the edges having no node of the final time step)
//
// Detail: for consistency assessment or orientation step with latent variable
// discovery is enabled, we duplicated edges over history. Here, we ensure that
// the edges are restored to their state prior before duplication.
//
// Params:
// - Environment&: the environment structure
//--------------------------------------------------------------------------------
void dropPastEdges (Environment& environment) {
//
// We iterate over computed edges to find edges previously duplicated using stationary.
// All the edges duplicated are disconnected and removed from environment.connected_list
//
auto it = begin(environment.connected_list);
while ( it != end(environment.connected_list) ) {
//
// When the two nodes are not on the layer 0, the edge is removed
//
if ( (it->X >= environment.n_nodes_not_lagged)
&& (it->Y >= environment.n_nodes_not_lagged) )
it = environment.connected_list.erase (it);
//
// When one of the nodes is not lagged (i.e. contextual)
// and the other not on the layer 0, the edge is removed
//
else if ( (it->X < environment.n_nodes_not_lagged)
&& (environment.list_n_layers[it->X] <= 1)
&& (it->Y >= environment.n_nodes_not_lagged) )
it = environment.connected_list.erase (it);
else if ( (it->Y < environment.n_nodes_not_lagged)
&& (environment.list_n_layers[it->Y] <= 1)
&& (it->X >= environment.n_nodes_not_lagged) )
it = environment.connected_list.erase (it);
else
it++;
}
//
// We remove from the edges all those having pos > n_nodes_not_lagged
//
for (int node1_pos = environment.n_nodes_not_lagged; node1_pos < environment.n_nodes; ++node1_pos)
for (int node2_pos = environment.n_nodes_not_lagged; node2_pos < environment.n_nodes; ++node2_pos) {
environment.edges(node1_pos,node2_pos).status = 0;
environment.edges(node1_pos,node2_pos).status_init = 0;
environment.edges(node1_pos,node2_pos).status_prev = 0;
environment.edges(node1_pos,node2_pos).proba_head = -1;
}
//
// In addition, remove lagged edges added on non lagged variable (i.e.:contextual)
//
for (int i = 0; i < environment.n_nodes_not_lagged; i++)
if (environment.is_contextual[i])
for (int j = environment.n_nodes_not_lagged; j < environment.n_nodes; j++) {
environment.edges(i, j).status = 0;
environment.edges(i, j).status_init = 0;
environment.edges(i, j).status_prev = 0;
environment.edges(i, j).proba_head = -1;
environment.edges(j, i).status = 0;
environment.edges(j, i).status_init = 0;
environment.edges(j, i).status_prev = 0;
environment.edges(j, i).proba_head = -1;
}
}
} // namespace tmiic