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)
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, :]
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
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 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)