1616
1717def 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 )
0 commit comments