Mann-kendall (MK) trend test in Mathematica ?

Dear All,

The Mann-Kendall Test is used to determine whether a time series has a monotonic upward or downward trend. In MATLAB, Excel, and R there is a calculation command for this. I can’t find any command in Mathematica for MK test. I tried to write a small code in Mathematica, but I got some errors.
Any help would be appreciated.

data = {11.22, 12.29, 12.97, 14.15, 17.33, 17.60, 16.62, 14.43, 13.62,
    11.01};

n = Length[data]

S = \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n - 1\)]\(
\*UnderoverscriptBox[\(\[Sum]\), \(k = i + 1\), \(n\)]Sign[
\*SubscriptBox[\(x\), \(k\)] - 
\*SubscriptBox[\(x\), \(j\)]]\)\)

Zc = Piecewise[{{(S - 1)/Sqrt[(n (n - 1) (2 n + 5))/18], S > 0}, {0, 
    S = 0}, {(S + 1)/Sqrt[(n (n - 1) (2 n + 5))/18], S < 0}}]

\[Beta] = Median[(Subscript[x, i] - Subscript[x, j])/(i - j)]

Hi Alex,

Subscripts in WL don’t translate to array indexing. You have to do this to compute S

S = Sum[Sign[data[[k]] - data[[i]]], {i, 1, n - 1}, {k, i + 1, n}]
(* 7 *)

Then Zc evaluates to 6/(5 Sqrt[5]).

I don’t understand the formula for β, Median is computed on a single value which does not make sense. Can you explain?

The following is the estimate for \beta (Sen’s slope):

\[Beta] = Median[Flatten[Table[(data[[i]] - data[[j]])/(i - j), {i, 1, n - 1}, {j, i + 1, n}]]]

It’s the median of all pairwise estimates of slope.

And you need “==” instead of “=” for “S==0” in the Piecewise function.

Also, S can be determined in the following manner:

S = Binomial[n, 2] KendallTau[Range[n], data]

Thank you, Rohit.

β is the slope of n pairs of data points. If a significant trend is found, the rate of change can be calculated using the Sen slope estimator (β). In other words, computing the slope for all pairs of data that were used to compute S.

The main reference explains as follow: