MOD13Q1/mkt.py
def test(t, x, eps=None, alpha=None, Ha=None):
    """
    Runs the Mann-Kendall test for trend in time series data.

    Parameters
    ----------
    t : 1D numpy.ndarray
        array of the time points of measurements
    x : 1D numpy.ndarray
        array containing the measurements corresponding to entries of 't'
    eps : scalar, float, greater than zero
        least count error of measurements which help determine ties in the data
    alpha : scalar, float, greater than zero
        significance level of the statistical test (Type I error)
    Ha : string, options include 'up', 'down', 'upordown'
        type of test: one-sided ('up' or 'down') or two-sided ('updown')

    Returns
    -------
    MK : string
        result of the statistical test indicating whether or not to accept hte
        alternative hypothesis 'Ha'
    m : scalar, float
        slope of the linear fit to the data
    c : scalar, float
        intercept of the linear fit to the data
    p : scalar, float, greater than zero
        p-value of the obtained Z-score statistic for the Mann-Kendall test

    Raises
    ------
    AssertionError : error
                    least count error of measurements 'eps' is not given
    AssertionError : error
                    significance level of test 'alpha' is not given
    AssertionError : error
                    alternative hypothesis 'Ha' is not given

    """
    # assert a least count for the measurements x
    assert eps, "Please provide least count error for measurements 'x'"
    assert alpha, "Please provide significance level 'alpha' for the test"
    assert Ha, "Please provide the alternative hypothesis 'Ha'"

    # estimate sign of all possible (n(n-1)) / 2 differences
    n = len(t)
    sgn = np.zeros((n, n), dtype="int")
    for i in range(n):
        tmp = x - x[i]
        tmp[np.where(np.fabs(tmp) <= eps)] = 0.
        sgn[i] = np.sign(tmp)

    # estimate mean of the sign of all possible differences
    S = sgn[np.triu_indices(n, k=1)].sum()

    # estimate variance of the sign of all possible differences
    # 1. Determine no. of tie groups 'p' and no. of ties in each group 'q'
    np.fill_diagonal(sgn, eps * 1E6)
    i, j = np.where(sgn == 0.)
    ties = np.unique(x[i])
    p = len(ties)
    q = np.zeros(len(ties), dtype="int")
    for k in range(p):
        idx =  np.where(np.fabs(x - ties[k]) < eps)[0]
        q[k] = len(idx)
    # 2. Determine the two terms in the variance calculation
    term1 = n * (n - 1) * (2 * n + 5)
    term2 = (q * (q - 1) * (2 * q + 5)).sum()
    # 3. estimate variance
    varS = float(term1 - term2) / 18.

    # Compute the Z-score based on above estimated mean and variance
    if S > eps:
        Zmk = (S - 1) / np.sqrt(varS)
    elif np.fabs(S) <= eps:
        Zmk = 0.
    elif S < -eps:
        Zmk = (S + 1) / np.sqrt(varS)

    # compute test based on given 'alpha' and alternative hypothesis
    # note: for all the following cases, the null hypothesis Ho is:
    # Ho := there is no monotonic trend
    #
    # Ha := There is an upward monotonic trend
    if Ha == "up":
        Z_ = ndtri(1. - alpha)
        if Zmk >= Z_:
            MK = "accept Ha := upward trend"
        else:
            MK = "reject Ha := upward trend"
    # Ha := There is a downward monotonic trend
    elif Ha == "down":
        Z_ = ndtri(1. - alpha)
        if Zmk <= -Z_:
            MK = "accept Ha := downward trend"
        else:
            MK = "reject Ha := downward trend"
    # Ha := There is an upward OR downward monotonic trend
    elif Ha == "upordown":
        Z_ = ndtri(1. - alpha / 2.)
        if np.fabs(Zmk) >= Z_:
            MK = "accept Ha := upward OR downward trend"
        else:
            MK = "reject Ha := upward OR downward trend"

    # ----------
    # AS A BONUS
    # ----------
    # estimate the slope and intercept of the line
    m = np.corrcoef(t, x)[0, 1] * (np.std(x) / np.std(t))
    c = np.mean(x) - m * np.mean(t)

    # ----------
    # AS A BONUS
    # ----------
    # estimate the p-value for the obtained Z-score Zmk
    if S > eps:
        if Ha == "up":
            p = 1. - ndtr(Zmk)
        elif Ha == "down":
            p = ndtr(Zmk)
        elif Ha == "upordown":
            p = 0.5 * (1. - ndtr(Zmk))
    elif np.fabs(S) <= eps:
        p = 0.5
    elif S < -eps:
        if Ha == "up":
            p = 1. - ndtr(Zmk)
        elif Ha == "down":
            p = ndtr(Zmk)
        elif Ha == "upordown":
            p = 0.5 * (ndtr(Zmk))

    return Zmk, MK, m, c, p

    Runs the Mann-Kendall test for trend in time series data.

    Parameters
    ----------
    t : 1D numpy.ndarray
        array of the time points of measurements
    x : 1D numpy.ndarray
        array containing the measurements corresponding to entries of 't'
    eps : scalar, float, greater than zero
        least count error of measurements which help determine ties in the data
    alpha : scalar, float, greater than zero
        significance level of the statistical test (Type I error)
    Ha : string, options include 'up', 'down', 'upordown'
        type of test: one-sided ('up' or 'down') or two-sided ('updown')

    Returns
    -------
    MK : string
        result of the statistical test indicating whether or not to accept hte
        alternative hypothesis 'Ha'
    m : scalar, float
        slope of the linear fit to the data
    c : scalar, float
        intercept of the linear fit to the data
    p : scalar, float, greater than zero
        p-value of the obtained Z-score statistic for the Mann-Kendall test

    Raises
    ------
    AssertionError : error
                    least count error of measurements 'eps' is not given
    AssertionError : error
                    significance level of test 'alpha' is not given
    AssertionError : error
                    alternative hypothesis 'Ha' is not given

    
