Trend Detection#
statista.time_series.trend
#
Trend detection mixin for TimeSeries.
Trend
#
Bases: _TimeSeriesStub
Trend detection methods for TimeSeries.
Source code in src\statista\time_series\trend.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
mann_kendall(alpha=DEFAULT_ALPHA, method='original', lag=None, column=None)
#
Mann-Kendall trend test.
Tests the null hypothesis of no monotonic trend. Supports multiple variants to handle autocorrelated data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Significance level. Default 0.05. |
DEFAULT_ALPHA
|
method
|
str
|
Test variant. - "original": Standard MK (assumes serial independence). - "hamed_rao": Variance correction for autocorrelation (Hamed & Rao, 1998). Recommended for environmental data where autocorrelation inflates significance. - "yue_wang": Alternative autocorrelation correction (Yue & Wang, 2004). - "pre_whitening": Remove lag-1 autocorrelation before testing. - "trend_free_pre_whitening": Remove trend, pre-whiten, re-add trend, then test. |
'original'
|
lag
|
int
|
Maximum lag for autocorrelation correction (Hamed-Rao / Yue-Wang). If None, uses n//2 - 1. |
None
|
column
|
str
|
Column to test. If None, tests all columns. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: One row per column with: trend, h, p_value, z, tau, s, var_s, slope, intercept. |
Examples:
Detect a strong increasing trend in a linear signal with noise:
>>> import numpy as np
>>> from statista.time_series import TimeSeries
>>> np.random.seed(42)
>>> data = np.arange(50, dtype=float) + np.random.randn(50) * 3
>>> ts = TimeSeries(data)
>>> result = ts.mann_kendall()
>>> result.loc["Series1", "trend"]
'increasing'
>>> round(float(result.loc["Series1", "z"]), 4)
9.1177
>>> round(float(result.loc["Series1", "tau"]), 4)
0.8906
Verify no trend in pure random noise:
>>> np.random.seed(42)
>>> ts_noise = TimeSeries(np.random.randn(50))
>>> result_noise = ts_noise.mann_kendall()
>>> result_noise.loc["Series1", "trend"]
'no trend'
>>> round(float(result_noise.loc["Series1", "p_value"]), 4)
0.1701
Use the Hamed-Rao autocorrelation correction:
>>> np.random.seed(42)
>>> data_ac = np.arange(50, dtype=float) + np.random.randn(50) * 3
>>> ts_ac = TimeSeries(data_ac)
>>> result_ac = ts_ac.mann_kendall(method="hamed_rao")
>>> result_ac.loc["Series1", "trend"]
'increasing'
>>> round(float(result_ac.loc["Series1", "z"]), 4)
9.1177
References
Mann, H.B. (1945). Nonparametric tests against trend. Econometrica, 13(3), 245-259.
Hamed, K.H. and Rao, A.R. (1998). A modified Mann-Kendall trend test for autocorrelated data. Journal of Hydrology, 204(1-4), 182-196.
Yue, S. and Wang, C. (2004). The Mann-Kendall test modified by effective sample size to detect trend in serially correlated hydrological series. Water Resources Management, 18, 201-218.
Source code in src\statista\time_series\trend.py
sens_slope(alpha=DEFAULT_ALPHA, column=None)
#
Sen's slope estimator — robust non-parametric trend magnitude.
Computes the median of all pairwise slopes. More robust to outliers than OLS.
Always pair with mann_kendall() — Sen's slope gives the magnitude, MK gives
the significance.
Uses scipy.stats.theilslopes internally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Significance level for confidence interval. Default 0.05. |
DEFAULT_ALPHA
|
column
|
str
|
Column to analyze. If None, analyzes all columns. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: One row per column with: slope, intercept, slope_lower_ci, slope_upper_ci. |
Examples:
Estimate the slope of a linear signal (true slope = 2.0):
>>> import numpy as np
>>> from statista.time_series import TimeSeries
>>> np.random.seed(42)
>>> data = np.arange(50, dtype=float) * 2 + np.random.randn(50) * 3
>>> ts = TimeSeries(data)
>>> result = ts.sens_slope()
>>> round(float(result.loc["Series1", "slope"]), 4)
1.9629
>>> round(float(result.loc["Series1", "intercept"]), 4)
-0.6347
Check confidence interval bounds on the slope:
>>> round(float(result.loc["Series1", "slope_lower_ci"]), 4)
1.9046
>>> round(float(result.loc["Series1", "slope_upper_ci"]), 4)
2.0217
Weaker trend with more noise (true slope = 0.5):
>>> np.random.seed(42)
>>> data2 = np.arange(30, dtype=float) * 0.5 + np.random.randn(30) * 2
>>> ts2 = TimeSeries(data2)
>>> result2 = ts2.sens_slope()
>>> round(float(result2.loc["Series1", "slope"]), 4)
0.4304
>>> round(float(result2.loc["Series1", "intercept"]), 4)
0.0259
References
Sen, P.K. (1968). Estimates of the regression coefficient based on Kendall's tau. JASA, 63(324), 1379-1389.
Source code in src\statista\time_series\trend.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
detrend(method='linear', order=1)
#
Remove trend from the time series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Detrending method. - "linear": Remove linear trend via scipy.signal.detrend. - "constant": Subtract the mean. - "polynomial": Remove polynomial trend of given order. - "sens": Remove trend using Sen's slope (robust to outliers). |
'linear'
|
order
|
int
|
Polynomial order (only used when method="polynomial"). Default 1. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
TimeSeries |
Any
|
New TimeSeries with the trend removed. Same index as original. |
Examples:
Remove a linear trend (result has zero mean):
>>> import numpy as np
>>> from statista.time_series import TimeSeries
>>> np.random.seed(42)
>>> data = np.arange(100, dtype=float) + np.random.randn(100) * 5
>>> ts = TimeSeries(data)
>>> detrended = ts.detrend(method="linear")
>>> round(float(detrended.values.mean()), 4)
0.0
>>> round(float(detrended.values.std()), 4)
4.5136
Remove trend using robust Sen's slope estimator:
>>> np.random.seed(42)
>>> data2 = np.arange(50, dtype=float) * 2 + np.random.randn(50) * 3
>>> ts2 = TimeSeries(data2)
>>> detrended2 = ts2.detrend(method="sens")
>>> round(float(detrended2.values.mean()), 4)
0.8666
Subtract the mean (constant detrending):
>>> np.random.seed(42)
>>> ts3 = TimeSeries(np.random.randn(50) + 10)
>>> detrended3 = ts3.detrend(method="constant")
>>> round(float(detrended3.values.mean()), 4)
0.0
Source code in src\statista\time_series\trend.py
innovative_trend_analysis(column=None, **kwargs)
#
Innovative Trend Analysis (ITA) — Sen (2012) method.
Splits the sorted data into two halves and plots the first half (x-axis) against the second half (y-axis). Points above the 1:1 line indicate an increasing trend, points below indicate a decreasing trend.
A minimum of 20 observations is required so that each half has at least 10 points for a meaningful distribution comparison. This threshold reflects common practice for ITA; it is not prescribed by Sen (2012) but is widely used in the literature (e.g., Serinaldi et al., 2020).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
str
|
Column to analyze. If None, uses first column. |
None
|
**kwargs
|
Any
|
Passed to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[DataFrame, tuple[Figure, Axes]]
|
(results_df, (fig, ax)). results_df has columns: column, trend_indicator (positive = increasing). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the series has fewer than 20 observations after dropping NaN. |
Examples:
>>> import numpy as np
>>> from statista.time_series import TimeSeries
>>> ts = TimeSeries(np.arange(100, dtype=float))
>>> result_df, (fig, ax) = ts.innovative_trend_analysis()
References
Sen, Z. (2012). Innovative Trend Analysis Methodology. Journal of Hydrologic Engineering, 17(9), 1042-1046.
Serinaldi, F., Chebana, F., Kilsby, C.G. (2020). Dissecting innovative trend analysis. Stochastic Environmental Research and Risk Assessment, 34, 733-754.
Source code in src\statista\time_series\trend.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |