2.4.3. sdepy.SDE¶
-
class
sdepy.SDE(*, paths=1, vshape=(), dtype=None, steps=None, i0=0, info=None, getinfo=True, method='euler', **args)[source]¶ Class representation of a user defined Stochastic Differential Equation (SDE), intended for subclassing.
This class aims to provide an easy to use and flexible interface, allowing to specify user-defined SDEs and expose them in a standardized form to the cooperating
integratorclass (the latter should always follow in method resolution order). A minimal definition of an Ornstein-Uhlenbeck process is as follows:>>> from sdepy import SDE, integrator >>> class my_process(SDE, integrator): ... def sde(self, t, x, theta=1., k=1., sigma=1.): ... return {'dt': k*(theta - x), 'dw': sigma}
An SDE is stated as a dictionary, containing for each differential the value of the corresponding coefficient:
dx = f(t, x)*dt + g(t, x)*dw + h(t, x)*dj
translates to:
{'dt': f(t, x), 'dw': g(t, x), 'dj': h(t, x)}
Instances are callables with signature
(timeline)that integrate the SDE along the given timeline, using the configuration set out in the instantiation parameters:>>> P = my_process(x0=1, sigma=0.5, paths=100*1000, steps=100) >>> x = P(timeline=(0., 0.5, 1.)) >>> x.shape (3, 100000)
Subclasses can specify or customize: the equation and its parameters (
sdemethod), initial conditions and preprocessing (initmethod andlogattribute), shape of the values to be computed and stored (shapesmethod), stochastic differentials appearing in the equation (sourcesattribute) and their parameters and initialization (methodssource_dt,source_dw,source_dn,source_dj, or any customsource_{id}method for a corresponding differential'{id}'declared insourcesand used as a key insdereturn values), optional non array-like parameters (moremethod), how to store results at points on the requested timeline (letmethod), and postprocessing (resultmethod andlogattribute).Parameters: - paths : int
Number of paths of the process.
- vshape : int or tuple of int
Shape of the values of the process.
- dtype : data-type, optional
Data-type of the process. Defaults to the numpy default.
- steps : iterable, or int, or None
Specification of the time points to be touched during integration (as accepted by a cooperating
integratorclass). Default behaviour is:- if
None, the simulated steps coincide with the timeline; - if
int, the simulated steps touch all timeline points, as well asstepsequally spaced points between the minimum and maximum point in the timeline; - if iterable, the simulated steps touch all timeline points,
as well as all values in
stepsbetween the minimum and maximum points in the timeline.
- if
- i0 : int
Index along the timeline at which the integration starts. The timeline is assumed to be in ascending order. Initial conditions are set at
timeline[i0], the integration is performed backwards fromtimeline[i0]totimeline[0], and forwards fromtimeline[i0]totimeline[-1].- info : dict, optional
Diagnostic information about the integration is stored in this dictionary and is accessible as the
infoattribute. Defaults to a new emptydict.- getinfo : bool
If
True, subclass methodsinfo_begin,info_next,info_store,info_endare invoked during integration. Defaults toTrue.- method : str
Integration method, as accepted by the
integratorcooperating class.- **args : SDE-specific parameters
SDE parameters and initial conditions, as implied by the signature of
sde,initandmoremethods, and stochasticity sources parameters, as implied by the signature ofsource_{id}methods. Each keyword should be used once (e.g.corr, asource_dwparameter, cannot be used as the name of a SDE parameter).
Returns: - process
Once instantiated as
p,p(timeline)performs the integration along the given timeline, based on parameters of instantiation, and returns the resulting process as defined by subclass methods. Defaults to a process ofnumpy.nanalong the given timeline.
See also
Notes
Custom stochastic differentials used in the SDE should be recognized, and treated appropriately, by the chosen integration method. This may require customization of the
nextmethod of theintegratorclass.All named initialization parameters (
paths,stepsetc.) are stored as attributes.- Notes on SDE-specific parameters:
initparameters are converted to arrays vianp.asarray.sdeand source quantitative parameters may be array-like, or time dependent with signature(t).- both are converted to arrays via
np.asarray, and for both, their constant value, or values at each time point, should be broadcastable to a shapewshape + (paths,). moreparameters undergo no further initialization, before being made available to theshapesandmoremethods.
If
getinfoisTrue, the invoked info subclass methods may initialize and cumulate diagnostic information in items of theinfodictionary, based on read-only access of the internal variables set during integration bypaths_generatorandintegratorcooperating classes, as exposed in theitervarsattribute.Attributes: - sources : set or dict
As a class attribute, holds the names of the differentials
'dz'expected to appear in the equation. As an instance attribute,sources['dz']is an object, complying with thesourceprotocol, that instantiates the differential'dz'used during integration.sources['dz'](t, dt)is computed at every step for each'dz' in sources, as required by the chosen integration method.args: dictStores parameters passed as
**argsupon initialization of the SDE.- log : bool
If True, the natural logarithm of the initial values set by the
initmethod is taken as the initial value of the integration, and the result of the integration is exponentiated back before serving it to theresultmethod. Thesdeshould expose the appropriate equation for integrating the logarithm of the intended process.
Methods
sde(t, x)Stochastic Differential Equation (SDE) to be integrated. shapes(vshape)Shape of the values to be computed and stored upon integration of the SDE. source_dt()Setup a source of deterministic increments, to be used as ‘dt’ during integration. source_dw([dw, corr, rho])Setup a source of standard Wiener process (Brownian motion) increments, to be used as ‘dw’ during integration. source_dn([dn, ptype, lam])Setup a source of Poisson process increments, to be used as ‘dn’ during integration. source_dj([dj, dn, ptype, lam, y])Set up a source of compound Poisson process increments (jumps), to be used as ‘dj’ during integration. more()Further optional non array parameters, and initializations. init(t, out_x[, x0])Set initial conditions for SDE integration. let(t, out_x, x)Store the value of the integrated process at time point tbelonging to the requested output timeline.result(tt, xx)Compute the integration output. info_begin()Optional diagnostic information logging function, called before the integration begins. info_next()Optional diagnostic information logging function, called after each integration step. info_store()Optional diagnostic information logging function, called after each invocation of the let method. info_end()Optional diagnostic information logging function, called after the integration has been completed.