Runs the Mann-Kendall test for trend in time series data.
Parameters



Returns

MK : string
result of the statistical test indicating whether or not to accept hte
alternative hypothesis 'Ha'
m : scalar, float
slope of the linear fit to the data
c : scalar, float
intercept of the linear fit to the data
p : scalar, float, greater than zero
p-value of the obtained Z-score statistic for the Mann-Kendall test

Raises


Runs the Mann - Kendall test for trend in time series data . Parameters Returns MK : string result of the statistical test indicating whether or not to accept hte alternative hypothesis ' Ha ' m : scalar float slope of the linear fit to the data c : scalar float intercept of the linear fit to the data p : scalar float greater than zero p - value of the obtained Z - score statistic for the Mann - Kendall test Raises
def test(t, x, eps=None, alpha=None, Ha=None):
    assert eps, "Please provide least count error for measurements 'x'"
    assert alpha, "Please provide significance level 'alpha' for the test"
    assert Ha, "Please provide the alternative hypothesis 'Ha'"
    n = len(t)
    sgn = np.zeros((n, n), dtype="int")
    for i in range(n):
        tmp = x - x[i]
        tmp[np.where(np.fabs(tmp) <= eps)] = 0.
        sgn[i] = np.sign(tmp)
    S = sgn[np.triu_indices(n, k=1)].sum()
    np.fill_diagonal(sgn, eps * 1E6)
    i, j = np.where(sgn == 0.)
    ties = np.unique(x[i])
    p = len(ties)
    q = np.zeros(len(ties), dtype="int")
    for k in range(p):
        idx =  np.where(np.fabs(x - ties[k]) < eps)[0]
        q[k] = len(idx)
    term1 = n * (n - 1) * (2 * n + 5)
    term2 = (q * (q - 1) * (2 * q + 5)).sum()
    varS = float(term1 - term2) / 18.
    if S > eps:
        Zmk = (S - 1) / np.sqrt(varS)
    elif np.fabs(S) <= eps:
        Zmk = 0.
    elif S < -eps:
        Zmk = (S + 1) / np.sqrt(varS)
     Ha := There is an upward monotonic trend
    if Ha == "up":
        Z_ = ndtri(1. - alpha)
        if Zmk >= Z_:
            MK = "accept Ha := upward trend"
        else:
            MK = "reject Ha := upward trend"
     Ha := There is a downward monotonic trend
    elif Ha == "down":
        Z_ = ndtri(1. - alpha)
        if Zmk <= -Z_:
            MK = "accept Ha := downward trend"
        else:
            MK = "reject Ha := downward trend"
     Ha := There is an upward OR downward monotonic trend
    elif Ha == "upordown":
        Z_ = ndtri(1. - alpha / 2.)
        if np.fabs(Zmk) >= Z_:
            MK = "accept Ha := upward OR downward trend"
        else:
            MK = "reject Ha := upward OR downward trend"
     ----------
     AS A BONUS
     ----------
     estimate the slope and intercept of the line
    m = np.corrcoef(t, x)[0, 1] * (np.std(x) / np.std(t))
    c = np.mean(x) - m * np.mean(t)
     ----------
     AS A BONUS
     ----------
     estimate the p-value for the obtained Z-score Zmk
    if S > eps:
        if Ha == "up":
            p = 1. - ndtr(Zmk)
        elif Ha == "down":
            p = ndtr(Zmk)
        elif Ha == "upordown":
            p = 0.5 * (1. - ndtr(Zmk))
    elif np.fabs(S) <= eps:
        p = 0.5
    elif S < -eps:
        if Ha == "up":
            p = 1. - ndtr(Zmk)
        elif Ha == "down":
            p = ndtr(Zmk)
        elif Ha == "upordown":
            p = 0.5 * (ndtr(Zmk))
    return Zmk, MK, m, c, p
