diff --git a/examples/1-basic-backdoor/deconfound.py b/examples/1-basic-backdoor/deconfound.py new file mode 100644 index 0000000..ae88152 --- /dev/null +++ b/examples/1-basic-backdoor/deconfound.py @@ -0,0 +1,22 @@ +from pathlib import Path + +from do import API, Expression, Intervention, Outcome + +api = API() + +file = Path("pearl-3.4.yml") +model = api.instantiate_model(file) + +xj = Outcome("Xj", "xj") +xi = Intervention("Xi", "xi") +e = Expression(xj, [xi]) + +# basic inference won't work! +try: + api.probability(e, model) + print("This cannot happen!") + +except Exception: + e2 = Expression(xj) + result = api.treat(e2, [xi], model) + print(result) diff --git a/examples/1-basic-backdoor/pearl-3.4.yml b/examples/1-basic-backdoor/pearl-3.4.yml new file mode 100644 index 0000000..403319c --- /dev/null +++ b/examples/1-basic-backdoor/pearl-3.4.yml @@ -0,0 +1,116 @@ +name: 'Pearl: Figure 3.4' +endogenous: + X1: + outcomes: + - x1 + - ~x1 + parents: [] + table: [ + [x1, 0.4], + [~x1, 0.6] + ] + X2: + outcomes: + - x2 + - ~x2 + parents: [] + table: [ + [x2, 0.15], + [~x2, 0.85] + ] + X3: + outcomes: + - x3 + - ~x3 + parents: + - X1 + table: [ + [x3, x1, 0.1], + [x3, ~x1, 0.3], + [~x3, x1, 0.9], + [~x3, ~x1, 0.7] + ] + X4: + outcomes: + - x4 + - ~x4 + parents: + - X1 + - X2 + table: [ + [x4, x1, x2, 0.7], + [x4, x1, ~x2, 0.9], + [x4, ~x1, x2, 0.55], + [x4, ~x1, ~x2, 0.15], + [~x4, x1, x2, 0.3], + [~x4, x1, ~x2, 0.1], + [~x4, ~x1, x2, 0.45], + [~x4, ~x1, ~x2, 0.85] + ] + X5: + outcomes: + - x5 + - ~x5 + parents: + - X2 + table: [ + [x5, x2, 0.8], + [x5, ~x2, 0.25], + [~x5, x2, 0.2], + [~x5, ~x2, 0.75] + ] + X6: + outcomes: + - x6 + - ~x6 + parents: + - Xi + table: [ + [x6, xi, 0.9], + [x6, ~xi, 0.25], + [~x6, xi, 0.1], + [~x6, ~xi, 0.75] + ] + Xi: + outcomes: + - xi + - ~xi + parents: + - X3 + - X4 + table: [ + [xi, x3, x4, 0.5], + [xi, x3, ~x4, 0.65], + [xi, ~x3, x4, 0.1], + [xi, ~x3, ~x4, 0.25], + [~xi, x3, x4, 0.5], + [~xi, x3, ~x4, 0.35], + [~xi, ~x3, x4, 0.9], + [~xi, ~x3, ~x4, 0.75] + ] + Xj: + outcomes: + - xj + - ~xj + parents: + - X6 + - X4 + - X5 + table: [ + [xj, x6, x4, x5, 0.0], + [xj, x6, x4, ~x5, 0.25], + [xj, x6, ~x4, x5, 0.7], + [xj, x6, ~x4, ~x5, 0.45], + [xj, ~x6, x4, x5, 0.15], + [xj, ~x6, x4, ~x5, 0.8], + [xj, ~x6, ~x4, x5, 0.95], + [xj, ~x6, ~x4, ~x5, 0.05], + [~xj, x6, x4, x5, 1.0], + [~xj, x6, x4, ~x5, 0.75], + [~xj, x6, ~x4, x5, 0.3], + [~xj, x6, ~x4, ~x5, 0.55], + [~xj, ~x6, x4, x5, 0.85], + [~xj, ~x6, x4, ~x5, 0.2], + [~xj, ~x6, ~x4, x5, 0.05], + [~xj, ~x6, ~x4, ~x5, 0.95] + ]