neurolang.probabilistic.cplogic.gm_provenance_solver module

class neurolang.probabilistic.cplogic.gm_provenance_solver.CPLogicGraphicalModelProvenanceSolver(graphical_model)

Bases: PatternWalker

Solver that constructs an RAP expression that calculates probabilities of sets of random variables in a graphical model.

Walking a probability calculation operation will result in the creation of an expression with nested provenance operations on provenance relations.

To calculate the actual probabilities, one must walk the resulting expression using a provenance solver of her choice.

Attributes:
patterns

Property holding an iterator of triplets (pattern, guard, action).

Methods

and_truth_conditional_probability(operation)

Construct the provenance expression that calculates the truth conditional probabilities of an AND random variable in the graphical model.

bernoulli_probability(operation)

Construct the provenance algebra set that represents the truth probabilities of a set of independent Bernoulli-distributed random variables.

match(expression)

Find the action for a given expression by going through the patterns.

nary_choice_probability(operation)

Construct the provenance relation that represents the truth probability of a specific value of a n-ary choice random variable in the graphical model.

pattern_match(pattern, expression)

Return True if pattern matches expression.

single_node_truth_probability(operation)

Calculate the marginal truth probability of any node representing a set of boolean random variables.

type

nary_choice_result

nary_choice_result_truth_conditional_probability

node_always_true

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

walk

and_truth_conditional_probability(operation)

Construct the provenance expression that calculates the truth conditional probabilities of an AND random variable in the graphical model.

Given an implication rule of the form

Z(x, y) <- Q(x, y), P(x, y)

The AND conditional probability distribution is obtained by applying a provenance natural join to the parent value relations

Q(x, y) P(x, y) Z(x, y)

_p_ | x | y _p_ | x | y _p_ | x | y ====|===|=== ============ ====|===|=== 1.0 | a | a 1.0 | a | a 1.0 | a | a 0.0 | a | b 1.0 | b | b

where the probabilities in the provenance column _p_ always are 1.0 or 0.0 because this is a deterministic CPD and all random variables that play a role here are boolean.

bernoulli_probability(operation)

Construct the provenance algebra set that represents the truth probabilities of a set of independent Bernoulli-distributed random variables.

nary_choice_probability(operation)

Construct the provenance relation that represents the truth probability of a specific value of a n-ary choice random variable in the graphical model.

Let the probabilistic choice for predicate symbol P be

P_i : p_1 v … v P_n : p_n :- T

where P_i = P(a_i1, …, a_im) and a_i1, …, a_im are constant terms.

The distribution of the choice variable associated with this probabilistic choice is represented by the relation

_p_ | x_1 | … | x_m ====|======|=====|===== p_1 | a_11 | … | a_1m … | … | … | … p_n | a_n1 | … | a_nm

Given a specific value of the choice variable

x_1 | … | x_m

======|=====|=====

a_i1 | … | a_im

this function returns the provenance relation

_p_ | x_1 | … | x_m ====|======|=====|===== p_i | a_i1 | … | a_im

representing that Pr[ c_P = i ] = p_i.

nary_choice_result(nv)
nary_choice_result_truth_conditional_probability(operation)
node_always_true(nv)
single_node_truth_probability(operation)

Calculate the marginal truth probability of any node representing a set of boolean random variables.

type = typing.Any
class neurolang.probabilistic.cplogic.gm_provenance_solver.NodeValue(node, value)

Bases: Definition

Methods

__call__(*args, **kwargs)

Call self as a function.

apply(*args)

Builds a new expression using a tuple of its parameters

type

unapply()

Returns a tuple of parameters used to build the expression.

cast

change_type

get_wrapped_attribute

type = typing.Any
class neurolang.probabilistic.cplogic.gm_provenance_solver.ProbabilityOperation(valued_node, condition_valued_nodes)

Bases: Definition

Operation representing a probability calculation on multiple sets of random variables (possibly with conditioning random variables).

Attributes:
valued_nodetuple of (PlateNode, Constant) tuples

Plate nodes containing the random variables whose probability is calculated for specific values. If TRUE is given, it is seen as all the random variables being True.

condition_valued_nodestuple of (PlateNode, Constant) tuples

Plate nodes containing the random variables conditioning the evaluated probabilities and their values.

Methods

__call__(*args, **kwargs)

Call self as a function.

apply(*args)

Builds a new expression using a tuple of its parameters

type

unapply()

Returns a tuple of parameters used to build the expression.

cast

change_type

get_wrapped_attribute

type = typing.Any
class neurolang.probabilistic.cplogic.gm_provenance_solver.ProvenanceExpressionTransformer(*args, **kwargs)

Bases: PatternWalker

Attributes:
patterns

Property holding an iterator of triplets (pattern, guard, action).

Methods

match(expression)

Find the action for a given expression by going through the patterns.

pattern_match(pattern, expression)

Return True if pattern matches expression.

type

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

ra_operation

walk

ra_operation(op)
type = typing.Any
class neurolang.probabilistic.cplogic.gm_provenance_solver.SelectionOutPusher(*args, **kwargs)

Bases: SelectionOutPusherMixin, ProvenanceExpressionTransformer, ExpressionWalker

Attributes:
patterns

Property holding an iterator of triplets (pattern, guard, action).

Methods

match(expression)

Find the action for a given expression by going through the patterns.

pattern_match(pattern, expression)

Return True if pattern matches expression.

type

nested_same_selection

nested_selections_not_same_tuple_symbol

nested_tuple_selection

njoin_left_selection

njoin_right_selection

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

process_expression

process_iterable_argument

ra_operation

