コード例 #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
ファイル: ar_test.py プロジェクト: Skye777/ndw-climate
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
ファイル: ar_test.py プロジェクト: vejmelkam/ndw-climate
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
ファイル: ssa.py プロジェクト: jajcayn/multi-scale
    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))