# Mann-Kendall Trend Test Using Python

Rajat Sarker 03 Jul, 2023 â€¢ 5 min read

## Introduction

The Mann-Kendall trend test, named after H. A. Mann and D. R. Kendall, It’s non-parametric test used to determine the trend to be significant overtime. The trend can be monotonically increasing or decreasing overtime. Since it is non-parametric test so we donâ€™t have to worry about distribution of the data. But the data should not have serial correlation/Autocorrelation (the error term in time series transfer from one period to another).

The Mann-Kendall test is designed to detect monotonic trends, which are trends that consistently increase or decrease over time, without assuming any specific distribution for the data. It is particularly useful when dealing with data that may not meet the assumptions of parametric tests, such as normality.

This article was published as a part of the Data Science Blogathon.

## Sample Size Requirement

If you have very small samples as small as 3 or 4, then there is high probability of not finding any trend. The more samples we have overtime, the more reliable will be the test statistics. Though test can be performed with very sample samples as well. The recommended data is therefore at least 10.

## Objective of Test

In this article, we study the accidents related to derailments of our train over time. Recent train derailment in Odisha has again questioned about safety in railways.  Railway accidents may be classified by their accident types (examples head on collisions, rear-end collisions, explosions, side collisions, derailments, fires etc.). Over time there have been many improvements in railways technically and infrastructure-wise. With all modern advancement in place yet train accidents are common across the world. Train accidents are unfortunate incidents that occur in railway systems around the world. These accidents can have devastating consequences, leading to loss of life, injuries, and damage to property.

In this study, we will determine, overtime, have we been able to reduce the train accidents (we will study derailments category of accident here) in India given all the advancements made over the years. The data we have procured about derailments in India is time series in nature. We have data of derailments from 2001 to 2016. The data is arranged in a chronological order.

## Our Data

 Year Derailments 2001 350 2002 280 2003 218 2004 202 2005 138 2006 131 2007 96 2008 100 2009 85 2010 80 2011 80 2012 55 2013 49 2014 53 2015 63 2016 65

From theÂ above table we can clearly see a decreasing trend in the data. Since 2001 the number of derailments has reduce to a very great extent. In 2001, we had 350 derailment related accidents which has reduced to 65 in 2016. Since the data is arranged in order, we can directly feed it in python environment and work on it.Â Letâ€™s have aÂ plot to visualize the data properly in python.

``````!pip install seaborn
import seaborn as sns
import matplotlib.pyplot as plt
fig = plt.subplots(figsize=(20, 5))
sns.lineplot(x='Year', y='Derailments', data=df)
sns.set_theme(style='white', font_scale=3)``````

From the above plot, we can clearly see there is decline trend. But can we say this declining trend is significant. Though it is evident from the plot highly
likely to be significant. Letâ€™s check the same using Mann-Kendall trend hypothesis testing.

## Steps

• Null Hypothesis (H0) of this test is there is no monotonic trend in the data.
• Alternative Hypothesis (H1) is that there exist a trend. The trend can be monotonic increasing positive trend or monotonic decreasing negative Trend.
• Test Statistic: The M test produces a test statistic denoted as “T”. A positive value of T indicates an increasing trend, while a negative value indicates a decreasing trend. The magnitude of T represents the strength of the trend.
• Significance (Alpha): A significance level (e.g., 0.05 or 0.10) to determine the threshold for statistical significance. This represents the maximum probability of observing a trend when there is no actual trend in the data.
• P-Value: The M-K test calculates a p-value that quantifies the probability of observing a test statistic as extreme as the observed one, assuming the null hypothesis is true. The p-value represents the evidence against the null hypothesis.
• If the p-value is less than the significance level (p < Î±), it suggests strong evidence to reject the null hypothesis. This indicates a statistically significant trend in the data.
• If the p-value is greater than the significance level (p â‰¥ Î±), it suggests insufficient evidence to reject the null hypothesis. This means there is no statistically significant trend in the data.
• Conclusion: Based on the p-value and the significance level, you can draw a conclusion regarding the presence or absence of a significant trend in the data.
• If p < Î±, you can conclude that there is a statistically significant trend in the data. Make sure to consider the direction of the trend based on the sign of the test statistic (T).
• If p â‰¥ Î±, you can conclude that there is no statistically significant trend in the data. There is insufficient evidence to suggest the presence of a trend.

#### Python Code Steps

``````import pandas as pd
YearDerailments
02001350
12002280
22003218
32004202
42005138
• Import the libraries:
``````!pip install pymannkendall
import numpy as np
import pymannkendall ``````
• Mann Kendall Test:
``mk.original_test(df["Derailments"])``

## Conclusion

The trend is decreasing, P-Value is highly significant. Hence we reject the null Hypothesis and conclude that the train derailments has significantly reduced over time. The Technical advancement and Infrastructural change has lead to significant reduction of derailment related accidents.

• Robust and versatile: The Mann-Kendall test is robust against outliers and does not assume a specific data distribution.
• Wide applicability: The Mann-Kendall test finds applications in various fields, including climate science, hydrology, environmental monitoring, economics, and other disciplines dealing with time series data.
• Simple calculation: The calculation of the Mann-Kendall statistic involves ranking the data, determining the signs of pairwise differences, and summing these signs. The resulting statistic is then used to assess the presence of a trend.

Q1. What are the assumptions of the Mann-Kendall test?

A. The Mann-Kendall test is a non-parametric or distribution-free test, meaning it does not assume any specific probability distribution for the data. It is also not affected by outliers. Though, it assumes that the observations are independent and that there are no serial correlation in the data.

Q2. How does the Mann-Kendall test work?

A. The test generates the no. of concordant and discordant pairs within the time series data. It uses these pairs to compute a test statistic that follows a normal distribution under the null hypothesis of no trend. The test statistic is then compared to critical values to determine if the trend is statistically significant.

Q3. What is the scope of Mann-Kendall?

A. Any time series related data can be tested for monotonously significant. For example the temperature across the world is increasing can be tested (Global warming Phenomenon), ice-berg is melting faster, the sea-level is monotonously increasing, islands has disappeared overtime. In the medical field- More number of patient are being diagnosed with diabetes, heart failure problem, obesity.

Q4. Can the Mann-Kendall test determine the direction of the trend?

A. Yes, it can determine the direction of the trend. The sign of the test statistic (positive or negative) indicates the direction of the trend: positive for an increasing trend and negative for a decreasing trend. A test statistic of zero suggests no trend.

Q5. Are there any alternatives to the Mann-Kendall test?

A. Alternative trend tests, such as the Sen’s slope estimator, the Spearman’s rank correlation test, and the Theil-Sen estimator have different assumptions. Maybe more suitable depending on the specific characteristics of the data or research question.