selection_in_projection

swap_rename_selection

union_of_projection

union_of_selection_not_same_tuple_symbol

walk

type = typing.Any
class neurolang.probabilistic.cplogic.gm_provenance_solver.SelectionOutPusherMixin(*args, **kwargs)

Bases: PatternWalker

Attributes:
patterns

Property holding an iterator of triplets (pattern, guard, action).

Methods

match(expression)

Find the action for a given expression by going through the patterns.

pattern_match(pattern, expression)

Return True if pattern matches expression.

type

nested_same_selection

nested_selections_not_same_tuple_symbol

nested_tuple_selection

njoin_left_selection

njoin_right_selection

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

selection_in_projection

swap_rename_selection

union_of_projection

union_of_selection_not_same_tuple_symbol

walk

nested_same_selection(op)
nested_selections_not_same_tuple_symbol(op)
nested_tuple_selection(op)
njoin_left_selection(njoin)
njoin_right_selection(njoin)
selection_in_projection(proj)
swap_rename_selection(rename)
type = typing.Any
union_of_projection(union)
union_of_selection_not_same_tuple_symbol(op)
class neurolang.probabilistic.cplogic.gm_provenance_solver.TupleEqualSymbol(columns, tuple_symbol)

Bases: Definition

Methods

__call__(*args, **kwargs)

Call self as a function.

apply(*args)

Builds a new expression using a tuple of its parameters

type

unapply()

Returns a tuple of parameters used to build the expression.

cast

change_type

get_wrapped_attribute

type = typing.Any
class neurolang.probabilistic.cplogic.gm_provenance_solver.TupleSymbol(name)

Bases: Symbol

Methods

__call__(*args, **kwargs)

Call self as a function.

apply(*args)

Builds a new expression using a tuple of its parameters

type

unapply()

Returns a tuple of parameters used to build the expression.

cast

change_type

fresh

get_wrapped_attribute

type = typing.Any
class neurolang.probabilistic.cplogic.gm_provenance_solver.UnionOverTuples(relation, tuple_symbol)

Bases: RelationalAlgebraOperation

Methods

__call__(*args, **kwargs)

Call self as a function.

apply(*args)

Builds a new expression using a tuple of its parameters

type

unapply()

Returns a tuple of parameters used to build the expression.

cast

change_type

columns

get_wrapped_attribute

type = typing.Any
class neurolang.probabilistic.cplogic.gm_provenance_solver.UnionRemover(*args, **kwargs)

Bases: UnionRemoverMixin, ProvenanceExpressionTransformer, ExpressionWalker

Attributes:
patterns

Property holding an iterator of triplets (pattern, guard, action).

Methods

match(expression)

Find the action for a given expression by going through the patterns.

pattern_match(pattern, expression)

Return True if pattern matches expression.

type

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

process_expression

process_iterable_argument

ra_operation

union_of_selection_same_tuple_symbol

walk

type = typing.Any
class neurolang.probabilistic.cplogic.gm_provenance_solver.UnionRemoverMixin(*args, **kwargs)

Bases: PatternWalker

Attributes:
patterns

Property holding an iterator of triplets (pattern, guard, action).

Methods

match(expression)

Find the action for a given expression by going through the patterns.

pattern_match(pattern, expression)

Return True if pattern matches expression.

type

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

union_of_selection_same_tuple_symbol

walk

type = typing.Any
union_of_selection_same_tuple_symbol(union)
neurolang.probabilistic.cplogic.gm_provenance_solver.add_query_to_program(query, program)
neurolang.probabilistic.cplogic.gm_provenance_solver.make_conjunction_rule(conjunction)
neurolang.probabilistic.cplogic.gm_provenance_solver.ra_binary_to_nary(op)
neurolang.probabilistic.cplogic.gm_provenance_solver.solve_marg_query(query_predicate, evidence, cpl_program)
neurolang.probabilistic.cplogic.gm_provenance_solver.solve_succ_query(query_predicate, cpl_program)

Obtain the solution of a SUCC query on a CP-Logic program.

The SUCC query must take the form

SUCC[ P(x) ]

where

  • P(x) is a positive literal

  • P is any (probabilistic, intensional, extensional) predicate symbol

  • x is a universally-quantified variable

TODO: add support for SUCC[ P(a) ] where a is a constant term.

Solving a SUCC query is a multi-step process:

  1. First, the program is grounded. That is because the algorithm used to convert it to a graphical model in the next step only works on grounded CP-Logic programs. A grounding of a program is an ExpressionBlock where each expression is a Grounding expression. Each of this Grounding contains both the grounded expression itself and a relation representing all the replacements of the variables in the grounded expression. This way, we can represent many rules of the program using relations.

  2. Then, a graphical model representation of that grounded program is constructed, using the algorithm detailed in [Rc8709c4d3831-1]. In the resulting graphical model, each ground atom in the grounded program is associated with a random variable. The random variables are represented using relations for compactness and make it possible to leverage relational algebra operations to solve queries on the graphical model, as is detailed in the next step. The links between the random variables of the graphical model anad their CPDs are determined by the structure of the initial program. A view of the initial grounded expressions is maintained because it is necessary for solving queries.

  3. Finally, the query is solved from the joint probability distribution defined by the graphical model. Only the parts of the graphical model necessary to solve the query are calculated. The solver generates a provenance relational expression that is left un-evaluated (lazy calculation) and that is solved only at the end. This separation between the representation of the operations necessary to solve the query and the actual resolution makes it possible to choose between multiple solvers.

CP-Logic Theories by Leveraging Bayesian Network Learning Techniques,” n.d., 30.