Diff of /pyeeg/hurst.py [000000] .. [1bd16f]

Switch to unified view

a b/pyeeg/hurst.py
1
import numpy
2
3
4
def hurst(X):
5
    """ Compute the Hurst exponent of X. If the output H=0.5,the behavior
6
    of the time-series is similar to random walk. If H<0.5, the time-series
7
    cover less "distance" than a random walk, vice verse.
8
9
    Parameters
10
    ----------
11
12
    X
13
14
        list
15
16
        a time series
17
18
    Returns
19
    -------
20
    H
21
22
        float
23
24
        Hurst exponent
25
26
    Notes
27
    --------
28
    Author of this function is Xin Liu
29
30
    Examples
31
    --------
32
33
    >>> import pyeeg
34
    >>> from numpy.random import randn
35
    >>> a = randn(4096)
36
    >>> pyeeg.hurst(a)
37
    0.5057444
38
39
    """
40
    X = numpy.array(X)
41
    N = X.size
42
    T = numpy.arange(1, N + 1)
43
    Y = numpy.cumsum(X)
44
    Ave_T = Y / T
45
46
    S_T = numpy.zeros(N)
47
    R_T = numpy.zeros(N)
48
49
    for i in range(N):
50
        S_T[i] = numpy.std(X[:i + 1])
51
        X_T = Y - T * Ave_T[i]
52
        R_T[i] = numpy.ptp(X_T[:i + 1])
53
54
    R_S = R_T / S_T
55
    R_S = numpy.log(R_S)[1:]
56
    n = numpy.log(T)[1:]
57
    A = numpy.column_stack((n, numpy.ones(n.size)))
58
    [m, c] = numpy.linalg.lstsq(A, R_S)[0]
59
    H = m
60
    return H