Test the seasonality of a given Time-Series Dataset🔗
Introduction🔗
Summary
As stated by Rob Hyndman and George Athanasopoulos
In describing these time series, we have used words such as "trend" and "seasonal" which need to be defined more carefully.
Trend A trend exists when there is a long-term increase or decrease in the data. It does not have to be linear. Sometimes we will refer to a trend as "changing direction", when it might go from an increasing trend to a decreasing trend.
Seasonal A seasonal pattern occurs when a time series is affected by seasonal factors such as the time of the year or the day of the week. Seasonality is always of a fixed and known frequency.
Cyclic A cycle occurs when the data exhibit rises and falls that are not of a fixed frequency. These fluctuations are usually due to economic conditions, and are often related to the "business cycle". The duration of these fluctuations is usually at least 2 years.
For more info, see: Forecasting: Principles and Practice - Time Series Patterns
| Information | Details |
|---|---|
| Module | ts_stat_tests.seasonality.tests |
| Algorithms | qs, ocsb, ch, seasonal_strength, trend_strength, spikiness |
| Complexity | \(O(n \log n)\) to \(O(n^2)\) depending on algorithm |
| Implementation | statsmodels, pmdarima, tsfeatures |
Source Library
We leverage several industry-standard libraries for the underlying statistical tests in this module.
pmdarima: Provides the implementation for the Canova-Hansen (ch) and Osborn-Chui-Smith-Birchenhall (ocsb) tests through itsnsdiffsand estimator classes.tsfeatures: Used as the basis for calculating seasonal strength, trend strength, and spikiness, following the approach popularized by the Rfeastsandtsfeaturespackages.seastests: An R package that served as the primary reference and inspiration for our Python implementation of the Quenouille-Sarle (qs) test.
Source Module
The source code for the seasonality tests is organized into two primary layers:
src.ts_stat_tests.seasonality.algorithms: Contains the core mathematical implementations and wrappers for third-party libraries.src.ts_stat_tests.seasonality.tests: Provides the top-level user interface, including theseasonalitydispatcher and theis_seasonalboolean check.
Modules🔗
ts_stat_tests.seasonality.tests
🔗
Summary
This module contains functions to assess the seasonality of time series data.
The implemented algorithms include:
- QS Test
- OCSB Test
- CH Test
- Seasonal Strength
- Trend Strength
- Spikiness
Each function is designed to analyze a univariate time series and return relevant statistics or indicators of seasonality. This module provides both a dispatcher for flexible algorithm selection and a boolean check for easy integration into pipelines.
seasonality
🔗
seasonality(
x: ArrayLike,
algorithm: str = "qs",
**kwargs: Union[float, int, str, bool, ArrayLike, None]
) -> Union[
float, int, tuple[Union[float, int, ARIMA, None], ...]
]
Summary
Dispatcher for seasonality algorithms. This function provides a unified interface to call various seasonality tests.
Details
The seasonality function acts as a centralized dispatcher for the various seasonality algorithms implemented in the algorithms.seasonality module. It allows users to easily switch between different tests by specifying the algorithm name.
The supported algorithms include:
"qs": The QS (Quenouille-Sarle) test for seasonality."ocsb": The Osborn-Chui-Smith-Birchenhall test for seasonal differencing."ch": The Canova-Hansen test for seasonal stability."seasonal_strength"(or"ss"): The STL-based seasonal strength measure."trend_strength"(or"ts"): The STL-based trend strength measure."spikiness": The STL-based spikiness measure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data to be checked. |
required |
algorithm
|
str
|
Which seasonality algorithm to use. |
'qs'
|
kwargs
|
Union[float, int, str, bool, ArrayLike, None]
|
Additional arguments to pass to the underlying algorithm. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[float, int, tuple[Union[float, int, object, None], ...]]
|
The result of the seasonality test. The return type depends on the chosen algorithm:
- |
Examples
| Basic usage | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
Source code in src/ts_stat_tests/seasonality/tests.py
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 | |
is_seasonal
🔗
is_seasonal(
x: ArrayLike,
algorithm: str = "qs",
alpha: float = 0.05,
**kwargs: Union[float, int, str, bool, ArrayLike, None]
) -> dict[str, Union[str, float, bool, None]]
Summary
Boolean check for seasonality. This function wraps the seasonality dispatcher and returns a standardized dictionary indicating whether the series is seasonal based on a significance level or threshold.
Details
The is_seasonal function interprets the results of the underlying seasonality tests to provide a boolean "result".
- For
"qs", the test is considered seasonal if the p-value is less thanalpha. - For
"ocsb"and"ch", the test is considered seasonal if the returned integer is 1. - For
"seasonal_strength", the test is considered seasonal if the strength is greater than 0.64 (a common threshold in literature). - For others, it checks if the statistic is greater than 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data to be checked. |
required |
algorithm
|
str
|
Which seasonality algorithm to use. |
'qs'
|
alpha
|
float
|
The significance level for the test (used by |
0.05
|
kwargs
|
Union[float, int, str, bool, ArrayLike, None]
|
Additional arguments to pass to the underlying algorithm. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Union[str, float, bool, None]]
|
A dictionary containing:
|
Examples
| Standard check | |
|---|---|
1 2 3 4 5 6 7 8 | |
Source code in src/ts_stat_tests/seasonality/tests.py
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 | |
ts_stat_tests.seasonality.algorithms
🔗
Summary
Seasonality tests are statistical tests used to determine whether a time series exhibits seasonal patterns or cycles. Seasonality refers to the regular and predictable fluctuations in a time series that occur at specific intervals, such as daily, weekly, monthly, or yearly.
Seasonality tests help identify whether a time series has a seasonal component that needs to be accounted for in forecasting models. By detecting seasonality, analysts can choose appropriate models that capture these patterns and improve the accuracy of their forecasts.
Common seasonality tests include the QS test, OCSB test, Canova-Hansen test, and others. These tests analyze the autocorrelation structure of the time series data to identify significant seasonal patterns.
Overall, seasonality tests are essential tools in time series analysis and forecasting, as they help identify and account for seasonal patterns that can significantly impact the accuracy of predictions.
qs
🔗
qs(
x: ArrayLike,
freq: int = 0,
diff: bool = True,
residuals: bool = False,
autoarima: bool = True,
) -> Union[
tuple[float, float],
tuple[float, float, Optional[ARIMA]],
]
Summary
The \(QS\) test, also known as the Ljung-Box test, is a statistical test used to determine whether there is any seasonality present in a time series forecasting model. It is based on the autocorrelation function (ACF) of the residuals, which is a measure of how correlated the residuals are at different lags.
Details
If residuals=False the autoarima settings are ignored.
If residuals=True, a non-seasonal ARIMA model is estimated for the time series. And the residuals of the fitted model are used as input to the test statistic. If an automatic order selection is used, the Hyndman-Khandakar algorithm is employed with: \(\max(p)=\max(q)<=3\).
The null hypothesis is that there is no correlation in the residuals beyond the specified lags, indicating no seasonality. The alternative hypothesis is that there is significant correlation, indicating seasonality.
Here are the steps for performing the \(QS\) test:
- Fit a time series model to your data, such as an ARIMA or SARIMA model.
- Calculate the residuals, which are the differences between the observed values and the predicted values from the model.
- Calculate the ACF of the residuals.
- Calculate the Q statistic, which is the sum of the squared values of the autocorrelations at different lags, up to a specified lag. Using the formula above.
- Compare the Q statistic to the critical value from the chi-squared distribution with degrees of freedom equal to the number of lags. If the Q statistic is greater than the critical value, then the null hypothesis is rejected, indicating that there is evidence of seasonality in the residuals.
In summary, the \(QS\) test is a useful tool for determining whether a time series forecasting model has adequately accounted for seasonality in the data. By detecting any seasonality present in the residuals, it helps to ensure that the model is capturing all the important patterns in the data and making accurate predictions.
This function will implement the Python version of the R function qs() from the seastests library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The univariate time series data to test. |
required |
freq
|
int
|
The frequency of the time series data. |
0
|
diff
|
bool
|
Whether or not to run |
True
|
residuals
|
bool
|
Whether or not to run & return the residuals from the function. |
False
|
autoarima
|
bool
|
Whether or not to run the |
True
|
Raises:
| Type | Description |
|---|---|
AttributeError
|
If |
ValueError
|
If, after differencing the data (by using |
Returns:
| Type | Description |
|---|---|
Union[tuple[float, float], tuple[float, float, Optional[ARIMA]]]
|
The results of the QS test.
- stat (float): The \(\text{QS}\) score for the given data set.
- pval (float): The p-value of the given test. Calculated using the survival function of the chi-squared algorithm (also defined as \(1-\text{cdf(...)}\)). For more info, see: scipy.stats.chi2
- model (Optional[ARIMA]): The ARIMA model used in the calculation of this test. Returned if |
Examples
| Basic usage | |
|---|---|
1 2 3 4 5 | |
| Advanced usage | |
|---|---|
1 2 3 4 5 6 | |
Calculation
The \(Q\) statistic is given by:
where:
- \(n\) is the sample size,
- \(r_k\) is the autocorrelation at lag \(k\), and
- \(h\) is the maximum lag to be considered.
QS = n(n+2) * sum(r_k^2 / (n-k)) for k = 1 to h
Credit
- All credit goes to the
seastestslibrary.
References
- Hyndman, R. J. and Y. Khandakar (2008). Automatic Time Series Forecasting: The forecast Package for R. Journal of Statistical Software 27 (3), 1-22.
- Maravall, A. (2011). Seasonality Tests and Automatic Model Identification in TRAMO-SEATS. Bank of Spain.
- Ollech, D. and Webel, K. (2020). A random forest-based approach to identifying the most informative seasonality tests. Deutsche Bundesbank's Discussion Paper series 55/2020.
See Also
Source code in src/ts_stat_tests/seasonality/algorithms.py
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 | |
ocsb
🔗
ocsb(
x: ArrayLike,
m: int,
lag_method: str = "aic",
max_lag: int = 3,
) -> int
Summary
Compute the Osborn, Chui, Smith, and Birchenhall (\(OCSB\)) test for an input time series to determine whether it needs seasonal differencing. The regression equation may include lags of the dependent variable. When lag_method="fixed", the lag order is fixed to max_lag; otherwise, max_lag is the maximum number of lags considered in a lag selection procedure that minimizes the lag_method criterion, which can be "aic", "bic" or corrected AIC "aicc".
Details
The \(OCSB\) test is a statistical test that is used to check the presence of seasonality in time series data. Seasonality refers to a pattern in the data that repeats itself at regular intervals.
The \(OCSB\) test is based on the null hypothesis that there is no seasonality in the time series data. If the p-value of the test is less than the significance level (usually \(0.05\)), then the null hypothesis is rejected, and it is concluded that there is seasonality in the data.
The \(OCSB\) test involves dividing the data into two halves and calculating the mean of each half. Then, the differences between the means of each pair of halves are calculated for each possible pair of halves. Finally, the mean of these differences is calculated, and a test statistic is computed.
The \(OCSB\) test is useful for testing seasonality in time series data because it can detect seasonal patterns that are not obvious in the original data. It is also a useful diagnostic tool for determining the appropriate seasonal differencing parameter in ARIMA models.
Critical values for the test are based on simulations, which have been smoothed over to produce critical values for all seasonal periods
The null hypothesis of the \(OCSB\) test is that there is no seasonality in the time series, and the alternative hypothesis is that there is seasonality. The test statistic is compared to a critical value from a chi-squared distribution with degrees of freedom equal to the number of possible pairs of halves. If the test statistic is larger than the critical value, then the null hypothesis is rejected, and it is concluded that there is evidence of seasonality in the time series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The time series vector. |
required |
m
|
int
|
The seasonal differencing term. For monthly data, e.g., this would be 12. For quarterly, 4, etc. For the OCSB test to work, |
required |
lag_method
|
str
|
The lag method to use. One of ( |
'aic'
|
max_lag
|
int
|
The maximum lag order to be considered by |
3
|
Returns:
| Type | Description |
|---|---|
int
|
The seasonal differencing term. For different values of |
Examples
| Basic usage | |
|---|---|
1 2 3 4 5 | |
Calculation
The equation for the \(OCSB\) test statistic for a time series of length n is:
where:
- \(n\) is the sample size, and
- \(x[i]\) is the \(i\)-th observation in the time series.
OCSB = (1 / (n - 1)) * sum( ((x[i] - x[n/2+i]) - (x[n/2+i] - x[i+n/2+1]))^2 )
In this equation, the time series is split into two halves, and the difference between the means of each half is calculated for each possible pair of halves. The sum of the squared differences is then divided by the length of the time series minus one to obtain the \(OCSB\) test statistic.
Credit
- All credit goes to the
pmdarimalibrary with the implementation ofpmdarima.arima.OCSBTest.
References
- Osborn DR, Chui APL, Smith J, and Birchenhall CR (1988) "Seasonality and the order of integration for consumption", Oxford Bulletin of Economics and Statistics 50(4):361-377.
- R's forecast::OCSB test source code: https://bit.ly/2QYQHno
See Also
Source code in src/ts_stat_tests/seasonality/algorithms.py
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 | |
ch
🔗
ch(x: ArrayLike, m: int) -> int
Summary
The Canova-Hansen test for seasonal differences. Canova and Hansen (1995) proposed a test statistic for the null hypothesis that the seasonal pattern is stable. The test statistic can be formulated in terms of seasonal dummies or seasonal cycles. The former allows us to identify seasons (e.g. months or quarters) that are not stable, while the latter tests the stability of seasonal cycles (e.g. cycles of period 2 and 4 quarters in quarterly data).
Warning
This test is generally not used directly, but in conjunction with pmdarima.arima.nsdiffs(), which directly estimates the number of seasonal differences.
Details
The \(CH\) test (also known as the Canova-Hansen test) is a statistical test for detecting seasonality in time series data. It is based on the idea of comparing the goodness of fit of two models: a non-seasonal model and a seasonal model. The null hypothesis of the \(CH\) test is that the time series is non-seasonal, while the alternative hypothesis is that the time series is seasonal.
The test statistic is compared to a critical value from the chi-squared distribution with degrees of freedom equal to the difference in parameters between the two models. If the test statistic exceeds the critical value, the null hypothesis of non-seasonality is rejected in favor of the alternative hypothesis of seasonality.
The \(CH\) test is based on the following steps:
- Fit a non-seasonal autoregressive integrated moving average (ARIMA) model to the time series data, using a criterion such as Akaike Information Criterion (AIC) or Bayesian Information Criterion (BIC) to determine the optimal model order.
- Fit a seasonal ARIMA model to the time series data, using the same criterion to determine the optimal model order and seasonal period.
- Compute the sum of squared residuals (SSR) for both models.
- Compute the test statistic \(CH\) using the formula above.
- Compare the test statistic to a critical value from the chi-squared distribution with degrees of freedom equal to the difference in parameters between the two models. If the test statistic exceeds the critical value, reject the null hypothesis of non-seasonality in favor of the alternative hypothesis of seasonality.
The \(CH\) test is a powerful test for seasonality in time series data, as it accounts for both the presence and the nature of seasonality. However, it assumes that the time series data is stationary, and it may not be effective for detecting seasonality in non-stationary or irregular time series data. Additionally, it may not work well for time series data with short seasonal periods or with low seasonal amplitudes. Therefore, it should be used in conjunction with other tests and techniques for detecting seasonality in time series data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The time series vector. |
required |
m
|
int
|
The seasonal differencing term. For monthly data, e.g., this would be 12. For quarterly, 4, etc. For the Canova-Hansen test to work, |
required |
Returns:
| Type | Description |
|---|---|
int
|
The seasonal differencing term. The \(CH\) test defines a set of critical values: For different values of |
Examples
| Basic usage | |
|---|---|
1 2 3 4 5 | |
Calculation
The test statistic for the \(CH\) test is given by:
where:
- \(SSRns\) is the \(SSR\) for the non-seasonal model,
- \(SSRs\) is the \(SSR\) for the seasonal model,
- \(n\) is the sample size,
- \(p\) is the number of parameters in the non-seasonal model, and
- \(s\) is the number of parameters in the seasonal model.
CH = [(SSRns - SSRs) / (n - p - 1)] / (SSRs / (n - p - s - 1))
Notes
This test is generally not used directly, but in conjunction with pmdarima.arima.nsdiffs(), which directly estimates the number of seasonal differences.
Credit
- All credit goes to the
pmdarimalibrary with the implementation ofpmdarima.arima.CHTest.
References
- Testing for seasonal stability using the Canova and Hansen test statistic: http://bit.ly/2wKkrZo
- R source code for CH test: https://github.com/robjhyndman/forecast/blob/master/R/arima.R#L148
See Also
Source code in src/ts_stat_tests/seasonality/algorithms.py
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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
seasonal_strength
🔗
seasonal_strength(x: ArrayLike, m: int) -> float
Summary
The seasonal strength test is a statistical test for detecting the strength of seasonality in time series data. It measures the extent to which the seasonal component of a time series explains the variation in the data.
Details
The seasonal strength test involves computing the seasonal strength index (\(SSI\)).
The \(SSI\) ranges between \(0\) and \(1\), with higher values indicating stronger seasonality in the data. The critical value for the \(SSI\) can be obtained from statistical tables based on the sample size and level of significance. If the \(SSI\) value exceeds the critical value, the null hypothesis of no seasonality is rejected in favor of the alternative hypothesis of seasonality.
The seasonal strength test involves the following steps:
- Decompose the time series data into its seasonal, trend, and residual components using a method such as seasonal decomposition of time series (STL) or moving average decomposition.
- Compute the variance of the seasonal component \(Var(S)\) and the variance of the residual component \(Var(R)\).
- Compute the \(SSI\) using the formula above.
- Compare the \(SSI\) to a critical value from a statistical table for a given significance level and sample size. If the \(SSI\) exceeds the critical value, reject the null hypothesis of no seasonality in favor of the alternative hypothesis of seasonality.
The seasonal strength test is a simple and intuitive test for seasonality in time series data. However, it assumes that the seasonal component is additive and that the residuals are independent and identically distributed. Moreover, it may not be effective for detecting complex seasonal patterns or seasonality in non-stationary or irregular time series data. Therefore, it should be used in conjunction with other tests and techniques for detecting seasonality in time series data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The time series vector. |
required |
m
|
int
|
The seasonal differencing term. For monthly data, e.g., this would be 12. For quarterly, 4, etc. For the seasonal strength test to work, |
required |
Returns:
| Type | Description |
|---|---|
float
|
The seasonal strength value. |
Examples
| Basic usage | |
|---|---|
1 2 3 4 5 | |
Calculation
The \(SSI\) is computed using the following formula:
where:
- \(Var(S)\) is the variance of the seasonal component, and
- \(Var(R)\) is the variance of the residual component obtained after decomposing the time series data into its seasonal, trend, and residual components using a method such as STL or moving average decomposition.
SSI = Var(S) / (Var(S) + Var(R))
References
- Wang, X, Hyndman, RJ, Smith-Miles, K (2007) "Rule-based forecasting filters using time series features", Computational Statistics and Data Analysis, 52(4), 2244-2259.
See Also
Source code in src/ts_stat_tests/seasonality/algorithms.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
trend_strength
🔗
trend_strength(x: ArrayLike, m: int) -> float
Summary
The trend strength test is a statistical test for detecting the strength of the trend component in time series data. It measures the extent to which the trend component of a time series explains the variation in the data.
Details
The trend strength test involves computing the trend strength index (\(TSI\)).
The \(TSI\) ranges between \(0\) and \(1\), with higher values indicating stronger trend in the data. The critical value for the \(TSI\) can be obtained from statistical tables based on the sample size and level of significance. If the \(TSI\) value exceeds the critical value, the null hypothesis of no trend is rejected in favor of the alternative hypothesis of trend.
The trend strength test involves the following steps:
- Decompose the time series data into its trend, seasonal, and residual components using a method such as seasonal decomposition of time series (STL) or moving average decomposition.
- Compute the variance of the trend component, denoted by \(Var(T)\).
- Compute the variance of the residual component, denoted by \(Var(R)\).
- Compute the trend strength index (\(TSI\)) using the formula above.
- Compare the \(TSI\) value to a critical value based on the sample size and level of significance. If the \(TSI\) value exceeds the critical value, reject the null hypothesis of no trend in favor of the alternative hypothesis of trend.
The trend strength test is a useful tool for identifying the strength of trend in time series data, and it can be used in conjunction with other tests and techniques for detecting trend. However, it assumes that the time series data is stationary and that the trend component is linear. Additionally, it may not be effective for time series data with short time spans or with nonlinear trends. Therefore, it should be used in conjunction with other tests and techniques for detecting trend in time series data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The time series vector. |
required |
m
|
int
|
The frequency of the time series data set. For the trend strength test to work, |
required |
Returns:
| Type | Description |
|---|---|
float
|
The trend strength score. |
Examples
| Basic usage | |
|---|---|
1 2 3 4 5 | |
Calculation
The trend strength test involves computing the trend strength index (\(TSI\)) using the following formula:
where:
- \(Var(T)\) is the variance of the trend component, and
- \(Var(R)\) is the variance of the residual component obtained after decomposing the time series data into its trend, seasonal, and residual components using a method such as STL or moving average decomposition.
TSI = Var(T) / (Var(T) + Var(R))
References
- Wang, X, Hyndman, RJ, Smith-Miles, K (2007) "Rule-based forecasting filters using time series features", Computational Statistics and Data Analysis, 52(4), 2244-2259.
See Also
Source code in src/ts_stat_tests/seasonality/algorithms.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
spikiness
🔗
spikiness(x: ArrayLike, m: int) -> float
Summary
The spikiness test is a statistical test that measures the degree of spikiness or volatility in a time series data. It aims to detect the presence of spikes or sudden changes in the data that may indicate important events or anomalies in the underlying process.
Details
The spikiness test involves computing the spikiness index (\(SI\)). The \(SI\) measures the intensity of spikes or outliers in the data relative to the overall variation. A higher \(SI\) value indicates a more spiky or volatile time series, while a lower \(SI\) value indicates a smoother or less volatile time series.
The spikiness test involves the following steps:
- Decompose the time series data into its seasonal, trend, and residual components using a method such as STL or moving average decomposition.
- Compute the mean absolute deviation of the residual component (\(MADR\)).
- Compute the mean absolute deviation of the seasonal component (\(MADS\)).
- Compute the spikiness index (\(SI\)) using the formula above.
The spikiness test can be used in conjunction with other tests and techniques for detecting spikes in time series data, such as change point analysis and outlier detection. However, it assumes that the time series data is stationary and that the spikes are abrupt and sudden. Additionally, it may not be effective for time series data with long-term trends or cyclical patterns. Therefore, it should be used in conjunction with other tests and techniques for detecting spikes in time series data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The time series vector. |
required |
m
|
int
|
The frequency of the time series data set. For the spikiness test to work, |
required |
Returns:
| Type | Description |
|---|---|
float
|
The spikiness score. |
Examples
| Basic usage | |
|---|---|
1 2 3 4 5 | |
Calculation
The spikiness test involves computing the spikiness index (\(SI\)) using the following formula:
where:
- \(MADR\) is the mean absolute deviation of the residuals, and
- \(MADS\) is the mean absolute deviation of the seasonal component.
SI = MADR / MADS
Credit
- All credit to the
tsfeatureslibrary. This code is a direct copy+paste from thetsfeatures.pymodule.
It is not possible to refer directly to aspikinessfunction in thetsfeaturespackage because the process to calculate seasonal strength is embedded within theirstl_featuresfunction. Therefore, it it necessary to copy it here.
References
- Wang, X, Hyndman, RJ, Smith-Miles, K (2007) "Rule-based forecasting filters using time series features", Computational Statistics and Data Analysis, 52(4), 2244-2259.
See Also
Source code in src/ts_stat_tests/seasonality/algorithms.py
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 | |