do-calculus-backdoor-finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Copyright 2021 stormouse | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
# A practice on finding and blocking backdoor path in Pearlian do-calculus. | |
# Reference: https://www.bradyneal.com/causal-inference-course | |
# Note: the code is not production ready, non-performant and might even be INCORRECT. | |
from functools import reduce | |
import itertools | |
graph = { | |
"nodes": ['U', 'V', 'W', 'X', 'Z', 'Y'], | |
"edges": [ | |
('U', 'V'), | |
('U', 'X'), | |
('W', 'V'), | |
('W', 'Y'), | |
('V', 'X'), | |
('V', 'Y'), | |
('X', 'Z'), | |
('Z', 'Y'), | |
] | |
} | |
def list_paths(t, y, edges): | |
adj_list = {} | |
for a, b in edges: | |
adj_list.setdefault(a, {}) | |
adj_list.setdefault(b, {}) | |
adj_list[a][b] = 1 | |
adj_list[b][a] = -1 | |
results = [] | |
def traverse(cur, path): | |
if cur == y: | |
results.append(path.copy()) | |
else: | |
for next in adj_list[cur]: | |
if next not in path: | |
path.append(next) | |
traverse(next, path) | |
del path[-1] | |
init_path = [t] | |
traverse(t, init_path) | |
return results | |
def list_backdoor_paths(t, y, edges): | |
all_paths = list_paths(t, y, edges) | |
def is_backdoor(path): | |
return len(path) > 1 and path[0] == t and (path[1], path[0]) in edges | |
return filter(is_backdoor, all_paths) | |
def list_unblocked_backdoor_paths(t, y, edges, conditioned): | |
def has_descendent(conditioned): | |
descendents = set(t) | |
while True: | |
n = len(descendents) | |
descendents |= set(edge[1] for edge in edges if edge[0] in descendents) | |
if len(descendents) == n: | |
break | |
return any([node in descendents for node in conditioned if node != t]) | |
def is_blocked(path): | |
if len(path) < 3: | |
return False | |
for i in range(len(path) - 2): | |
a, b, c = path[i], path[i + 1], path[i + 2] | |
if (b, a) in edges and b in conditioned: | |
return True | |
elif (a, b) in edges and (c, b) in edges and b not in conditioned: | |
return True | |
return False | |
return list(filter(lambda p: not is_blocked(p), list_backdoor_paths(t, y, edges))) | |
def suggest_backdoor_closer(t, y, edges, unblocked_paths): | |
if len(unblocked_paths) == 0: | |
return [] | |
candidates = [set(i) for i in itertools.product(*[path[1:-1] for path in unblocked_paths])] | |
valid_candidates = list(filter(lambda x: len(list_unblocked_backdoor_paths(t, y, edges, x)) == 0, candidates)) | |
return None if len(valid_candidates) == 0 else valid_candidates | |
def print_path(path): | |
return ' ' + ' -> '.join([str(x) for x in path]) | |
print("Unblocked backdoor paths:") | |
print('\n'.join(print_path(p) for p in list_unblocked_backdoor_paths('X', 'Y', graph['edges'], set()))) | |
print("\nPossible variable sets to condition on to close backdoors:") | |
print(suggest_backdoor_closer('X', 'Y', graph['edges'], list_unblocked_backdoor_paths('X', 'Y', graph['edges'], set()))) | |
print("\nUnblocked backdoor paths conditioning on {{'U', 'V'}}:") | |
print(list_unblocked_backdoor_paths('X', 'Y', graph['edges'], set(['U', 'V']))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment