pqp.identification package¶
Submodules¶
pqp.identification.estimands module¶
- class pqp.identification.estimands.ATE(outcome, treatment_condition, control_condition=None)¶
Bases:
AbstractCausalEstimand
Causal estimand for the average treatment effect
To define the average treatment effect, it’s necessary to specify what is meant by “treatment” and “control” in this context. You can do this by passing either a
dict
or a list ofStatisticalEvent
objects to each of thetreatment_condition
andcontrol_condition
arguments. If adict
is passed, the keys must beVariable
orstr
, and the values must not beVariable
. If alist
is passed, it must contain only instances ofStatisticalEvent
.Example
>>> # treatment condition is x = 1, control condition is x = 0 in both of these >>> ATE(outcome, treatment_condition={"x": 1}, control_condition={"x": 0}) >>> ATE(outcome, treatment_condition=[EqualityEvent("x", 1)], control_condition=[EqualityEvent("x", 0)]) >>> >>> # treatment condition is x = 1 and y = "red", control condition is x = 0 and y = "blue" >>> ATE(outcome, treatment_condition={"x": 1, y: "red"}, control_condition={"x": 0, y: "blue"})
- Parameters:
outcome (
Variable
) – the outcome variabletreatment_condition (
dict
orlist
) – the treatment conditioncontrol_condition (
dict
orlist
) – the control condition
- LITERAL_CLASS¶
alias of
NewLiteral
- expression()¶
Derive the expression for the causal estimand
- Returns:
the expression for the causal estimand
- Return type:
AbstractExpression
- literal()¶
The estimand represented as a function call, as an Expression
- Returns:
the literal for the causal estimand
- Return type:
AbstractExpression
- class pqp.identification.estimands.AbstractCausalEstimand¶
Bases:
object
Abstract base class for causal estimands
- display()¶
Display the causal estimand
- abstract expression()¶
Derive the expression for the causal estimand
- Returns:
the expression for the causal estimand
- Return type:
AbstractExpression
- abstract literal()¶
The estimand represented as a function call, as an Expression
- Returns:
the literal for the causal estimand
- Return type:
AbstractExpression
- class pqp.identification.estimands.CATE(outcome, treatment_condition, control_condition, subpopulation)¶
Bases:
ATE
Causal estimand for the conditional average treatment effect
To define the conditional average treatment effect, it’s necessary to specify what is meant by treatment and control in this context, and you need to specify the subpopulation in which to measure the effect. You can do this by passing either a
dict
or a list ofStatisticalEvent
objects to each of thetreatment_condition
andcontrol_condition
arguments. If adict
is passed, the keys must beVariable
orstring
, and the values must not beVariable
. If alist
is passed, it must contain only instances ofStatisticalEvent
.Example
>>> # treatment condition is x = 1, control condition is x = 0 in both of these >> # in both, we are measuring the effect in the subpopulation where z = 1 >>> CATE(outcome, treatment_condition={"x": 1}, control_condition={"x": 0}, subpopulation={"z": 1}) >>> CATE( ... outcome, ... treatment_condition=[EqualityEvent("x", 1)], ... control_condition=[EqualityEvent("x", 0)], ... subpopulation=[EqualityEvent("z", 1)] ... ) >>> >>> # treatment condition is x = 1 and y = "red", control condition is x = 0 and y = "blue" >>> # we are measuring the effect in the subpopulation where z = 1 >>> CATE( ... outcome, ... treatment_condition={"x": 1, y: "red"}, ... control_condition={"x": 0, y: "blue"}, ... subpopulation={"z": 1} ... )
- Parameters:
outcome (
Variable
) – the outcome variabletreatment_condition (
dict
orlist
) – the treatment conditioncontrol_condition (
dict
orlist
) – the control conditionsubpopulation (
dict
orlist
) – the subpopulation in which to measure the effect
- LITERAL_CLASS¶
alias of
NewLiteral
- expression()¶
Derive the expression for the causal estimand
- Returns:
the expression for the causal estimand
- Return type:
AbstractExpression
- literal()¶
The estimand represented as a function call, as an Expression
- Returns:
the literal for the causal estimand
- Return type:
AbstractExpression
- class pqp.identification.estimands.CausalEstimand(exp)¶
Bases:
AbstractCausalEstimand
Subclass of AbstractCausalEstimand which carries its expression as a literal
- LITERAL_CLASS¶
alias of
NewLiteral
- expression()¶
Derive the expression for the causal estimand
- Returns:
the expression for the causal estimand
- Return type:
AbstractExpression
- literal()¶
The estimand represented as a function call, as an Expression
- Returns:
the literal for the causal estimand
- Return type:
AbstractExpression
pqp.identification.graph module¶
- class pqp.identification.graph.BidirectedEdge(a, b)¶
Bases:
object
A bidirected edge between two variables, represents confounding in the causal model
- Parameters:
a (
Variable
) – one of the variablesb (
Variable
) – the other variable
- class pqp.identification.graph.DirectedEdge(start, end)¶
Bases:
object
A directed edge between two variables, represents a causal relationship
- Parameters:
start (
Variable
) – the start of the edgeend (
Variable
) – the end of the edge
- class pqp.identification.graph.Graph(edges=[])¶
Bases:
object
A causal graph
The best way to create a
Graph
is using the<=
and&
infix operators. When you use these operators betweenVariable
s, they create aDirectedEdge
orBidirectedEdge
respectively.- Example: The Front-Door Model
>>> x, y, m = make_vars("xym") >>> g = Graph([ ... m <= x, ... y <= m, ... y & x, ... ])
- Example: The Back-Door Model
>>> x, y, z = make_vars("xyz") >>> g = Graph([ ... y <= [x, z], ... x <= z, ... ])
You can use the
identify
method to identify a causal estimand. The estimand can either be passed as an expression or as an instance ofAbstractCausalEstimand
, such asATE
orCATE
.Example
>>> x = Variable("X") >>> y = Variable("Y") >>> g = Graph([ ... y <= x, ... ]) >>> g.identify(ATE([y], [x])) P(y | x)
- Parameters:
edges (
list
ofDirectedEdge
orBidirectedEdge
) – the edges in the graph
- add_edge(edge)¶
Adds an edge to the graph
- Parameters:
edge (
DirectedEdge
orBidirectedEdge
) – the edge to add
- add_edges(edges)¶
Add multiple edges to the graph
- Parameters:
edges (
list
ofDirectedEdge
orBidirectedEdge
) – the edges to add
- add_node(node)¶
Adds a node to the graph
- Parameters:
node (
Variable
) – the node to add
- add_nodes(nodes)¶
Adds multiple nodes to the graph
- Parameters:
nodes (
list
ofVariable
) – the nodes to add
- dfs(start, end)¶
Performs a depth-first search over directed edges in the graph, returning a generator over paths
- Parameters:
start (
Variable
) – the start of the searchend (
Variable
) – the end of the search
- Returns:
the path from start to end
- Return type:
Generator[
List[DirectedEdge]
]
- dir_edge_dict()¶
Returns a dict mapping each node to its children in the graph
- draw()¶
Draws the causal diagram using networkx (must be installed).
- identify(**kwargs)¶
Uses IDC to identify an arbitrary estimand
Finds the interventional distributions in a causal estimand and replaces them with equivalent conditional probability expressions using IDC.
- Parameters:
estimand (
Expression
) – the estimand to identify- Returns:
the identified estimand, containing no do-operators
- Return type:
AbstractExpression
- identify_ate(outcome, treatment_condition, control_condition)¶
Identifies the average treatment effect
- Parameters:
outcome (
Variable
) – the outcome variabletreatment_condition (
dict
) – the treatment conditioncontrol_condition (
dict
) – the control condition
- Returns:
the identified average treatment effect
- Return type:
AbstractExpression
- identify_cate(outcome, treatment_condition, control_condition, subpopulation)¶
Identifies the conditional average treatment effect
- Parameters:
outcome (
Variable
) – the outcome variabletreatment_condition (
dict
) – the treatment conditioncontrol_condition (
dict
) – the control conditionsubpopulation (
dict
) – the subpopulation condition
- Returns:
the identified conditional average treatment effect
- Return type:
AbstractExpression
- class pqp.identification.graph.IdentificationResult(operation, step)¶
Bases:
Result
Stores the result of identification
- identified_estimand¶
the identified causal estimand
- Type:
AbstractExpression
- class pqp.identification.graph.SearchNode(prev: Optional[ForwardRef('SearchNode')], graph_node: Any)¶
Bases:
object
- graph_node: Any¶
- prev: SearchNode | None¶
- unpack(_path=None)¶