天天看點

趨勢檢驗方法(二)MK趨勢檢驗MK(Mann-Kendall)檢驗

MK(Mann-Kendall)檢驗

a基本原理:

使用MK算法檢驗時序資料大緻趨勢,趨勢分為無明顯趨勢(穩定)、趨勢上升、趨勢下降。

MK檢驗的基礎:

  1. 當沒有趨勢時,随時間獲得的資料是獨立同分布的,資料随着時間不是連續相關的。
  2. 所獲得的時間序列上的資料代表了采樣時的真實條件,樣本要具有代表性。
  3. MK檢驗不要求資料是正态分布,也不要求變化趨勢是線性的。
  4. 如果有缺失值或者值低于一個或多個檢測限制,是可以計算MK檢測的,但檢測性 能會受到不利影響。
  5. 獨立性假設要求樣本之間的時間足夠大,這樣在不同時間收集的測量值之間不存在 相關性。

b MK算法原理:

趨勢檢驗方法(二)MK趨勢檢驗MK(Mann-Kendall)檢驗
趨勢檢驗方法(二)MK趨勢檢驗MK(Mann-Kendall)檢驗
趨勢檢驗方法(二)MK趨勢檢驗MK(Mann-Kendall)檢驗

c方法優缺點:

優點:功能強大,不需要樣本遵從一定的分布,部分資料缺失不會對結果造成影響,不受少數異常值的幹擾,适用性強。

不但可以檢驗時間序列的變化趨勢,還可以檢驗時間序列是否發生了突變。

缺點:暫未發現,待後續補充。

d算法入口:

目前MK檢驗還沒有可直接調用的函數,具體MK模闆可以根據下面所寫的執行個體去修改

e執行個體參考:

from scipy.stats import norm
import numpy as np


def mk(x, alpha=0.1):  # 0<alpha<0.5 1-alpha/2為置信度
    n = len(x)

    # 計算S的值
    s = 0
    for j in range(n - 1):
        for i in range(j + 1, n):
            s += np.sign(x[i] - x[j])

    # 判斷x裡面是否存在重複的數,輸出唯一數隊列unique_x,重複數數量隊列tp
    unique_x, tp = np.unique(x, return_counts=True)
    g = len(unique_x)

    # 計算方差VAR(S)
    if n == g:  # 如果不存在重複點
        var_s = (n * (n - 1) * (2 * n + 5)) / 18
    else:
        var_s = (n * (n - 1) * (2 * n + 5) - np.sum(tp * (tp - 1) * (2 * tp + 5))) / 18

    # 計算z_value
    if n <= 10:  # n<=10屬于特例
        z = s / (n * (n - 1) / 2)
    else:
        if s > 0:
            z = (s - 1) / np.sqrt(var_s)
        elif s < 0:
            z = (s + 1) / np.sqrt(var_s)
        else:
            z = 0

    # 計算p_value,可以選擇性先對p_value進行驗證
    p = 2 * (1 - norm.cdf(abs(z)))

    # 計算Z(1-alpha/2)
    h = abs(z) > norm.ppf(1 - alpha / 2)

    # 趨勢判斷
    if (z < 0) and h:
        trend = 'decreasing'
    elif (z > 0) and h:
        trend = 'increasing'
    else:
        trend = 'no trend'

    return trend
           

f參考文獻:

python中的Mann-Kendall單調趨勢檢驗--及原理說明https://blog.csdn.net/liuchengzimozigreat/article/details/87931248

norm.ppf() norm.cdf() https://blog.csdn.net/shanchuan2012/article/details/52901758/

知乎 時序資料常用趨勢檢測方法 https://zhuanlan.zhihu.com/p/112703276

序列的趨勢存在性檢驗:Cox-Stuart test和Mann-Kendall testhttps://blog.csdn.net/weixin_43850016/article/details/106457201

時間序列資料趨勢分析 Cox-Stuart、Mann-Kendall、Dickey-Fullerhttps://blog.csdn.net/qq_34356768/article/details/106559399