-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathARmodel.py
237 lines (176 loc) · 5.54 KB
/
ARmodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
'''
Autoregressive model example
X(t) = 0.9X(t-1) - 0.5X(t-2) + e(t)
Y(t) = 0.8Y(t-1) - 0.5Y(t-2) + 0.16X(t-1) - 0.2X(t-2) + n(t)
'''
import matplotlib.pyplot as plt
import numpy as np
import scipy.signal as sig
np.random.seed(1981)
def psd(x, y=[], T=1000):
'''
Computes the power spectrum of x, if y is given then the cross spectral density is computed instead.
x: x time series
y: y time series
T: Time length
'''
Xf = np.fft.fft(x)[:len(x)//2 + 1]
if len(y) > 0:
Yf = np.fft.fft(y)[:len(y)//2 + 1]
Sxy = np.multiply(Yf, np.conjugate(Xf)) / float( T )
return Sxy
else:
Sxx = np.multiply(Xf, np.conjugate(Xf)) / float( T )
return Sxx
def ARmodel(nfreq = 1024, T = 100):
N = nfreq * T
cov = np.array([ [1.00, 0.40],
[0.40, 0.70] ])
E = nz = np.random.multivariate_normal(np.zeros(cov.shape[0]), cov, size=(N,))
X = np.random.rand(N)
Y = np.random.rand(N)
for i in range(2, N):
X[i] = 0.9 * X[i-1] - 0.5 * X[i-2] + E[:,0][i]
Y[i] = 0.8 * Y[i-1] - 0.5 * Y[i-2] + 0.16 * X[i-1] - 0.2 * X[i-2] + E[:,1][i]
return X, Y
def cov_matrix(Z, lags):
N = len(Z)
L = len(Z[0])
R = np.zeros([N, N, len(lags)])
for i in range( len(lags) ):
n = -lags[i]
for r in range(N):
for c in range(N):
if i == 0:
R[r, c, i] += np.dot(Z[r],Z[c].T)
else:
R[r, c, i] += np.dot(Z[r][lags[i]:],Z[c][:-lags[i]].T)
R[:,:,i] /= (L-n)
return R
def transfer_function(coeff_mtx, cov_mtx, nfreq):
w = np.arange(0,np.pi, np.pi/(nfreq//2+1))
N = len(coeff_mtx)
a2 = 1 - np.sum( np.squeeze([coeff_mtx[i-1][0,0] * np.exp(-1j*w*i) for i in range(1,N+1)]), axis = 0 )
b2 = 0 - np.sum( np.squeeze([coeff_mtx[i-1][0,1] * np.exp(-1j*w*i) for i in range(1,N+1)]), axis = 0 )
c2 = 0 - np.sum( np.squeeze([coeff_mtx[i-1][1,0] * np.exp(-1j*w*i) for i in range(1,N+1)]), axis = 0 )
d2 = 1 - np.sum( np.squeeze([coeff_mtx[i-1][1,1] * np.exp(-1j*w*i) for i in range(1,N+1)]), axis = 0 )
A = np.array([ [a2, b2],
[c2, d2] ])
H = np.array([ [d2, -b2],
[-c2, a2]]) / ((A[0,0]*A[1,1]-A[1,0]*A[0,1]))
return w, H
def spectral_matrix(Hw, cov):
Sw = (1+1j)*np.zeros([2, 2, Hw.shape[-1]])
t00 = cov[0, 0] * Hw[0, 0].conj() + cov[0, 1] * Hw[0, 1].conj()
t01 = cov[0, 0] * Hw[1, 0].conj() + cov[0, 1] * Hw[1, 1].conj()
t10 = cov[1, 0] * Hw[0, 0].conj() + cov[1, 1] * Hw[0, 1].conj()
t11 = cov[1, 0] * Hw[1, 0].conj() + cov[1, 1] * Hw[1, 1].conj()
Sw[0, 0] = Hw[0, 0] * t00 + Hw[0, 1] * t10
Sw[0, 1] = Hw[0, 0] * t01 + Hw[0, 1] * t11
Sw[1, 0] = Hw[1, 0] * t00 + Hw[1, 1] * t10
Sw[1, 1] = Hw[1, 0] * t01 + Hw[1, 1] * t11
return Sw
def granger_causality(coeff_mtx, cov, nfreq):
w, H = transfer_function(coeff_mtx, cov, nfreq)
S = spectral_matrix(H, cov)
Hxx_tilda = H[0,0,:] + cov[1,0]/cov[0,0] * H[0,1,:]
Hyy_circn = H[1,1,:] + cov[1,0]/cov[1,1] * H[1,0,:]
fx2y = np.log( S[1,1,:] / (Hyy_circn*cov[1,1]*np.conj(Hyy_circn)) )
fy2x = np.log( S[0,0,:] / (Hxx_tilda*cov[0,0]*np.conj(Hxx_tilda)) )
fxy = np.log( (Hxx_tilda*cov[0,0]*np.conj(Hxx_tilda)) * (Hyy_circn*cov[1,1]*np.conj(Hyy_circn)) / (S[0,0]*S[1,1]-S[1,0]*S[0,1]) )
return fx2y, fy2x, fxy
'''
a1 = np.array([ [0.90, 0.00], k = np.array([0, 1, 2])
Rxx = np.zeros([2, 2, len(k)])
for i in range(N):
Rxx += cov_matrix(z[i], k) / float(N)
[0.16, 0.80] ])
a2 = -np.array([ [0.50, 0.00],
[0.20, 0.50] ])
cov = np.array([ [1.00, 0.40],
[0.40, 0.70] ])
sxx = (1+1j) * np.zeros(129)
syy = (1+1j) * np.zeros(129)
sxy = (1+1j) * np.zeros(129)
k = np.array([0, 1, 2])
Trials = 1000
R = np.zeros([2, 2, len(k)])
for i in range(Trials):
X, Y = ARmodel()
R += cov_matrix([X, Y], k) / float(Trials)
_, sxx_aux = sig.welch(X, fs = 1024)
_, syy_aux = sig.welch(Y, fs = 1024)
_, sxy_aux = sig.csd(X, Y, fs = 1024)
sxx+=sxx_aux/Trials
syy+=syy_aux/Trials
sxy+=sxy_aux/Trials
'''
import numpy as np
import matplotlib.pyplot as plt
import nitime.algorithms as alg
import nitime.utils as utils
'''
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]])
'''
#Number of realizations of the process
N = 500
k = np.array([0, 1, 2])
Rxx = np.zeros([2, 2, len(k)])
for i in range(N):
#Rxx += cov_matrix(z[i], k) / float(N)
X, Y = ARmodel(nfreq = 1024, T = 1)
Rxx += cov_matrix([X,Y], k) / float(N)
R0 = Rxx[..., 0]
Rm = Rxx[..., 1:]
Rxx = Rxx.transpose(2, 0, 1)
a, ecov = alg.lwr_recursion(Rxx)
fx2y, fy2x, fxy = granger_causality(-a, ecov, nfreq=1024)
plt.plot(fx2y.real)
plt.plot(fy2x.real)
plt.plot(fxy.real)
plt.xlim([0,515])
plt.ylim([-0.01,0.7])
'''
import numpy as np
import matplotlib.pyplot as plt
import nitime.algorithms as alg
import nitime.utils as utils
np.random.seed(1981)
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 = alg.transfer_function_xy(am, n_freqs=n_freqs)
Sw_true = alg.spectral_matrix_xy(Hw, cov)
#Number of realizations of the process
N = 500
#Length of each realization:
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)
Rxx = np.empty((N, n_process, n_process, n_lags))
for i in range(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 = alg.lwr_recursion(Rxx)
'''