def preprocess(self, ts, train_percentage): if len(ts.shape) == 1: ts = ts.reshape(1, len(ts)) # Regress original ts X_orig, y_orig = sliding_window(ts, p=self.q, drop_last_dim=True) self.regressor_model.preprocess(ts, 1.0) self.regressor_model.fit(X_orig, y_orig) # Predict with regressor model and substract original to obtain residuals. pred = self.regressor_model.model.predict(X_orig) pred = pred.reshape(1, len(pred)) residual_ts = np.concatenate((np.zeros( (1, self.q)), pred), axis=1) - ts # Define sliding window on residuals and compute historic vols from it X_residual, y_residual = sliding_window(residual_ts, p=self.q, drop_last_dim=False) y_sigmas = X_residual.std(axis=1) y_sigmas_squared = y_sigmas * y_sigmas X_residual_squared = X_residual * X_residual # Compute train-test-split and set internal variables (X_train, y_train), (X_test, y_test) = train_test_split( X_residual_squared, y_sigmas_squared, train_percentage=train_percentage) self.X_train = X_train self.y_train = y_train self.X_test = X_test self.y_test = y_test self.has_data = True
def sliding_window_test(): X, y = sliding_window(X_dummy, p=3) assert X.shape == (6, 3, 1) assert y.shape == (2 * (6 - 3), ) assert np.array_equal(X[0, :, 0], np.array([1, 2, 3])) assert np.array_equal(y[0], 4) assert np.array_equal(X[5, :, 0], np.array([9, 10, 11])) assert np.array_equal(y[4], 11)
def preprocess(self, ts, train_percentage): if len(ts.shape) == 1: ts = ts.reshape(1, len(ts)) X, y = sliding_window(ts, p=self.p) (X_train, y_train), (X_test, y_test) = train_test_split( X, y, train_percentage=train_percentage) self.X_train = X_train self.y_train = y_train self.X_test = X_test self.y_test = y_test self.has_data = True
def sliding_window_multi_test(): X, y = sliding_window(X_mult_dummy, p=3) assert X.shape == (3, 3, 2) assert y.shape == (6 - 3, 2)