整体的思想是:
- 首先从中间对数据集切一刀,切成两半
- 两边的数据一一对应的比大小,搜集一边的数据就行,看有多少比对面的大,有多少不如对面的
- 如果比对面大的数和不如对面的数,近似服从二项分布,这不就成掷色子了么?说明没趋势,随机的啊;但如果不近似服从二项分布,就说明有趋势
以上便是Cox-Staut趋势检验的基本思想
方便理解可以参考:https://wenku.baidu.com/view/cec731b981c758f5f61f6760.html
示例代码
import numpy as np
def cos_staut_check(data):
"""Cox-Staut趋势检验
"""
from scipy.stats import binom
lst = data.copy()
raw_len = len(lst)
if raw_len % 2 == 1:
lst = np.delete(lst, int((raw_len - 1) / 2))
c = int(len(lst) / 2)
n_pos = n_neg = 0
for i in range(c):
diff = lst[i + c] - lst[i]
if diff > 0:
n_pos += 1
elif diff < 0:
n_neg += 1
else:
continue
num = n_pos + n_neg
k = min(n_pos, n_neg)
p_value = 2 * binom.cdf(k, num, 0.5)
if p_value >= 0.05:
return "no trend"
elif n_neg > n_pos:
return "decreasing"
elif n_pos > n_neg:
return "increasing"
if __name__ == '__main__':
data = np.array([1, 2, 3, 4, 3, 5, 5, 7, 4, 7, ])
print(cos_staut_check(data))