Skip to content

Probabilistic Boolean network

A probabilistic Boolean network.

  • Class: ProbabilisticBooleanNetworkSystem
  • Import: from skcomplex.systems.biology.systems_biology import ProbabilisticBooleanNetworkSystem
  • Field: biology/systems_biology
  • Topics: boolean network, gene regulation, discrete map, stochastic
  • References:
    • Shmulevich et al. 2002
    • Raman 2021

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.biology.systems_biology import ProbabilisticBooleanNetworkSystem

trajectory = ProbabilisticBooleanNetworkSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
network None
inputs ('x1', 'x2', 'x3')
active ('x1',)
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time discrete
Space none
Laws update
Randomness stochastic
Domains boolean, integer
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class ProbabilisticBooleanNetworkSystem(BaseSystem):
    """A probabilistic Boolean network.

    Its nodes draw one of their candidate functions at every step.

    Every node has candidate truth tables of the inputs, each with a probability, and at every step every node draws one
    of its tables independently and takes its value, all nodes together. The default network follows the three-gene
    example of Shmulevich et al., in which `x1` and `x3` have two candidate functions and `x2` one. From one initial
    state the network reaches different attractors in different runs. The system has the values `<node>.x` and the
    indices of the drawn functions `<node>.function`.

    Args:
        network:
            A mapping from node name to its candidate tables with their probabilities, or `None` for the three-gene
            example.

        inputs:
            The nodes every table reads, in the order of its bits, from the most significant.

        active:
            The nodes that are on at the start.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> from skcomplex.systems.biology.systems_biology import ProbabilisticBooleanNetworkSystem
        >>> ProbabilisticBooleanNetworkSystem().prepare().system_.interactions[0].probabilities
        (0.6, 0.4)
    """

    field = 'biology/systems_biology'
    topics = ('boolean network', 'gene regulation', 'discrete map', 'stochastic')
    references = ('Shmulevich et al. 2002', 'Raman 2021')
    until = 100
    dt = 1

    def __init__(
        self,
        network: Mapping[str, Candidates] | None = None,
        *,
        inputs: Sequence[str] = SHMULEVICH_INPUTS,
        active: Sequence[str] = ('x1',),
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.network = network
        self.inputs = inputs
        self.active = active

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of a probabilistic Boolean network.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        chosen = SHMULEVICH_NETWORK if self.network is None else self.network
        nodes = [ProbabilisticNodeElement(name, x=int(name in self.active)) for name in chosen]
        functions = [
            ProbabilisticFunctionInteraction(
                f'functions_{name}',
                target=name,
                inputs=tuple(self.inputs),
                tables=tuple(tuple(table) for table, _ in candidates),
                probabilities=tuple(probability for _, probability in candidates),
            )
            for name, candidates in chosen.items()
        ]
        self.system_ = System(self.label, elements=nodes, interactions=functions)

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

  • ProbabilisticNodeElement, from Element

Interactions:

  • ProbabilisticFunctionInteraction, from Interaction
class ProbabilisticNodeElement(Element):
    """Node of a probabilistic Boolean network, on or off.

    It holds the index of the function that set it last.

    Args:
        label:
            The label of the node.

        x:
            The initial value, 0 or 1.

        function:
            The index of the candidate function of the last update, 0 at the start.

    Examples:
        >>> from skcomplex.systems.biology.systems_biology import ProbabilisticNodeElement
        >>> ProbabilisticNodeElement('x1', x=1).x
        1
    """

    def __init__(
        self,
        label: str,
        *,
        x: Annotated[float, FiniteEnumeratedSet([0, 1])] = 0,
        function: Annotated[float, NN] = 0,
    ) -> None:
        super().__init__(label)
        self.x = x
        self.function = function

class ProbabilisticFunctionInteraction(Interaction):
    """Synchronous map of a node by one of several truth tables.

    The table is drawn with given probabilities at every step.

    Each table of `2^k` entries reads the `k` inputs as the bits of a binary number, from the most significant, and
    gives the next value `x` of the target. A second update of the same step records the index of the drawn table in
    the state `function` of the target, from the same draw.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the updated node, a `ProbabilisticNodeElement`.

        inputs:
            The paths of the input nodes, in the order of the bits of the tables.

        tables:
            The candidate truth tables.

        probabilities:
            The probability of every table.

    Raises:
        ValidationError:
            The tables and the probabilities differ in number, or a table does not have one entry per pattern.

    Examples:
        >>> from skcomplex.systems.biology.systems_biology import ProbabilisticFunctionInteraction
        >>> function = ProbabilisticFunctionInteraction(
        ...     'f', target='c', inputs=['a', 'b'], tables=((0, 0, 0, 1), (0, 1, 1, 1)), probabilities=(0.7, 0.3)
        ... )
        >>> function.probabilities
        (0.7, 0.3)
    """

    draws = True

    law_classes = (UpdateLaw,)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        inputs: tuple[Role, ...],
        tables: Sequence[Sequence[int]] = ((0,),),
        probabilities: Sequence[float] = (1.0,),
    ) -> None:
        super().__init__(label)
        self.target = target
        self.inputs = inputs
        self.tables = tables
        self.probabilities = probabilities

    def _get_pattern(self, inputs: Sequence[Expression], index: int) -> Expression:
        """Return the product that is 1 when the inputs spell an index.

        The inputs are read as bits from the most significant.
        """
        bits = [(index >> (len(inputs) - 1 - position)) & 1 for position in range(len(inputs))]
        return prod((value if bit else 1 - value for value, bit in zip(inputs, bits, strict=True)), start=SR(1))

    def _get_table_value(self, inputs: list[Expression], table: Sequence[int]) -> Expression:
        """Return the value of one truth table of the inputs."""
        return sum((self._get_pattern(inputs, index) for index, entry in enumerate(table) if entry), start=SR(0))

    def _check_arguments(self) -> None:
        """Raise an error for an argument the interaction cannot take.

        Raises:
            ValidationError:
                The tables and the probabilities differ in number, or a table does not have one entry per pattern.
        """
        super()._check_arguments()
        if len(self.tables) != len(self.probabilities):
            error_msg = (
                f'ProbabilisticFunction {self.label} has {len(self.tables)} tables and {len(self.probabilities)} '
                'probabilities. Give one probability per table.'
            )
            raise ValidationError(error_msg)
        patterns = 2 ** len(self.inputs)
        if any(len(table) != patterns for table in self.tables):
            error_msg = (
                f'ProbabilisticFunction {self.label} reads {len(self.inputs)} inputs, which take {patterns} '
                'patterns, but one of its tables has another number of entries.'
            )
            raise ValidationError(error_msg)

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the map of the target by the drawn table and the record of the drawn index.

        The tables and the probabilities are invalid when they differ in number, or a table has the wrong size.

        Args:
            context:
                The namespace of the target and its inputs.
        """
        super()._prepare(context)
        self.laws_: list[BaseLaw] = [
            UpdateLaw(context.target.x, self._get_drawn_value(context)),
            UpdateLaw(context.target.function, self._get_draw()),
        ]

    def _get_draw(self) -> Expression:
        """Return the index of the table drawn in a step.

        One draw serves the value and its record.
        """
        return categorical('function', list(self.probabilities))

    def _get_drawn_value(self, namespace: SimpleNamespace) -> Expression:
        """Return the value of the drawn truth table of the inputs."""
        inputs = [item.x for item in namespace.inputs]
        chosen = self._get_draw()
        values = [self._get_table_value(inputs, table) for table in self.tables]
        value = values[-1]
        for index in range(len(values) - 2, -1, -1):
            value = where(chosen == index, values[index], value)
        return value

Equations

The equations are those that symbolic returns for the system with its default parameters.

x1.x: x_x1(t + 1) == where(where(1.0*categorical_function_functions_x1 >= 0.6, 1, 0) == 0, (x_x1(t) - 1)*(x_x3(t) - 1)*x_x2(t) - (x_x3(t) - 1)*x_x1(t)*x_x2(t) + (x_x1(t) - 1)*(x_x2(t) - 1)*x_x3(t) - (x_x2(t) - 1)*x_x1(t)*x_x3(t) - (x_x1(t) - 1)*x_x2(t)*x_x3(t) + x_x1(t)*x_x2(t)*x_x3(t), -(x_x3(t) - 1)*x_x1(t)*x_x2(t) - (x_x2(t) - 1)*x_x1(t)*x_x3(t) - (x_x1(t) - 1)*x_x2(t)*x_x3(t) + x_x1(t)*x_x2(t)*x_x3(t))
x1.function: function_x1(t + 1) == where(1.0*categorical_function_functions_x1 >= 0.6, 1, 0)
x2.x: x_x2(t + 1) == (x_x1(t) - 1)*(x_x3(t) - 1)*x_x2(t) + (x_x1(t) - 1)*(x_x2(t) - 1)*x_x3(t) - (x_x2(t) - 1)*x_x1(t)*x_x3(t) + x_x1(t)*x_x2(t)*x_x3(t)
x2.function: function_x2(t + 1) == 0
x3.x: x_x3(t + 1) == where(where(1.0*categorical_function_functions_x3 >= 0.5, 1, 0) == 0, -(x_x3(t) - 1)*x_x1(t)*x_x2(t) - (x_x2(t) - 1)*x_x1(t)*x_x3(t) - (x_x1(t) - 1)*x_x2(t)*x_x3(t) + x_x1(t)*x_x2(t)*x_x3(t), x_x1(t)*x_x2(t)*x_x3(t))
x3.function: function_x3(t + 1) == where(1.0*categorical_function_functions_x3 >= 0.5, 1, 0)