-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrec.R
More file actions
3924 lines (2576 loc) · 85.7 KB
/
librec.R
File metadata and controls
3924 lines (2576 loc) · 85.7 KB
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# librec.R - A small library of recursive methods in R, v01.22 (2026-04-06)
#
# Adaptation to R with extensions of the Thomas Sargent's MatLab toolkits available at:
#
# http://www.tomsargent.com/source_code/mitbook.zip
# http://www.tomsargent.com/source_code/hansarprograms.zip
#
# Pedro Afonso Fernandes, UCP, CLSBE, Lisbon, Portugal (paf@ucp.pt)
#
# This library is free software; you can redistribute it and/or modify it under the terms of
# the GNU General Public License as published by the Free Software Foundation. The library is
# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details: https://www.gnu.org/licenses/.
#
# Load other (auxiliary) libraries
library(MASS)
### THOMAS SARGENT'S FUNCTIONS ADAPTED TO R ###
# compn(a)
#
# Creates companion matrix B for the vector a such that
#
# | a(1) a(2) ... a(n-1) a(n) |
# | 1 0 0 0 |
# B = | 0 1 0 0 |
# | . |
# | . |
# | . |
# | 0 0 1 0 |
#
# The matrix B is returned
compn <- function(a){
n <- length(a)
m <- n-1
B <- rbind(a, cbind(diag(m), rep(0,m)))
return(B)
}
# cmean(A,G,x0,N)
#
# In the framework of a discrete-time linear state-space (lss) system, computes the conditional mean:
#
# E[y(t) | x(0)] = G * A^t * x0 for t = 1,2,...,N
#
# where:
#
# A is a n*n transition matrix of the lss system
# G is a m*n output matrix of the lss system
# x0 is a n*1 vector with the initial value of the state (condition for the expected value)
# N is the time horizon (integer)
#
# NB: this function can be used to predict the expected values of the observations in a lss system
# or to simulate the control in the optimal linear regulator problem (OLRP) by making G = -F
# and A = A0 = (A-BF)
#
# NB: this function can be used to simulate the state in the optimal linear regulator problem (OLRP)
# by making G = diag(n) and A = A0 = (A-BF)
#
# It returns a m*(1+N) matrix B with first column B[,1] = G * x0
cmean <- function(A,G,x0,N){
m <- nrow(G)
B1 <- matrix(0,m,N)
for (i in 1:N){
B1[,i] <- G %*% mpower(A,i) %*% x0
}
B <- cbind(G %*% x0, B1)
return(B)
}
# dlsim(A,C,G,D,w,x0)
#
# Simulate the response of the discrete-time linear state-space (lss) system:
#
# x(t+1) = A * x(t) + C * w(t+1)
# y(t) = G * x(t) + D * w(t+1)
#
# to a N*p matrix of perturbations/shocks w (given) where:
#
# N is the number of simulations (obs)
# x(t) is a n*1 vector of the states with an initial condition x0 (given)
# A is the n*n transition matrix of the lss system
# G is the m*n output matrix of the lss system
# C is a n*p state's volatility matrix
# D is a m*p output's volatility matrix
#
# It returns the m time series y(t) for t = 0, ..., N with y(0) = t(G * x0)
dlsim <- function(A,C,G,D,w,x0){
n <- nrow(A)
m <- nrow(G)
N <- nrow(w)
y1 <- matrix(0,m,N)
x1 <- matrix(0,n,N)
x_0 <- x0
for (i in 1:N){
x_1 <- A %*% x_0 + C %*% t(w[i,])
y1[,i] <- G %*% x_1 + D %*% t(w[i,])
x1[,i] <- x_1
x_0 <- x_1
}
y <- cbind(G %*% x0, y1)
ty <- ts(data = t(y), start = 0, end = N)
}
# dimpulse(A,C,G,D,wj,N)
#
# Impulse response of the discrete-time linear state-space (lss) system:
#
# x(t+1) = A * x(t) + C * w(t+1)
# y(t) = G * x(t) + D * w(t+1)
#
# to an unit shock applied to the wj perturbation with 1 <= wj <= p where p
# is the number of columns of the matrices C and D
#
# Integer N specifies how many points of the impulse response to find
#
# It returns a time series object y(t), t = 0, ..., N with m columns
dimpulse <- function(A,C,G,D,wj,N){
n <- nrow(A)
x0 <- as.matrix(rep(0,n))
w <- as.matrix(rep(0,N))
w[1] <- 1
I <- dlsim(A,as.matrix(C[,wj]),G,as.matrix(D[,wj]),w,x0)
}
# doublej(A,B)
#
# Solves the discrete Lyapunov equation V = A * V * A' + B using the Sargent's "doubling algorithm"
# implemented with the routine doublej.m from http://www.tomsargent.com/source_code/mitbook.zip
#
# This algorithm computes V = SUM (A^j) * B * t(A^j) from j = 0 to j = infinity
doublej <- function(A,B){
A0 <- A
V0 <- B
dif <- 9999
i <- 1
maxit <- 1000
while (dif > 1e-15) {
A1 <- A0 %*% A0
V1 <- V0 + A0 %*% V0 %*% t(A0)
dif <- max(abs(V1-V0))
V0 <- V1
A0 <- A1
i <- i + 1
if (i > maxit) break
}
return(V1)
}
# kfilter(A,Q,G,R)
#
# Calculates the time-invariant gain, K, and the stationary covariance matrix, S, using the
# Kalman filter for the linear state space (lss) system:
#
# x(t+1) = A x(t) + C w(t+1)
# y(t) = G x(t) + v(t)
#
# where:
#
# x(t) is the n*1 vector of unobserved states at time t = 0,1,2,...
# A is the n*n transition matrix of the lss system
# w(t+1) is an iid sequence of p*1 Gaussian random numbers ~ N(O,I); typically, p=1
# C is the n*p state's volatility matrix
#
# y(t) is the m*1 vector of observations at time t; typically, m=1
# G is the m*n output matrix
# v(t+1) is an iid sequence of m*1 Gaussian random numbers ~ N(O,R)
# R is the m*m observation's volatility matrix; typically, a single number
#
# NB: the argument C should be passed as Q = C %*% t(C), a matrix n*n; i.e. C*w[t+1] ~ N(0,Q=CC')
#
# Based on the Sargent's routine kfilter.m from http://www.tomsargent.com/source_code/mitbook.zip
# that iterates on the Riccati diference equation:
#
# S1 = Q + A*S0*A' - A*S0*G' / (G*S0*G'+ R) * G*S0*A' = Q + A*S0*A' - K0 * G*S0*A'
#
# starting from a initial covariance matrix S0 = 0.01 * I(n) and noting that the Kalman gain is
#
# K0 = A * S0 * G' / (G * S0 * G'+ R)
#
# It returns a bundle with the time-invariant gain K, the stationary covariance matrix S and
# the number of steps i until convergence
kfilter <- function(A,Q,G,R){
n <- nrow(A)
S0 <- 0.01 * diag(n)
dif <- 9999
i <- 1
maxit <- 1000
while (dif > 1e-8) {
K0 <- A %*% S0 %*% t(G) %*% solve(G %*% S0 %*% t(G) + R) # Kalman gain
S1 <- Q + A %*% S0 %*% t(A) - K0 %*% G %*% S0 %*% t(A) # Riccati dif equation
K1 <- A %*% S1 %*% t(G) %*% solve(G %*% S1 %*% t(G) + R) # Kalman gain (next iteration)
dif <- max(abs(K1-K0))
S0 <- S1
i <- i + 1
if (i > maxit) break
}
aux <- list("K" = K1, "S" = S1, "i" = i)
return(aux)
}
# ikalman(A,Q,G,R)
#
# Improved version of the Kalman filter that uses a Howard policy improvement algorithm as suggested by
# Ljungqvist and Sargent (2018, p. 103), Exercice 2.29, part e)
#
# Instead of iterating on the Riccati diference equation, this algorithm starts from a guess K0 to the
# Kalman gain (the "policy function" in this context) and then solves the following Lyapunov equation for S:
#
# S = (A - K0 * G) * S * (A - K0 * G)' + (Q + K0 * R * K0')
#
# Given this time-invariant covariance matrix S, it recomputes the Kalman gain:
#
# K1 = A * S * G' / (G * S * G'+ R)
#
# and continues by solving again the Lyapunov equation given K1 until convergence between K1 and K0
#
# It returns a bundle with the time-invariant gain K, the stationary covariance matrix S and
# the number of steps i until convergence
ikalman <- function(A,Q,G,R){
n <- nrow(A)
m <- nrow(G)
K0 <- matrix(0.01,n,m)
dif <- 9999
i <- 1
maxit <- 1000
while (dif > 1e-8) {
S <- doublej(A - K0 %*% G, Q + K0 %*% R %*% t(K0)) # Lyapunov equation
K1 <- A %*% S %*% t(G) %*% solve(G %*% S %*% t(G) + R) # Kalman gain (next iteration)
dif <- max(abs(K1-K0))
K0 <- K1
i <- i + 1
if (i > maxit) break
}
aux <- list("K" = K1, "S" = S, "i" = i)
return(aux)
}
# krec(A,Q,G,R,y,x0)
#
# Computes the Kalman recursions:
#
# xhat(t+1) = A * xhat(t) + K * a(t) with the inovation a(t) = y(t) - G * xhat(t)
#
# where:
#
# A is the n*n transition matrix of the linear state space (lss) system
# Q = CC' is the n*n state's volatility matrix
# G is the m*n output matrix
# R is the m*m observation's volatility matrix
# K is the Kalman gain computed with the aux function kfilter(A,Q,G,R)
# y is a N*m matrix of observations
# x0 is a n*1 vector with the initial value of the state
#
# It returns a time series with the original N*m series filtered, i.e., yhat(t) = G * xhat(t)
krec <- function(A,Q,G,R,y,x0){
RS <- kfilter(A,Q,G,R)
n <- nrow(A)
N <- nrow(y)
B1 <- matrix(0,n,N)
ty <- t(y)
xhat_0 <- x0
for (i in 1:N){
a_0 <- ty[,i] - G %*% xhat_0
xhat_1 <- A %*% xhat_0 + RS$K %*% a_0
B1[,i] <- xhat_1
xhat_0 <- xhat_1
}
B <- G %*% B1
T <- ts(data = t(B), start = 1, end = N)
return(T)
}
# markov(T,n,s0,v)
#
# Generates a Markov chain where:
#
# T is a transition matrix with size m*m
# n is the number of periods to simulate + initial period (n > 1)
# s0 is the initial state (integer number from 1 to m)
# v is a row vector with the quantity (or value) corresponding to each state i = 1,...,m
#
# It returns a list with:
#
# Chain of values with size 1*n
# Matrix of states with size m*n
markov <- function(T,n,s0,v){
m <- nrow(T)
X <- runif(n-1)
s <- matrix(0,m,1)
s[s0] <- 1
S <- matrix(0,m,n)
S[,1] <- s
A <- matrix(1,m,m)
A[lower.tri(A)] <- 0
cum <- T %*% A # Cumulative probabilities of transition from state/row i to state/column j
for (k in 1:(n-1)){
aux <- t(s) %*% cum
ppi <- cbind(0, aux)
s <- as.numeric(X[k] <= ppi[2:(m+1)]) * as.numeric(X[k] > ppi[1:m])
S[,k+1] <- s
}
c <- v %*% S
list("chain" = c, "states" = S)
}
# sims(A,G,C,x0,N)
#
# In the framework of a discrete-time linear state-space (lss) system, simulates a sequence {y(t)} of observations such that:
#
# y(t) = G * x(t) with x(t) = A * x(t-1) + C * w(t) for t = 1,2,...,N
#
# where:
#
# x(t) is a n*1 vector of unobserved states with an initial condition x(0) = x0
# A is the n*n transition matrix of the lss system
# G is the m*n output matrix of the lss system
# C is a n*1 state's volatility matrix
# w(t+1) is an iid sequence of Gaussian random numbers ~ N(O,I), generated by the function
# N is the time horizon (integer)
#
# It returns a m*(1+N) matrix B with B[,1] = G*x0
sims <- function(A,G,C,x0,N){
m <- nrow(G)
B1 <- matrix(0,m,N)
w <- rnorm(N, mean=0, sd=1)
x_0 <- x0
for (i in 1:N){
x_1 <- A %*% x_0 + C * w[i]
B1[,i] <- G %*% x_1
x_0 <- x_1
}
B <- cbind(G %*% x0, B1)
return(B)
}
# olrp(beta,A,B,R,Q,H)
#
# Solves the discounted Optimal Linear Regulator Problem (OLRP):
#
# Maximize { sum [beta^t (x'Rx + u'Qu + 2u'Hx)] }
#
# subject to
#
# x(t+1) = A x(t) + B u(t)
#
# where:
#
# x(t) is a n*1 vector of states at time t = 0,1,2,... with x(0) given
# u(t) is a k*1 vector of controls
# A is the n*n transition matrix associated with the states
# B is the n*k transition matrix associated with the controls
# R is a n*n positive semidefinite symmetric matrix
# Q is a k*k positive definite symmetric matrix
# H is a k*n cross-product matrix
# beta is the discount factor (0.96 by default)
#
# by iterating on the following Riccati diference equation:
#
# P1 = R + beta * A'*P0*A - (beta * A'*P0*B + H') / (Q + beta * B'*P0*B) * (beta * B'*P0*A + H)
#
# starting from an initial matrix P0 = 0
#
# The optimal value function will be x'Px associated with the optimal policy function u = -Fx where
#
# F = (Q + beta * B' * P * B) \ (beta * B' * P * A + H)
#
# It returns a bundle with the optimal policy matrix F, the steady-state solution P of the Riccati dif
# equation, the optimal closed loop transition matrix A0 = A - BF and the number of steps i until convergence
olrp <- function(beta = 0.96,A,B,R,Q,H){
n <- ncol(A)
P0 <- matrix(0.01,n,n)
F0 <- solve(Q + beta * t(B) %*% P0 %*% B) %*% (beta * t(B) %*% P0 %*% A + H)
dif <- 9999
i <- 1
maxit <- 1000
while (dif > 1e-6) {
# Ricatti dif equation:
P1 <- R + beta * t(A) %*% P0 %*% A - (beta * t(A) %*% P0 %*% B + t(H)) %*% F0
F1 <- solve(Q + beta * t(B) %*% P1 %*% B) %*% (beta * t(B) %*% P1 %*% A + H)
dif <- max(abs(F1-F0))
P0 <- P1
F0 <- F1
i <- i + 1
if (i > maxit) break
}
A0 <- A - B %*% F1
list("F" = F1, "P" = P1, "A0" = A0, "i" = i)
}
# policyi(beta,A,B,R,Q,H)
#
# Howard policy improvement algorithm that computes the matrix F from the feedback rule u = -Fx for the
# Optimal Linear Regulator Problem (OLRP):
#
# Maximize { sum [beta^t (x'Rx + u'Qu + 2u'Hx)] }
#
# subject to
#
# x(t+1) = A x(t) + B u(t)
#
# where:
#
# x(t) is a n*1 vector of states at time t = 0,1,2,... with x(0) given
# u(t) is a k*1 vector of controls
# A is the n*n transition matrix associated with the states
# B is the n*k transition matrix associated with the controls
# R is a n*n positive semidefinite symmetric matrix
# Q is a k*k positive definite symmetric matrix
# H is a k*n cross-product matrix
# beta is the discount factor (0.96 by default)
#
# Instead of iterating on the Riccati diference equation, this algorithm starts from a guess F0 to the
# policy function and then solves the following Lyapunov equation for P:
#
# P = (R + F0' * Q * F0 - 2 * F0' * H) + beta * (A - B * F0)' * P * (A - B * F0)
#
# Given this steady-state solution P, it recomputes the policy function:
#
# F1 = (Q + beta * B' * P * B) \ (beta * B' * P * A + H)
#
# and continues by solving again the Lyapunov equation given F1 until convergence between F1 and F0
#
# The optimal value function will be x'Px associated with the optimal policy function u = -Fx
#
# It returns a bundle with the optimal policy matrix F, the steady-state solution P of the Riccati dif
# equation, the optimal closed loop transition matrix A0 = A - BF and the number of steps i until convergence
policyi <- function(beta = 0.06,A,B,R,Q,H){
k <- ncol(B)
n <- ncol(A)
s <- rnorm(k*n,0,0.01)
F0 <- matrix(s,k,n)
dif <- 9999
i <- 1
maxit <- 1000
while (dif > 1e-6) {
U <- t(A - B %*% F0) * sqrt(beta)
V <- R + t(F0) %*% Q %*% F0 - 2 * t(F0) %*% H
P <- doublej(U, V) # Lyapunov equation
F1 <- solve(Q + beta * t(B) %*% P %*% B) %*% (beta * t(B) %*% P %*% A + H) # Improved policy (next iteration)
dif <- max(abs(F1-F0))
F0 <- F1
i <- i + 1
if (i > maxit) break
}
A0 <- A - B %*% F1
list("F" = F1, "P" = P, "A0" = A0, "steps" = i)
}
# nash(beta,A,B1,B2,R1,R2,Q1,Q2,S1,S2,W1,W2,M1,M2)
#
# Computes the limit of a Nash-Markov linear quadratic two-player game by iterating on a pair of Ricatti equations.
# This is a complex version of the algorithm, with cross terms between states and controls
#
# Each player i:
#
# Maximize { sum [beta^t (x' Ri x + ui' Qi ui + uj' Si uj + 2 x' Wi ui + 2 uj' Mi ui)] }
#
# subject to
#
# x(t+1) = A x(t) + Bi ui(t) + Bj uj(t)
#
# and a perceived control uj(t) = - Fj x(t) for the other played j.
#
# where:
#
# x(t) is a n*1 vector of states at time t = 0,1,2,... with x(0) given
# ui(t) is a ki*1 vector of controls
# A is the n*n transition matrix associated with the states
# Bi is the n*ki transition matrix associated with the controls of player i
# Ri is a n*n positive semidefinite symmetric matrix
# Qi is a ki*ki positive definite symmetric matrix
# Si is a kj*kj positive definite symmetric matrix
# Wi is n x ki
# Mi is kj x ki
# beta is the discount factor (0.96 by default)
#
nash <- function(beta=0.96,A,B1,B2,R1,R2,Q1,Q2,S1,S2,W1,W2,M1,M2){
n <- ncol(A) # Number of states
k1 <- ncol(Q1) # Number of controls of player 1
k2 <- ncol(Q2) # Number of controls of player 2
P1 <- matrix(0,n,n)
P2 <- matrix(0,n,n)
v1 <- rnorm(k1*n,0,0.01)
v2 <- rnorm(k2*n,0,0.01)
F1 <- matrix(v1,k1,n)
F2 <- matrix(v2,k2,n)
H1 <- t(W1) - t(M1) %*% F2
H2 <- t(W2) - t(M2) %*% F1
dif <- 9999
i <- 1
maxit <- 1000
while (dif > 1e-6) {
F10 <- F1
F20 <- F2
F1 <- solve(Q1 + beta * t(B1) %*% P1 %*% B1) %*% (beta * t(B1) %*% P1 %*% (A - B2 %*% F20) + H1)
F2 <- solve(Q2 + beta * t(B2) %*% P2 %*% B2) %*% (beta * t(B2) %*% P2 %*% (A - B1 %*% F10) + H2)
A1 <- A - B2 %*% F2
A2 <- A - B1 %*% F1
H1 <- t(W1) - t(M1) %*% F2
H2 <- t(W2) - t(M2) %*% F1
# Ricatti dif equation:
P1 <- (R1 + t(F2) %*% S1 %*% F2) + beta * t(A1) %*% P1 %*% A1 - (beta * t(A1) %*% P1 %*% B1 + t(H1)) %*% F1
P2 <- (R2 + t(F1) %*% S2 %*% F1) + beta * t(A2) %*% P2 %*% A2 - (beta * t(A2) %*% P2 %*% B2 + t(H2)) %*% F2
dif <- max(abs(F10-F1))+max(abs(F20-F2))
i <- i + 1
if (i > maxit) break
}
A0 <- A - B1 %*% F1 - B2 %*% F2
list("F1" = F1, "F2" = F2, "P1" = P1, "P2" = P2, "A0" = A0, "i" = i)
}
# nash2(beta,A,B1,B2,R1,R2,Q1,Q2,S1,S2)
#
# Computes the limit of a Nash-Markov linear quadratic two-player game by iterating on a pair of Ricatti equations.
# This is a simple version of the algorithm, without cross terms between states and controls
#
# Each player i:
#
# Maximize { sum [beta^t (x' Ri x + ui' Qi ui + uj' Si uj)] }
#
# subject to
#
# x(t+1) = A x(t) + Bi ui(t) + Bj uj(t)
#
# and a perceived control uj(t) = - Fj x(t) for the other played j.
#
# where:
#
# x(t) is a n*1 vector of states at time t = 0,1,2,... with x(0) given
# ui(t) is a ki*1 vector of controls
# A is the n*n transition matrix associated with the states
# Bi is the n*ki transition matrix associated with the controls of player i
# Ri is a n*n positive semidefinite symmetric matrix
# Qi is a ki*ki positive definite symmetric matrix
# Si is a kj*kj positive definite symmetric matrix
# beta is the discount factor (0.96 by default)
#
nash2 <- function(beta=0.96,A,B1,B2,R1,R2,Q1,Q2,S1,S2){
n <- ncol(A) # Number of states
k1 <- ncol(Q1) # Number of controls of player 1
k2 <- ncol(Q2) # Number of controls of player 2
P1 <- matrix(0,n,n)
P2 <- matrix(0,n,n)
v1 <- rnorm(k1*n,0,0.01)
v2 <- rnorm(k2*n,0,0.01)
F1 <- matrix(v1,k1,n)
F2 <- matrix(v2,k2,n)
dif <- 9999
i <- 1
maxit <- 1000
while (dif > 1e-6) {
F10 <- F1
F20 <- F2
F1 <- solve(Q1 + beta * t(B1) %*% P1 %*% B1) %*% (beta * t(B1) %*% P1 %*% (A - B2 %*% F20))
F2 <- solve(Q2 + beta * t(B2) %*% P2 %*% B2) %*% (beta * t(B2) %*% P2 %*% (A - B1 %*% F10))
A1 <- A - B2 %*% F2
A2 <- A - B1 %*% F1
# Ricatti dif equation:
P1 <- (R1 + t(F2) %*% S1 %*% F2) + beta * t(A1) %*% P1 %*% A1 - (beta * t(A1) %*% P1 %*% B1) %*% F1
P2 <- (R2 + t(F1) %*% S2 %*% F1) + beta * t(A2) %*% P2 %*% A2 - (beta * t(A2) %*% P2 %*% B2) %*% F2
dif <- max(abs(F10-F1))+max(abs(F20-F2))
i <- i + 1
if (i > maxit) break
}
A0 <- A - B1 %*% F1 - B2 %*% F2
list("F1" = F1, "F2" = F2, "P1" = P1, "P2" = P2, "A0" = A0, "i" = i)
}
# nash2s(beta,A,B1,B2,R1,R2,Q1,Q2)
#
# Slim version of the nash2() function without matrices Si, that is, each player i = 1,2 simply:
#
# Maximize { sum [beta^t (x' Ri x + ui' Qi ui)] }
#
# subject to
#
# x(t+1) = A x(t) + Bi ui(t) + Bj uj(t)
#
nash2s <- function(beta=0.96,A,B1,B2,R1,R2,Q1,Q2){
n <- ncol(A) # Number of states
k1 <- ncol(Q1) # Number of controls of player 1
k2 <- ncol(Q2) # Number of controls of player 2
P1 <- matrix(0,n,n)
P2 <- matrix(0,n,n)
v1 <- rnorm(k1*n,0,0.01)
v2 <- rnorm(k2*n,0,0.01)
F1 <- matrix(v1,k1,n)
F2 <- matrix(v2,k2,n)
dif <- 9999
i <- 1
maxit <- 1000
while (dif > 1e-6) {
F10 <- F1
F20 <- F2
F1 <- solve(Q1 + beta * t(B1) %*% P1 %*% B1) %*% (beta * t(B1) %*% P1 %*% (A - B2 %*% F20))
F2 <- solve(Q2 + beta * t(B2) %*% P2 %*% B2) %*% (beta * t(B2) %*% P2 %*% (A - B1 %*% F10))
A1 <- A - B2 %*% F2
A2 <- A - B1 %*% F1
# Ricatti dif equation:
P1 <- R1 + beta * t(A1) %*% P1 %*% A1 - (beta * t(A1) %*% P1 %*% B1) %*% F1
P2 <- R2 + beta * t(A2) %*% P2 %*% A2 - (beta * t(A2) %*% P2 %*% B2) %*% F2
dif <- max(abs(F10-F1))+max(abs(F20-F2))
i <- i + 1
if (i > maxit) break
}
A0 <- A - B1 %*% F1 - B2 %*% F2
list("F1" = F1, "F2" = F2, "P1" = P1, "P2" = P2, "A0" = A0, "i" = i)
}
# nash8s(beta,A,B1,B2,B3,B4,B5,B6,B7,B8,R1,R2,R3,R4,R5,R6,R7,R8,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8)
#
# Version of the nash2s() algorithm for 8 players where each player i
#
# Maximize { sum [beta^t (x' Ri x + ui' Qi ui)] }
#
# subject to
#
# x(t+1) = A x(t) + B1 u1(t) + ... + Bi ui(t) + ... + B8 u8(t)
#
nash8s <- function(beta=0.96,A,B1,B2,B3,B4,B5,B6,B7,B8,R1,R2,R3,R4,R5,R6,R7,R8,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8){
n <- ncol(A) # Number of states
k1 <- ncol(Q1) # Number of controls of player 1
k2 <- ncol(Q2) # Number of controls of player 2
k3 <- ncol(Q3) # Number of controls of player 3
k4 <- ncol(Q4) # Number of controls of player 4
k5 <- ncol(Q5) # Number of controls of player 5
k6 <- ncol(Q6) # Number of controls of player 6
k7 <- ncol(Q7) # Number of controls of player 7
k8 <- ncol(Q8) # Number of controls of player 8
P1 <- matrix(0,n,n)
P2 <- matrix(0,n,n)
P3 <- matrix(0,n,n)
P4 <- matrix(0,n,n)
P5 <- matrix(0,n,n)
P6 <- matrix(0,n,n)
P7 <- matrix(0,n,n)
P8 <- matrix(0,n,n)
v1 <- rnorm(k1*n,0,0.01)
v2 <- rnorm(k2*n,0,0.01)
v3 <- rnorm(k3*n,0,0.01)
v4 <- rnorm(k4*n,0,0.01)
v5 <- rnorm(k5*n,0,0.01)
v6 <- rnorm(k6*n,0,0.01)
v7 <- rnorm(k7*n,0,0.01)
v8 <- rnorm(k8*n,0,0.01)
F1 <- matrix(v1,k1,n)
F2 <- matrix(v2,k2,n)
F3 <- matrix(v3,k3,n)
F4 <- matrix(v4,k4,n)
F5 <- matrix(v5,k5,n)
F6 <- matrix(v6,k6,n)
F7 <- matrix(v7,k7,n)
F8 <- matrix(v8,k8,n)
dif <- 9999
i <- 1
maxit <- 1000
while (dif > 1e-6) {
F10 <- F1
F20 <- F2
F30 <- F3
F40 <- F4
F50 <- F5
F60 <- F6
F70 <- F7
F80 <- F8
F1 <- solve(Q1 + beta * t(B1) %*% P1 %*% B1) %*% (beta * t(B1) %*% P1 %*% (A - B2 %*% F20 - B3 %*% F30 - B4 %*% F40 - B5 %*% F50 - B6 %*% F60 - B7 %*% F70 - B8 %*% F80))
F2 <- solve(Q2 + beta * t(B2) %*% P2 %*% B2) %*% (beta * t(B2) %*% P2 %*% (A - B1 %*% F10 - B3 %*% F30 - B4 %*% F40 - B5 %*% F50 - B6 %*% F60 - B7 %*% F70 - B8 %*% F80))
F3 <- solve(Q3 + beta * t(B3) %*% P3 %*% B3) %*% (beta * t(B3) %*% P3 %*% (A - B1 %*% F10 - B2 %*% F20 - B4 %*% F40 - B5 %*% F50 - B6 %*% F60 - B7 %*% F70 - B8 %*% F80))
F4 <- solve(Q4 + beta * t(B4) %*% P4 %*% B4) %*% (beta * t(B4) %*% P4 %*% (A - B1 %*% F10 - B2 %*% F20 - B3 %*% F30 - B5 %*% F50 - B6 %*% F60 - B7 %*% F70 - B8 %*% F80))
F5 <- solve(Q5 + beta * t(B5) %*% P5 %*% B5) %*% (beta * t(B5) %*% P5 %*% (A - B1 %*% F10 - B2 %*% F20 - B3 %*% F30 - B4 %*% F40 - B6 %*% F60 - B7 %*% F70 - B8 %*% F80))
F6 <- solve(Q6 + beta * t(B6) %*% P6 %*% B6) %*% (beta * t(B6) %*% P6 %*% (A - B1 %*% F10 - B2 %*% F20 - B3 %*% F30 - B4 %*% F40 - B5 %*% F50 - B7 %*% F70 - B8 %*% F80))
F7 <- solve(Q7 + beta * t(B7) %*% P7 %*% B7) %*% (beta * t(B7) %*% P7 %*% (A - B1 %*% F10 - B2 %*% F20 - B3 %*% F30 - B4 %*% F40 - B5 %*% F50 - B6 %*% F60 - B8 %*% F80))
F8 <- solve(Q8 + beta * t(B8) %*% P8 %*% B8) %*% (beta * t(B8) %*% P8 %*% (A - B1 %*% F10 - B2 %*% F20 - B3 %*% F30 - B4 %*% F40 - B5 %*% F50 - B6 %*% F60 - B7 %*% F70 ))
A1 <- A - B2 %*% F2 - B3 %*% F3 - B4 %*% F4 - B5 %*% F5 - B6 %*% F6 - B7 %*% F7 - B8 %*% F8
A2 <- A - B1 %*% F1 - B3 %*% F3 - B4 %*% F4 - B5 %*% F5 - B6 %*% F6 - B7 %*% F7 - B8 %*% F8
A3 <- A - B1 %*% F1 - B2 %*% F2 - B4 %*% F4 - B5 %*% F5 - B6 %*% F6 - B7 %*% F7 - B8 %*% F8
A4 <- A - B1 %*% F1 - B2 %*% F2 - B3 %*% F3 - B5 %*% F5 - B6 %*% F6 - B7 %*% F7 - B8 %*% F8
A5 <- A - B1 %*% F1 - B2 %*% F2 - B3 %*% F3 - B4 %*% F4 - B6 %*% F6 - B7 %*% F7 - B8 %*% F8
A6 <- A - B1 %*% F1 - B2 %*% F2 - B3 %*% F3 - B4 %*% F4 - B5 %*% F5 - B7 %*% F7 - B8 %*% F8
A7 <- A - B1 %*% F1 - B2 %*% F2 - B3 %*% F3 - B4 %*% F4 - B5 %*% F5 - B6 %*% F6 - B8 %*% F8
A8 <- A - B1 %*% F1 - B2 %*% F2 - B3 %*% F3 - B4 %*% F4 - B5 %*% F5 - B6 %*% F6 - B7 %*% F7
# Ricatti dif equation:
P1 <- R1 + beta * t(A1) %*% P1 %*% A1 - (beta * t(A1) %*% P1 %*% B1) %*% F1
P2 <- R2 + beta * t(A2) %*% P2 %*% A2 - (beta * t(A2) %*% P2 %*% B2) %*% F2
P3 <- R3 + beta * t(A3) %*% P3 %*% A3 - (beta * t(A3) %*% P3 %*% B3) %*% F3
P4 <- R4 + beta * t(A4) %*% P4 %*% A4 - (beta * t(A4) %*% P4 %*% B4) %*% F4
P5 <- R5 + beta * t(A5) %*% P5 %*% A5 - (beta * t(A5) %*% P5 %*% B5) %*% F5
P6 <- R6 + beta * t(A6) %*% P6 %*% A6 - (beta * t(A6) %*% P6 %*% B6) %*% F6
P7 <- R7 + beta * t(A7) %*% P7 %*% A7 - (beta * t(A7) %*% P7 %*% B7) %*% F7
P8 <- R8 + beta * t(A8) %*% P8 %*% A8 - (beta * t(A8) %*% P8 %*% B8) %*% F8
dif <- max(abs(F10-F1)) + max(abs(F20-F2)) + max(abs(F30-F3)) + max(abs(F40-F4)) + max(abs(F50-F5)) + max(abs(F60-F6)) + max(abs(F70-F7)) + max(abs(F80-F8))
i <- i + 1
if (i > maxit) break
}
A0 <- A - B1 %*% F1 - B2 %*% F2 - B3 %*% F3 - B4 %*% F4 - B5 %*% F5 - B6 %*% F6 - B7 %*% F7 - B8 %*% F8
list("F1" = F1, "F2" = F2, "F3" = F3, "F4" = F4, "F5" = F5, "F6" = F6, "F7" = F7, "F8" = F8, "P1" = P1, "P2" = P2, "P3" = P3, "P4" = P4, "P5" = P5, "P6" = P6, "P7" = P7, "P8" = P8, "A0" = A0, "i" = i)
}
### TIME SERIES FILTERS AND FORECASTING METHODS ###
# hpfilt(y, lambda)
#
# Computes the Hodrick-Prescott (HP) trend component of series y for some lambda value, namely:
#
# lambda = 6.25 or 100 for yearly data;
# lambda = 1600 for quarterly data (default value);
# lambda = 14400 for monthly data.
#
# ... following the computational strategy of Kim et al. (2009).
#
# It returns a time series object with the original data y filtered.
hpfilt <- function(y, lambda = 1600){
n <- length(y)
V <- rep(0, n)
V[1] <- 1
V[2] <- -2
V[3] <- 1
T <- toeplitz(V)
T[lower.tri(T)] <- 0
m <- n-2
D <- T[1:m,]
D2 <- crossprod(D)
A <- solve( diag(n) + lambda * D2 )
x <- A %*% y
out <- ts(data = x, start = 1, end = n)
}
# bhpfilt(y, lambda, m)
#
# Computes the Boosted Hodrick-Prescott (HP) trend component of series y for some lambda value and
# m iterations, following the computational strategy of Phillips and Shi (2021).
#
# It returns a time series object with the original data y filtered.