Esempio n. 1
0
    def _granger_causality(self):
        """
        This returns a dict with the values computed by
        :func:`granger_causality_xy`, rather than arrays, so that we can delay
        the allocation of arrays as much as possible.

        """
        gc = dict(frequencies={},
                  gc_xy={},
                  gc_yx={},
                  gc_sim={},
                  spectral_density={})
        for i, j in self.ij:
            w, f_x2y, f_y2x, f_xy, Sw = \
               alg.granger_causality_xy(self.model_coef[i, j],
                                        self.error_cov[i, j],
                                        n_freqs=self._n_freqs)

            # All other measures are dependent on i, j:
            gc['gc_xy'][i, j] = f_x2y
            gc['gc_yx'][i, j] = f_y2x
            gc['gc_sim'][i, j] = f_xy
            gc['spectral_density'][i, j] = Sw

        return gc
Esempio n. 2
0
    def _granger_causality(self):
        """
        This returns a dict with the values computed by
        :func:`granger_causality_xy`, rather than arrays, so that we can delay
        the allocation of arrays as much as possible.

        """
        gc = dict(frequencies={}, gc_xy={}, gc_yx={}, gc_sim={},
                  spectral_density={})
        for i, j in self.ij:
            w, f_x2y, f_y2x, f_xy, Sw = \
               alg.granger_causality_xy(self.model_coef[i, j],
                                        self.error_cov[i, j],
                                        n_freqs=self._n_freqs)

            # All other measures are dependent on i, j:
            gc['gc_xy'][i, j] = f_x2y
            gc['gc_yx'][i, j] = f_y2x
            gc['gc_sim'][i, j] = f_xy
            gc['spectral_density'][i, j] = Sw

        return gc
Esempio n. 3
0
We use the Levinson-Whittle(-Wiggins) and Robinson algorithm, as described in [Morf1978]_
, in order to estimate the MAR coefficients and the covariance matrix:

"""

a, ecov = alg.lwr_recursion(Rxx)

"""

Next, we use the calculated coefficients and covariance matrix, in order to
calculate Granger 'causality':

"""

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


"""

This results in several different outputs, which we will proceed to plot.

First, we will plot the estimated spectrum. This will be compared to two other
estimates of the spectrum. The first is the 'true' spectrum, calculated from
the known coefficients that generated the data:

"""

fig01 = plt.figure()
ax01 = fig01.add_subplot(1, 1, 1)
Esempio n. 4
0
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)
Esempio n. 5
0
Rbxx = Rbxx.transpose(2, 0, 1)
b_est, cov_est2 = alg.lwr_recursion(Rbxx)
b_xy_est, cov_xy_est2 = alg.lwr_recursion(xyRb.transpose(2, 0, 1))
b_xz_est, cov_xz_est2 = alg.lwr_recursion(xzRb.transpose(2, 0, 1))
b_yz_est, cov_yz_est2 = alg.lwr_recursion(yzRb.transpose(2, 0, 1))


"""

We proceed to visualize these relationships:

"""

fig01 = pp.figure()

w, x2y_a, y2x_a, _, _ = alg.granger_causality_xy(a_xy_est, cov_xy_est1)
w, x2y_b, y2x_b, _, _ = alg.granger_causality_xy(b_xy_est, cov_xy_est2)
ax01 = fig01.add_subplot(321)
ax01.plot(w, x2y_a, 'b--')
ax01.plot(w, x2y_b, 'b')
ax01.set_title('x to y')
ax01.set_ylim((0, 6))
ax02 = fig01.add_subplot(322)
ax02.plot(w, y2x_a, 'b--')
ax02.plot(w, y2x_b, 'b')
ax02.set_title('y to x')
ax02.set_ylim((0, 6))

w, y2z_a, z2y_a, _, _ = alg.granger_causality_xy(a_yz_est, cov_yz_est1)
w, y2z_b, z2y_b, _, _ = alg.granger_causality_xy(b_yz_est, cov_yz_est2)
ax03 = fig01.add_subplot(323)
Esempio n. 6
0
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)
Esempio n. 7
0
a_yz_est, cov_yz_est1 = alg.lwr_recursion(yzRa.transpose(2, 0, 1))

Rbxx = Rbxx.transpose(2, 0, 1)
b_est, cov_est2 = alg.lwr_recursion(Rbxx)
b_xy_est, cov_xy_est2 = alg.lwr_recursion(xyRb.transpose(2, 0, 1))
b_xz_est, cov_xz_est2 = alg.lwr_recursion(xzRb.transpose(2, 0, 1))
b_yz_est, cov_yz_est2 = alg.lwr_recursion(yzRb.transpose(2, 0, 1))
"""

We proceed to visualize these relationships:

"""

fig01 = pp.figure()

w, x2y_a, y2x_a, _, _ = alg.granger_causality_xy(a_xy_est, cov_xy_est1)
w, x2y_b, y2x_b, _, _ = alg.granger_causality_xy(b_xy_est, cov_xy_est2)
ax01 = fig01.add_subplot(321)
ax01.plot(w, x2y_a, 'b--')
ax01.plot(w, x2y_b, 'b')
ax01.set_title('x to y')
ax01.set_ylim((0, 6))
ax02 = fig01.add_subplot(322)
ax02.plot(w, y2x_a, 'b--')
ax02.plot(w, y2x_b, 'b')
ax02.set_title('y to x')
ax02.set_ylim((0, 6))

w, y2z_a, z2y_a, _, _ = alg.granger_causality_xy(a_yz_est, cov_yz_est1)
w, y2z_b, z2y_b, _, _ = alg.granger_causality_xy(b_yz_est, cov_yz_est2)
ax03 = fig01.add_subplot(323)