16
16
17
17
def moments (timeseries : np .ndarray , bw : float = None , bins : np .ndarray = None ,
18
18
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 :
21
21
r"""
22
22
Estimates the moments of the Kramers─Moyal expansion from a timeseries using
23
23
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,
28
28
timeseries: np.ndarray
29
29
A 1-dimensional timeseries.
30
30
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``)
32
36
The number of bins for each dimension, defaults to ``np.array([5000])``.
33
37
This is the underlying space for the Kramers─Moyal conditional moments.
34
38
35
- power: int (defaul ``6``)
39
+ power: int (default ``6``)
36
40
Upper limit of the the Kramers─Moyal conditional moments to calculate.
37
41
It will generate all Kramers─Moyal conditional moments up to power.
38
42
39
- lag: list (defaul ``1``)
43
+ lag: list (default ``1``)
40
44
Calculates the Kramers─Moyal conditional moments at each indicated lag,
41
45
i.e., for ``timeseries[::lag[]]``. Defaults to ``1``, the shortest
42
46
timestep in the data.
43
47
44
- corrections: bool (defaul ``True``)
48
+ corrections: bool (default ``True``)
45
49
Implements the second-order corrections of the Kramers─Moyal conditional
46
50
moments directly
47
51
48
- norm: bool (defaul ``False``)
52
+ norm: bool (default ``False``)
49
53
Sets the normalisation. ``False`` returns the Kramers─Moyal conditional
50
54
moments, and ``True`` returns the Kramers─Moyal coefficients.
51
55
52
- kernel: callable (defaul ``None``)
56
+ kernel: callable (default ``None``)
53
57
Kernel used to convolute with the Kramers─Moyal conditional moments. To
54
58
select example an Epanechnikov kernel use
55
59
kernel = kernels.epanechnikov
56
60
If None the Epanechnikov kernel will be used.
57
61
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``)
63
63
Round to zero absolute values smaller than ``tol``, after convolutions.
64
64
65
- conv_method: str
65
+ conv_method: str (default ``auto``)
66
66
A string indicating which method to use to calculate the convolution.
67
67
docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve.
68
68
69
+ verbose: bool (default ``False``)
70
+ If ``True`` will report on the bandwidth used.
71
+
69
72
Returns
70
73
-------
71
74
edges: np.ndarray
@@ -83,11 +86,9 @@ def moments(timeseries: np.ndarray, bw: float=None, bins: np.ndarray=None,
83
86
if len (timeseries .shape ) == 1 :
84
87
timeseries = timeseries .reshape (- 1 , 1 )
85
88
86
- assert len (timeseries .shape ) == 2 , "Timeseries must (n, dims) shape "
89
+ assert len (timeseries .shape ) == 2 , "Timeseries must be 1-dimensional "
87
90
assert timeseries .shape [0 ] > 0 , "No data in timeseries"
88
91
89
- n , dims = timeseries .shape
90
-
91
92
if bins is None :
92
93
bins = np .array ([5000 ])
93
94
@@ -98,21 +99,22 @@ def moments(timeseries: np.ndarray, bw: float=None, bins: np.ndarray=None,
98
99
if len (powers .shape ) == 1 :
99
100
powers = powers .reshape (- 1 , 1 )
100
101
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
-
105
102
if bw is None :
106
- bw = silvermans_rule (timeseries )* 2.
103
+ bw = silvermans_rule (timeseries )
107
104
elif callable (bw ):
108
105
bw = bw (timeseries )
106
+
109
107
assert bw > 0.0 , "Bandwidth must be > 0"
110
108
111
109
if kernel is None :
112
110
kernel = epanechnikov
113
111
assert kernel in _kernels , "Kernel not found"
114
112
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 )
116
118
117
119
if correction == True :
118
120
moments = corrections (m = moments , power = power )
0 commit comments