2.4.4. sdepy.SDEs

class sdepy.SDEs(*, paths=1, vshape=(), dtype=None, steps=None, i0=0, info=None, getinfo=True, method='euler', **args)[source]

Class representation of a user defined system of Stochastic Differential Equations (SDEs), intended for subclassing.

The parent SDE class represents a single SDE, scalar or multidimensional: by an appropriate choice of the vshape parameter, and composition of equation values, it suffices to describe any system of SDEs.

Its SDEs subclass is added for convenience of representation: it allows to state each equation separately and to retrieve separate processes as a result. The number of equations must be stated as the q attribute. The vshape parameter is taken as the common shape of values in each equation in the system.

A minimal definition of a lognormal process x with stochastic volatility y is as follows:

>>> from sdepy import SDEs, integrator
>>> class my_process(SDEs, integrator):
...     q = 2
...     def sde(self, t, x, y, mu=0., sigma=1., xi=1.):
...         return ({'dt': mu*x, 'dw': y*x},
...                 {'dt': 0,    'dw': xi*y})

>>> P = my_process(x0=(1., 2.), xi=0.5, vshape=5,
...                paths=100*1000, steps=100, )
>>> x, y = P(timeline=(0., 0.5, 1.))
>>> x.shape, y.shape
((3, 5, 100000), (3, 5, 100000))

Notes

By default, the stochasticity sources of each component equation are realized independently, even if represented in the sde output by the same key ('dw' in the example above).

The way stochasticity sources are instantiated and dispatched to each equation, and how correlations of the Wiener source are set via the corr parameter, depend on the value of the addaxis attribute:

  • If True, source values have shape vshape + (q,), and the [kk, i] component of source values is dispatched to the kk component of equation i (kk is a multiindex spanning shape vshape). If given, corr must be of shape (q, q) and correlates corresponding components across equations.
  • If addaxis is False (default) and N is the size of the last axis of vshape, the values of the sources have shape vshape[:-1] + (N*q,), and the [kk, i*N + h] component of the source values is dispatched to the [kk, h] component of equation i (kk is a multiindex spanning shape vshape[:-1], and h is in range(N)). If given, corr must be of shape (N*q, N*q), and correlates all last components of all equations to each other.

After instantiation, stochasticity sources and correlation matrices may be inspected as follows:

>>> P = my_process(vshape=(), rho=0.5)
>>> P.sources['dw'].vshape
(2,)
>>> P.sources['dw'].corr.shape
(2, 2)
>>> P.sources['dw'].corr[0, 1]
0.5
Attributes:
q : int

Number of equations.

addaxis : bool

Affects the internal representation of the equations: if True, a last axis of size q is added to vshape, if False, components are stacked onto the last axis of vshape. Defaults to False. It is forced to True if the process components have scalar values.

Methods

pack(xs) Packs the given arrays (one per equation) into a single array.
unpack(X) Unpacks the given array into multiple arrays (one per equation).