Beispiel #1
0
    def _eigval_decomp_SZ(self, irf_resim):
        """
        Returns
        -------
        W: array of eigenvectors
        eigva: list of eigenvalues
        k: matrix indicating column # of largest eigenvalue for each c_i,j

        """
        neqs = self.neqs
        periods = self.periods

        cov_hold = np.zeros((neqs, neqs, periods, periods))
        for i in xrange(neqs):
            for j in xrange(neqs):
                cov_hold[i, j, :, :] = np.cov(irf_resim[:, 1:, i, j], rowvar=0)

        W = np.zeros((neqs, neqs, periods, periods))
        eigva = np.zeros((neqs, neqs, periods, 1))
        k = np.zeros((neqs, neqs))

        for i in xrange(neqs):
            for j in xrange(neqs):
                W[i, j, :, :], eigva[i, j, :, 0], k[i, j] = util.eigval_decomp(cov_hold[i, j, :, :])
        return W, eigva, k
Beispiel #2
0
    def _eigval_decomp_SZ(self, irf_resim):
        """
        Returns
        -------
        W: array of eigenvectors
        eigva: list of eigenvalues
        k: matrix indicating column # of largest eigenvalue for each c_i,j

        """
        neqs = self.neqs
        periods = self.periods

        cov_hold = np.zeros((neqs, neqs, periods, periods))
        for i in xrange(neqs):
            for j in xrange(neqs):
                cov_hold[i,j,:,:] = np.cov(irf_resim[:,1:,i,j],rowvar=0)

        W = np.zeros((neqs, neqs, periods, periods))
        eigva = np.zeros((neqs, neqs, periods, 1))
        k = np.zeros((neqs, neqs))

        for i in xrange(neqs):
            for j in xrange(neqs):
                W[i,j,:,:], eigva[i,j,:,0], k[i,j] = util.eigval_decomp(cov_hold[i,j,:,:])
        return W, eigva, k
Beispiel #3
0
    def err_band_sz3(self, orth=False, repl=1000, signif=0.05, seed=None, burn=100, component=None):
        """
        IRF Sims-Zha error band method 3. Does not assume symmetric error bands around mean.

        Parameters
        ----------
        orth : bool, default False
            Compute orthogonalized impulse responses
        repl : int, default 1000
            Number of MC replications
        signif : float (0 < signif < 1)
            Significance level for error bars, defaults to 95% CI
        seed : int, default None
            np.random seed
        burn : int, default 100
            Number of initial simulated obs to discard
        component : vector length neqs, default to largest for each
            Index of column of eigenvector/value to use for each error band
            Note: period of impulse (t=0) is not included when computing
                  principle component

        Reference
        ---------
        Sims, Christopher A., and Tao Zha. 1999. "Error Bands for Impulse
        Response". Econometrica 67: 1113-1155.
        """

        model = self.model
        periods = self.periods
        if orth:
            irfs = self.orth_irfs
        elif svar:
            irfs = self.svar_irfs
        else:
            irfs = self.irfs
        neqs = self.neqs
        irf_resim = model.irf_resim(orth=orth, repl=repl, T=periods, seed=seed, burn=100)
        stack = np.zeros((neqs, repl, periods * neqs))

        # stack left to right, up and down

        for p in xrange(repl):
            for i in xrange(neqs):
                stack[i, p, :] = np.ravel(irf_resim[p, 1:, :, i].T)

        stack_cov = np.zeros((neqs, periods * neqs, periods * neqs))
        W = np.zeros((neqs, periods * neqs, periods * neqs))
        eigva = np.zeros((neqs, periods * neqs))
        k = np.zeros((neqs))

        if component != None:
            if np.size(component) != (neqs):
                raise ValueError("Component array must be of length " + str(neqs))
            if np.argmax(component) >= neqs * periods:
                raise ValueError("Atleast one of the components does not exist")
            else:
                k = component

        # compute for eigen decomp for each stack
        for i in xrange(neqs):
            stack_cov[i] = np.cov(stack[i], rowvar=0)
            W[i], eigva[i], k[i] = util.eigval_decomp(stack_cov[i])

        gamma = np.zeros((repl, periods + 1, neqs, neqs))
        for p in xrange(repl):
            c = 0
            for j in xrange(neqs):
                for i in xrange(neqs):
                    gamma[p, 1:, i, j] = W[j, k[j], i * periods : (i + 1) * periods] * irf_resim[p, 1:, i, j]
                    if i == neqs - 1:
                        gamma[p, 1:, i, j] = W[j, k[j], i * periods :] * irf_resim[p, 1:, i, j]

        gamma_sort = np.sort(gamma, axis=0)  # sort to get quantiles
        indx = round(signif / 2 * repl) - 1, round((1 - signif / 2) * repl) - 1

        lower = np.copy(irfs)
        upper = np.copy(irfs)
        for i in xrange(neqs):
            for j in xrange(neqs):
                lower[:, i, j] = irfs[:, i, j] + gamma_sort[indx[0], :, i, j]
                upper[:, i, j] = irfs[:, i, j] + gamma_sort[indx[1], :, i, j]

        return lower, upper
