Ejemplo n.º 1
0
    def _run_varLiNGAM(self, xt, verbose=False):
        """ Run the VarLiNGAM algorithm on data.

        Args:
            xt : time series matrix with size n*m (length*num_variables)

        Returns:
            Tuple: (Bo, Bhat) Instantaneous and lagged causal coefficients

        """
        Ident = np.identity(xt.shape[1])

        # Step 1: VAR estimation
        model = VAR(xt)
        results = model.fit(self.lag)
        Mt_ = results.params[1:, :]

        # Step 2: LiNGAM on Residuals
        resid_VAR = results.resid
        model = LiNGAM(verbose=verbose)
        data = pd.DataFrame(resid_VAR)
        Bo_ = model._run_LiNGAM(data)

        # Step 3: Get instantaneous matrix Bo from LiNGAM
        # Bo_ = pd.read_csv("results.csv").values

        # Step 4: Calculation of lagged Bhat
        Bhat_ = np.dot((Ident - Bo_), Mt_)
        return (Bo_, Bhat_)
Ejemplo n.º 2
0
    def estimateVARLiNGAM(self, xt):
        I = np.identity(xt.shape[1])

        # -------------------- Step 1: VAR estimation ----------------------------- #
        model = VAR(xt)
        results = model.fit(self.lag)
        Mt_ = results.params[1:, :]

        # -------------------- Step 2: LiNGAM on Residuals ------------------------ #
        resid_VAR = results.resid
        model = LiNGAM()
        data = pd.DataFrame(resid_VAR)
        model._run_LiNGAM(data)

        # ---------------------Step 3: Get instantaneous matrix Bo from LiNGAM----- #
        Bo_ = pd.read_csv("results.csv").values

        # ---------------------Step 4: Calculation of lagged Bhat ----------------- #
        Bhat_ = np.dot((I - Bo_), Mt_)
        return (Bo_, Bhat_)