Open In App

How to Perform a Mann-Kendall Trend Test in Python

In this article, we will be looking at the various approaches to perform a Mann-Kendall test in Python.

Mann-Kendall Trend Test is used to determine whether or not a trend exists in time series data. It is a non-parametric test, meaning there is no underlying assumption made about the normality of the data.



Mann-Kendall Trend Test using mk.original_test() function

In this approach, the mk.original_test() function with required parameters from the pymannkendall library to conduct the Mann-Kendall Trend test on the given data in the python programming language. To install the pymannkendall library for mk.original_test() function:

pip install pymannkendall

 Syntax: mk.original_test() function:



Syntax: mk.original_test(x, alternative = c(“two.sided”, “greater”, “less”), continuity = TRUE)

Parameters:

  • x :a vector of class “numeric”
  • alternative: the alternative hypothesis, defaults to two.sided
  • continuity: logical, indicates whether a continuity correction should be applied, defaults to TRUE.

This is a hypotheses test and the two hypotheses are as follows:

Example 1: Mann-Kendall Trend  test on the no trend present  in the data

In this example, we will be simply using the mk.original_test() function from the Lyman Kendall library to Conduct a Mann-Kendall Trend test on the random data with 10 data points in python.




import pymannkendall as mk
 
gfg_data = [54, 52, 53, 59, 56, 57, 51, 52, 50, 53]
 
# perform Mann-Kendall Trend Test
mk.original_test(gfg_data)

Output:

Mann_Kendall_Test(trend=’no trend’, h=False, p=0.3672323880406272, z=-0.9016696346674322, Tau=-0.24444444444444444, 

s=-11.0, var_s=123.0, slope=-0.2857142857142857, intercept=54.285714285714285)

Output Interpretation:

The output interpretation can be done in the following different ways:

Since in the above example, the p-value is 0.36 which is more than the threshold(0.5) which is the alpha(0.5) then we fail to reject the accepted hypothesis i.e. we do have sufficient evidence to say that sample does not have any trend present.

Visualizing the trend of the data

In this, we will be plotting the data, Visualizing its trend, and matching the trend.




import matplotlib.pyplot as plt
 
gfg_data = [54, 52, 53, 59, 56, 57,
            51, 52, 50, 53]
 
plt.plot(gfg_data)

Output:

Example 2: Mann-Kendall Trend  test on the trend present  in the data:




import pymannkendall as mk
 
gfg_data = [1, 2, 3, 4, 5]
 
# perform Mann-Kendall Trend Test
mk.original_test(gfg_data)

Output:

Mann_Kendall_Test(trend=’increasing’, h=True, p=0.0274863361115103, z=2.2045407685048604, Tau=1.0, s=10.0,

 var_s=16.666666666666668, slope=1.0, intercept=1.0)

Output Interpretation:

Since in the above example, the p-value is 0.027 which is less than the threshold(0.5) which is the alpha(0.5) then we fail not to reject the accepted hypothesis i.e. we do have sufficient evidence to say that sample has a trend present.

Visualizing the trend of the data:

In this, we will be plotting the data, Visualizing its trend, and matching the trend.




import matplotlib.pyplot as plt
 
gfg_data = [1, 2, 3, 4, 5]
 
plt.plot(gfg_data)

Output:


Article Tags :