def test_MAR_est_LWR(): """ Test the LWR MAR estimator against the power of the signal This also tests the functions: transfer_function_xy, spectral_matrix_xy, coherence_from_spectral and granger_causality_xy """ # This is the same processes as those in doc/examples/ar_est_2vars.py: a1 = np.array([ [0.9, 0], [0.16, 0.8] ]) a2 = np.array([ [-0.5, 0], [-0.2, -0.5] ]) am = np.array([ -a1, -a2 ]) x_var = 1 y_var = 0.7 xy_cov = 0.4 cov = np.array([ [x_var, xy_cov], [xy_cov, y_var] ]) n_freqs = 1024 w, Hw = tsa.transfer_function_xy(am, n_freqs=n_freqs) Sw = tsa.spectral_matrix_xy(Hw, cov) # This many realizations of the process: N = 500 # Each one this long L = 1024 order = am.shape[0] n_lags = order + 1 n_process = am.shape[-1] z = np.empty((N, n_process, L)) nz = np.empty((N, n_process, L)) for i in xrange(N): z[i], nz[i] = utils.generate_mar(am, cov, L) a_est = [] cov_est = [] # This loop runs MAR_est_LWR: for i in xrange(N): Rxx = (tsa.MAR_est_LWR(z[i],order=n_lags)) a_est.append(Rxx[0]) cov_est.append(Rxx[1]) a_est = np.mean(a_est,0) cov_est = np.mean(cov_est,0) # This tests transfer_function_xy and spectral_matrix_xy: w, Hw_est = tsa.transfer_function_xy(a_est, n_freqs=n_freqs) Sw_est = tsa.spectral_matrix_xy(Hw_est, cov_est) # coherence_from_spectral: c = tsa.coherence_from_spectral(Sw) c_est = tsa.coherence_from_spectral(Sw_est) # granger_causality_xy: w, f_x2y, f_y2x, f_xy, Sw = tsa.granger_causality_xy(am, cov, n_freqs=n_freqs) w, f_x2y_est, f_y2x_est, f_xy_est, Sw_est = tsa.granger_causality_xy(a_est, cov_est, n_freqs=n_freqs) # interdependence_xy i_xy = tsa.interdependence_xy(Sw) i_xy_est = tsa.interdependence_xy(Sw_est) # This is all very approximate: npt.assert_almost_equal(Hw,Hw_est,decimal=1) npt.assert_almost_equal(Sw,Sw_est,decimal=1) npt.assert_almost_equal(c,c_est,1) npt.assert_almost_equal(f_xy,f_xy_est,1) npt.assert_almost_equal(f_x2y,f_x2y_est,1) npt.assert_almost_equal(f_y2x,f_y2x_est,1) npt.assert_almost_equal(i_xy,i_xy_est,1)
xy_cov = 0.4 cov = np.array([[x_var, xy_cov], [xy_cov, y_var]]) """ We can calculate the spectral matrix analytically, based on the known coefficients, for 1024 frequency bins: """ n_freqs = 1024 w, Hw = alg.transfer_function_xy(am, n_freqs=n_freqs) Sw_true = alg.spectral_matrix_xy(Hw, cov) """ Next, we will generate 500 example sets of 100 points of these processes, to analyze: """ #Number of realizations of the process N = 500 #Length of each realization: L = 1024 order = am.shape[0] n_lags = order + 1
def test_MAR_est_LWR(): """ Test the LWR MAR estimator against the power of the signal This also tests the functions: transfer_function_xy, spectral_matrix_xy, coherence_from_spectral and granger_causality_xy """ # This is the same processes as those in doc/examples/ar_est_2vars.py: a1 = np.array([[0.9, 0], [0.16, 0.8]]) a2 = np.array([[-0.5, 0], [-0.2, -0.5]]) am = np.array([-a1, -a2]) x_var = 1 y_var = 0.7 xy_cov = 0.4 cov = np.array([[x_var, xy_cov], [xy_cov, y_var]]) n_freqs = 1024 w, Hw = tsa.transfer_function_xy(am, n_freqs=n_freqs) Sw = tsa.spectral_matrix_xy(Hw, cov) # This many realizations of the process: N = 500 # Each one this long L = 1024 order = am.shape[0] n_lags = order + 1 n_process = am.shape[-1] z = np.empty((N, n_process, L)) nz = np.empty((N, n_process, L)) for i in range(N): z[i], nz[i] = utils.generate_mar(am, cov, L) a_est = [] cov_est = [] # This loop runs MAR_est_LWR: for i in range(N): Rxx = (tsa.MAR_est_LWR(z[i], order=n_lags)) a_est.append(Rxx[0]) cov_est.append(Rxx[1]) a_est = np.mean(a_est, 0) cov_est = np.mean(cov_est, 0) # This tests transfer_function_xy and spectral_matrix_xy: w, Hw_est = tsa.transfer_function_xy(a_est, n_freqs=n_freqs) Sw_est = tsa.spectral_matrix_xy(Hw_est, cov_est) # coherence_from_spectral: c = tsa.coherence_from_spectral(Sw) c_est = tsa.coherence_from_spectral(Sw_est) # granger_causality_xy: w, f_x2y, f_y2x, f_xy, Sw = tsa.granger_causality_xy(am, cov, n_freqs=n_freqs) w, f_x2y_est, f_y2x_est, f_xy_est, Sw_est = tsa.granger_causality_xy( a_est, cov_est, n_freqs=n_freqs) # interdependence_xy i_xy = tsa.interdependence_xy(Sw) i_xy_est = tsa.interdependence_xy(Sw_est) # This is all very approximate: npt.assert_almost_equal(Hw, Hw_est, decimal=1) npt.assert_almost_equal(Sw, Sw_est, decimal=1) npt.assert_almost_equal(c, c_est, 1) npt.assert_almost_equal(f_xy, f_xy_est, 1) npt.assert_almost_equal(f_x2y, f_x2y_est, 1) npt.assert_almost_equal(f_y2x, f_y2x_est, 1) npt.assert_almost_equal(i_xy, i_xy_est, 1)