Hi everybody, and first of all thanks for the great job you have been
doing, on which I relied *A LOT* over time.
Stochastic calculus and Stochastic Differential Equations are a significant
branch of mathematics that to my understanding is underrepresented in
scipy, as well as in the open source python stack at large. I may well be
missing something, but as I can see one can find out there many recipies or
collections of recipies, and a number of packages/projects skewed towards
finance, but no structured framework for generating and handling stochastic
processes and numerically solving SDEs.
This is a pity, since scipy/numpy provide efficen implementations of all
the basic ingredients needed to do the trick. Out of professional need, and
prompted by this gap, I have developed a general purpose package of which I
own the copyright, and which I would be happy to release under the BSD
license as part of scipy, in case there is a consensus among the scipy
community that it makes sense to do so (the package has no dependencies
beyond standard library, numpy and scipy, and might probably fit best as a
subpackage of scipy.stats).
Before setting up a PR, I would be happy to hear your thoughts: I have
pasted below the package docstring, that should give an overview of its
scope and limitations. Please note that some of the realized processes
stray into mathematical finance territory, somehow inevitably since finance
is one major field of application of stochastic calculus, but the focus is
the latter, not the former, at least in my intentions.
Thanks in advance for taking your time to go through this, and for your
comments and suggestions.
Maurizio
“””
===========================================================
Stochastic - Monte Carlo simulation of stochastic processes
===========================================================
This package provides:
1. A `process` class, a subclass of `numpy.ndarray` representing
a sequence of values in time, realized in one or several paths.
Algebraic manipulations and ufunc computations are supported for
instances that share the same timeline and comply with numpy
broadcasting rules. Interpolation along the timeline is supported
via callability of `process` instances. Process-specific
functionalities,
such as averaging and indexing along time or across paths, are delegated
to process-specific methods, attributes and properties (no overriding
of `numpy.ndarray` operations).
2. The `source` class and its subclasses, stochasticity sources providing
numerical realizations of the differentials commonly found
in stochastic differential equations (SDEs), with or without
memory of formerly invoked realizations.
3. The `integrator` class, a general framework for numerical SDE
integration, intended for subclassing, to assist the definition of step
by
step integration algorithms and the computation of numerical
realizations of
stochastic processes.
4. Several objects providing realizations of specific stochastic processes
along a given timeline and across a requested number of paths.
Realizations are obtained via explicit formulae, in case the relevant
SDE
has a closed form solution, or otherwise as `integrator` subclasses
performing
Euler-Maruyama numerical integration.
5. A `montecarlo` class, as an aid to cumulate the results of several
Monte Carlo simulations of a given stochastic variable, and to extract
summary estimates for its probability distribution function and
statistics, thus supporting simulations across a number of paths that
exceeds
the maximum allowed by available memory.
6. Several analytical results relating to the supported stochastic
processes, as a general reference and for testing purpouses.
For all sources and realizations, process values can take any shape,
scalar or multidimensional. Correlated multivariate stochasticity sources
are
supported. Time-varying process parameters (correlations, intensity of
Poisson
processes, volatilities etc.) are allowed whenever applicable.
`process` instances act as valid stochasticity source realizations (as does
any callable object complying with a `source` protocol), and may be
passed as a source specification when computing the realization of a given
process.
Computations are fully vectorized across paths, providing an efficient
infrastructure for simulating a large number of process realizations.
Less so, for large number of time steps: integrating 1000 time steps
across 100000 paths takes seconds, one million time steps across
100 paths takes minutes.
General infrastructure
======================
.. autosummary::
:toctree: generated/
process -- Array representation of a process (a subclass of
-- numpy.ndarray).
kfunc -- Base class for functions with managed keyword arguments.
source -- Base class for stochasticity sources.
true_source -- Base class for stochasticity sources with memory.
integrator -- General framework for numerical SDE integration.
montecarlo -- Summmary statistics of Monte Carlo simulations.
Stochasticity sources
=====================
.. autosummary::
:toctree: generated/
wiener -- dW, a source of standard Wiener process (Brownian
-- motion) increments.
symmetric_wiener -- as above, with symmetric paths (averages exactly
0).
true_wiener -- dW, a source of standard Wiener process (Brownian
-- motion) increments, with memory.
poisson -- dN, a source of Poisson process increments.
compound_poisson -- dJ, a source of compound Poisson process
increments.
-- (jumps distributed in time as a Poisson
process,
-- and in size as a given `scipy.stats`
distribution).
compound_poisson_rv -- Preset jump size distributions for compound
Poisson
-- process increments.
Stochastic process realizations
===============================
.. autosummary::
:toctree: generated/
const_wiener_process -- Wiener process (Brownian motion), with
-- time-independent parameters.
const_lognorm_process -- Lognormal process, with time-independent
-- parameters.
wiener_process -- Wiener process (Brownian motion).
lognorm_process -- Lognormal process.
ornstein_uhlenbeck_process -- Ornstein-Uhlenbeck process
(mean-reverting
-- Brownian motion).
hull_white_process -- F-factors Hull-White process (sum of F
-- correlated mean-reverting Brownian
-- motions).
hull_white_1factor_process -- 1-factor Hull-White process (F=1
Hull-White
-- process with F-index collapsed to a
-- scalar).
cox_ingersoll_ross_process -- Cox-Ingersoll-Ross mean-reverting
process.
full_heston_process -- Heston stochastic volatility process
-- (returns both process and
volatility).
heston_process -- Heston stochastic volatility process
-- (stores and returns process only).
jump_diffusion_process -- Jump-diffusion process (lognormal
process
-- with compound Poisson jumps).
merton_jump_diffusion_process -- Merton jump-diffusion process
-- (jump-diffusion process with normal
-- jump size distribution).
kou_jump_diffusion_process -- Kou jump-diffusion process
(jump-diffusion
-- process with double exponential
-- jump size distribution).
Shortcuts::
lognorm -- lognorm_process
oruh -- ornstein_uhlenbeck_process
hwp -- hull_white_process
hw1f -- hull_white_1factor_process
cir -- cox_ingersoll_ross_process
heston -- heston_process
mjd -- merton_jump_diffusion_process
kou -- kou_jump_diffusion_process
Analytical results
==================
Exact statistics for the realized stochastic processes
are listed below, limited to the case of constant process parameters and
with
some further limitations to the parameters' domains.
Function arguments are consistent with those of the corresponding processes.
Suffixes `pdf`, `cdf` and `chf` stand respectively for probability
distribution
function, cumulative probability distribution function, and characteristic
function. Black-Scholes formulae for the valuation of call and put options
have been
included (prefixed with `bs` below).
.. autosummary::
:toctree: generated/
wiener_mean
wiener_var
wiener_std
wiener_pdf
wiener_cdf
wiener_chf
lognorm_mean
lognorm_var
lognorm_std
lognorm_pdf
lognorm_cdf
lognorm_log_chf
oruh_mean
oruh_var
oruh_std
oruh_pdf
oruh_cdf
hw2f_mean
hw2f_var
hw2f_std
hw2f_pdf
hw2f_cdf
cir_mean
cir_var
cir_std
cir_pdf
heston_log_mean
heston_log_var
heston_log_std
heston_log_pdf
heston_log_chf
mjd_log_pdf
mjd_log_chf
kou_mean
kou_log_pdf
kou_log_chf
bsd1d2
bscall
bscall_delta
bsput
bsput_delta
Notes
=====
To the benefit of interactive and notebook sessions, and as an aid to
fluently
freeze or vary the plurality of parameters that define each stochastic
process,
all sources, process realizations and analytical results are objects
with managed keywords (subclasses of `kfunc`): if the corresponding classes
are instantiated with both variables (non keyword arguments) and parameters
(keyword arguments), they behave as functions returning the computations'
result; if called with parameters only, return an instance that stores the
set
parameters, and exposes the same calling pattern (call with variables
and optionally with parameters to get results, call with parameters only
to get a new instance storing modified parameters).
Parameters that are not specified fall back to the class defaults, if
calling
the class, or to the instance's stored values, if calling an instance.
"""