-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDynamicNets_functions.jl
692 lines (504 loc) · 16.8 KB
/
DynamicNets_functions.jl
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#**************************************************************************
# by Michael Ellington, Lubos Hanus and Jozef Barunik
#**************************************************************************
## Set of functions to estimate TVP QBLL
function MinnNWprior(Y, T, N, L, shrinkage)
K = N * L + 1
SI = vcat(zeros(1, N), 0.1 .* diagm(0 => ones(N)), zeros((L-1)*N, N))
PI = zeros(K, 1)
sigma_sq = zeros(N, 1)
for i in 1:N
# Create lags of dependent variable
Y_i = mlag2(Y[:, i], L)
Y_i = Y_i[(L+1):T, :]
X_i = [ones(T - L, 1) Y_i]
y_i = Y[(L+1):T, i]
# OLS estimates of i-th equation
alpha_i = (X_i' * X_i) \ (X_i' * y_i)
sigma_sq[i, 1] = (1.0 ./ (T-L+1)) * (y_i .- X_i * alpha_i)' * (y_i .- X_i * alpha_i)
end
s = 1.0 ./ sigma_sq
for ii=1:L
PI[(2 + N * (ii-1)):(1+N*ii)] = (shrinkage * shrinkage) * s / (ii * ii)
end
PI[1] = 10^2 # prior variance for constant is loose
PI2 = Diagonal(vec(PI)) .* ones(K, K)
# now for Wishart priors following Petrova (2018)
a = max(N+2, N+2*8-T)
RI = (a-N-1) * sigma_sq
RI2 = Diagonal(vec(RI)) .* ones(N, N)
return(SI, PI2, a, RI2)
end
# MinnNWprior(randn(100, 2), 100, 2, 3, 0.05)
function normker(T, H)
ww = zeros(T, T)
for j in 1:T
for i in 1:T
z = (i - j)/H
ww[i, j] = (1.0 / sqrt(2.0 * pi)) * exp((-1.0 / 2.0) * (z * z))
end
end
s = sum(ww, dims=2)
adjw = zeros(T, T)
for k in 1:T
adjw[k, :] = ww[k, :] / s[k]
end
cons = sum(adjw .^ 2.0, dims = 2)
for k in 1:T
adjw[k, :] = (1.0 / cons[k]) * (adjw[k, :])
end
return adjw
end
function mlag2(X, p)
if ndims(X) == 1
Traw = length(X)
N = 1
else
Traw, N = size(X)
end
Xlag = zeros(Traw, N*p)
for ii in 1:p
Xlag[(p+1):Traw, (N*(ii - 1)+1):(N*ii)] = X[(p+1-ii):(Traw-ii), 1:N]
end
return Xlag
end
function lag0(x, p)
R, C = size(x)
# Take the first R-p rows of matrix x
x1 = x[1:(R-p), :]
# Preceed them with p rows of zeros and return
return vcat(zeros(p, C), x1)
end
function Lsize(N, nsd)
LL = (nsd/N - 1)/N
convert(Int, LL)
end
# GET CONNECTEDNESS FUNCTIONS
function varcompanion(A, ndet, n, p)
# create companion matrix of A
A = A[:, (ndet+1):end]
A = [A; [Diagonal(ones(n*(p-1))) zeros(n*(p-1),n)]]
return A
end
function getCoefsStable(bayesalpha, bayesgamma, bayessv, BB, N, L)
B0 = zeros(N, N*L+1)
A0 = zeros(N, N)
Ficom = zeros(N*L, N*L)
mm = 0
while mm < 1
A0 = rand(InverseWishart(bayesalpha, bayesgamma))
nu = randn(N*L+1, N)
B0 = (BB + cholesky(Symmetric(bayessv).*1.0).U' * (nu * (cholesky(A0).U)))'
Ficom[1:N, :] = B0[:, 2:(N*L+1)]
for pp in 2:L
Ficom[(1 + N * (pp-1)):(pp*N), (1 + N * (pp-2)):(N*(pp-1))] = diagm(0 => ones(N)); # companion without constant
end
maxEig = maximum(abs.(eigvals(Ficom)))
if maxEig < .999 # check stability of draw
stabInd = 1
mm = 1
end
end
return B0, A0
end
function get_GIRF(B, A0, ND::Int, N::Int, L::Int, HORZ::Int,corr)
# Get GIRFs as in Equation (10) KPP(1996)
# B is N, N*L+1 coefficient matrix
# A0 is N x N covariance matrix
# ND = 1 a constant or not. # L is lag length
if corr == true
A0 = Diagonal(A0);
end
B = convert(Array{Float64}, varcompanion(B, ND, N, L))
J = convert(Array{Float64}, [Diagonal(ones(N)) zeros(N, N*(L-1))])
ir1 = Array{Float64}(undef, N, N, HORZ+1);
wold = Array{Float64}(undef, N, N, HORZ+1);
# GET MA coefficients
jT = J'
bh = B ^ 0
@views @inbounds for h in 0:HORZ
wold[:, :, h+1] = J * ((bh) * jT)
bh = bh * B
end
A0 = (1.0 ./ sqrt.(diag(A0))) .* A0
@views @inbounds for h in 1:(HORZ+1)
@inbounds for i in 1:N
ir1[:, i, h] = A0[i, :]' * wold[:, :, h]';
end
end
return ir1, wold
end
function get_GIRF_fast(B, A0, ND::Int, N::Int, L::Int, HORZ::Int)
# Get WOLD ..
# B is N, N*L+1 coefficient matrix
# A0 is N x N covariance matrix
# ND = 1 a constant or not. # L is lag length
B = convert(Array{Float64}, varcompanion(B, ND, N, L))
J = convert(Array{Float64}, [Diagonal(ones(N)) zeros(N, N*(L-1))])
wold = Array{Float64}(undef, N, N, HORZ+1);
# GET MA coefficients
jT = J'
bh = B ^ 0
@views @inbounds for h in 0:HORZ
wold[:, :, h+1] = J * (bh * jT)
bh = bh * B
end
return wold
end
function var_decomp(nvars, nsteps, ir)
# calculates variance decomposition and accumulate vardeco
resp6 = Array{Float64}(undef, nvars, nvars, nsteps);
resp7 = zeros(nsteps, 1);
vardecomp = Array{Float64}(undef, nvars, nvars, nsteps);
@inbounds for j in 1:nvars
@inbounds for i in 1:nvars
# variance of the forecast error: conditional variance
resp6[i, j, :] = @views cumsum((ir[i, j, :] .* ir[i, j, :]));
end
end
@inbounds for j in 1:nvars
@inbounds for i in 1:nvars
@inbounds for k in 1:nsteps
resp7[k, 1] = @views sum(resp6[i, :, k]);
end
# conditional/unconditional variance
vardecomp[i, j, :] = @views (resp6[i, j, :])' ./ resp7';
end
end
return(vardecomp)
end
function get_timenet(N, HO, irf)
fev = var_decomp(N, HO, irf);
fev = fev[:, :, end];
FF = sum(fev);
timecon = 100.0 .* (1.0 .- tr(fev) ./ FF);
trT = zeros(N)
ttT = zeros(N)
DCRTnorm = zeros(N)
DCTTnorm = zeros(N)
for i in 1:N
trT[i] = 100.0 .* ( sum(fev[i, :]) .- fev[i, i] ) ./FF
ttT[i] = 100.0 .* ( sum(fev[:, i]) .- fev[i, i] ) ./FF
DCRTnorm[i]= sum(fev[i, :] ./ fev[i, i])
DCTTnorm[i]= sum(fev[:, i] ./ fev[i, i])
end
NDCT = ttT .- trT
return (timecon, trT, ttT, DCRTnorm, DCTTnorm, NDCT)
end
function oneTimePrior(kk, weights1, priorprec0, X, y, SI, PI, a, RI)
w = weights1[kk, :]
bayesprec = (priorprec0 .+ X' * Diagonal(w) * X)
bayessv = inv(bayesprec)
BB = bayessv * ((X' * Diagonal(w)) * y .+ priorprec0 * SI)
bayesalpha = a + sum(w)
g1 = SI' * priorprec0 * SI
g2 = y' * Diagonal(w) * y
g3 = BB' * bayesprec * BB
bayesgamma = RI + g1 + g2 - g3
bayesgamma = 0.5 * bayesgamma + 0.5 * bayesgamma' # it is symmetric but just in case
return bayesalpha, bayessv, bayesgamma, BB
end
function get_dynnet(wo, TT, sig,corr,cut1::Int,cut2::Int)
Tw = 200 # Define frequency window;
omeg = LinRange(0.0, pi, Tw) # create equally spaced line from 0 to pi in 261 intervals
# Define bands
omeg2 = pi ./ omeg;
d1 = omeg2 .> cut2 # long term equals (20,260+] days
d2 = (omeg2 .<= cut2) .* (omeg2 .> cut1) # medium term equals (5,20] days
d3 = omeg2 .<= cut1 # short term equals [1,5] days
N = size(wo, 1)
HO = size(wo, 3)
if corr == true
diag_sig = Diagonal(sig);
end
if corr == false
diag_sig = sig;
end
expnnom = exp.(-im .* repeat(omeg, 1, HO) .* repeat((1:HO)', Tw, 1));
expnnom = convert.(Complex{Float32}, expnnom)
Omeg2 = Array{Float64}(undef, N, N)
@views @inbounds for hh in 1:HO
Omeg2 += wo[:,:,hh] * (diag_sig * wo[:,:,hh]')
end
Omeg2 = diag(Omeg2)
wo = convert.(Float32, wo);
FC = Array{Float64}(undef, N, N, Tw);
GI = Array{Complex{Float32}}(undef, N, N);
@views @inbounds for w in 1:Tw
fill!(GI, 0.0);
for nn in 1:HO
GI .+= wo[:, :, nn] .* expnnom[w, nn];
end
PS = abs2.(GI * diag_sig);
@inbounds for k in 1:N
@inbounds for j in 1:N
FC[j, k, w] = PS[j, k] ./ (Omeg2[j] .* sig[k, k]);
end
end
end
PP1 = dropdims(sum(FC, dims = 3), dims=3);
@views for w in 1:Tw
for j in 1:N
FC[j, :, w] = FC[j, :, w] ./ sum(PP1[j, :]);
end
end
thetainf = dropdims(sum(FC, dims = 3), dims=3);
### BANDS : d1 d2 d3, Long, Medium, Short
# theta_{d_i} summed over bands
temp1 = dropdims(sum(FC[:, :, d1], dims = 3), dims=3);
temp2 = dropdims(sum(FC[:, :, d2], dims = 3), dims=3);
temp3 = dropdims(sum(FC[:, :, d3], dims = 3), dims=3);
for j in 1:N
sumthetaj = sum(thetainf[j, :]);
temp1[j, :] = temp1[j, :] ./ sumthetaj;
temp2[j, :] = temp2[j, :] ./ sumthetaj;
temp3[j, :] = temp3[j, :] ./ sumthetaj;
end
## GET Net Directional Connectedness and Transmitters + Recievers
trL = zeros(N)
ttL = zeros(N)
trM = zeros(N)
ttM = zeros(N)
trS = zeros(N)
ttS = zeros(N)
trT = zeros(N)
ttT = zeros(N)
DCRL = zeros(N)
DCTL = zeros(N)
DCRM = zeros(N)
DCTM = zeros(N)
DCRS = zeros(N)
DCTS = zeros(N)
DCRT = zeros(N)
DCTT = zeros(N)
DCRLnorm = zeros(N)
DCTLnorm = zeros(N)
DCRMnorm = zeros(N)
DCTMnorm = zeros(N)
DCRSnorm = zeros(N)
DCTSnorm = zeros(N)
DCRTnorm = zeros(N)
DCTTnorm = zeros(N)
for i in 1:N
trL[i] = sum(temp1[i, :]) .- temp1[i, i]
ttL[i] = sum(temp1[:, i]) .- temp1[i, i]
DCRL[i]= sum(temp1[i, :])
DCTL[i]= sum(temp1[:, i])
DCRLnorm[i]= sum(temp1[i, :] ./ temp1[i, i])
DCTLnorm[i]= sum(temp1[:, i] ./ temp1[i, i])
trM[i] = sum(temp2[i, :]) .- temp2[i, i]
ttM[i] = sum(temp2[:, i]) .- temp2[i, i]
DCRM[i]= sum(temp2[i, :])
DCTM[i]= sum(temp2[:, i])
DCRMnorm[i]= sum(temp2[i, :] ./ temp2[i, i])
DCTMnorm[i]= sum(temp2[:, i] ./ temp2[i, i])
trS[i] = sum(temp3[i, :]) .- temp3[i, i]
ttS[i] = sum(temp3[:, i]) .- temp3[i, i]
DCRS[i]= sum(temp3[i, :])
DCTS[i]= sum(temp3[:, i])
DCRSnorm[i]= sum(temp3[i, :] ./ temp3[i, i])
DCTSnorm[i]= sum(temp3[:, i] ./ temp3[i, i])
trT[i] = sum(thetainf[i, :]) .- thetainf[i, i]
ttT[i] = sum(thetainf[:, i]) .- thetainf[i, i]
DCRT[i]= sum(thetainf[i, :])
DCTT[i]= sum(thetainf[:, i])
DCRTnorm[i]= sum(thetainf[i, :] ./ thetainf[i, i])
DCTTnorm[i]= sum(thetainf[:, i] ./ thetainf[i, i])
end
NDCL = ttL .- trL
NDCM = ttM .- trM
NDCS = ttS .- trS
NDCT = ttT .- trT
# Connectedness measures
WC1 = 100.0 .* (1 .- tr(temp1) ./ sum(temp1));
TC1 = WC1 .* (sum(temp1) ./ sum(thetainf));
WC2 = 100 .* (1 .- tr(temp2) ./ sum(temp2));
TC2 = WC2 .* (sum(temp2) ./ sum(thetainf));
WC3 = 100 .* (1 .- tr(temp3) ./ sum(temp3));
TC3 = WC3 .* (sum(temp3) ./ sum(thetainf));
# % Total Frequency Connect
TFC = TC1 .+ TC2 .+ TC3;
return(TFC, TC1, TC2, TC3, WC1, WC2, WC3, DCTL, DCTM, DCTS, DCTT, DCRL, DCRM, DCRS, DCRT, DCTLnorm, DCTMnorm, DCTSnorm, DCTTnorm, DCRLnorm, DCRMnorm, DCRSnorm, DCRTnorm, NDCL, NDCM, NDCS, NDCT)
end
function f_all(it, ij, cut1, cut2,T::Int, N::Int, L::Int, weights1, priorprec0, X, y, SI, PI, a, RI,corr)
bayesalpha, bayessv, bayesgamma, BB = oneTimePrior(it, weights1, priorprec0, X, y, SI, PI, a, RI)
HO = 100 + 1 # IRF Horizon, Also use this for FEVD horizon.
ND = 1
HORZ = HO - 1
B0, A0 = getCoefsStable(bayesalpha, bayesgamma, bayessv, BB, N, L)
wold = get_GIRF_fast(B0, A0, ND, N, L, HORZ)
tfc, tcl, tcm, tcs, wcl, wcm, wcs, dctl, dctm, dcts, dctt, dcrl, dcrm, dcrs, dcrt, dctlnorm, dctmnorm, dctsnorm, dcttnorm, dcrlnorm, dcrmnorm, dcrsnorm, dcrtnorm, ndcl, ndcm, ndcs, ndct = get_dynnet(wold, T, A0,corr,cut1,cut2);
return [[tfc, tcl, tcm, tcs, wcl, wcm, wcs]; dctl; dctm; dcts; dctt; dcrl; dcrm; dcrs; dcrt; dctlnorm; dctmnorm; dctsnorm; dcttnorm; dcrlnorm; dcrmnorm; dcrsnorm; dcrtnorm; ndcl; ndcm; ndcs; ndct]
end
function DynNet(data,cut1::Int,cut2::Int,L::Int,H::Int,Nsim::Int,corr)
shrinkage = 0.05
T, N = size(data)
K = N * L + 1
X = zeros(Float64, T - L, K-1)
for i in 1:L
temp = lag0(data, i)
X[:, (1 + N*(i-1) : i*N)] = temp[(1+L:T), :]
end
y = data[(1+L):T, :]
T = T - L
X = [ones(Float64, T, 1) X]
K = N * L + 1
SI, PI, a, RI = MinnNWprior(data, T, N, L, shrinkage)
weights1 = convert.(Float64, normker(T, H))
priorprec0 = convert.(Float64, inv(PI));
xmean=zeros(7 + (2*4*2+4)*N,T);
xci1=zeros(7 + (2*4*2+4)*N,T);
xci2=zeros(7 + (2*4*2+4)*N,T);
for it=1:T
out = zeros(7 + (2*4*2+4)*N,Nsim)
for i in 1:Nsim
out[:,i]=f_all(it, i, cut1,cut2,T, N, L, weights1, priorprec0, X, y, SI, PI, a, RI,corr)
end
#xmean[:,it] = mean(out,dims=2)
#xsd[:,it] = std(out,dims=2)
xmean[:,it] = [quantile(out[i,:],0.5) for i=1:size(out)[1]]
xci1[:,it] = [quantile(out[i,:],0.025) for i=1:size(out)[1]]
xci2[:,it] = [quantile(out[i,:],0.975) for i=1:size(out)[1]]
end
return(xmean,xci1,xci2)
end
function f_time(it, ij, T::Int, N::Int, L::Int, weights1, priorprec0, X, y, SI, PI, a, RI,HH,corr)
bayesalpha, bayessv, bayesgamma, BB = oneTimePrior(it, weights1, priorprec0, X, y, SI, PI, a, RI)
HO = HH + 1 # IRF Horizon, Also use this for FEVD horizon.
ND = 1
HORZ = HO - 1
B0, A0 = getCoefsStable(bayesalpha, bayesgamma, bayessv, BB, N, L)
irf, = get_GIRF(B0, A0, ND, N, L, HORZ,corr)
timecon, DCTT, DCRT, DCRTnorm, DCTTnorm, NDCT = get_timenet(N, HO, irf);
return [timecon;DCTT;DCRT;DCRTnorm;DCTTnorm;NDCT]
end
function DynNet_time(data,L::Int,HH::Int,H::Int,Nsim::Int,corr)
# L - VAR lags
# H - bandwidth width
# Nsim - no of simulations
shrinkage = 0.05
T, N = size(data)
K = N * L + 1
X = zeros(Float64, T - L, K-1)
for i in 1:L
temp = lag0(data, i)
X[:, (1 + N*(i-1) : i*N)] = temp[(1+L:T), :]
end
y = data[(1+L):T, :]
T = T - L
X = [ones(Float64, T, 1) X]
K = N * L + 1
SI, PI, a, RI = MinnNWprior(data, T, N, L, shrinkage)
weights1 = convert.(Float64, normker(T, H))
priorprec0 = convert.(Float64, inv(PI))
xmean=zeros(1+5*N,T);
xci1=zeros(1+5*N,T);
xci2=zeros(1+5*N,T);
for it=1:T
out = zeros(1+5*N,Nsim)
for i in 1:Nsim
out[:,i]=f_time(it, i, T, N, L, weights1, priorprec0, X, y, SI, PI, a, RI,HH,corr)
end
#xmean[:,it] = mean(out,dims=2)
#xsd[:,it] = std(out,dims=2)
xmean[:,it] = [quantile(out[i,:],0.5) for i=1:size(out)[1]]
xci1[:,it] = [quantile(out[i,:],0.025) for i=1:size(out)[1]]
xci2[:,it] = [quantile(out[i,:],0.975) for i=1:size(out)[1]]
end
return(xmean,xci1,xci2)
end
##### GET dynamic tables
function DynNet_table(data,it,cut1::Int,cut2::Int,L::Int,H::Int,Nsim::Int,corr)
shrinkage = 0.05
T, N = size(data)
K = N * L + 1
X = zeros(Float64, T - L, K-1)
for i in 1:L
temp = lag0(data, i)
X[:, (1 + N*(i-1) : i*N)] = temp[(1+L:T), :]
end
y = data[(1+L):T, :]
T = T - L
X = [ones(Float64, T, 1) X]
K = N * L + 1
SI, PI, a, RI = MinnNWprior(data, T, N, L, shrinkage)
weights1 = convert.(Float64, normker(T, H))
priorprec0 = convert.(Float64, inv(PI));
xmean=zeros(7 + (2*4*2+4)*N,T);
xci1=zeros(7 + (2*4*2+4)*N,T);
xci2=zeros(7 + (2*4*2+4)*N,T);
out = [f_all_table(it, i, cut1,cut2,T, N, L, weights1, priorprec0, X, y, SI, PI, a, RI,corr) for i in 1:Nsim]
table_all=[mean(filter(!isnan,[out[i][1][k,j] for i=1:Nsim])) for k=1:N, j=1:N]
table_long=[mean(filter(!isnan,[out[i][2][k,j] for i=1:Nsim])) for k=1:N, j=1:N]
table_medium=[mean(filter(!isnan,[out[i][3][k,j] for i=1:Nsim])) for k=1:N, j=1:N]
table_short=[mean(filter(!isnan,[out[i][4][k,j] for i=1:Nsim])) for k=1:N, j=1:N]
return(table_all,table_long,table_medium,table_short)
end
function f_all_table(it, ij, cut1, cut2,T::Int, N::Int, L::Int, weights1, priorprec0, X, y, SI, PI, a, RI,corr)
bayesalpha, bayessv, bayesgamma, BB = oneTimePrior(it, weights1, priorprec0, X, y, SI, PI, a, RI)
HO = 100 + 1 # IRF Horizon, Also use this for FEVD horizon.
ND = 1
HORZ = HO - 1
B0, A0 = getCoefsStable(bayesalpha, bayesgamma, bayessv, BB, N, L)
wold = get_GIRF_fast(B0, A0, ND, N, L, HORZ)
thetainf,temp1, temp2, temp3 = get_dynnet_table(wold, T, A0,corr,cut1,cut2);
return (thetainf,temp1, temp2, temp3)
end
function get_dynnet_table(wo, TT, sig,corr,cut1::Int,cut2::Int)
Tw = 200 # Define frequency window;
omeg = LinRange(0.0, pi, Tw) # create equally spaced line from 0 to pi in 261 intervals
# Define bands
omeg2 = pi ./ omeg;
d1 = omeg2 .> cut2 # long term equals (20,260+] days
d2 = (omeg2 .<= cut2) .* (omeg2 .> cut1) # medium term equals (5,20] days
d3 = omeg2 .<= cut1 # short term equals [1,5] days
N = size(wo, 1)
HO = size(wo, 3)
if corr == true
diag_sig = Diagonal(sig);
end
if corr == false
diag_sig = sig;
end
expnnom = exp.(-im .* repeat(omeg, 1, HO) .* repeat((1:HO)', Tw, 1));
expnnom = convert.(Complex{Float32}, expnnom)
Omeg2 = Array{Float64}(undef, N, N)
@views @inbounds for hh in 1:HO
Omeg2 += wo[:,:,hh] * (diag_sig * wo[:,:,hh]')
end
Omeg2 = diag(Omeg2)
wo = convert.(Float32, wo);
FC = Array{Float64}(undef, N, N, Tw);
GI = Array{Complex{Float32}}(undef, N, N);
@views @inbounds for w in 1:Tw
fill!(GI, 0.0);
for nn in 1:HO
GI .+= wo[:, :, nn] .* expnnom[w, nn];
end
PS = abs2.(GI * diag_sig);
@inbounds for k in 1:N
@inbounds for j in 1:N
FC[j, k, w] = PS[j, k] ./ (Omeg2[j] .* sig[k, k]);
end
end
end
PP1 = dropdims(sum(FC, dims = 3), dims=3);
@views for w in 1:Tw
for j in 1:N
FC[j, :, w] = FC[j, :, w] ./ sum(PP1[j, :]);
end
end
thetainf = dropdims(sum(FC, dims = 3), dims=3);
### BANDS : d1 d2 d3, Long, Medium, Short
# theta_{d_i} summed over bands
temp1 = dropdims(sum(FC[:, :, d1], dims = 3), dims=3);
temp2 = dropdims(sum(FC[:, :, d2], dims = 3), dims=3);
temp3 = dropdims(sum(FC[:, :, d3], dims = 3), dims=3);
for j in 1:N
sumthetaj = sum(thetainf[j, :]);
temp1[j, :] = temp1[j, :] ./ sumthetaj;
temp2[j, :] = temp2[j, :] ./ sumthetaj;
temp3[j, :] = temp3[j, :] ./ sumthetaj;
end
return(thetainf,temp1, temp2, temp3)
end