Skip to content

Random walks

Independent walkers on the integers that step at random times.

  • Class: RandomWalkSystem
  • Import: from skcomplex.systems.mathematics.stochastic_processes import RandomWalkSystem
  • Field: mathematics/stochastic_processes
  • Topics: random walk, jump process, diffusion
  • References:
    • Pearson 1905
    • Argun et al. 2021
    • Charbonneau 2017
    • Jensen 2023

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 RandomWalkSystem

trajectory = RandomWalkSystem().prepare().simulate()

Parameters

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

Parameter Default
n_walkers 100
rate 1.0
bias 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 continuous
Space continuous
Laws jump
Randomness stochastic
Domains integer
Structure fixed

Class

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

class RandomWalkSystem(BaseSystem):
    """Independent walkers on the integers that step at random times.

    This is a continuous-time random walk.

    Every walker steps up at rate `rate * bias` and down at rate `rate * (1 - bias)`, so its mean moves with velocity
    `rate (2 bias - 1)` and its variance grows as `4 rate bias (1 - bias) t`. The system has the positions `walkers.x`.

    Args:
        n_walkers:
            The number of walkers.

        rate:
            The rate of steps of a walker.

        bias:
            The probability that a step goes up.

        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 RandomWalkSystem
        >>> RandomWalkSystem(20).prepare().system_.elements[0].size
        20
    """

    field = 'mathematics/stochastic_processes'
    topics = ('random walk', 'jump process', 'diffusion')
    references = ('Pearson 1905', 'Argun et al. 2021', 'Charbonneau 2017', 'Jensen 2023')
    until = 100.0
    dt = 1.0

    def __init__(
        self,
        n_walkers: int = 100,
        *,
        rate: float = 1.0,
        bias: float = 0.5,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n_walkers = n_walkers
        self.rate = rate
        self.bias = bias

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the walkers on a line and their steps."""
        super()._prepare(context)
        walkers = RandomWalkerElement('walkers', size=self.n_walkers, space=ContinuousSpace(n_dim=1))
        steps = RandomStepInteraction('steps', target='walkers', rate=self.rate, bias=self.bias)
        self.system_ = System(self.label, elements=[walkers], interactions=[steps])

Components

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

Elements:

  • RandomWalkerElement, from Element

Interactions:

  • RandomStepInteraction, from Interaction
class RandomWalkerElement(Element):
    """Walkers on the integers, each with its position `x`.

    Args:
        label:
            The label of the walkers.

        x:
            The initial position, one per walker or shared.

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

        space:
            The line the walkers live on, whose axis `x` makes `x` their position, or `None`.

    Examples:
        >>> from skcomplex.systems.mathematics.stochastic_processes import RandomWalkerElement
        >>> RandomWalkerElement('walker', x=2.0).x
        2.0
    """

    def __init__(
        self,
        label: str,
        *,
        x: Annotated[object, ZZ] = 0.0,
        size: int | None = None,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, size=size, space=space)
        self.x = x

class RandomStepInteraction(Interaction):
    """Steps of a walker on the integers at the rate `rate`, each up with the probability `bias`.

    The walker steps up at the rate `rate * bias` and down at the rate `rate * (1 - bias)`.

    Args:
        label:
            The label of the interaction.

        target:
            The walkers whose positions `x` the steps change.

        rate:
            The rate of steps.

        bias:
            The probability that a step goes up.

    Examples:
        >>> from skcomplex.systems.mathematics.stochastic_processes import RandomStepInteraction
        >>> RandomStepInteraction('steps', bias=0.7).bias
        0.7
    """

    law_classes = (JumpLaw,)

    def __init__(self, label: str, *, target: Role = 'walkers', rate: float = 1.0, bias: float = 0.5) -> None:
        super().__init__(label)
        self.target = target
        self.rate = rate
        self.bias = bias

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the steps up and down as the laws.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        x = context.target.x
        self.laws_: list[BaseLaw] = [
            JumpLaw(context.rate * context.bias, {x: 1}),
            JumpLaw(context.rate * (1 - context.bias), {x: -1}),
        ]