Stationarity Testing#
statista.time_series.stationarity
#
Stationarity testing mixin for TimeSeries.
Stationarity
#
Bases: _TimeSeriesStub
Stationarity tests for TimeSeries.
Source code in src\statista\time_series\stationarity.py
19 20 21 22 23 24 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 | |
adf_test(regression='c', max_lag=None, column=None)
#
Augmented Dickey-Fuller unit root test.
Tests the null hypothesis that a unit root is present (series is non-stationary). Rejecting the null (p-value < alpha) indicates the series is stationary.
Implemented from scratch using OLS regression and MacKinnon (1994) approximate
p-values via scipy.stats.distributions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regression
|
str
|
Deterministic terms to include. - "c": constant only (default). Tests level stationarity. - "ct": constant + linear trend. Tests trend stationarity. - "n": no constant, no trend. |
'c'
|
max_lag
|
int
|
Maximum number of lagged differences to include. If None,
uses |
None
|
column
|
str
|
Column to test. If None, tests all columns. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: One row per column with: statistic, p_value, used_lag, n_obs, crit_1%, crit_5%, crit_10%, conclusion. |
Examples:
Stationary white noise rejects the null (p < 0.05):
>>> np.random.seed(42)
>>> ts = TimeSeries(np.random.randn(200))
>>> result = ts.adf_test()
>>> round(float(result.loc["Series1", "statistic"]), 4)
-3.309
>>> round(float(result.loc["Series1", "p_value"]), 4)
0.0187
>>> result.loc["Series1", "conclusion"]
'Stationary'
Non-stationary random walk fails to reject:
>>> np.random.seed(10)
>>> rw = np.cumsum(np.random.randn(200))
>>> ts_rw = TimeSeries(rw)
>>> result_rw = ts_rw.adf_test()
>>> round(float(result_rw.loc["Series1", "p_value"]), 4)
0.2937
>>> result_rw.loc["Series1", "conclusion"]
'Non-stationary'
Real hydrological data:
>>> data = np.loadtxt("examples/data/time_series1.txt")
>>> ts = TimeSeries(data)
>>> result = ts.adf_test()
>>> round(float(result.loc["Series1", "statistic"]), 4)
-2.0713
References
Dickey, D.A. and Fuller, W.A. (1979). Distribution of the estimators for autoregressive time series with a unit root. JASA, 74(366), 427-431.
MacKinnon, J.G. (1994). Approximate asymptotic distribution functions for unit-root and cointegration tests. JBES, 12(2), 167-176.
Source code in src\statista\time_series\stationarity.py
kpss_test(regression='c', n_lags=None, column=None)
#
KPSS stationarity test.
Tests the null hypothesis that the series IS stationary. Rejecting the null (p-value < alpha) indicates non-stationarity. This is the opposite of ADF.
Implemented from scratch following Kwiatkowski et al. (1992).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regression
|
str
|
Type of stationarity to test. - "c": level stationarity (default). Null: stationary around a constant. - "ct": trend stationarity. Null: stationary around a linear trend. |
'c'
|
n_lags
|
int
|
Lag truncation for the Newey-West estimator. If None,
uses |
None
|
column
|
str
|
Column to test. If None, tests all columns. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: One row per column with: statistic, p_value, lags, crit_10%, crit_5%, crit_2.5%, crit_1%, conclusion. |
Examples:
Stationary white noise does not reject the null (p > 0.05):
>>> np.random.seed(42)
>>> ts = TimeSeries(np.random.randn(200))
>>> result = ts.kpss_test()
>>> round(float(result.loc["Series1", "statistic"]), 4)
0.1974
>>> round(float(result.loc["Series1", "p_value"]), 1)
0.1
>>> result.loc["Series1", "conclusion"]
'Stationary'
Non-stationary random walk rejects the null:
>>> np.random.seed(10)
>>> rw = np.cumsum(np.random.randn(200))
>>> ts_rw = TimeSeries(rw)
>>> result_rw = ts_rw.kpss_test()
>>> round(float(result_rw.loc["Series1", "statistic"]), 4)
2.8977
>>> result_rw.loc["Series1", "conclusion"]
'Non-stationary'
Real hydrological data:
>>> data = np.loadtxt("examples/data/time_series1.txt")
>>> ts = TimeSeries(data)
>>> result = ts.kpss_test()
>>> round(float(result.loc["Series1", "statistic"]), 4)
0.1003
References
Kwiatkowski, D., Phillips, P.C.B., Schmidt, P. and Shin, Y. (1992). Testing the null hypothesis of stationarity against the alternative of a unit root. Journal of Econometrics, 54(1-3), 159-178.
Source code in src\statista\time_series\stationarity.py
stationarity_summary(alpha=DEFAULT_ALPHA)
#
Combined ADF + KPSS stationarity diagnosis.
Runs both ADF and KPSS tests and produces an interpretation:
+---------------+----------------+-------------------------------------------+ | ADF rejects? | KPSS rejects? | Diagnosis | +===============+================+===========================================+ | Yes | No | Stationary | +---------------+----------------+-------------------------------------------+ | No | Yes | Non-stationary (unit root) | +---------------+----------------+-------------------------------------------+ | Yes | Yes | Trend-stationary | +---------------+----------------+-------------------------------------------+ | No | No | Inconclusive | +---------------+----------------+-------------------------------------------+
Constant series (std=0) are treated as a special case and diagnosed as
"Stationary (constant)", since they are trivially stationary by
definition (constant mean, zero variance, constant autocorrelation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Significance level for both tests. Default 0.05. |
DEFAULT_ALPHA
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: One row per column with: adf_stat, adf_pvalue, kpss_stat, kpss_pvalue, diagnosis. |
Examples:
Stationary white noise (ADF rejects, KPSS does not):
>>> np.random.seed(42)
>>> ts = TimeSeries(np.random.randn(200))
>>> result = ts.stationarity_summary()
>>> result.loc["Series1", "diagnosis"]
'Stationary'
>>> round(float(result.loc["Series1", "adf_stat"]), 4)
-3.309
>>> round(float(result.loc["Series1", "kpss_stat"]), 4)
0.1974
Non-stationary random walk (ADF fails to reject, KPSS rejects):
>>> np.random.seed(10)
>>> rw = np.cumsum(np.random.randn(200))
>>> ts_rw = TimeSeries(rw)
>>> result_rw = ts_rw.stationarity_summary()
>>> result_rw.loc["Series1", "diagnosis"]
'Non-stationary (unit root)'
Source code in src\statista\time_series\stationarity.py
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 | |