Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _fit_ocsb(x, m, lag, max_lag):
"""Fit the linear model used to compute the test statistic"""
y_first_order_diff = diff(x, m)
y = diff(y_first_order_diff)
ylag = OCSBTest._gen_lags(y, lag)
if max_lag > -1:
# y = tail(y, -maxlag)
y = y[max_lag:]
# A constant term is added in the R code's lm formula. We do that in
# the linear model's constructor
mf = ylag[:y.shape[0]]
ar_fit = sm.OLS(y, add_constant(mf)).fit(method='qr')
# Create Z4
z4_y = y_first_order_diff[lag:] # new endog
z4_lag = OCSBTest._gen_lags(y_first_order_diff, lag)[:z4_y.shape[0], :]
z4_preds = ar_fit.predict(add_constant(z4_lag)) # preds
def _fit_ocsb(x, m, lag, max_lag):
"""Fit the linear model used to compute the test statistic"""
y_first_order_diff = diff(x, m)
y = diff(y_first_order_diff)
ylag = OCSBTest._gen_lags(y, lag)
if max_lag > -1:
# y = tail(y, -maxlag)
y = y[max_lag:]
# A constant term is added in the R code's lm formula. We do that in
# the linear model's constructor
mf = ylag[:y.shape[0]]
ar_fit = sm.OLS(y, add_constant(mf)).fit(method='qr')
# Create Z4
z4_y = y_first_order_diff[lag:] # new endog
z4_lag = OCSBTest._gen_lags(y_first_order_diff, lag)[:z4_y.shape[0], :]
z4_preds = ar_fit.predict(add_constant(z4_lag)) # preds
z4 = z4_y - z4_preds # test residuals
y = y[max_lag:]
# A constant term is added in the R code's lm formula. We do that in
# the linear model's constructor
mf = ylag[:y.shape[0]]
ar_fit = sm.OLS(y, add_constant(mf)).fit(method='qr')
# Create Z4
z4_y = y_first_order_diff[lag:] # new endog
z4_lag = OCSBTest._gen_lags(y_first_order_diff, lag)[:z4_y.shape[0], :]
z4_preds = ar_fit.predict(add_constant(z4_lag)) # preds
z4 = z4_y - z4_preds # test residuals
# Create Z5. Looks odd because y and lag depend on each other and go
# back and forth for two stages
z5_y = diff(x)
z5_lag = OCSBTest._gen_lags(z5_y, lag)
z5_y = z5_y[lag:]
z5_lag = z5_lag[:z5_y.shape[0], :]
z5_preds = ar_fit.predict(add_constant(z5_lag))
z5 = z5_y - z5_preds
# Finally, fit a linear regression on mf with z4 & z5 features added
data = np.hstack((
mf,
z4[:mf.shape[0]].reshape(-1, 1),
z5[:mf.shape[0]].reshape(-1, 1)
))
return sm.OLS(y, data).fit(method='qr')
if is_constant(x):
return d
# get initial diff
pval, dodiff = testfunc(x)
# if initially NaN, return 0
if np.isnan(pval):
return 0 # (d is zero, but this is more explicit to the reader)
# Begin loop.
while dodiff and d < max_d:
d += 1
# do differencing
x = diff(x)
if is_constant(x):
return d
# get new result
pval, dodiff = testfunc(x)
# if it's NaN now, take the last non-null one
if np.isnan(pval):
return d - 1
# when d >= max_d
return d
More directly, whether to difference the time series.
"""
if not self._base_case(x):
return np.nan, False
# ensure vector
x = check_endog(x, dtype=DTYPE)
# if k is none...
k = self.k
if k is None:
k = np.trunc(np.power(x.shape[0] - 1, 1 / 3.0))
# See [2] for the R source. This is L153 - L160
k = int(k) + 1
y = diff(x) # diff(as.vector(x, mode='double'))
n = y.shape[0]
z = self._embed(y, k).T # Same as R embed(x, k)
# Compute ordinary least squares
res = self._ols(x, y, z, k)
STAT = self._ols_std_error(res)
# In the past we assigned to the np memory view which is slower
tableipl = np.array([
approx(self.tableT, self.table[:, i], xout=n, rule=2)[1] # xt,yt
for i in range(self.tablen)])
# make sure to do 1 - x...
_, interpol = approx(tableipl, self.tablep, xout=STAT, rule=2)
# Added in v1.1.0. Not sure whether it's likely we'll hit it, but it's
if max_D <= 0:
raise ValueError('max_D must be a positive integer')
# get the test - this validates m internally
testfunc = get_callable(test, VALID_STESTS)(m, **kwargs)\
.estimate_seasonal_differencing_term
x = check_endog(x, dtype=DTYPE, copy=False)
if is_constant(x):
return 0
D = 0
dodiff = testfunc(x)
while dodiff == 1 and D < max_D:
D += 1
x = diff(x, lag=m)
if is_constant(x):
return D
dodiff = testfunc(x)
return D