Persistent random walk
Walkers on the integers that keep the direction of their last step.
- Class:
PersistentRandomWalkSystem - Import:
from skcomplex.systems.mathematics.stochastic_processes import PersistentRandomWalkSystem - Field:
mathematics/stochastic_processes - Topics:
random walk,discrete map,persistence,diffusion - References:
- 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 PersistentRandomWalkSystem
trajectory = PersistentRandomWalkSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_walkers |
2000 |
persistence |
0.8 |
random_state |
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 | integer |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class PersistentRandomWalkSystem(BaseSystem):
"""Walkers on the integers that keep the direction of their last step.
A walker keeps the direction with a probability.
At every step a walker moves by its direction `d = ±1`, and keeps the direction with probability `q` and reverses it
otherwise. Successive steps are correlated with `rho = 2 q - 1`, so the walk diffuses faster than a simple random
walk for `q > 1/2`, with a mean squared displacement that grows as `t (1 + rho) / (1 - rho)` at long times. The
system has the directions `walkers.d` and the positions `walkers.x`.
Args:
n_walkers:
The number of independent walkers, all started at 0.
persistence:
The probability `q` of keeping the direction.
random_state:
The seed or generator of the initial directions.
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 PersistentRandomWalkSystem
>>> PersistentRandomWalkSystem(10).prepare().system_.elements[0].size
10
"""
field = 'mathematics/stochastic_processes'
topics = ('random walk', 'discrete map', 'persistence', 'diffusion')
references = ('Jensen 2023',)
until = 200
dt = 1
def __init__(
self,
n_walkers: int = 2000,
*,
persistence: float = 0.8,
random_state: int | np.random.Generator | None = 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.persistence = persistence
self.random_state = random_state
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of the walkers on a line and their persistent steps."""
super()._prepare(context)
directions = np.random.default_rng(self.random_state).choice([-1.0, 1.0], size=self.n_walkers)
walkers = PersistentWalkerElement('walkers', d=directions, size=self.n_walkers, space=ContinuousSpace(n_dim=1))
step = PersistentStepInteraction('step', target='walkers', persistence=self.persistence)
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:
PersistentWalkerElement, fromElement
Interactions:
PersistentStepInteraction, fromInteraction
class PersistentWalkerElement(Element):
"""Walkers on the integers, each with its direction `d = ±1` and its position `x`.
Args:
label:
The label of the walkers.
d:
The initial direction, 1 or -1, one per walker or shared.
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 PersistentWalkerElement
>>> PersistentWalkerElement('walker', d=-1.0).d
-1.0
"""
def __init__(
self,
label: str,
*,
d: Annotated[object, ZZ] = 1.0,
x: Annotated[object, ZZ] = 0.0,
size: int | None = None,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, size=size, space=space)
self.d = d
self.x = x
class PersistentStepInteraction(Interaction):
"""Step of a persistent walk, `x <- x + d`, whose direction `d` reverses with the probability `1 - persistence`.
The reversal is drawn anew at every step for every walker.
Args:
label:
The label of the interaction.
target:
The walkers whose directions `d` and positions `x` the step sets.
persistence:
The probability of keeping the direction at a step.
Examples:
>>> from skcomplex.systems.mathematics.stochastic_processes import PersistentStepInteraction
>>> PersistentStepInteraction('step', persistence=0.9).persistence
0.9
"""
law_classes = (UpdateLaw,)
draws = True
def __init__(self, label: str, *, target: Role = 'walkers', persistence: float = 0.8) -> None:
super().__init__(label)
self.target = target
self.persistence = persistence
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the next direction and the next position as the laws.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
d, x = context.target.d, context.target.x
self.laws_: list[BaseLaw] = [
UpdateLaw(d, d * (1 - 2 * bernoulli('turn', 1 - context.persistence))),
UpdateLaw(x, x + d),
]