Skip to content

Bernoulli process

Independent Bernoulli processes, one success or failure per step.

  • Class: BernoulliProcessSystem
  • Import: from skcomplex.systems.mathematics.stochastic_processes import BernoulliProcessSystem
  • Field: mathematics/stochastic_processes
  • Topics: counting process, agents, independent trials
  • References:
    • Thurner et al. 2018

Use this system

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

from skcomplex.systems.mathematics.stochastic_processes import BernoulliProcessSystem

trajectory = BernoulliProcessSystem().prepare().simulate()

Parameters

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

Parameter Default
n_sequences 20
p 0.5
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 variable membership

Class

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

class BernoulliProcessSystem(BaseSystem):
    """Independent Bernoulli processes, one success or failure per step.

    Their success counts are binomial.

    The system has the group `trials` of states `outcome` and `successes`.

    Args:
        n_sequences:
            The number of independent sequences of trials.

        p:
            The probability of a success.

        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.mathematics.stochastic_processes import BernoulliProcessSystem
        >>> BernoulliProcessSystem(3).prepare().system_.elements[0].size
        3
    """

    field = 'mathematics/stochastic_processes'
    topics = ('counting process', 'agents', 'independent trials')
    references = ('Thurner et al. 2018',)
    until = 100

    def __init__(
        self,
        n_sequences: int = 20,
        *,
        p: float = 0.5,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n_sequences = n_sequences
        self.p = p

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the sequences and their trials."""
        super()._prepare(context)
        self.system_ = System(
            self.label,
            elements=[BernoulliTrialElement('trials', size=self.n_sequences)],
            interactions=[BernoulliTrialInteraction('trial', target='trials', p=self.p)],
        )

Components

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

Elements:

  • BernoulliTrialElement, from Element

Interactions:

  • BernoulliTrialInteraction, from Interaction
class BernoulliTrialElement(Element):
    """Sequences of trials, each with the outcome `outcome` of its last trial and its number `successes` of successes.

    Args:
        label:
            The label of the sequences.

        outcome:
            The initial outcome, 1 for a success and 0 for a failure, one per sequence or shared.

        successes:
            The initial number of successes, one per sequence or shared.

        size:
            The number of sequences, or `None` for one.

    Examples:
        >>> from skcomplex.systems.mathematics.stochastic_processes import BernoulliTrialElement
        >>> BernoulliTrialElement('trials', successes=2.0, size=4).successes
        2.0
    """

    visual = VisualSpec('histograms', {'color_by': 'successes'})

    def __init__(
        self,
        label: str,
        *,
        outcome: Annotated[object, FiniteEnumeratedSet([0, 1])] = 0.0,
        successes: Annotated[object, NN] = 0.0,
        size: int | None = None,
    ) -> None:
        super().__init__(label, size=size)
        self.outcome = outcome
        self.successes = successes

class BernoulliTrialInteraction(Interaction):
    """A Bernoulli trial at every step, a success with the probability `p`, counted in `successes`.

    Every sequence runs the rule once per step.

    Args:
        label:
            The label of the interaction.

        target:
            The sequences whose outcomes and numbers of successes the rule sets.

        p:
            The probability of a success.

    Examples:
        >>> from skcomplex.systems.mathematics.stochastic_processes import BernoulliTrialInteraction
        >>> BernoulliTrialInteraction('trial', p=0.2).p
        0.2
    """

    law_classes = (UpdateLaw,)
    draws = True

    def __init__(self, label: str, *, target: Role = 'trials', p: float = 0.5) -> None:
        super().__init__(label)
        self.target = target
        self.p = p

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the trial of every sequence as the rule.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        self.laws_: list[BaseLaw] = [UpdateLaw(context.target, self._run_trial)]

    def _run_trial(self, agent: BernoulliTrialView, system: SystemView) -> None:
        """Run one trial of a sequence and count its success."""
        agent.outcome = float(system.rng.random() < self.p)
        agent.successes += agent.outcome

Equations

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