Example #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) 
Example #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)
Example #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, :]
Example #4
0
def constructVAR(S, cs, ar_rng, nghb_rng):
    """
    Based on a grid indicating which grid points are associated together
    in a cluster, the SMG constructs a VAR model that represents the spatial
    dependencies in the data runs the VAR model to generate model time series
    
    Construct an SMG based on the spatial matrix S and the cluster strengths cs.
    cs indicates for each cluster 1, 2, ... num_clusters what are the cross ar
    coefficients.  Each time series has autoregressive coefficient ar.
    
    Elements in S are processed row-wise.  The ravel()ed structural matrix is returned.
    """
    C = S.shape[1]
    Sr = S.ravel()  # automatically in C order (row-wise)
    N = Sr.shape[0]
    A = np.zeros(shape=(N, N), dtype=np.float64)
    w = np.zeros(shape=(N, ), dtype=np.float64)

    # read the elements in C order (row by row)
    for i in range(N):
        A[i, i] = np.random.uniform(ar_rng[0], ar_rng[1])

        if Sr[i] > 0:
            blk_driver = np.nonzero(Sr == Sr[i])[0][0]
            if i > blk_driver:
                A[i, blk_driver] = cs[Sr[i]]


#                A[i, i] -= cs[Sr[i]]

    set_neighbor_weights(A, C, nghb_rng)

    # check stability of process
    if np.any(np.abs(scipy.linalg.eig(A, right=False)) > 1.0):
        raise ValueError("Unstable system constructed!")

    U = np.identity(N, dtype=np.float64)

    var = VARModel()
    var.set_model(A, w, U)
    return var, Sr
Example #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)))
Example #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)))
def constructVAR(S, cs, ar_rng, nghb_rng):
    """
    Based on a grid indicating which grid points are associated together
    in a cluster, the SMG constructs a VAR model that represents the spatial
    dependencies in the data runs the VAR model to generate model time series
    
    Construct an SMG based on the spatial matrix S and the cluster strengths cs.
    cs indicates for each cluster 1, 2, ... num_clusters what are the cross ar
    coefficients.  Each time series has autoregressive coefficient ar.
    
    Elements in S are processed row-wise.  The ravel()ed structural matrix is returned.
    """
    C = S.shape[1]
    Sr = S.ravel()  # automatically in C order (row-wise)
    N = Sr.shape[0]
    A = np.zeros(shape=(N, N), dtype=np.float64)
    w = np.zeros(shape=(N,), dtype=np.float64)

    # read the elements in C order (row by row)
    for i in range(N):
        A[i, i] = np.random.uniform(ar_rng[0], ar_rng[1])

        if Sr[i] > 0:
            blk_driver = np.nonzero(Sr == Sr[i])[0][0]
            if i > blk_driver:
                A[i, blk_driver] = cs[Sr[i]]
    #                A[i, i] -= cs[Sr[i]]

    set_neighbor_weights(A, C, nghb_rng)

    # check stability of process
    if np.any(np.abs(scipy.linalg.eig(A, right=False)) > 1.0):
        raise ValueError("Unstable system constructed!")

    U = np.identity(N, dtype=np.float64)

    var = VARModel()
    var.set_model(A, w, U)
    return var, Sr
Example #8
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))
Example #9
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, :]
Example #10
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))
Example #11
0
def ident_model(ts):
    v2 = VARModel()
    v2.estimate(ts, [1, 30], True, 'sbc', None)
    return v2.order()
Example #12
0
def ident_model(ts):
    v2 = VARModel()
    v2.estimate(ts, [1, 30], True, 'sbc', None)
    return v2.order()
Example #13
0
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)