Skip to content

Commit 03b67ba

Browse files
committed
Reorganize and update docs
1 parent df6bcd6 commit 03b67ba

File tree

4 files changed

+63
-28
lines changed

4 files changed

+63
-28
lines changed

src/y0/algorithm/identify/id_std.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ def identify(identification: Identification) -> Expression:
1414
:param identification: The identification tuple
1515
:returns: the expression corresponding to the identification
1616
:raises Unidentifiable: If no appropriate identification can be found
17+
18+
If you have an instance of a :class:`y0.graph.NxMixedGraph` and a
19+
query as an instance of a :class:`y0.dsl.Probability`, use the following:
20+
21+
.. code-block:: python
22+
23+
from y0.algorithm.identify import identify, Identification
24+
graph = ...
25+
query = ...
26+
identification = Identification.from_expression(graph=graph, query=query)
27+
estimand = identify(identification)
1728
"""
1829
graph = identification.graph
1930
treatments = identification.treatments

src/y0/controls.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Predicates for good, bad, and neutral controls."""
4+
5+
from .dsl import Probability, Variable
6+
from .graph import NxMixedGraph
7+
8+
__all__ = [
9+
"is_good_control",
10+
"is_bad_control",
11+
]
12+
13+
14+
def _control_precondition(graph: NxMixedGraph, query: Probability, variable: Variable):
15+
if missing := query.get_variables().difference(graph.nodes()):
16+
raise ValueError(f"Query variables missing: {missing}")
17+
if variable not in graph.nodes():
18+
raise ValueError(f"Test variable missing: {variable}")
19+
# TODO does this need to be extended to check that the
20+
# query and variable aren't counterfactual?
21+
22+
23+
def is_good_control(graph: NxMixedGraph, query: Probability, variable: Variable) -> bool:
24+
"""Return if the variable is a good control.
25+
26+
:param graph: An ADMG
27+
:param query: A query in the form of ``P(Y @ X)``
28+
:param variable: The variable to check
29+
:return: If the variable is a good control
30+
"""
31+
_control_precondition(graph, query, variable)
32+
33+
raise NotImplementedError
34+
35+
36+
def is_bad_control(graph: NxMixedGraph, query: Probability, variable: Variable) -> bool:
37+
"""Return if the variable is a bad control.
38+
39+
A bad control is a variable that does not appear in the estimand produced
40+
by :func:`y0.algorithm.identify.identify` when applied to a given graph
41+
and query.
42+
43+
:param graph: An ADMG
44+
:param query: A query in the form of ``P(Y @ X)``
45+
:param variable: The variable to check
46+
:return: If the variable is a bad control
47+
"""
48+
_control_precondition(graph, query, variable)
49+
50+
raise NotImplementedError

src/y0/predicates.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
"""Predicates for expressions."""
44

5-
from .dsl import Expression, Fraction, Probability, Product, Sum, Variable
6-
from .graph import NxMixedGraph
5+
from .dsl import Expression, Fraction, Probability, Product, Sum
76

87
__all__ = [
98
"has_markov_postcondition",
10-
"is_good_control",
11-
"is_bad_control",
129
]
1310

1411

@@ -31,26 +28,3 @@ def has_markov_postcondition(expression: Expression) -> bool:
3128
)
3229
else:
3330
raise TypeError
34-
35-
36-
def _control_precondition(graph: NxMixedGraph, query: Probability, variable: Variable):
37-
if missing := query.get_variables().difference(graph.nodes()):
38-
raise ValueError(f"Query variables missing: {missing}")
39-
if variable not in graph.nodes():
40-
raise ValueError(f"Test variable missing: {variable}")
41-
# TODO does this need to be extended to check that the
42-
# query and variable aren't counterfactual?
43-
44-
45-
def is_good_control(graph: NxMixedGraph, query: Probability, variable: Variable) -> bool:
46-
"""Return if the variable is a good control."""
47-
_control_precondition(graph, query, variable)
48-
49-
raise NotImplementedError
50-
51-
52-
def is_bad_control(graph: NxMixedGraph, query: Probability, variable: Variable) -> bool:
53-
"""Return if the variable is a good control."""
54-
_control_precondition(graph, query, variable)
55-
56-
raise NotImplementedError

tests/test_controls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import unittest
66

7+
from y0.controls import is_bad_control, is_good_control
78
from y0.dsl import U1, U2, M, P, Variable, X, Y, Z
89
from y0.graph import NxMixedGraph
9-
from y0.predicates import is_bad_control, is_good_control
1010

1111
U = Variable("U")
1212
query = P(Y @ X)

0 commit comments

Comments
 (0)