我有近 40 个站点、36 年的变风时间序列数据 ()(详细信息请参见示例屏幕截图)。
我需要根据建议对该数据运行标准正态均匀性检验和佩蒂特检验。它们在 python 中可用吗?
我在 python 文档和包中找不到上述测试的任何代码。
我需要一些帮助来了解是否有任何包包含这些测试。
R中的代码如下:
snht(data, period, robust = F, time = NULL, scaled = TRUE, rmSeasonalPeriod = Inf, ...)
但是,到目前为止还没有结果......只有错误。
关于 Pettitt 测试,我发现了 this python 实现。
我认为有一个小错字:第 19 行的
t+1
实际上应该是 t
。
我还开发了一种更快的矢量化实现::
import numpy as np
def pettitt_test(X):
"""
Pettitt test calculated following Pettitt (1979): https://www.jstor.org/stable/2346729?seq=4#metadata_info_tab_contents
"""
T = len(X)
U = []
for t in range(T): # t is used to split X into two subseries
X_stack = np.zeros((t, len(X[t:]) + 1), dtype=int)
X_stack[:,0] = X[:t] # first column is each element of the first subseries
X_stack[:,1:] = X[t:] # all rows after the first element are the second subseries
U.append(np.sign(X_stack[:,0] - X_stack[:,1:].transpose()).sum()) # sign test between each element of the first subseries and all elements of the second subseries, summed.
tau = np.argmax(np.abs(U)) # location of change (first data point of second sub-series)
K = np.max(np.abs(U))
p = 2 * np.exp(-6 * K**2 / (T**3 + T**2))
return (tau, p)