pub fn kpss_test(
data: &[f64],
model: KpssModel,
lags: KpssLags,
) -> Result<KpssResult, TimeSeriesError>Expand description
KPSS test of stationarity: H₀ the series is stationary around a level (or a trend), H₁ it has a unit root.
The complement of adf_test, whose null is the unit root: a series ADF
does not reject and KPSS does is non-stationary; differencing it once and
testing again is how the number of differences is chosen (Hyndman &
Khandakar 2008, ndiffs).
§Algorithm
Residuals eₜ of the series on a constant (or constant + trend); η = Σ Sₜ² / n² with Sₜ the partial sums of eₜ; σ̂² the Newey-West long-run variance with Bartlett weights 1 − i/(l + 1); statistic η / σ̂². The p-value is interpolated in the table of critical values.
§Errors
TimeSeriesError: a non-finite value, fewer than 10 values, a fixed
lag outside [0, n − 1] (lags), or no variation about the level or
trend (σ̂² not positive against the series’ size).
§References
- Kwiatkowski, Phillips, Schmidt & Shin (1992). “Testing the null hypothesis of stationarity against the alternative of a unit root.” Journal of Econometrics, 54, 159–178.
- Hobijn, Franses & Ooms (1998). “Generalisations of the KPSS-test for stationarity.” Econometric Institute Report 9802/A.
- Newey & West (1994). “Automatic lag selection in covariance matrix estimation.” Review of Economic Studies, 61(4), 631–653.
§Examples
use u_analytics::testing::{kpss_test, KpssLags, KpssModel};
// A walk that drifts: stationarity around a level is rejected at 5 %.
let mut walk = vec![0.0];
for i in 1..80 {
walk.push(walk[i - 1] + 1.0 + 0.1 * (((i * 7919) % 13) as f64 - 6.0));
}
let r = kpss_test(&walk, KpssModel::Level, KpssLags::Hobijn).unwrap();
assert!(r.rejected[1]);