-
Notifications
You must be signed in to change notification settings - Fork 1
/
Assignment4.py
115 lines (77 loc) · 1.91 KB
/
Assignment4.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
#####################
# Linear Time Iteration
#####################
import numpy as np
import matplotlib.pyplot as plt
from PB_Assignment2 import sol, params
from Assignment3 import plots
np.random.seed(123)
A = np.array([[1,0,1/params['sigma']],
[-params['kappa'],1, 0],
[0,0,1]])
M = np.array([[1, 1/params['sigma'], 0],
[0, params['beta'],0],
[params['phi_2'],params['phi_1'],0]])
D = np.zeros((3,3))
print(A)
print(M)
print(D)
# 1
F_init = np.random.randn(3,3)
def F_update(F, A = A, M = M, D = D):
return np.linalg.inv(A - M @ F) @ D
Fnew = F_update(F_init)
print(Fnew)
# 2
F = F_update(Fnew)
print(F)
# 3
def get_Q(A,M,F):
return np.linalg.inv(A - M @ F)
Q = get_Q(A, M, F)
print(Q)
# 4
rho_e = 0.8
Anew = np.array([[1,0,0,0],
[-1,1,0,1/params['sigma']],
[0,-params['kappa'],1, 0],
[0,0,0,1]])
Mnew = np.array([[0,0,0,0],
[0,1, 1/params['sigma'], 0],
[0,0, params['beta'],0],
[0,params['phi_2'],params['phi_1'],0]])
Dnew = np.zeros((4,4))
Dnew[0,0] = rho_e
#def F_update(F, A = Anew, M = Mnew, D = Dnew):
# return np.linalg.inv(A - M @ F) @ D
F_init = np.random.randn(4,4)
Fnew = F_update(F_init, Anew, Mnew, Dnew)
# 5
F = F_init
while np.max(abs(F - Fnew)) > 1e-6:
F = Fnew
Fnew = F_update(F, Anew, Mnew, Dnew)
F_final = Fnew
print(F_final)
#6
Q = get_Q(Anew, Mnew, F_final)
print(Q)
# 8
C1 = F_final[:,0]
C5 = Q[:,0]
rho_e = 0.1
N = 20
eps = 0.01
z = [Q * eps]
e = [eps]
for i in range(N):
et = rho_e * e[-1]
zt = F @ np.array([[et],[0],[0],[0]], dtype=float)
e.append(et)
z.append(zt)
error = [x[0][0] for x in z]
output = [x[1][0] for x in z]
inflation = [x[2][0] for x in z]
interest = [x[3][0] for x in z]
# 9
plots(paths = [error,output, inflation, interest], path_names=['error', 'output', 'inflation', 'interest rate'])