Graph types and Meek's rules¶
TimeSeriesGraph
dataclass
¶
TimeSeriesGraph(
n_vars: int,
tau_max: int,
lagged_edges: set[tuple[int, int, int]] = set(),
changing_modules: set[int] = set(),
var_names: list[str] | None = None,
)
Causal graph over a time-series with surrogate variable.
Edge encoding for the contemporaneous adjacency matrix contemp_adj:
contemp_adj[i, j] == 1andcontemp_adj[j, i] == 0:X_i -> X_j.contemp_adj[i, j] == 1andcontemp_adj[j, i] == 1:X_i -- X_j(undirected; orientation not yet determined).contemp_adj[i, j] == 0andcontemp_adj[j, i] == 0: no edge.
Lagged edges are stored as a set of (source, target, lag) tuples,
always interpreted as directed X_source[t-lag] -> X_target[t].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_vars
|
int
|
Number of observed variables (excluding the surrogate |
required |
tau_max
|
int
|
Maximum lag considered. |
required |
add_contemp_undirected ¶
Add an undirected contemporaneous edge X_i -- X_j.
remove_contemp_edge ¶
Remove a contemporaneous edge between X_i and X_j.
orient_contemp ¶
Orient a contemporaneous edge as X_src -> X_dst.
is_contemp_directed ¶
True iff there is a directed edge in either direction (not undirected).
contemp_neighbors ¶
All variables connected to i by any contemporaneous edge.
add_lagged_edge ¶
Record a lagged edge X_src[t-lag] -> X_dst[t].
lagged_parents ¶
Return {(source_var, lag)} for all lagged parents of X_j.
lagged_parents_union ¶
Union of lagged parents across multiple targets.
mark_changing ¶
Mark variable i as having a changing mechanism (C -> X_i).
directed_contemp_edges ¶
Yield (src, dst) for every contemporaneous edge oriented as src -> dst.
undirected_contemp_edges ¶
Yield (i, j) with i < j for each undirected contemporaneous edge.
to_networkx ¶
Export as a NetworkX directed graph.
Nodes are tuples (var_index, "now") for current-time variables,
(var_index, lag) for lagged variables (lag >= 1), and the string
"C" for the surrogate node when there are changing modules.
Undirected contemporaneous edges are emitted as a pair of directed edges.
apply_meek_rules ¶
Iteratively apply Meek's rules to a contemporaneous PDAG until a fixed point.
Modifies graph in place and returns it.
Rules implemented (over contemporaneous edges only):
- R1 — if
a -> bandb -- candaandcare not adjacent, orientb -> c(avoids new v-structure). - R2 — if
a -> b -> canda -- c, orienta -> c(avoids cycle). - R3 — if
a -- b,a -- c,a -- d,c -> b,d -> b, andcanddare not adjacent, orienta -> b. - R4 — if
a -- b,a -- c,c -> d,d -> b, andaanddare not adjacent, orienta -> b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
TimeSeriesGraph
|
Time-series graph with some contemporaneous edges already oriented. |
required |
max_iter
|
int
|
Safety bound on the fixed-point iteration. The algorithm always converges in finite steps for finite graphs; this is paranoia. |
100
|
Returns:
| Type | Description |
|---|---|
TimeSeriesGraph
|
The same graph, mutated in place. |