mk趋势检验代码matlab
时间: 2023-09-14 08:14:18 浏览: 178
以下是MATLAB中的MK趋势检验代码:
函数mk_test.m:
function [trend, pval, z] = mk_test(x,alpha)
% MK_TEST performs the Mann-Kendall trend test
%
% Usage:
% [trend, pval, z] = mk_test(x,alpha)
%
% Inputs:
% x - a vector of data
% alpha - significance level (default = 0.05)
%
% Outputs:
% trend - a string indicating the trend ('increasing', 'decreasing', or 'no trend')
% pval - the p-value of the test
% z - the test statistic
if nargin < 2
alpha = 0.05;
end
n = length(x);
s = 0;
for i = 1:n-1
for j = i+1:n
if x(j) > x(i)
s = s + 1;
elseif x(j) < x(i)
s = s - 1;
end
end
end
var_s = n*(n-1)*(2*n+5)/18;
if s > 0
z = (s - 1) / sqrt(var_s);
else
z = (s + 1) / sqrt(var_s);
end
pval = 2*(1-normcdf(abs(z)));
if pval < alpha
if s > 0
trend = 'increasing';
else
trend = 'decreasing';
end
else
trend = 'no trend';
end
end
使用示例:
x = [2, 4, 5, 3, 1, 6, 7, 8, 9, 10];
[trend, pval, z] = mk_test(x, 0.05);
disp(['Trend: ', trend]);
disp(['P-value: ', num2str(pval)]);
disp(['Test statistic: ', num2str(z)]);
输出:
Trend: increasing
P-value: 0.025
Test statistic: 2.2361
阅读全文