Anomalous processes
This page writes processes that depart from ordinary diffusion and exponential decay. They have heavy-tailed jumps, events that fire after waiting times from any distribution, noise whose increments are correlated over the whole run, or derivatives of fractional order. A heavy tail means that very large values are far more likely than under a normal distribution.
Draws from SciPy distributions
This section draws from any distribution of scipy.stats, such as a Lévy stable law for long flights. The function
draw(name, distribution, **parameters) takes the name of the distribution, such as 'pareto', 'lomax',
'expon', 'beta', or 'levy_stable', with its shape parameters, loc, and scale as keywords.
Here 100 fliers, an Element with size=100, each take a step drawn
from a symmetric stable law of index 1.5 on every step, through an
UpdateLaw of a one-off interaction.
from skcomplex.core.components.elements import Element
from skcomplex.core.components.interactions import Interaction
from skcomplex.core.components.laws import UpdateLaw
from skcomplex.core.components.laws.utils import draw
from skcomplex.core.systems import System
def fly(s):
x = s.target.x
return [UpdateLaw(x, x + draw('jump', 'levy_stable', alpha=1.5, beta=0.0))]
fliers = Element('fliers', values={'x': 0.0}, size=100)
system = System(elements=[fliers], interactions=[Interaction('flight', roles={'target': 'fliers'}, laws=fly)])
flight = system.prepare().simulate(until=20, dt=1.0, random_state=0)
flight.get_values('fliers.x').shape
(21, 100)
The positions hold 21 frames, the start and 20 steps, of 100 fliers each. The steps show the heavy tail. The largest step is about ninety times the median one.
import numpy as np
steps = np.abs(np.diff(flight.get_values('fliers.x'), axis=0))
int(steps.max()), round(float(np.median(steps)), 1)
(91, 1.0)
The largest of the 2000 steps has a length of 91, and the median length is 1.0. A SciPy draw follows these rules.
- The keywords can be expressions of states and parameters.
- A SciPy draw follows the rules of the other named draws, and it samples with the distribution's
rvsand the random generator of the run. - Compiling rejects a name
scipy.statsdoes not provide, listing the closest names, and keywords the distribution does not take. - In update laws, a SciPy draw is sampled for every member before each step, and the map then steps in Python with the compiled update of the backend.
Jumps on clocks of waiting times
This section fires an event after a random waiting time, such as a trapped particle that escapes after a
heavy-tailed wait. JumpLaw(waiting=..., reset=..., label=...) fires after a
waiting time, applies its reset to the states all at once, and draws the next waiting time from the states after the
reset. A renewal is one such firing.
Here 1000 walkers wait a Pareto time of index 0.5, which has no finite mean, and then jump by a normal step.
from skcomplex.core.components.elements import Element
from skcomplex.core.components.interactions import Interaction
from skcomplex.core.components.laws import JumpLaw
from skcomplex.core.components.laws.utils import draw, normal
from skcomplex.core.systems import System
from skcomplex.core.types import Role
class Trapping(Interaction):
def __init__(self, label, *, target: Role, alpha=0.5):
super().__init__(label)
self.target = target
self.alpha = alpha
def _prepare(self, s):
super()._prepare(s)
x, waiting = s.target.x, draw('wait', 'pareto', b=s.alpha)
self.laws_ = [JumpLaw(waiting=waiting, reset={x: x + normal('jump', 0, 1)}, label='jumps')]
walkers = Element('walkers', values={'x': 0.0}, size=1000)
system = System(elements=[walkers], interactions=[Trapping('trapping', target='walkers')]).prepare()
walk = system.simulate(until=1000, dt=100.0, random_state=0)
system.form_, bool(walk.get_values('jumps.renewals')[-1] > 1000)
('renewal process', True)
The system compiles to the form 'renewal process', and the 1000 walkers renew more than 1000 times by time 1000.
The clocks follow these rules.
- Every member of a group renews on its own clock.
- Draws in the waiting time and the reset take a fresh value at every renewal of every member, and every use of one name in a renewal reads the same value.
- A waiting time that is a single draw must come from a distribution with nonnegative support. Compiling rejects one
that can be negative, such as
normal.
A system or partition whose only laws are such jumps compiles to the renewal form. RenewalSolver runs it exactly, from
renewal to renewal with no time grid, and a frame holds the states the last renewal at or before its time set.
These jumps also combine with rate and noise laws on the same states, which evolve them between renewals.
RK4Solver and EulerMaruyamaSolver stop every member at its own renewals when the members do not interact. When a
law sums over the group, they split the step at every renewal time for all members. Other steppers reject these jumps.
The trajectory records every renewal pass as a RenewalRecord among its events, with the time and the member of every
renewal and the member's states before and after it. The observable <label>.renewals counts the renewals up to every
frame. The Rerun observer streams it as a time series, and the renewals as text entries under renewals/<label>. The
compiler keeps the count of every member in a hidden state, <group>.<label>~renewals.
Fractional noise
This section drives a state with noise that remembers its past, as in rough or persistent paths.
NoiseLaw(targets, value, hurst=hurst) drives a state with fractional
Brownian motion of Hurst index 0 < H < 1. The Hurst index sets how the increments correlate.
Here 500 walkers follow rough paths of index 0.3 with amplitude 1 up to time 1.
from skcomplex.core.components.elements import Element
from skcomplex.core.components.interactions import Interaction
from skcomplex.core.components.laws import NoiseLaw
from skcomplex.core.engine.solvers import FractionalEulerMaruyamaSolver
from skcomplex.core.systems import System
walkers = Element('walkers', values={'x': 0.0}, size=500)
roughness = Interaction('roughness', roles={'target': 'walkers'}, laws=lambda s: [NoiseLaw(s.target.x, 1, hurst=0.3)])
system = System(elements=[walkers], interactions=[roughness], solver=FractionalEulerMaruyamaSolver(dt=0.01))
rough = system.prepare().simulate(until=1, dt=0.1, random_state=0)
round(float(rough.get_values('walkers.x')[-1].var()), 1)
1.0
The variance of the positions at time 1 is 1.0, the variance t^(2H) of fractional Brownian motion at t = 1. The
noise follows these rules.
- The increments anticorrelate for
H < 1/2and correlate forH > 1/2. - A Hurst index of 1/2 compiles to a noise law without one. For other indices the amplitude must not depend on any state.
FractionalEulerMaruyamaSolver(dt, horizon)is the stepper of systems with fractional noise. When it first advances, it draws the increments of the whole run on its grid, with the circulant embedding of Davies and Harte overscipy.fft. So frames must fall on multiples ofdt.simulatepasses the end of the run as the horizon, and advancing beyond it raisesHorizonError.- Other SDE steppers reject fractional noise with an error that names
FractionalEulerMaruyamaSolver. fractional_gaussian_noisereturns the noise paths themselves.
These ready-made systems have memory: ContinuousTimeRandomWalkSystem, LevyWalkSystem,
AnnealedTransientTimeMotionSystem, FractionalBrownianMotionSystem, and LevyFlightSystem of
skcomplex.systems.physics.soft_matter, and TrapSystem of skcomplex.systems.physics.statistical_mechanics.
Fractional derivatives
This section gives a state a derivative of fractional order, for relaxation that is slower than any exponential. A
rate law whose target is fractional(x, 0.6) sets the Caputo derivative of that order of the state x. The Caputo
derivative of order alpha weighs the whole past of the rate of change with a kernel that decays as a power of time.
Here a state relaxes under the RateLaw RateLaw(fractional(x, 0.6), -x), the
equation D^0.6 x = -x, from 1.0, and the run compares with the exact solution at time 20.
from skcomplex.core.analysis import symbolic
from skcomplex.core.components.elements import Element
from skcomplex.core.components.interactions import Interaction
from skcomplex.core.components.laws import RateLaw
from skcomplex.core.components.laws.utils import fractional
from skcomplex.core.engine.solvers import L1Solver
from skcomplex.core.systems import System
def relax(s):
return [RateLaw(fractional(s.target.x, 0.6), -s.target.x)]
relaxation = Interaction('relaxation', roles={'target': 'a'}, laws=relax)
solver = L1Solver(dt=0.01, memory=10.0)
system = System(elements=[Element('a', values={'x': 1.0})], interactions=[relaxation], solver=solver)
relaxing = system.prepare().simulate(until=20, dt=1.0)
solution = symbolic(system, kind='solve', use_initial_state=True).solutions['a.x'].rhs()
exact = solution.subs({solution.variables()[0]: 20.0})
system.form_, abs(float(relaxing.get_values('a.x')[-1]) - float(exact)) < 1e-3
('fractional differential equations', True)
The system compiles to the form 'fractional differential equations', and the simulated relaxation matches the
exact Mittag-Leffler solution to within 1e-3 at time 20.
The rate laws on a fractional state give its derivative, D^alpha x = f(t, x), and superpose as for any state, so no
other law kind is needed. Order 1 is the ordinary rate law. The derivative of order alpha at a time depends on the
whole past of the state, so the solution relaxes like a Mittag-Leffler function, slower than any exponential.
A system with a state of a fractional order compiles to the fractional form, with the order of every state component
in problem.orders. The states of order 1 of the same system keep their ordinary equations. Three steppers integrate
the fractional form on a fixed grid from the start of the run, so frames must fall on multiples of their dt.
GrunwaldLetnikovSolver(dt, memory=None)weighs the past states with the binomial weights(-1)^j binomial(alpha, j).L1Solver(dt, memory=None)interpolates the state linearly between grid points inside the Caputo integral.FractionalABMSolver(dt)is the predictor-corrector of Diethelm, Ford, and Freed, of order1 + alphafor smooth solutions, and the default.
The two explicit schemes are of order 1. All three keep the whole history, so a run of n steps costs n^2
operations. memory bounds that cost. The history of the last memory time units enters with its exact weights, and
the older history enters through about 40 exponential modes fitted to the weights, so every step costs the same. For
fractional relaxation of order 0.6, a window of 10 time units stays within 1e-8 of the whole history at time 20. A
change of a fractional state between frames, such as one a schedule makes, starts a new history from
the changed state.
prepare reports the misuses of fractional orders before compiling, each with the path of the state and a fix.
fractional-order: an order that is neither an integer nor in(0, 1).order-unsupported: a chosen solver or backend that does not integrate fractional orders.solver-unsupported: a law on a fractional state that no fractional solver runs, such as a noise law.
Solvers and backends declare the kinds of order they integrate in the orders field of their SupportSpec,
'integer' or 'fractional'. The NumPy and Numba backends run the fractional form.
symbolic(system, kind='solve') solves one linear fractional relaxation D^alpha x = c x as
x(0) E_alpha(c t^alpha). It writes the solution with the symbolic function mittag_leffler, which evaluates
numerically. It sums the power series in arbitrary precision with Sage for moderate arguments, and takes the asymptotic
expansion for large ones.
symbolic(system) writes a Caputo derivative as caputo(x(t), t, alpha).
The fractional section of the laws page shows the operator in full. These
ready-made
systems of skcomplex.systems.mathematics.fractional_dynamics are written this way and take the order as a model
parameter: FractionalRelaxationSystem, FractionalOscillatorSystem, FractionalVanDerPolSystem, and
FractionalLorenzSystem.