matlab定义了MK趋势检验函数。在计算[a1,a2]=MKtrend([295.744784211355 295.869657515568 295.776872743116 295.827931230325 295.767622091993 295.825248763572 295.841459525828 295.596370973988 295.988910863802 296.184767378278 295.825783291105 295.892601193441 295.933778313033 296.024551318227 295.978955352449 296.223861316325 296.098171360482 296.140965276669 296.123895689794 296.215794404633]); 时,返回的a2值为2.468135655243587e+02,但是直接计算的话a2应该是0.022961370859517,定义的函数是哪里出错了?
%% MK趋势检验
function [z,beta] = MKtrend(time_series)
n = length(time_series);
for i = 1:n
s(i) = sum(time_series(i:end)>time_series(i)) - sum(time_series(i:end)<time_series(i));
end
S = sum(s);
%s是正态分布,均值为零,方差:
varS = n*(n-1)*(2*n+5)/18;
%当n>10时,标准的正态统计变量计算:
if S > 0
z = (S-1)/sqrt(varS);
elseif S == 0
z = 0;
else
z = (S+1)/sqrt(varS);
end
%计算slope
ndash=n*(n-1)/2;
b=zeros(ndash,1);
j=1;
for ii=1:n-1
for jj=ii+1:n
b(j)=(time_series(jj)-time_series(ii))/(jj-ii);
j=j+1;
end
end
beta=median(b);
end