def test ( t , x , eps = None , alpha = None , Ha = None ) : assert eps , "Please provide least count error for measurements 'x'" assert alpha , "Please provide significance level 'alpha' for the test" assert Ha , "Please provide the alternative hypothesis 'Ha'" n = len ( t ) sgn = np . zeros ( ( n , n ) , dtype = "int" ) for i in range ( n ) : tmp = x - x [ i ] tmp [ np . where ( np . fabs ( tmp ) <= eps ) ] = 0. sgn [ i ] = np . sign ( tmp ) S = sgn [ np . triu_indices ( n , k = 1 ) ] . sum ( ) np . fill_diagonal ( sgn , eps * 1E6 ) i , j = np . where ( sgn == 0. ) ties = np . unique ( x [ i ] ) p = len ( ties ) q = np . zeros ( len ( ties ) , dtype = "int" ) for k in range ( p ) : idx = np . where ( np . fabs ( x - ties [ k ] ) < eps ) [ 0 ] q [ k ] = len ( idx ) term1 = n * ( n - 1 ) * ( 2 * n + 5 ) term2 = ( q * ( q - 1 ) * ( 2 * q + 5 ) ) . sum ( ) varS = float ( term1 - term2 ) / 18. if S > eps : Zmk = ( S - 1 ) / np . sqrt ( varS ) elif np . fabs ( S ) <= eps : Zmk = 0. elif S < - eps : Zmk = ( S + 1 ) / np . sqrt ( varS ) if Ha == "up" : Z_ = ndtri ( 1. - alpha ) if Zmk >= Z_ : MK = "accept Ha := upward trend" else : MK = "reject Ha := upward trend" elif Ha == "down" : Z_ = ndtri ( 1. - alpha ) if Zmk <= - Z_ : MK = "accept Ha := downward trend" else : MK = "reject Ha := downward trend" elif Ha == "upordown" : Z_ = ndtri ( 1. - alpha / 2. ) if np . fabs ( Zmk ) >= Z_ : MK = "accept Ha := upward OR downward trend" else : MK = "reject Ha := upward OR downward trend" m = np . corrcoef ( t , x ) [ 0 , 1 ] * ( np . std ( x ) / np . std ( t ) ) c = np . mean ( x ) - m * np . mean ( t ) if S > eps : if Ha == "up" : p = 1. - ndtr ( Zmk ) elif Ha == "down" : p = ndtr ( Zmk ) elif Ha == "upordown" : p = 0.5 * ( 1. - ndtr ( Zmk ) ) elif np . fabs ( S ) <= eps : p = 0.5 elif S < - eps : if Ha == "up" : p = 1. - ndtr ( Zmk ) elif Ha == "down" : p = ndtr ( Zmk ) elif Ha == "upordown" : p = 0.5 * ( ndtr ( Zmk ) ) return Zmk , MK , m , c , p
Runs the Mann-Kendall test for trend in time series data.
Runs the Mann - Kendall test for trend in time series data .
"""
    Runs the Mann-Kendall test for trend in time series data.

    Parameters
    ----------
    t : 1D numpy.ndarray
        array of the time points of measurements
    x : 1D numpy.ndarray
        array containing the measurements corresponding to entries of 't'
    eps : scalar, float, greater than zero
        least count error of measurements which help determine ties in the data
    alpha : scalar, float, greater than zero
        significance level of the statistical test (Type I error)
    Ha : string, options include 'up', 'down', 'upordown'
        type of test: one-sided ('up' or 'down') or two-sided ('updown')

    Returns
    -------
    MK : string
        result of the statistical test indicating whether or not to accept hte
        alternative hypothesis 'Ha'
    m : scalar, float
        slope of the linear fit to the data
    c : scalar, float
        intercept of the linear fit to the data
    p : scalar, float, greater than zero
        p-value of the obtained Z-score statistic for the Mann-Kendall test

    Raises
    ------
    AssertionError : error
                    least count error of measurements 'eps' is not given
    AssertionError : error
                    significance level of test 'alpha' is not given
    AssertionError : error
                    alternative hypothesis 'Ha' is not given

    """ # assert a least count for the measurements x # estimate sign of all possible (n(n-1)) / 2 differences # estimate mean of the sign of all possible differences # estimate variance of the sign of all possible differences # 1. Determine no. of tie groups 'p' and no. of ties in each group 'q' # 2. Determine the two terms in the variance calculation # 3. estimate variance # Compute the Z-score based on above estimated mean and variance # compute test based on given 'alpha' and alternative hypothesis # note: for all the following cases, the null hypothesis Ho is: # Ho := there is no monotonic trend # # Ha := There is an upward monotonic trend # Ha := There is a downward monotonic trend # Ha := There is an upward OR downward monotonic trend # ---------- # AS A BONUS # ---------- # estimate the slope and intercept of the line # ---------- # AS A BONUS # ---------- # estimate the p-value for the obtained Z-score Zmk
t
x
eps
alpha
Ha