Exemple #1
0
def _prepare_AR_surrogates(a):
    from var_model import VARModel
    i, order_range, crit, ts = a
    if not np.any(np.isnan(ts)):
        v = VARModel()
        v.estimate(ts, order_range, True, crit, None)
        r = v.compute_residuals(ts)
    else:
        v = None
        r = np.nan
    return (i, v, r) 
Exemple #2
0
def _prepare_AR_surrogates(a):
    from var_model import VARModel
    i, order_range, crit, ts = a
    if not np.any(np.isnan(ts)):
        v = VARModel()
        v.estimate(ts, order_range, True, crit, None)
        r = v.compute_residuals(ts)
    else:
        v = None
        r = np.nan
    return (i, v, r)
Exemple #3
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))
Exemple #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))
Exemple #5
0
def run_parallel_sims():

    ts = read_data2()

    print("Fitting VAR model to data")
    v = VARModel()
    v.estimate(ts[:,0], [1, 30], True, 'sbc')
    res = v.compute_residuals(ts[:, 0])
    
#    cProfile.run('simulate_model((v, res))')
    
    print("Running simulations")
    t1 = datetime.now()
    
    # simulate 10000 time series (one surrogate)
    p = Pool(4)
#    sim_ts_all = p.map(ident_model, [ts[:,0]] * 10000)
    sim_ts_all = p.map(simulate_model, [(v, res)] * 100)
    
    delta = datetime.now() - t1
    
    print("DONE after %s" % (str(delta)))
Exemple #6
0
def run_parallel_sims():

    ts = read_data2()

    print("Fitting VAR model to data")
    v = VARModel()
    v.estimate(ts[:, 0], [1, 30], True, 'sbc')
    res = v.compute_residuals(ts[:, 0])

    #    cProfile.run('simulate_model((v, res))')

    print("Running simulations")
    t1 = datetime.now()

    # simulate 10000 time series (one surrogate)
    p = Pool(4)
    #    sim_ts_all = p.map(ident_model, [ts[:,0]] * 10000)
    sim_ts_all = p.map(simulate_model, [(v, res)] * 100)

    delta = datetime.now() - t1

    print("DONE after %s" % (str(delta)))
Exemple #7
0
def ident_model(ts):
    v2 = VARModel()
    v2.estimate(ts, [1, 30], True, 'sbc', None)
    return v2.order()
Exemple #8
0
def ident_model(ts):
    v2 = VARModel()
    v2.estimate(ts, [1, 30], True, 'sbc', None)
    return v2.order()
def _prepare_surrogates(a):
    i, j, order_range, crit, ts = a
    v = VARModel()
    v.estimate(ts, order_range, True, crit, None)
    r = v.compute_residuals(ts)
    return (i, j, v, r)
def _prepare_surrogates(a):
    i, j, order_range, crit, ts = a
    v = VARModel()
    v.estimate(ts, order_range, True, crit, None)
    r = v.compute_residuals(ts)
    return (i, j, v, r)