version 1.0.0.0 (1.93 KB) by
Simone Fatichi

Mann-Kendall non-parametric trend test.

The code performs original two tailed Mann-Kendall test. It tests the null hypothesis of trend absence in the vector V, against the alternative of trend.

The result of the test is returned in H = 1 indicates a rejection of the null hypothesis at the alpha significance level. H = 0 indicates a failure to reject the null hypothesis at the alpha significance level.

Simone Fatichi (2021). Mann-Kendall Test (https://www.mathworks.com/matlabcentral/fileexchange/25531-mann-kendall-test), MATLAB Central File Exchange. Retrieved .

Created with
R2007b

Compatible with any release

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Gaige HydenRunning the code after defining my V and alpha returns a logical answer of 1? It does not give me H or P values... does the logical answer of 1 mean its equivalent to H = 1?

Thanks

Dominic RichardsonIris CaoAndreas ReulDear Simone.

Great work. Please could yo tell me how to interprete the p value 0-1 (with alpha 0,05(default).

1 would be an p value of 0,05?, yst to explain the reader.

I have a map with 250 X 600 pixel, is possible to know if the trend is positive or negative?

Thanks in advance.

Jitesh DadichKirsten PurschkeWhat means the p_value exactly?

Abhishek BanerjeeHow can I apply MK trend test on the grid data. not in the station data? please suggest

jitesh dadhichHIMANSHU PORWALAbed ChehadehHello, I have gaps in my precipitation data. If I don't fill these (i.e. I keep them NaN) I am unable to obtain p and z values. Could you plz suggest on how to overcome this issue as other softwares (R, minitab, xlstat) are capable of doing the analysis with missing values. If i fill the missing cells with the average I am getting different values compared to those obtained in aforementioned softwares.

Thank you,

Nirav RajaniWhat is the procedure to run the mann-kendall trend test

MOHAMMAD YASIR ALIfor every value of alpha I am getting p_value=0 but pz is changing. what does this signify for my result. z is 8.3496 and s is 3366.

AdamIt would be good if the function didn't rely on other specialized functions that are only available in toolboxes.

Andre AlmagroHi everyone! I'm working with a 34 years of annual rainfall data. I have 11299 points (latitude and longitude) that cover all Brazil's territory. How can I apply this function for each point? First column represent the year of observation and the others represent each point. So I have a variable with 34x11300 values. How can I solve this? Thank you!!!

Lei HeIsabel Espadas SánchezI'm working with a time series that have NaNs and I need to test it with the MK test, but at this moment I have any idea how use this code with my data since always gives me H = 0 and p_value=NaN. You have any idea how I can solve my problem?

arain zzAli A@blanca ortiz

You can use the following code

function[H,p_value]=Mann_Kendall(V,alpha)

V=reshape(V,length(V),1);

alpha = alpha/2; %

n=length(V);

S=0;

for i=1:n-1

S = S + sum(sign(V(i+1:n) - V(i)));

end

h=1;

while ~isempty(V)

g=find(V==V(1));

tp=length(g);

Sum(h)=tp*(tp-1)*(2*tp+5); %#ok<AGROW>

V(g)= [];

h=h+1;

end

VarS=((n*(n-1)*(2*n+5))-sum(Sum))/18;

%Standard deviation

De=sqrt(VarS);

if S>=0;

z= ((S-1)/De)*(S~=0);

else

z= ((S+1)/De);

end

p_value=2*(1-normcdf(abs(z),0,1)); % tail on both sides

pz=norminv(1-alpha,0,1);

H=abs(z)>pz;

return

Ali AAli Ablanca ortizCan anyone help me please? I have tried different types of vectors and it doesn't work. I am getting two errors, first:

??? Undefined function or method 'Sqrt' for input arguments of type 'double'

and then too many outputs.

I would really appreciate if anyone could tell me an example of the vectors and if there is any other mistake. Thanks

V=reshape(V,length(V),1);

alpha = alpha/2; %

n=length(V);

i=0;S=0;

for i=1:n-1

S = S + sum(sign(V(i+1:n) - V(i)));

end

h=1;

while ~isempty(V)

g=find(V==V(1));

tp=length(g);

Sum(h)=tp*(tp-1)*(2*tp+5);

V(g)= [];

h=h+1;

end

VarS=((n*(n-1)*(2*n-5))-sum(Sum))/18;

%Standard deviation

De=Sqrt(VarS);

If S>=0;

Z= ((S-1)/De)*(S~=0);

else

z= ((S+1)/De);

end

p_value=2*(1-normcdf(abs(z),0,1)); % tail on both sides

pz=norminv(1-alpha,0,1);

H=abs(z)>pz;

return

JieWhat is the vector type for this test? I tried different types but they didn't work.

HydroI found exactly what i was doing..however, i am getting an error message of too many output argument.Any help would be appreciated.

function [H,P] = mann_kendall(x,alpha)

if nargin<2;

alpha=0.05;

end

n=length(x);

i=0; j=0; S=0;

alpha=alpha/2;

for i = 1:n-1

for j=i+1:n

S = S + sign(x(j) - x(i));

end

end

VS=(n*(n-1))*(2*n+5)/18; % Variance

De=sqrt(VS); % Standard Deviation

if S >= 0;

Z=((S-1)/De)*(S~=0);

else

Z=((S+1)/De);

end

P=2*(1-normcdf(abs(Z),0,1)); % tail on both side

pz = norminv(1-alpha,0,1);

H=abs(Z)> pz;

end

Roque SantosHello,

Is showing an error that says: "Undefined function 'Mann_Kendall_Modified' for input arguments of type 'double'."

How do I correct?

Ehsan JalilvandHi Simone,

First of all thank you. I've made some minor change to your code as follow,to include tied groups in Calculating the variance:

after line 48:

% Determining the variance (tied group included)

h=1;

while ~isempty(V)

g=find(V==V(1));

tp=length(g);

Sum(h)=tp*(tp-1)*(2*tp+5);

V(g)=[];

h=h+1;

end

VarS=((n*(n-1)*(2*n+5))-sum(Sum))/18;

Ehsan

Brian NovakVectorizing the inner loop in the double sum for calculating S speeds it up significantly:

for i = 1:n-1

S = S + sum(sign(V(i+1:n) - V(i)));

end

JanakHi Fatichi,

I want to run your code for trend analysis. I have matlab 11 and statistical tool box in my computer. Can you provide example data file for this code.

Ram

HariHi, Simone Fatichi,

I am new to Mat lab.Please tell me how to give input data and run your Mann_Kendall.m code using Mat lab.

Hanna ModinThank you!

It is working very well and is very convenient to use since it returns the result of the hypothesis test, not just the test statistic. But: the alternative of S = 0 --> Z = 0 is not accounted for if I'm right.

For future updates I would also wish for a default significance level (for convenience). And perhaps a mention in the help text that there should be no autocorrelation in residuals.