Stats TimeSeries stationarity kpss - CyrilB1531/lodestar GitHub Wiki
Home âş Stats-TimeSeries âş Stationarity tests
The KPSS test, against the null of stationarity around a level or a line.
public static KpssResult Kpss(ReadOnlySpan<double> series, KpssOptions options = null)Parameters â series are the observations, in time order. options sets the null's trend and
the lag window rule, or null for the reference's defaults: a level, and Hobijn's automatic window.
Returns â KpssResult: the statistic, its tabulated p-value, the window used,
and whether the p-value was clamped at the end of its table.
Exceptions â ArgumentException when series carries a non-finite value, is constant, or lies
on one straight line under TrendTerms.ConstantAndTrend â its least-squares residuals within n¡ξ
of its largest observation and its largest second difference within eight ulps of it; or when
options fixes a window at or above the series length.
Example â the drifting series
AugmentedDickeyFuller reads, around a level and around a
line.
using Lodestar.Stats.TimeSeries;
double[] walk = [0.0, 1.2, 0.7, 2.1, 3.0, 2.4, 3.9, 5.1, 4.6, 6.0, 7.3, 6.8,
8.2, 9.5, 9.1, 10.4, 11.8, 11.2, 12.7, 14.0, 13.5, 14.9, 16.2, 15.8];
KpssResult level = Stationarity.Kpss(walk);
double levelStatistic = Math.Round(level.Statistic, 4); // => 0.7082
double levelP = Math.Round(level.PValue, 4); // => 0.0128
int levelWindow = level.LagCount; // => 3
KpssResult line = Stationarity.Kpss(walk, new KpssOptions { Regression = TrendTerms.ConstantAndTrend });
double lineStatistic = Math.Round(line.Statistic, 4); // => 0.1533
double lineP = Math.Round(line.PValue, 4); // => 0.0439
int lineWindow = line.LagCount; // => 9Remarks â the statistic is the sum of the squared partial sums of the residuals, over n², over
a Newey-West long-run variance with a Bartlett kernel. The residuals are the series less its mean,
or less its least-squares line under TrendTerms.ConstantAndTrend.
The p-value is interpolated in a four-point table, and clamped at both ends. Past either end the
reference returns the end value and warns; this sets
KpssResult.PValueBound instead, so the direction is a value a caller can read.
using Lodestar.Stats.TimeSeries;
double[] walk = [0.0, 1.2, 0.7, 2.1, 3.0, 2.4, 3.9, 5.1, 4.6, 6.0, 7.3, 6.8,
8.2, 9.5, 9.1, 10.4, 11.8, 11.2, 12.7, 14.0, 13.5, 14.9, 16.2, 15.8];
KpssResult narrow = Stationarity.Kpss(walk, new KpssOptions { LagRule = KpssLagRule.Fixed, LagCount = 2 });
double statistic = Math.Round(narrow.Statistic, 4); // => 0.8976
double p = narrow.PValue; // => 0.01
PValueBound bound = narrow.PValueBound; // => ActualIsSmallerApplies to â net10.0, netstandard2.0.
See also â Stationarity.AugmentedDickeyFuller,
KpssOptions, KpssResult, the
Python equivalence table.