Skip to content

Commit b09569e

Browse files
committed
Reorganizing parameters moments. References in Qratio & parameters updated
1 parent 438137a commit b09569e

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

JumpDiff/Qratio.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## This is based on Lehnertz 2018, now implemented by Leonardo Rydin Gorjão and
2-
# Pedro G. Lind for direct application.
1+
## This is based on Anvari, M., Tabar, M. R. R., Peinke, J., Lehnertz, K.,
2+
# 'Disentangling the stochastic behavior of complex time series.' Scientific
3+
# Reports, 6, 35435, 2016. doi: 10.1038/srep35435, now implemented by Leonardo
4+
# Rydin Gorjão and Pedro G. Lind for direct application.
35

46
import numpy as np
57
from .moments import moments
@@ -44,9 +46,13 @@ def Qratio(lag: np.ndarray, timeseries: np.ndarray, loc: int=None,
4446
4547
References
4648
----------
47-
K. Lehnertz, L. Zabawa, and M. Reza Rahimi Tabar 'Characterizing abrupt
48-
transitions in stochastic dynamics'. New Journal of Physics, 20(11):113043,
49-
2018, doi: 10.1088/1367-2630/aaf0d7.
49+
Anvari, M., Tabar, M. R. R., Peinke, J., Lehnertz, K., 'Disentangling the
50+
stochastic behavior of complex time series.' Scientific Reports, 6, 35435,
51+
2016. doi: 10.1038/srep35435.
52+
53+
Lehnertz, K., Zabawa, L., and Tabar, M. R. R., 'Characterizing abrupt
54+
transitions in stochastic dynamics.' New Journal of Physics, 20(11):113043,
55+
2018. doi: 10.1088/1367-2630/aaf0d7.
5056
"""
5157

5258
# Force lag to be ints, ensure lag > order + 1, and removes duplicates
@@ -55,7 +61,7 @@ def Qratio(lag: np.ndarray, timeseries: np.ndarray, loc: int=None,
5561

5662
# Assert if timeseries is 1 dimensional
5763
if timeseries.ndim > 1:
58-
assert timeseries.shape[1] == 1, "Timeseries needs to be 1 dimensional"
64+
assert timeseries.shape[1] == 1, "Timeseries needs to be 1-dimensional"
5965

6066
# Find maximum of distribution
6167
if loc == None:

JumpDiff/formulae.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Delevoped b Leonardo Rydin Gorjão and Pedro G. Lind
1+
## Delevoped by Leonardo Rydin Gorjão and Pedro G. Lind.
22

33
from sympy import bell, symbols, factorial, simplify
44

@@ -9,10 +9,9 @@ def M_formula(power, tau = True):
99
1010
.. math::
1111
12-
M_n(x^{\prime},\tau) \sim (n!)\tau D_n(x^{\prime}) + \frac{(n!)\tau^2}{2}
12+
M_n(x^{\prime},\tau) \sim (n!)\tau D_n(x^{\prime}) + \frac{(n!)\tau^2}{2}
1313
\sum_{m=1}^{n-1} D_m(x^{\prime}) D_{n-m}(x^{\prime})
1414
15-
1615
Parameters
1716
----------
1817
power: int

JumpDiff/moments.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
def moments(timeseries: np.ndarray, bw: float=None, bins: np.ndarray=None,
1818
power: int=6, lag: list=[1], correction: bool=True, norm: bool=False,
19-
kernel: callable=None, tol: float=1e-10,
20-
conv_method: str='auto') -> np.ndarray:
19+
kernel: callable=None, tol: float=1e-10, conv_method: str='auto',
20+
verbose: bool=False) -> np.ndarray:
2121
r"""
2222
Estimates the moments of the Kramers─Moyal expansion from a timeseries using
2323
a Nadaraya─Watson kernel estimator method. These later can be turned into
@@ -28,44 +28,47 @@ def moments(timeseries: np.ndarray, bw: float=None, bins: np.ndarray=None,
2828
timeseries: np.ndarray
2929
A 1-dimensional timeseries.
3030
31-
bins: np.ndarray (defaul ``None``)
31+
bw: float
32+
Desired bandwidth of the kernel. A value of 1 occupies the full space of
33+
the bin space. Recommended are values ``0.005 < bw < 0.4``.
34+
35+
bins: np.ndarray (default ``None``)
3236
The number of bins for each dimension, defaults to ``np.array([5000])``.
3337
This is the underlying space for the Kramers─Moyal conditional moments.
3438
35-
power: int (defaul ``6``)
39+
power: int (default ``6``)
3640
Upper limit of the the Kramers─Moyal conditional moments to calculate.
3741
It will generate all Kramers─Moyal conditional moments up to power.
3842
39-
lag: list (defaul ``1``)
43+
lag: list (default ``1``)
4044
Calculates the Kramers─Moyal conditional moments at each indicated lag,
4145
i.e., for ``timeseries[::lag[]]``. Defaults to ``1``, the shortest
4246
timestep in the data.
4347
44-
corrections: bool (defaul ``True``)
48+
corrections: bool (default ``True``)
4549
Implements the second-order corrections of the Kramers─Moyal conditional
4650
moments directly
4751
48-
norm: bool (defaul ``False``)
52+
norm: bool (default ``False``)
4953
Sets the normalisation. ``False`` returns the Kramers─Moyal conditional
5054
moments, and ``True`` returns the Kramers─Moyal coefficients.
5155
52-
kernel: callable (defaul ``None``)
56+
kernel: callable (default ``None``)
5357
Kernel used to convolute with the Kramers─Moyal conditional moments. To
5458
select example an Epanechnikov kernel use
5559
kernel = kernels.epanechnikov
5660
If None the Epanechnikov kernel will be used.
5761
58-
bw: float
59-
Desired bandwidth of the kernel. A value of 1 occupies the full space of
60-
the bin space. Recommended are values ``0.005 < bw < 0.4``.
61-
62-
tol: float
62+
tol: float (default ``1e-10``)
6363
Round to zero absolute values smaller than ``tol``, after convolutions.
6464
65-
conv_method: str
65+
conv_method: str (default ``auto``)
6666
A string indicating which method to use to calculate the convolution.
6767
docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve.
6868
69+
verbose: bool (default ``False``)
70+
If ``True`` will report on the bandwidth used.
71+
6972
Returns
7073
-------
7174
edges: np.ndarray
@@ -83,11 +86,9 @@ def moments(timeseries: np.ndarray, bw: float=None, bins: np.ndarray=None,
8386
if len(timeseries.shape) == 1:
8487
timeseries = timeseries.reshape(-1, 1)
8588

86-
assert len(timeseries.shape) == 2, "Timeseries must (n, dims) shape"
89+
assert len(timeseries.shape) == 2, "Timeseries must be 1-dimensional"
8790
assert timeseries.shape[0] > 0, "No data in timeseries"
8891

89-
n, dims = timeseries.shape
90-
9192
if bins is None:
9293
bins = np.array([5000])
9394

@@ -98,21 +99,22 @@ def moments(timeseries: np.ndarray, bw: float=None, bins: np.ndarray=None,
9899
if len(powers.shape) == 1:
99100
powers = powers.reshape(-1, 1)
100101

101-
assert (powers[0] == [0] * dims).all(), "First power must be zero"
102-
assert dims == powers.shape[1], "Powers not matching timeseries' dimension"
103-
assert dims == bins.shape[0], "Bins not matching timeseries' dimension"
104-
105102
if bw is None:
106-
bw = silvermans_rule(timeseries)*2.
103+
bw = silvermans_rule(timeseries)
107104
elif callable(bw):
108105
bw = bw(timeseries)
106+
109107
assert bw > 0.0, "Bandwidth must be > 0"
110108

111109
if kernel is None:
112110
kernel = epanechnikov
113111
assert kernel in _kernels, "Kernel not found"
114112

115-
edges, moments = _moments(timeseries, bins, powers, lag, kernel, bw, tol, conv_method)
113+
if verbose == True:
114+
print(r'bandwidth = {:f}'.format(bw) + r', bins = {:d}'.format(bins[0]))
115+
116+
edges, moments = _moments(timeseries, bins, powers, lag, kernel, bw, tol,
117+
conv_method)
116118

117119
if correction == True:
118120
moments = corrections(m = moments, power = power)

JumpDiff/parameters.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ def jump_amplitude(moments: np.ndarray, tol: float=1e-10,
3939
4040
References
4141
----------
42-
K. Lehnertz, L. Zabawa, and M. Reza Rahimi Tabar 'Characterizing abrupt
43-
transitions in stochastic dynamics'. New Journal of Physics, 20(11):113043,
44-
2018, doi: 10.1088/1367-2630/aaf0d7.
42+
Anvari, M., Tabar, M. R. R., Peinke, J., Lehnertz, K., 'Disentangling the
43+
stochastic behavior of complex time series.' Scientific Reports, 6, 35435,
44+
2016. doi: 10.1038/srep35435.
45+
46+
Lehnertz, K., Zabawa, L., and Tabar, M. R. R., 'Characterizing abrupt
47+
transitions in stochastic dynamics.' New Journal of Physics, 20(11):113043,
48+
2018. doi: 10.1088/1367-2630/aaf0d7.
4549
"""
4650

4751
# pre-allocate variable
@@ -103,9 +107,13 @@ def jump_rate(moments: np.ndarray, xi_est: np.ndarray=None, tol: float=1e-10,
103107
104108
References
105109
----------
106-
K. Lehnertz, L. Zabawa, and M. Reza Rahimi Tabar 'Characterizing abrupt
107-
transitions in stochastic dynamics'. New Journal of Physics, 20(11):113043,
108-
2018, doi: 10.1088/1367-2630/aaf0d7.
110+
Anvari, M., Tabar, M. R. R., Peinke, J., Lehnertz, K., 'Disentangling the
111+
stochastic behavior of complex time series.' Scientific Reports, 6, 35435,
112+
2016. doi: 10.1038/srep35435.
113+
114+
Lehnertz, K., Zabawa, L., and Tabar, M. R. R., 'Characterizing abrupt
115+
transitions in stochastic dynamics.' New Journal of Physics, 20(11):113043,
116+
2018. doi: 10.1088/1367-2630/aaf0d7.
109117
"""
110118

111119
# pre-allocate variable

0 commit comments

Comments
 (0)