Test the normality of a given Time-Series Dataset🔗
Introduction🔗
Summary
As stated by the NIST/SEMATECH e-Handbook of Statistical Methods:
Normal distributions are important in statistics and are often used in the natural and social sciences to represent real-valued random variables whose distributions are not known. Their importance is partly due to the central limit theorem.
For more info, see: Engineering Statistics Handbook: Measures of Skewness and Kurtosis.
Info
The normality test is used to determine whether a data set is well-modeled by a normal distribution. In time series forecasting, we primarily test the residuals (errors) of a model for normality. If the residuals follow a normal distribution, it suggests that the model has successfully captured the systematic patterns in the data, and the remaining errors are random white noise.
If the residuals are not normally distributed, it may indicate that the model is missing important features, such as seasonal patterns or long-term trends, or that a transformation of the data (e.g., Log or Box-Cox) is required before modeling.
| library | category | algorithm | short | import script | url |
|---|---|---|---|---|---|
| scipy | Normality | Shapiro-Wilk Test | SW | from scipy.stats import shapiro |
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.shapiro.html |
| scipy | Normality | D'Agostino & Pearson's Test | DP | from scipy.stats import normaltest |
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.normaltest.html |
| scipy | Normality | Anderson-Darling Test | AD | from scipy.stats import anderson |
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.anderson.html |
| statsmodels | Normality | Jarque-Bera Test | JB | from statsmodels.stats.stattools import jarque_bera |
https://www.statsmodels.org/stable/generated/statsmodels.stats.stattools.jarque_bera.html |
| statsmodels | Normality | Omnibus Test | OB | from statsmodels.stats.diagnostic import omni_normtest |
https://www.statsmodels.org/stable/generated/statsmodels.stats.diagnostic.omni_normtest.html |
For more info, see: Hyndman & Athanasopoulos: Forecasting: Principles and Practice.
Source Library
The scipy and statsmodels packages were chosen because they provide standard, reliable implementations of classical statistical tests. scipy.stats provides implementations for Shapiro-Wilk, D'Agostino-Pearson, and Anderson-Darling tests, while statsmodels provides the Jarque-Bera and Omnibus tests.
Source Module
All of the source code can be found within these modules:
Modules🔗
ts_stat_tests.normality.tests
🔗
Summary
This module contains convenience functions and tests for normality measures, allowing for easy access to different normality algorithms.
normality
🔗
normality(
x: ArrayLike,
algorithm: str = "dp",
axis: int = 0,
nan_policy: VALID_DP_NAN_POLICY_OPTIONS = "propagate",
dist: VALID_AD_DIST_OPTIONS = "norm",
) -> Union[
tuple[float, ...],
NormaltestResult,
ShapiroResult,
AndersonResult,
]
Summary
Perform a normality test on the given data.
Details
This function is a convenience wrapper around the five underlying algorithms:
- jb()
- ob()
- sw()
- dp()
- ad()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data to be checked. Should be a |
required |
algorithm
|
str
|
Which normality algorithm to use. |
'dp'
|
axis
|
int
|
Axis along which to compute the test.
Default: |
0
|
nan_policy
|
VALID_DP_NAN_POLICY_OPTIONS
|
Defines how to handle when input contains |
'propagate'
|
dist
|
VALID_AD_DIST_OPTIONS
|
The type of distribution to test against. |
'norm'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When the given value for |
Returns:
| Type | Description |
|---|---|
Union[tuple[float, float], tuple[float, list[float], list[float]]]
|
If not |
Credit
Calculations are performed by scipy.stats and statsmodels.stats.
Examples
| Setup | |
|---|---|
1 2 3 | |
| Example 1: D'Agostino-Pearson test | |
|---|---|
1 2 3 4 5 | |
| Example 2: Jarque-Bera test | |
|---|---|
1 2 3 4 5 | |
Source code in src/ts_stat_tests/normality/tests.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 | |
is_normal
🔗
is_normal(
x: ArrayLike,
algorithm: str = "dp",
alpha: float = 0.05,
axis: int = 0,
nan_policy: VALID_DP_NAN_POLICY_OPTIONS = "propagate",
dist: VALID_AD_DIST_OPTIONS = "norm",
) -> dict[str, Union[str, float, bool, None]]
Summary
Test whether a given data set is normal or not.
Details
This function implements the given algorithm (defined in the parameter algorithm), and returns a dictionary containing the relevant data:
{
"result": ..., # The result of the test. Will be `True` if `p-value >= alpha`, and `False` otherwise
"statistic": ..., # The test statistic
"p_value": ..., # The p-value of the test (if applicable)
"alpha": ..., # The significance level used
}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data to be checked. Should be a |
required |
algorithm
|
str
|
Which normality algorithm to use. |
'dp'
|
alpha
|
float
|
Significance level.
Default: |
0.05
|
axis
|
int
|
Axis along which to compute the test.
Default: |
0
|
nan_policy
|
VALID_DP_NAN_POLICY_OPTIONS
|
Defines how to handle when input contains |
'propagate'
|
dist
|
VALID_AD_DIST_OPTIONS
|
The type of distribution to test against. |
'norm'
|
Returns:
| Type | Description |
|---|---|
dict[str, Union[str, float, bool, None]]
|
A dictionary containing:
- |
Credit
Calculations are performed by scipy.stats and statsmodels.stats.
Examples
| Setup | |
|---|---|
1 2 3 4 | |
| Example 1: Test normal data | |
|---|---|
1 2 3 4 5 | |
| Example 2: Test non-normal (random) data | |
|---|---|
1 2 3 | |
Source code in src/ts_stat_tests/normality/tests.py
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 | |
ts_stat_tests.normality.algorithms
🔗
Summary
This module provides implementations of various statistical tests to assess the normality of data distributions. These tests are essential in statistical analysis and time series forecasting, as many models assume that the underlying data follows a normal distribution.
VALID_DP_NAN_POLICY_OPTIONS
module-attribute
🔗
VALID_DP_NAN_POLICY_OPTIONS = Literal[
"propagate", "raise", "omit"
]
VALID_AD_DIST_OPTIONS
module-attribute
🔗
VALID_AD_DIST_OPTIONS = Literal[
"norm",
"expon",
"logistic",
"gumbel",
"gumbel_l",
"gumbel_r",
"extreme1",
"weibull_min",
]
jb
🔗
jb(
x: ArrayLike, axis: int = 0
) -> tuple[np.float64, np.float64, np.float64, np.float64]
Summary
The Jarque-Bera test is a statistical test used to determine whether a dataset follows a normal distribution. In time series forecasting, the test can be used to evaluate whether the residuals of a model follow a normal distribution.
Details
To apply the Jarque-Bera test to time series data, we first need to estimate the residuals of the forecasting model. The residuals represent the difference between the actual values of the time series and the values predicted by the model. We can then use the Jarque-Bera test to evaluate whether the residuals follow a normal distribution.
The Jarque-Bera test is based on two statistics, skewness and kurtosis, which measure the degree of asymmetry and peakedness in the distribution of the residuals. The test compares the observed skewness and kurtosis of the residuals to the expected values for a normal distribution. If the observed values are significantly different from the expected values, the test rejects the null hypothesis that the residuals follow a normal distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Data to test for normality. Usually regression model residuals that are mean 0. |
required |
axis
|
int
|
Axis to use if data has more than 1 dimension.
Default: |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input data |
Returns:
| Name | Type | Description |
|---|---|---|
JB |
float
|
The Jarque-Bera test statistic. |
JBpv |
float
|
The pvalue of the test statistic. |
skew |
float
|
Estimated skewness of the data. |
kurtosis |
float
|
Estimated kurtosis of the data. |
Examples
| Setup | |
|---|---|
1 2 3 4 | |
| Example 1: Using the airline dataset | |
|---|---|
1 2 3 | |
| Example 2: Using random noise | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
Calculation
The Jarque-Bera test statistic is defined as:
where:
- \(n\) is the sample size,
- \(S\) is the sample skewness, and
- \(K\) is the sample kurtosis.
Notes
Each output returned has 1 dimension fewer than data. The Jarque-Bera test statistic tests the null that the data is normally distributed against an alternative that the data follow some other distribution. It has an asymptotic \(\chi_2^2\) distribution.
Credit
All credit goes to the statsmodels library.
References
- Jarque, C. and Bera, A. (1980) "Efficient tests for normality, homoscedasticity and serial independence of regression residuals", 6 Econometric Letters 255-259.
Source code in src/ts_stat_tests/normality/algorithms.py
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 | |
ob
🔗
ob(x: ArrayLike, axis: int = 0) -> tuple[float, float]
Summary
The Omnibus test is a statistical test used to evaluate the normality of a dataset, including time series data. In time series forecasting, the Omnibus test can be used to assess whether the residuals of a model follow a normal distribution.
Details
The Omnibus test uses a combination of skewness and kurtosis measures to assess whether the residuals follow a normal distribution. Skewness measures the degree of asymmetry in the distribution of the residuals, while kurtosis measures the degree of peakedness or flatness. If the residuals follow a normal distribution, their skewness and kurtosis should be close to zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Data to test for normality. Usually regression model residuals that are mean 0. |
required |
axis
|
int
|
Axis to use if data has more than 1 dimension.
Default: |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input data |
Returns:
| Name | Type | Description |
|---|---|---|
statistic |
float
|
The Omnibus test statistic. |
pvalue |
float
|
The p-value for the hypothesis test. |
Examples
| Setup | |
|---|---|
1 2 3 4 | |
| Example 1: Using the airline dataset | |
|---|---|
1 2 3 | |
| Example 2: Using random noise | |
|---|---|
1 2 3 | |
Calculation
The D'Agostino's \(K^2\) test statistic is defined as:
where:
- \(Z_1(g_1)\) is the standard normal transformation of skewness, and
- \(Z_2(g_2)\) is the standard normal transformation of kurtosis.
Notes
The Omnibus test statistic tests the null that the data is normally distributed against an alternative that the data follow some other distribution. It is based on D'Agostino's \(K^2\) test statistic.
Credit
All credit goes to the statsmodels library.
References
- D'Agostino, R. B. and Pearson, E. S. (1973), "Tests for departure from normality," Biometrika, 60, 613-622.
- D'Agostino, R. B. and Stephens, M. A. (1986), "Goodness-of-fit techniques," New York: Marcel Dekker.
Source code in src/ts_stat_tests/normality/algorithms.py
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 | |
sw
🔗
sw(x: ArrayLike) -> ShapiroResult
Summary
The Shapiro-Wilk test is a statistical test used to determine whether a dataset follows a normal distribution.
Details
The Shapiro-Wilk test is based on the null hypothesis that the residuals of the forecasting model are normally distributed. The test calculates a test statistic that compares the observed distribution of the residuals to the expected distribution under the null hypothesis of normality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Array of sample data. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input data |
Returns:
| Type | Description |
|---|---|
ShapiroResult
|
A named tuple containing the test statistic and p-value: - statistic (float): The test statistic. - pvalue (float): The p-value for the hypothesis test. |
Examples
| Setup | |
|---|---|
1 2 3 4 | |
| Example 1: Using the airline dataset | |
|---|---|
1 2 3 | |
| Example 2: Using random noise | |
|---|---|
1 2 3 | |
Calculation
The Shapiro-Wilk test statistic is defined as:
where:
- \(x_{(i)}\) are the ordered sample values,
- \(\bar{x}\) is the sample mean, and
- \(a_i\) are constants generated from the covariances, variances and means of the order statistics of a sample of size \(n\) from a normal distribution.
Notes
The algorithm used is described in (Algorithm as R94 Appl. Statist. (1995)) but censoring parameters as described are not implemented. For \(N > 5000\) the \(W\) test statistic is accurate but the \(p-value\) may not be.
Credit
All credit goes to the scipy library.
References
- Shapiro, S. S. & Wilk, M.B (1965). An analysis of variance test for normality (complete samples), Biometrika, Vol. 52, pp. 591-611.
- Algorithm as R94 Appl. Statist. (1995) VOL. 44, NO. 4.
Source code in src/ts_stat_tests/normality/algorithms.py
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 | |
dp
🔗
dp(
x: ArrayLike,
axis: int = 0,
nan_policy: VALID_DP_NAN_POLICY_OPTIONS = "propagate",
) -> NormaltestResult
Summary
The D'Agostino and Pearson's test is a statistical test used to evaluate whether a dataset follows a normal distribution.
Details
The D'Agostino and Pearson's test uses a combination of skewness and kurtosis measures to assess whether the residuals follow a normal distribution. Skewness measures the degree of asymmetry in the distribution of the residuals, while kurtosis measures the degree of peakedness or flatness.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The array containing the sample to be tested. |
required |
axis
|
int
|
Axis along which to compute test. If |
0
|
nan_policy
|
VALID_DP_NAN_POLICY_OPTIONS
|
Defines how to handle when input contains nan.
Default: |
'propagate'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input data |
Returns:
| Type | Description |
|---|---|
NormaltestResult
|
A named tuple containing the test statistic and p-value: - statistic (float): The test statistic (\(K^2\)). - pvalue (float): A 2-sided chi-squared probability for the hypothesis test. |
Examples
| Setup | |
|---|---|
1 2 3 4 | |
| Example 1: Using the airline dataset | |
|---|---|
1 2 3 | |
| Example 2: Using random noise | |
|---|---|
1 2 3 | |
Calculation
The D'Agostino's \(K^2\) test statistic is defined as:
where:
- \(Z_1(g_1)\) is the standard normal transformation of skewness, and
- \(Z_2(g_2)\) is the standard normal transformation of kurtosis.
Notes
This function is a wrapper for the scipy.stats.normaltest function.
Credit
All credit goes to the scipy library.
References
- D'Agostino, R. B. (1971), "An omnibus test of normality for moderate and large sample size", Biometrika, 58, 341-348
- D'Agostino, R. and Pearson, E. S. (1973), "Tests for departure from normality", Biometrika, 60, 613-622
Source code in src/ts_stat_tests/normality/algorithms.py
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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
ad
🔗
ad(
x: ArrayLike, dist: VALID_AD_DIST_OPTIONS = "norm"
) -> AndersonResult
Summary
The Anderson-Darling test is a statistical test used to evaluate whether a dataset follows a normal distribution.
Details
The Anderson-Darling test tests the null hypothesis that a sample is drawn from a population that follows a particular distribution. For the Anderson-Darling test, the critical values depend on which distribution is being tested against.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Array of sample data. |
required |
dist
|
VALID_AD_DIST_OPTIONS
|
The type of distribution to test against.
Default: |
'norm'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input data |
Returns:
| Type | Description |
|---|---|
AndersonResult
|
A named tuple containing the test statistic, critical values, and significance levels: - statistic (float): The Anderson-Darling test statistic. - critical_values (list[float]): The critical values for this distribution. - significance_level (list[float]): The significance levels for the corresponding critical values in percents. |
Examples
| Setup | |
|---|---|
1 2 3 4 | |
| Example 1: Using the airline dataset | |
|---|---|
1 2 3 | |
| Example 2: Using random normal data | |
|---|---|
1 2 3 | |
Calculation
The Anderson-Darling test statistic \(A^2\) is defined as:
where:
- \(n\) is the sample size,
- \(F\) is the cumulative distribution function of the specified distribution, and
- \(x_i\) are the ordered sample values.
Notes
Critical values provided are for the following significance levels: - normal/exponential: 15%, 10%, 5%, 2.5%, 1% - logistic: 25%, 10%, 5%, 2.5%, 1%, 0.5% - Gumbel: 25%, 10%, 5%, 2.5%, 1%
Credit
All credit goes to the scipy library.
References
- Stephens, M. A. (1974). EDF Statistics for Goodness of Fit and Some Comparisons, Journal of the American Statistical Association, Vol. 69, pp. 730-737.
Source code in src/ts_stat_tests/normality/algorithms.py
427 428 429 430 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 503 504 505 506 507 508 509 510 511 512 | |