Пример #1
0
    def _get_MC_realizations(self, n=100, multivariate=False, residuals=True):
        """
        Gets n surrogates for Monte Carlo testing. 
        If multivariate True, extimates AR(1) model for whole data, if False, treats as univariate
        and estimates each channel separately.
        If residuals True, generates AR model using actual residuals from fitting, if False,
        only uses model matrix A.
        """

        from var_model import VARModel

        self.MCsurrs = np.zeros([n] + list(self.X.shape))

        # multivariate model
        if multivariate:
            v = VARModel()
            v.estimate(self.X, [1, 1], True, 'sbc', None)
            if residuals:
                r = v.compute_residuals(self.X)

        # univariate model - estimating for each channel separately
        else:
            vs = {}
            for d in range(self.X.shape[1]):
                vs[d] = VARModel()
                vs[d].estimate(self.X[:, d], [1, 1], True, 'sbc', None)
                if residuals:
                    vs['res' + str(d)] = vs[d].compute_residuals(self.X[:, d])

        for i in range(n):
            if multivariate:
                if not residuals:
                    self.MCsurrs[i, ...] = v.simulate(N=self.X.shape[0])
                else:
                    self.MCsurrs[i, ...] = v.simulate_with_residuals(
                        r, orig_length=True)
            else:
                for d in range(self.X.shape[1]):
                    if not residuals:
                        self.MCsurrs[i, :, d] = np.squeeze(
                            vs[d].simulate(N=self.X.shape[0]))
                    else:
                        self.MCsurrs[i, :, d] = np.squeeze(
                            vs[d].simulate_with_residuals(vs['res' + str(d)],
                                                          orig_length=True))
Пример #2
0
def test_simulation_residuals():

    res = read_data3()
    print res[:10, :]

    v = VARModel()
    v.w = np.array([1.0, 1.4, -2.0])

    v.A = np.array([[0.3, 0.0, 0.2, 0.2, 0.3, 0.05],
                    [0.1, 0.6, 0.0, 0.0, 0.1, 0.10],
                    [0.0, 0.0, 0.0, 0.4, 0.0, 0.00]])

    v.U = np.array([[1.0, 0.1, -0.2], [0.0, 1.0, -0.3], [0.0, 0.0, 1.0]])

    ts = v.simulate_with_residuals(res[:10, :], ndisc=0)
    print()
    print()
    print ts[:10, :]
Пример #3
0
def test_simulation_residuals():
    
    res = read_data3()
    print res[:10, :]
    
    v = VARModel()
    v.w = np.array([1.0, 1.4, -2.0])
    
    v.A = np.array([ [0.3, 0.0, 0.2, 0.2, 0.3, 0.05],
                     [0.1, 0.6, 0.0, 0.0, 0.1, 0.10],
                     [0.0, 0.0, 0.0, 0.4, 0.0, 0.00] ])
    
    v.U = np.array([ [1.0, 0.1, -0.2],
                     [0.0, 1.0, -0.3],
                     [0.0, 0.0,  1.0] ])
    
    ts = v.simulate_with_residuals(res[:10, :], ndisc = 0)
    print()
    print()
    print ts[:10, :]
Пример #4
0
    def _get_MC_realizations(self, n = 100, multivariate = False, residuals = True):
        """
        Gets n surrogates for Monte Carlo testing. 
        If multivariate True, extimates AR(1) model for whole data, if False, treats as univariate
        and estimates each channel separately.
        If residuals True, generates AR model using actual residuals from fitting, if False,
        only uses model matrix A.
        """

        from var_model import VARModel

        self.MCsurrs = np.zeros([n] + list(self.X.shape))
        
        # multivariate model
        if multivariate:
            v = VARModel()
            v.estimate(self.X, [1,1], True, 'sbc', None)
            if residuals:
                r = v.compute_residuals(self.X)
        
        # univariate model - estimating for each channel separately
        else:
            vs = {}
            for d in range(self.X.shape[1]):
                vs[d] = VARModel()
                vs[d].estimate(self.X[:, d], [1,1], True, 'sbc', None)
                if residuals:
                    vs['res' + str(d)] = vs[d].compute_residuals(self.X[:, d])

        for i in range(n):
            if multivariate:
                if not residuals:
                    self.MCsurrs[i, ...] = v.simulate(N = self.X.shape[0])
                else:
                    self.MCsurrs[i, ...] = v.simulate_with_residuals(r, orig_length = True)
            else:
                for d in range(self.X.shape[1]):   
                    if not residuals:
                        self.MCsurrs[i, :, d] = np.squeeze(vs[d].simulate(N = self.X.shape[0]))
                    else:
                        self.MCsurrs[i, :, d] = np.squeeze(vs[d].simulate_with_residuals(vs['res' + str(d)], orig_length = True))