Provides the ExplicitTPM and related classes.

class pyphi.tpm.ProxyMetaclass(type_name, bases, dct)

A metaclass to create wrappers for the TPM array’s special attributes.

The CPython interpreter resolves double-underscore attributes (e.g., the method definitions of mathematical operators) by looking up in the class’ static methods, not in the instance methods. This makes it impossible to intercept calls to them when an instance’s __getattr__() is implicitly invoked, which in turn means there are only two options to wrap the special methods of the array inside our custom objects (in order to perform arithmetic operations with the TPM while also casting the result to our custom class type):

  1. Manually “overload” all the necessary methods.

  2. Use this metaclass to introspect the underlying array and automatically overload methods in our custom TPM class definition.

class pyphi.tpm.Wrapper

Proxy to the array inside PyPhi’s custom TPM class.

class pyphi.tpm.ExplicitTPM(tpm, validate=False)

An explicit network TPM in multidimensional form.

property tpm

Return the underlying tpm object.


Validate this TPM.

property number_of_units

Return the current TPM re-represented in multidimensional state-by-node form.

See the PyPhi documentation on Transition probability matrix conventions for more information.


The TPM in multidimensional state-by-node format.

Return type:



Validate that the TPM is conditionally independent.

condition_tpm(condition: Mapping[int, int])

Return a TPM conditioned on the given fixed node indices, whose states are fixed according to the given state-tuple.

The dimensions of the new TPM that correspond to the fixed nodes are collapsed onto their state, making those dimensions singletons suitable for broadcasting. The number of dimensions of the conditioned TPM will be the same as the unconditioned TPM.


condition (dict[int, int]) – A mapping from node indices to the state to condition on for that node.


A conditioned TPM with the same number of dimensions, with singleton dimensions for nodes in a fixed state.

Return type:



Marginalize out nodes from this TPM.


node_indices (list[int]) – The indices of nodes to be marginalized out.


A TPM with the same number of dimensions, with the nodes marginalized out.

Return type:



Return whether the TPM is deterministic.


Return True if tpm is in state-by-state form, otherwise False.

subtpm(fixed_nodes, state)

Return the TPM for a subset of nodes, conditioned on other nodes.

  • fixed_nodes (tuple[int]) – The nodes to select.

  • state (tuple[int]) – The state of the fixed nodes.


The TPM of just the subsystem of the free nodes.

Return type:



>>> from pyphi import examples
>>> # Get the TPM for nodes only 1 and 2, conditioned on node 0 = OFF
>>> examples.grid3_network().tpm.subtpm((0,), (0,))
ExplicitTPM([[[[0.02931223 0.04742587]
   [0.07585818 0.88079708]]

  [[0.81757448 0.11920292]
   [0.92414182 0.95257413]]]])

Broadcast a state-by-node TPM so that singleton dimensions are expanded over the full network.

infer_edge(a, b, contexts)

Infer the presence or absence of an edge from node A to node B.

Let \(S\) be the set of all nodes in a network. Let \(A' = S - \{A\}\). We call the state of \(A'\) the context \(C\) of \(A\). There is an edge from \(A\) to \(B\) if there exists any context \(C(A)\) such that \(\Pr(B \mid C(A), A = 0) \neq \Pr(B \mid C(A), A = 1)\).

  • a (int) – The index of the putative source node.

  • b (int) – The index of the putative sink node.

  • contexts (tuple[tuple[int]]) – The tuple of states of a


True if the edge \(A \rightarrow B\) exists, False otherwise.

Return type:



Infer the connectivity matrix associated with a state-by-node TPM in multidimensional form.


Return the indices of nodes in the TPM.

array_equal(o: object)

Return whether this TPM equals the other object.

Two TPMs are equal if they are instances of the ExplicitTPM class and their numpy arrays are equal.


Reconstitute the TPM of a subsystem using the individual node TPMs.

pyphi.tpm.probability_of_current_state(sbn_tpm, current_state)

Return the probability of the current state as a distribution over previous states.

  • sbn_tpm (ExplicitTPM) – State-by-node TPM.

  • current_state (tuple[int]) – The current state.

pyphi.tpm.backward_tpm(forward_tpm: ExplicitTPM, current_state: tuple[int], system_indices: Iterable[int], remove_background: bool = False) ExplicitTPM

Compute the backward TPM for a given network state.