-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
187 lines (145 loc) · 3.97 KB
/
utilities.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
"""
Functions to facilitate the computation in expansion solvers.
"""
import numpy as np
from scipy import linalg
from numba import njit
@njit
def mat(h, shape):
r"""
For a vector (column or row) vec of length mn, mat(h, (m, n))
produces an (m, n) matrix created by ‘columnizing’ the vector:
.. math::
H_{ij} = h_{(j-1)m+i}
Parameters
----------
h : (mn, 1) ndarray
shape : tuple of ints
Shape of H.
Returns
-------
H : (m, n) ndarray
References
---------
Borovicka, Hansen (2014). See http://larspeterhansen.org/wp-content/uploads/2016/10/Examining-Macroeconomic-Models-through-the-Lens-of-Asset-Pricing.pdf
"""
H = h.reshape((shape[1], shape[0])).T
return H
@njit
def vec(H):
r"""
For an (m, n) matrix H , vec(H) produces a column vector
of length mn created by stacking the columns of H:
.. math::
[vec(H)]_{(j-1)m+i} = H_{ij}
Parameters
----------
H : (m, n) ndarray
Returns
-------
h : (n*m, 1) ndarray
References
---------
Borovicka, Hansen (2014). See http://larspeterhansen.org/wp-content/uploads/2016/10/Examining-Macroeconomic-Models-through-the-Lens-of-Asset-Pricing.pdf
"""
H_T = H.T.copy()
h = H_T.reshape(-1, 1)
return h
@njit
def sym(M):
r"""
Computes :math:`\frac{1}{2} (M + M^T)`.
Parameters
----------
M : (m, m) ndarray
Returns
-------
sym_M : (m, m) ndarray
References
---------
Borovicka, Hansen (2014). See http://larspeterhansen.org/wp-content/uploads/2016/10/Examining-Macroeconomic-Models-through-the-Lens-of-Asset-Pricing.pdf
"""
sym_M = (M + M.T) / 2
return sym_M
@njit
def cal_E_ww(E_w, Cov_w):
"""
Computes expectation of :math:`W \otimes W`, where W follows multivariate normal distribution.
Parameters
----------
E_w : (m, 1) ndarray
Expectation of W.
Cov_w : (m, m) ndarray
Covariance matrix of W.
Returns
-------
E_ww: (m*m, 1) ndarray
Expectaton of :math:`W \otimes W`.
"""
m = E_w.shape[0]
E_ww = np.zeros((m**2, 1))
for i in range(m):
for j in range(m):
E_ww[i*m+j] = E_w[i, 0]*E_w[j, 0]+Cov_w[i,j]
return E_ww
@njit
def solve_matrix_equation(A, B, C, D):
r"""
Solves for:
.. math::
A\psi + B\psiC + D = 0
The solution to the equation is:
.. math::
\psi = \text{mat}\{-[I\otimes A + C^\prime\otimes B\]^{-1}\text{vec}(D)}_{n,m}
Parameters
----------
A : (n, n) ndarray
B : (n, n) ndarray
C : (m, m) ndarray
D : (n, m) ndarray
Returns
-------
res : (n, m) ndarray
References
----------
Borovicka and Hansen (2014).
https://www.borovicka.org/research.html
"""
n = B.shape[1]
m = C.shape[0]
LHS = np.kron(np.eye(m), A) + np.kron(C.T, B)
RHS = - vec(D)
vec_res = np.linalg.solve(LHS, RHS)
res = mat(vec_res, (n, m))
return res
def gschur(A, B, tol=1e-9):
"""
Performs generalized schur decomposition (QZ decomposition) with reordering.
Pushes explosive eigenvalues (i.e., > 1) to the right bottom.
Parameters
----------
A: (m, m) ndarray to decompose
B: (m, m) ndarray to decompose
tol: a tolerance level added to the threshold 1, allowing for numerical disturbance
Returns
-------
AA : (m, m) ndarray
Generalized Schur form of A.
BB : (m, m) ndarray
Generalized Schur form of B.
a : (m,) ndarray
alpha = alphar + alphai * 1j.
b : (m,) ndarray
See reference.
Q : (m, m) ndarray
The left Schur vectors.
Z : (m, m) ndarray
The right Schur vectors.
References
---------
https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.ordqz.html
"""
def sort_req(alpha, beta):
return np.abs(alpha) <= (1+tol)*np.abs(beta)
BB, AA, a, b, Q, Z = linalg.ordqz(B, A, sort=sort_req)
return AA, BB, a, b, Q, Z