Skip to content

Commit 397447e

Browse files
authored
Merge pull request #140 from mschauer/bayesball
Add Bayesball graph
2 parents 48f9994 + 47a2822 commit 397447e

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

docs/src/library.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ graph_to_text
102102
kwargs_pdag_graphmakie
103103
CausalInference.combinations_without
104104
```
105+
## Bayes ball
106+
```@docs
107+
CausalInference.bayesball
108+
CausalInference.bayesball_graph
109+
```
105110

106111
## Adjustment
107112
```@docs
@@ -152,6 +157,12 @@ CausalInference.precompute_semidirected
152157
CausalInference.next_CPDAG
153158
```
154159

160+
## DAG Zig-Zag
161+
```@docs
162+
CausalInference.dagzigzag
163+
CausalInference.count_dag_moves
164+
```
165+
155166
## Exact
156167
```@docs
157168
CausalInference.exactscorebased

src/gensearch.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,30 @@ function descendants(g, X, veto = no_veto)
7676
return gensearch(g, X, (pe, ne, v, w) -> ne == RIGHT && !veto(pe, ne, v, w))
7777
end
7878

79+
"""
80+
bayesball(g, X, S = Set{eltype(g)}())
81+
82+
Return the set of vertices d-connected to the set of vertices X given set of vertices S in dag g.
83+
"""
7984
function bayesball(g, X, S = Set{eltype(g)}(), veto = no_veto)
8085
return gensearch(g, X, (pe, ne, v, w) -> !veto(pe, ne, v, w) && (pe == INIT || (v in S && pe == RIGHT && ne == LEFT) || (!(v in S) && !(pe == RIGHT && ne == LEFT))))
8186
end
8287

88+
89+
"""
90+
bayesball_graph(g, X, S = Set{eltype(g)}())
91+
92+
Return an undirected graph `b` containing edges for possible moves of the
93+
Bayes ball. Vertex `i` of `g` is vertex `2i-1` of `b`
94+
if entered forward, `2i` if entered backward.
95+
"""
96+
function bayesball_graph(g, X, S = Set{eltype(g)}())
97+
ι(e, i) = e == RIGHT ? 2i-1 : 2i
98+
edges = Pair{Int,Int}[]
99+
CausalInference.gensearch(g, X, (pe, ne, v, w) -> (pe == INIT || (v in S && pe == RIGHT && ne == LEFT) || (!(v in S) && !(pe == RIGHT && ne == LEFT))) && (push!(edges, ι(pe, v)=>ι(ne, w)); true))
100+
CausalInference.graph(edges, 2*nv(g))
101+
end
102+
83103
"""
84104
alt_test_dsep(g, X, Y, S, veto = no_veto)
85105

src/misc.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ end
1717
1818
Create `Graph` from edge-list.
1919
"""
20-
function graph(E)
21-
d = maximum(flatten(E))
20+
function graph(E, d = maximum(flatten(E)))
2221
g = Graph(d)
2322
for (i, j) in E
2423
add_edge!(g, i, j)

test/gensearch.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Graphs
22
using CausalInference
33
using Test
4+
using CausalInference: graph
45

56
# for some of the tests other results would not be wrong per se
67
# e.g. for the finding methods, other valid sets could be returned
@@ -93,4 +94,10 @@ end
9394
@test_throws ArgumentError dsep(g1, [1,2], [2,3], [4,5])
9495
@test_throws ArgumentError dsep(g1, [1,2], [3,4], [4,5])
9596
@test_throws ArgumentError dsep(g1, [1,2], [3,4], [5,1])
97+
end
98+
99+
@testset "bayesball_graph" begin
100+
g = digraph([1=>3, 2=>3, 3=>4, 2=>4, 1=>4])
101+
g2 = CausalInference.bayesball_graph(g, 2, [3])
102+
@test g2 == graph([(2, 5), (2, 7), (4, 5), (4, 7)], 8)
96103
end

0 commit comments

Comments
 (0)