Beispiel #4
0
    def err_band_sz3(self, orth=False, repl=1000, signif=0.05,
                     seed=None, burn=100, component=None):
        """
        IRF Sims-Zha error band method 3. Does not assume symmetric error bands around mean.

        Parameters
        ----------
        orth : bool, default False
            Compute orthogonalized impulse responses
        repl : int, default 1000
            Number of MC replications
        signif : float (0 < signif < 1)
            Significance level for error bars, defaults to 95% CI
        seed : int, default None
            np.random seed
        burn : int, default 100
            Number of initial simulated obs to discard
        component : vector length neqs, default to largest for each
            Index of column of eigenvector/value to use for each error band
            Note: period of impulse (t=0) is not included when computing
                  principle component

        Reference
        ---------
        Sims, Christoper A., and Tao Zha. 1999. “Error Bands for Impulse Response.” Econometrica 67: 1113-1155.
        """

        model = self.model
        periods = self.periods
        if orth:
            irfs = self.orth_irfs
        elif svar:
            irfs = self.svar_irfs
        else:
            irfs = self.irfs
        neqs = self.neqs
        irf_resim = model.irf_resim(orth=orth, repl=repl, T=periods, seed=seed,
                                   burn=100)
        stack = np.zeros((neqs, repl, periods*neqs))

        #stack left to right, up and down

        for p in xrange(repl):
            for i in xrange(neqs):
                stack[i, p,:] = np.ravel(irf_resim[p,1:,:,i].T)

        stack_cov=np.zeros((neqs, periods*neqs, periods*neqs))
        W = np.zeros((neqs, periods*neqs, periods*neqs))
        eigva = np.zeros((neqs, periods*neqs))
        k = np.zeros((neqs))

        if component != None:
            if np.size(component) != (neqs):
                raise ValueError("Component array must be of length " + str(neqs))
            if np.argmax(component) >= neqs*periods:
                raise ValueError("Atleast one of the components does not exist")
            else:
                k = component

        #compute for eigen decomp for each stack
        for i in xrange(neqs):
            stack_cov[i] = np.cov(stack[i],rowvar=0)
            W[i], eigva[i], k[i] = util.eigval_decomp(stack_cov[i])

        gamma = np.zeros((repl, periods+1, neqs, neqs))
        for p in xrange(repl):
            c=0
            for j in xrange(neqs):
                for i in xrange(neqs):
                        gamma[p,1:,i,j] = W[j,k[j],i*periods:(i+1)*periods] * irf_resim[p,1:,i,j]
                        if i == neqs-1:
                            gamma[p,1:,i,j] = W[j,k[j],i*periods:] * irf_resim[p,1:,i,j]

        gamma_sort = np.sort(gamma, axis=0) #sort to get quantiles
        indx = round(signif/2*repl)-1,round((1-signif/2)*repl)-1

        lower = np.copy(irfs)
        upper = np.copy(irfs)
        for i in xrange(neqs):
            for j in xrange(neqs):
                lower[:,i,j] = irfs[:,i,j] + gamma_sort[indx[0],:,i,j]
                upper[:,i,j] = irfs[:,i,j] + gamma_sort[indx[1],:,i,j]

        return lower, upper