Skip to content

Instantly share code, notes, and snippets.

@mschauer
Last active February 19, 2024 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mschauer/b8566ba25e336522ad7b27e98352e605 to your computer and use it in GitHub Desktop.
Save mschauer/b8566ba25e336522ad7b27e98352e605 to your computer and use it in GitHub Desktop.
Bayes ball
using CausalInference, Graphs
V = [:U, :T, :P, :O]
ι = Dict(v=>i for (i,v) in enumerate(V))
g = digraph([1=>3, 2=>3, 3=>4, 2=>4, 1=>4])
# Can estimate total effect T=>O without observing U?
u = ι[:T]
v = ι[:O]
∅ = Set{Int}()
observed = 2:4
C = collect(list_covariate_adjustment(g, u, v, ∅, observed))
∅ ∈ C
# No adjustment needed.
# What happens if we condition on P?
u = ι[:T]
C = [ι[:P]]
V2 = [:U, :U′, :T, :T′, :P, :P′, :O, :O′] # bayesball_graph has each vertex twice
g2 = CausalInference.bayesball_graph(g, u, C)
g2_sub, vmap = induced_subgraph(g2, findall(degree(g2) .> 0)) # drop degree 0 vertices
using GraphMakie, GLMakie, NetworkLayout
fig = Figure(resolution=(500, 500))
ax1 = Axis(fig[1,1])
hidespines!(ax1)
hidedecorations!(ax1)
graphplot!(ax1, g; layout=Stress(), arrow_shift = :end, ilabels=V, arrow_size=25, ilabels_fontsize = 30)
fig
ax2 = Axis(fig[1,2])
hidespines!(ax2)
hidedecorations!(ax2)
graphplot!(ax2, g2_sub; kwargs_pdag_graphmakie(g2_sub; ilabels=V2[vmap])...)
fig
@mschauer
Copy link
Author

Left: DAG. Right: When we condition on P, then a "Bayes ball" started in T can reach O directly, or via the opened collider path T-P-U-O. See https://twitter.com/MoritzSchauer/status/1758203552218919219

Screenshot 2024-02-19 at 15 16 38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment