def formulate_regression()

in neural/linear/lin_model_template.py [0:0]


    def formulate_regression(self, U, Y):
        """
        Input:
        -----

        U : numpy array (n_samples x n_times x n_channels_u)
        Y : numpy array (n_samples x n_times x n_channels_y)
        """

        if self.log:
            print("----------------------------------------- \n")
            print("\n FORMULATING REGRESSION... \n")
            print("----------------------------------------- \n")

        # initializing variables
        n_samples, n_times, self.n_channels_y = Y.shape
        self.n_channels_u = U.shape[2]

        self.n_feats_x = self.n_channels_y * self.lag_y
        self.n_feats_v = self.n_channels_u * self.lag_u

        # scaling input timeseries
        if self.scaling:
            Y = self.scaler_y.fit_transform(Y.reshape(-1, Y.shape[-1]))
            U = self.scaler_u.fit_transform(U.reshape(-1, U.shape[-1]))
            Y = Y.reshape(n_samples, n_times, self.n_channels_y)
            U = U.reshape(n_samples, n_times, self.n_channels_u)

        # # zero padding for initialization?
        # U_ini = np.zeros((n_samples, self.lag_u, self.n_channels_u))
        # Y_ini = np.zeros((n_samples, self.lag_y, self.n_channels_y))
        # U = np.concatenate([U_ini, U], axis=1)
        # Y = np.concatenate([Y_ini, Y], axis=1)

        # convert: canonical space timeseries (Y, U) -> state space timeseries (X, V)
        X = statespace_transform(Y, self.lag_y)
        V = statespace_transform(U, self.lag_u)

        # take common time-length
        # Y and U having different lags and nb channels, their statespace timeseries have different lengths
        n_common_times = np.min([X.shape[1], V.shape[1]])
        X = X[:, -n_common_times:, :]
        V = V[:, -n_common_times:, :]
        _, _, n_feats_x = X.shape  # (n_samples, n_common_times, n_feats_x)
        _, _, n_feats_v = V.shape

        # formulate the problem as a regression
        regression_mat = np.concatenate(
            [
                X[:, :-1, :].reshape((n_common_times - 1) * n_samples, -1).T,  # ()
                V[:, 1:, :].reshape((n_common_times - 1) * n_samples, -1).T
            ],
            axis=0).T  # (n_common_times * n_samples, n_feats_x + n_feats_u)

        target = Y[:, -n_common_times + 1:, :].reshape(-1, self.n_channels_y)

        if self.log:
            print("shape of target: ", target.shape, "\n")
            print("shape of regression matrix: ", regression_mat.shape, "\n")

        return target, regression_mat