Exact methods for simulating fractional Brownian motion and fractional Gaussian noise in python
Support of this package has been discontinued in favor of the more robust stochastic <https://github.com/crflynn/stochastic>
__ package, which includes fractional Brownian motion and multifractional Brownian motion implementations among many other stochastic processes for simulation.
|travis| |codecov| |pypi| |pyversions|
.. |travis| image:: https://img.shields.io/travis/crflynn/fbm.svg :target: https://travis-ci.org/crflynn/fbm
.. |codecov| image:: https://codecov.io/gh/crflynn/fbm/branch/master/graphs/badge.svg :target: https://codecov.io/gh/crflynn/fbm
.. |pypi| image:: https://img.shields.io/pypi/v/fbm.svg :target: https://pypi.python.org/pypi/fbm
.. |pyversions| image:: https://img.shields.io/pypi/pyversions/fbm.svg :target: https://pypi.python.org/pypi/fbm
The fbm package is available on PyPI and can be installed via pip:
.. code-block::
pip install fbm
Fractional Brownian motion can be generated via either Hosking's method, the Cholesky method, or the Davies-Harte method. All three methods are theoretically exact in generating a discretely sampled fBm/fGn.
Usage:
.. code-block:: python
from fbm import FBM
f = FBM(n=1024, hurst=0.75, length=1, method='daviesharte')
# or
f = FBM(1024, 0.75)
# Generate a fBm realization
fbm_sample = f.fbm()
# Generate a fGn realization
fgn_sample = f.fgn()
# Get the times associated with the fBm
t_values = f.times()
where n
is the number of equispaced increments desired for a fBm with Hurst
parameter hurst
on the interval [0, length
]. Method can be
either 'hosking'
, 'cholesky'
, or 'daviesharte'
. The fbm()
method returns a length n+1
array of discrete values for the fBm (includes
0). The fgn()
method returns a length n
array of fBm
increments, or fGn. The times()
method returns a length n+1
array of
times corresponding to the fBm realizations.
The n
and hurst
parameters are required. The length
parameter
defaults to 1 and method
defaults to 'daviesharte'
.
For simulating multiple realizations use the FBM class provided as above. Some intermediate values are cached for repeated simulation.
For one-off samples of fBm or fGn there are separate functions available:
.. code-block:: python
from fbm import fbm, fgn, times
# Generate a fBm realization
fbm_sample = fbm(n=1024, hurst=0.75, length=1, method='daviesharte')
# Generate a fGn realization
fgn_sample = fgn(n=1024, hurst=0.75, length=1, method='daviesharte')
# Get the times associated with the fBm
t_values = times(n=1024, length=1)
For fastest performance use the Davies and Harte method. Note that the
Davies and Harte method can fail if the Hurst parameter hurst
is close to
1 and there are a small amount of increments n
. If this occurs, a warning
is printed to the console and it will fallback to using Hosking's method to
generate the realization. See page 412 of the following paper for a more
detailed explanation:
Hosking's method:
Cholesky method:
Davies Harte method:
This package supports approximate generation of multifractional Brownian motion. The current method uses the Riemann–Liouville fractional integral representation of mBm.
Usage:
.. code-block:: python
import math
from fbm import MBM
# Example Hurst function with respect to time.
def h(t):
return 0.25 * math.sin(20*t) + 0.5
m = MBM(n=1024, hurst=h, length=1, method='riemannliouville')
# or
m = MBM(1024, h)
# Generate a mBm realization
mbm_sample = m.mbm()
# Generate a mGn realization
mgn_sample = m.mgn()
# Get the times associated with the mBm
t_values = m.times()
The hurst
argument here should be a callable that accepts one argument
and returns a float in (0, 1).
For one-off samples of mBm or mGn there are separate functions available:
.. code-block:: python
from fbm import mbm, mgn, times
# Define a hurst function
def h(t):
return 0.75 - 0.5 * t
# Generate a mbm realization
mbm_sample = mbm(n=1024, hurst=h, length=1, method='riemannliouville')
# Generate a fGn realization
mgn_sample = mgn(n=1024, hurst=h, length=1, method='riemannliouville')
# Get the times associated with the mBm
t_values = times(n=1024, length=1)
Riemann-Liouville representation method:
Approximate method originally proposed for fBm in
Adapted to approximate mBm in