コード例 #1
0
ファイル: test_utils.py プロジェクト: slnovak/nitime
def test_lwr():
    "test solution of lwr recursion"

    for trial in xrange(3):
        nc = np.random.randint(2, high=10)
        P = np.random.randint(2, high=6)
        # nc is channels, P is lags (order)
        r = np.random.randn(P + 1, nc, nc)
        r[0] = np.dot(r[0], r[0].T)  # force r0 to be symmetric

        a, Va = utils.lwr(r)
        # Verify the "orthogonality" principle of the mAR system
        # Set up a system in blocks to compute, for each k
        #   sum_{i=1}^{P} A(i)R(k-i) = -R(k) k > 0
        # = sum_{i=1}^{P} R(k-i)^T A(i)^T = -R(k)^T
        # = sum_{i=1}^{P} R(i-k)A(i)^T = -R(k)^T
        rmat = np.zeros((nc * P, nc * P))
        for k in xrange(1, P + 1):
            for i in xrange(1, P + 1):
                im = i - k
                if im < 0:
                    r1 = r[-im].T
                else:
                    r1 = r[im]
                rmat[(k - 1) * nc : k * nc, (i - 1) * nc : i * nc] = r1

        rvec = np.zeros((nc * P, nc))
        avec = np.zeros((nc * P, nc))
        for m in xrange(P):
            rvec[m * nc : (m + 1) * nc] = -r[m + 1].T
            avec[m * nc : (m + 1) * nc] = a[m].T

        l2_d = np.dot(rmat, avec) - rvec
        l2_d = (l2_d ** 2).sum() ** 0.5
        l2_r = (rvec ** 2).sum() ** 0.5

        # compute |Ax-b| / |b| metric
        yield nt.assert_almost_equal, l2_d / l2_r, 0
コード例 #2
0
ファイル: ar_est_2vars.py プロジェクト: slnovak/nitime
# Rxx_11(k) is E{ z1(t)z1*(t-k) }
# So only Rxx(0) is symmetric

Rxx = np.empty((N,n_process,n_process,n_lags))

for i in xrange(N):
    Rxx[i] = utils.autocov_vector(z[i],nlags=n_lags)

Rxx = Rxx.mean(axis=0)

R0 = Rxx[...,0]
Rm = Rxx[...,1:]

Rxx = Rxx.transpose(2,0,1)

a, ecov = utils.lwr(Rxx)

print 'compare coefficients to estimate:'
print a - am
print 'compare covariance to estimate:'
print ecov - cov

w, f_x2y, f_y2x, f_xy, Sw = alg.granger_causality_xy(a,ecov,Nfreqs=Nfreqs)

f = pp.figure()
c_x = np.empty((L,w.shape[0]))
c_y = np.empty((L,w.shape[0]))

for i in xrange(N):
    frex,c_x[i],nu = alg.multi_taper_psd(z[i][0])
    frex,c_y[i],nu = alg.multi_taper_psd(z[i][1])