pqp.symbols package¶
Submodules¶
pqp.symbols.abstract_math module¶
pqp.symbols.event module¶
- class pqp.symbols.event.EqualityEvent(var, val)¶
Bases:
StatisticalEvent
Represents setting a variable to a value
- val¶
The value to set the variable to (cannot be a Variable)
- Type:
object
- get_var()¶
Returns the variable which is constrained by the event
- to_latex()¶
- class pqp.symbols.event.Event¶
Bases:
AbstractMath
,ABC
Abstract base class for events
- abstract property get_var¶
Returns the variable which is constrained by the event
- class pqp.symbols.event.InterventionEvent(v)¶
Bases:
Event
Represents intervening on a variable
- assign(val)¶
- get_var()¶
Returns the variable which is constrained by the event
- to_latex()¶
- class pqp.symbols.event.StatisticalEvent¶
Bases:
Event
Abstract base class for events which are statistical events
StatisticalEvents represent constraints on the possible values of a Variable, including taking on a specific value. They go in probability expressions.
- pqp.symbols.event.do(v)¶
Convenience function for creating an InterventionEvent
pqp.symbols.p module¶
- class pqp.symbols.p.P(vars, given=None)¶
Bases:
AbstractExpression
Expression representing a probability or conditional probability
- Parameters:
- Raises:
ValueError – if a variable is repeated in vars or given
TypeError – if vars or given are not a list of Variable or Event
- copy()¶
Returns a copy of the expression (variables are not copied).
- get_conditioned_vars()¶
Get the set of variables which are conditioned on in this P
- Raises:
TypeError – if self.given contains an element which is not Variable, EqualityEvent or InterventionEvent
- Returns:
dict of Variable -> value
- get_intervened_vars()¶
Get the set of variables which are intervened on in this P
- Returns:
dict of Variable -> value
- Raises:
TypeError – if self.given contains an InterventionEvent which contains neither Variable nor EqualityEvent
- get_vars()¶
Get the set of variables to the left of the conditioning bar in this P
- Returns:
set of Variable
- Raises:
TypeError – if self.vars contains an element which is neither Variable nor EqualityEvent
- r_adapt_map(func)¶
Recursive map where the func decides callables used to transform children
DO NOT USE! This is a powerful and necessary method but it results in almost unreadable code.
So, func takes an AbstractExpression and needs to return a tuple of two things. First, a function A which maps from AbstractExpression to AbstractExpression. Second, a function B of the same time as func.
At each level of recursion, func will be called on an expression E to get A and B. This method, r_adapt_map will then be called on the children of E with B as input (if B is None, recursion terminates). An expression of the same type as E is then constructed with the results of the recursive calls using B, and A is applied to the result.
- Parameters:
func (function) – A function that takes an AbstractExpression and returns two functions (see desc)
- r_map(func)¶
Recursively maps a function over the expression tree
- sorted()¶
Returns a sorted copy of an expression for structural comparison.
- syntactic_eq(other)¶
Test whether two Expressions are syntactically identical (structural compare without sorting first)
- to_latex()¶
- unassigned = <object object>¶
pqp.symbols.parse module¶
- pqp.symbols.parse.parse_json(exp)¶
Parses JSON returned from backend.id() into an Expression object.
- Parameters:
exp (str or dict) – The JSON string or parsed JSON object.
- Returns:
The parsed expression.
- Return type:
Expression
pqp.symbols.relation module¶
- class pqp.symbols.relation.AbstractExpression¶
Bases:
AbstractMath
,ABC
Abstract class defining needed recursive operations for relations
- assign(var, val=None)¶
Assigns a value to a variable in an expression.
Note that assignment will not propogate downwards through a sum over a variable.
- Parameters:
var (Variable) – The variable to assign to
val (AbstractMath) – The value to assign, if this is P.unassigned, the expression is copied and returned
- Returns:
a new expression where the assignment has occured
- Return type:
- abstract copy()¶
Returns a copy of the expression (variables are not copied).
- free_variables()¶
Returns the set of free variables in an expression.
- intervene(var)¶
Intervenes on a variable in an expression.
Note that intervention will not propogate downwards through a sum over a variable.
- Parameters:
var (Variable or list) – The variable(s) to intervene on
- Returns:
a new expression where the intervention has occured
- Return type:
- abstract r_adapt_map(func)¶
Recursive map where the func decides callables used to transform children
DO NOT USE! This is a powerful and necessary method but it results in almost unreadable code.
So, func takes an AbstractExpression and needs to return a tuple of two things. First, a function A which maps from AbstractExpression to AbstractExpression. Second, a function B of the same time as func.
At each level of recursion, func will be called on an expression E to get A and B. This method, r_adapt_map will then be called on the children of E with B as input (if B is None, recursion terminates). An expression of the same type as E is then constructed with the results of the recursive calls using B, and A is applied to the result.
- Parameters:
func (function) – A function that takes an AbstractExpression and returns two functions (see desc)
- abstract r_map(func)¶
Recursively maps a function over the expression tree
- abstract sorted()¶
Returns a sorted copy of an expression for structural comparison.
- abstract syntactic_eq(other)¶
Test whether two Expressions are syntactically identical (structural compare without sorting first)
- class pqp.symbols.relation.Difference(a, b)¶
Bases:
AbstractExpression
Represents the difference between two expressions
Represents the expression $a - b$. Can be creating using the infix operator ‘-‘.
- Parameters:
a (AbstractExpression) – The first expression
b (AbstractExpression) – The second expression
- copy()¶
Returns a copy of the expression (variables are not copied).
- r_adapt_map(func)¶
Recursive map where the func decides callables used to transform children
DO NOT USE! This is a powerful and necessary method but it results in almost unreadable code.
So, func takes an AbstractExpression and needs to return a tuple of two things. First, a function A which maps from AbstractExpression to AbstractExpression. Second, a function B of the same time as func.
At each level of recursion, func will be called on an expression E to get A and B. This method, r_adapt_map will then be called on the children of E with B as input (if B is None, recursion terminates). An expression of the same type as E is then constructed with the results of the recursive calls using B, and A is applied to the result.
- Parameters:
func (function) – A function that takes an AbstractExpression and returns two functions (see desc)
- r_map(func)¶
Recursively maps a function over the expression tree
- sorted()¶
Returns a sorted copy of an expression for structural comparison.
- syntactic_eq(other)¶
Test whether two Expressions are syntactically identical (structural compare without sorting first)
- to_latex()¶
- class pqp.symbols.relation.Expectation(sub, expr)¶
Bases:
AbstractExpression
,_NamespaceModifier
Represents an expectation or a random variable
Equivalent to $sum_{domain(sub)} sub * expr$
- Parameters:
sub (Variable) – The variable to sum over
expr (AbstractExpression) – The expression to sum
- copy()¶
Returns a copy of the expression (variables are not copied).
- r_adapt_map(func)¶
Recursive map where the func decides callables used to transform children
DO NOT USE! This is a powerful and necessary method but it results in almost unreadable code.
So, func takes an AbstractExpression and needs to return a tuple of two things. First, a function A which maps from AbstractExpression to AbstractExpression. Second, a function B of the same time as func.
At each level of recursion, func will be called on an expression E to get A and B. This method, r_adapt_map will then be called on the children of E with B as input (if B is None, recursion terminates). An expression of the same type as E is then constructed with the results of the recursive calls using B, and A is applied to the result.
- Parameters:
func (function) – A function that takes an AbstractExpression and returns two functions (see desc)
- r_map(func)¶
Recursively maps a function over the expression tree
- sorted()¶
Returns a sorted copy of an expression for structural comparison.
- syntactic_eq(other)¶
Test whether two Expressions are syntactically identical (structural compare without sorting first)
- to_latex()¶
- class pqp.symbols.relation.Hedge¶
Bases:
AbstractExpression
Represents a failure to identify the query
- copy()¶
Returns a copy of the expression (variables are not copied).
- r_adapt_map(func)¶
Recursive map where the func decides callables used to transform children
DO NOT USE! This is a powerful and necessary method but it results in almost unreadable code.
So, func takes an AbstractExpression and needs to return a tuple of two things. First, a function A which maps from AbstractExpression to AbstractExpression. Second, a function B of the same time as func.
At each level of recursion, func will be called on an expression E to get A and B. This method, r_adapt_map will then be called on the children of E with B as input (if B is None, recursion terminates). An expression of the same type as E is then constructed with the results of the recursive calls using B, and A is applied to the result.
- Parameters:
func (function) – A function that takes an AbstractExpression and returns two functions (see desc)
- r_map(func)¶
Recursively maps a function over the expression tree
- sorted()¶
Returns a sorted copy of an expression for structural comparison.
- syntactic_eq(other)¶
Test whether two Expressions are syntactically identical (structural compare without sorting first)
- to_latex()¶
- class pqp.symbols.relation.Marginal(sub, expr)¶
Bases:
AbstractExpression
,_NamespaceModifier
Represents a sum
- Parameters:
sub (list of Variable) – The variables to sum over
expr (AbstractExpression) – The expression to sum
- copy()¶
Returns a copy of the expression (variables are not copied).
- r_adapt_map(func)¶
Recursive map where the func decides callables used to transform children
DO NOT USE! This is a powerful and necessary method but it results in almost unreadable code.
So, func takes an AbstractExpression and needs to return a tuple of two things. First, a function A which maps from AbstractExpression to AbstractExpression. Second, a function B of the same time as func.
At each level of recursion, func will be called on an expression E to get A and B. This method, r_adapt_map will then be called on the children of E with B as input (if B is None, recursion terminates). An expression of the same type as E is then constructed with the results of the recursive calls using B, and A is applied to the result.
- Parameters:
func (function) – A function that takes an AbstractExpression and returns two functions (see desc)
- r_map(func)¶
Recursively maps a function over the expression tree
- sorted()¶
Returns a sorted copy of an expression for structural comparison.
- syntactic_eq(other)¶
Test whether two Expressions are syntactically identical (structural compare without sorting first)
- to_latex()¶
- class pqp.symbols.relation.Product(expr)¶
Bases:
AbstractExpression
Represents a product of expressions
- Parameters:
expr (list of AbstractExpression) – The expressions to multiply
- copy()¶
Returns a copy of the expression (variables are not copied).
- r_adapt_map(func)¶
Recursive map where the func decides callables used to transform children
DO NOT USE! This is a powerful and necessary method but it results in almost unreadable code.
So, func takes an AbstractExpression and needs to return a tuple of two things. First, a function A which maps from AbstractExpression to AbstractExpression. Second, a function B of the same time as func.
At each level of recursion, func will be called on an expression E to get A and B. This method, r_adapt_map will then be called on the children of E with B as input (if B is None, recursion terminates). An expression of the same type as E is then constructed with the results of the recursive calls using B, and A is applied to the result.
- Parameters:
func (function) – A function that takes an AbstractExpression and returns two functions (see desc)
- r_map(func)¶
Recursively maps a function over the expression tree
- sorted()¶
Returns a sorted copy of an expression for structural comparison.
- syntactic_eq(other)¶
Test whether two Expressions are syntactically identical (structural compare without sorting first)
- to_latex()¶
- class pqp.symbols.relation.Quotient(numer, denom)¶
Bases:
AbstractExpression
Represents a quotient of expressions
- Parameters:
numer (AbstractExpression) – The numerator
denom (AbstractExpression) – The denominator
- copy()¶
Returns a copy of the expression (variables are not copied).
- r_adapt_map(func)¶
Recursive map where the func decides callables used to transform children
DO NOT USE! This is a powerful and necessary method but it results in almost unreadable code.
So, func takes an AbstractExpression and needs to return a tuple of two things. First, a function A which maps from AbstractExpression to AbstractExpression. Second, a function B of the same time as func.
At each level of recursion, func will be called on an expression E to get A and B. This method, r_adapt_map will then be called on the children of E with B as input (if B is None, recursion terminates). An expression of the same type as E is then constructed with the results of the recursive calls using B, and A is applied to the result.
- Parameters:
func (function) – A function that takes an AbstractExpression and returns two functions (see desc)
- r_map(func)¶
Recursively maps a function over the expression tree
- sorted()¶
Returns a sorted copy of an expression for structural comparison.
- syntactic_eq(other)¶
Test whether two Expressions are syntactically identical (structural compare without sorting first)
- to_latex()¶
- class pqp.symbols.relation.Value(var: Variable)¶
Bases:
AbstractExpression
- copy()¶
Returns a copy of the expression (variables are not copied).
- r_adapt_map(func)¶
Recursive map where the func decides callables used to transform children
DO NOT USE! This is a powerful and necessary method but it results in almost unreadable code.
So, func takes an AbstractExpression and needs to return a tuple of two things. First, a function A which maps from AbstractExpression to AbstractExpression. Second, a function B of the same time as func.
At each level of recursion, func will be called on an expression E to get A and B. This method, r_adapt_map will then be called on the children of E with B as input (if B is None, recursion terminates). An expression of the same type as E is then constructed with the results of the recursive calls using B, and A is applied to the result.
- Parameters:
func (function) – A function that takes an AbstractExpression and returns two functions (see desc)
- r_map()¶
Recursively maps a function over the expression tree
- sorted()¶
Returns a sorted copy of an expression for structural comparison.
- syntactic_eq(other)¶
Test whether two Expressions are syntactically identical (structural compare without sorting first)
- to_latex()¶
- pqp.symbols.relation.create_literal(name, arity=1, sep=', ', name_tex=None, sep_tex=None)¶
Class factory for creating expression literals
Suppose you have a function f that takes a single expression as an argument and you want to use this function in a symbolic expression.
>>> F = create_literal("f") >>> x = Variable("x") >>> F(x) f(x) >>> F(x).to_latex() $f(x)$
- Parameters:
name (str) – The name of the new class
arity (int, optional) – The arity of the new class. Defaults to 1. Zero is allowed.
sep (str, optional) – The separator to use when printing the expression. Defaults to “, “.
name_tex (str, optional) – The name of the new class in LaTeX. Defaults to name.
sep_tex (str, optional) – The separator to use when printing the expression in LaTeX. Defaults to sep.
- Returns:
A new subclass of AbstractExpression
pqp.symbols.variable module¶
- class pqp.symbols.variable.VarSet(variables, left='(', right=')')¶
Bases:
AbstractMath
- to_latex()¶
- class pqp.symbols.variable.Variable(name, domain=None)¶
Bases:
AbstractMath
A variable in the causal model
Dunder methods allow for convenient syntax for creating causal graphs.
Example
>>> x = Variable("x") >>> y = Variable("y") >>> x <= y DirectedEdge(Variable("x"), Variable("y")) >>> x & y BidirectedEdge(Variable("x"), Variable("y"))
- Parameters:
name (str) – the name of the variable
- to_latex()¶
- pqp.symbols.variable.make_vars(names)¶
Creates a list of variables from a list of names
Example
>>> make_vars(["x", "y", "z"]) [Variable("x"), Variable("y"), Variable("z")] >>> x, y, z = make_vars("xyz") [Variable("x"), Variable("y"), Variable("z")]