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:
- Ho(Accepted): There is no trend present in the data.(p-value>0.05)
- Ha(Rejected): A trend is present in the data.
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.
Python3
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:
- trend: This tells the trend-increasing, decreasing, or no trend.
- h: True if the trend is present. False if no trend is present.
- p: The p-value of the test.
- z: The normalized test statistic.
- Tau: Kendall Tau.
- s: Mann-Kendal’s score
- var_s: Variance S
- slope: Theil-Sen estimator/slope
- intercept: Intercept of Kendall-Theil Robust Line
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.
Python3
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:
Python3
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.
Python3
import matplotlib.pyplot as plt gfg_data = [ 1 , 2 , 3 , 4 , 5 ] plt.plot(gfg_data) |
Output: