Knowledge#

class causalbase.search.Knowledge(required=frozenset({}), forbidden=frozenset({}))[source]#

Adjacencies fixed in advance, by domain knowledge or by design.

Both kinds constrain whether two variables are adjacent, not the direction of the edge between them: a search over equivalence classes decides directions from the data, and a required edge keeps whichever direction the data favour.

Attributes:
requiredfrozenset of frozenset of int

Pairs that are always adjacent.

forbiddenfrozenset of frozenset of int

Pairs that are never adjacent.

classmethod from_pairs(required=(), forbidden=(), names=())[source]#

Build knowledge from pairs of variable names or column indices.

Parameters:
required, forbiddeniterable of pairs

Pairs of names in names or of column indices into it.

namessequence of str

The variables, in column order.

Raises:
ValueError

If a name is unknown, an index is not a column of names, a pair joins a variable to itself, or a pair is both required and forbidden.

may_add(x, y)[source]#

Return whether x and y may become adjacent.

may_remove(x, y)[source]#

Return whether the edge between x and y may be removed.

impose(dag)[source]#

Return dag without its forbidden adjacencies and with every required one.

A missing required edge is added in the direction of a topological order of dag, so the result stays acyclic.