Skip to content

Random walk with autoregressive velocity

Random walks whose velocity is an autoregressive process.

  • Class: RandomWalkWithMemorySystem
  • Import: from skcomplex.systems.mathematics.stochastic_processes import RandomWalkWithMemorySystem
  • Field: mathematics/stochastic_processes
  • Topics: random walk, discrete map, memory, autoregressive
  • References:
    • Bertin 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.mathematics.stochastic_processes import RandomWalkWithMemorySystem

trajectory = RandomWalkWithMemorySystem().prepare().simulate()

Parameters

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

Parameter Default
n_walkers 2000
alpha 0.5
sigma 1.0
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 continuous
Laws update
Randomness stochastic
Domains real
Structure fixed

Class

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

class RandomWalkWithMemorySystem(BaseSystem):
    """Random walks whose velocity is an autoregressive process.

    The walks follow `x ← x + v` and `v ← alpha v + xi`.

    The noise `xi` is normal with mean 0 and deviation `sigma`, and the walks start from `x = v = 0`. The pair `(x, v)`
    is Markovian while the displacements are correlated, so the mean squared displacement grows as `sigma² t / (1 -
    alpha)²` at long times instead of `sigma² t / (1 - alpha²)` for uncorrelated steps of the same variance. The system
    has the positions `walkers.x` and the velocities `walkers.v`.

    Args:
        n_walkers:
            The number of independent walkers.

        alpha:
            The memory `alpha` of the velocity, between 0 and 1.

        sigma:
            The deviation `sigma` of the noise.

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

    field = 'mathematics/stochastic_processes'
    topics = ('random walk', 'discrete map', 'memory', 'autoregressive')
    references = ('Bertin 2021',)
    until = 200
    dt = 1

    def __init__(
        self,
        n_walkers: int = 2000,
        *,
        alpha: float = 0.5,
        sigma: float = 1.0,
        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.alpha = alpha
        self.sigma = sigma

    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 = MemoryWalkerElement('walkers', size=self.n_walkers, space=ContinuousSpace(n_dim=1))
        step = MemoryStepInteraction('step', target='walkers', alpha=self.alpha, sigma=self.sigma)
        self.system_ = System(self.label, elements=[walkers], interactions=[step])

Components

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

Elements:

  • MemoryWalkerElement, from Element

Interactions:

  • MemoryStepInteraction, from Interaction
class MemoryWalkerElement(Element):
    """Walkers on the real line, each with its position `x` and its velocity `v`.

    Args:
        label:
            The label of the walkers.

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

        v:
            The initial velocity, 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 MemoryWalkerElement
        >>> MemoryWalkerElement('walker', v=0.3).v
        0.3
    """

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

class MemoryStepInteraction(Interaction):
    """Step of a walk whose velocity is an autoregressive process, `x <- x + v` and `v <- alpha v + xi`.

    The noise `xi` is normal with mean 0 and deviation `sigma`, drawn anew at every step for every walker.

    Args:
        label:
            The label of the interaction.

        target:
            The walkers whose positions `x` and velocities `v` the step sets.

        alpha:
            The memory of the velocity.

        sigma:
            The deviation of the noise of the velocity.

    Examples:
        >>> from skcomplex.systems.mathematics.stochastic_processes import MemoryStepInteraction
        >>> MemoryStepInteraction('step', alpha=0.3).alpha
        0.3
    """

    law_classes = (UpdateLaw,)
    draws = True

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

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the next position and the next velocity as the laws.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        x, v = context.target.x, context.target.v
        self.laws_: list[BaseLaw] = [
            UpdateLaw(x, x + v),
            UpdateLaw(v, context.alpha * v + normal('xi', 0, context.sigma)),
        ]