/
test_robustlq.py
119 lines (87 loc) · 3.22 KB
/
test_robustlq.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
"""
Author: Chase Coleman
Filename: test_robustlq
Tests for robustlq.py file
"""
import sys
import os
import unittest
import numpy as np
from scipy.linalg import LinAlgError
from numpy.testing import assert_allclose
from quantecon.lqcontrol import LQ
from quantecon.robustlq import RBLQ
class TestRBLQControl(unittest.TestCase):
def setUp(self):
# Initial Values
a_0 = 100
a_1 = 0.5
rho = 0.9
sigma_d = 0.05
beta = 0.95
c = 2
gamma = 50.0
theta = 0.002
ac = (a_0 - c) / 2.0
R = np.array([[0, ac, 0],
[ac, -a_1, 0.5],
[0., 0.5, 0]])
R = -R
Q = gamma / 2
Q_pf = 0.
A = np.array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., rho]])
B = np.array([[0.],
[1.],
[0.]])
B_pf = np.zeros((3, 1))
C = np.array([[0.],
[0.],
[sigma_d]])
# the *_pf endings refer to an example with pure forecasting
# (see p171 in Robustness)
self.rblq_test = RBLQ(Q, R, A, B, C, beta, theta)
self.rblq_test_pf = RBLQ(Q_pf, R, A, B_pf, C, beta, theta)
self.lq_test = LQ(Q, R, A, B, C, beta)
self.Fr, self.Kr, self.Pr = self.rblq_test.robust_rule()
self.Fr_pf, self.Kr_pf, self.Pr_pf = self.rblq_test_pf.robust_rule()
def tearDown(self):
del self.rblq_test
del self.rblq_test_pf
def test_pure_forecasting(self):
self.assertTrue(self.rblq_test_pf.pure_forecasting)
def test_robust_rule_vs_simple(self):
rblq = self.rblq_test
rblq_pf = self.rblq_test_pf
Fr, Kr, Pr = self.Fr, self.Kr, self.Pr
Fr_pf, Kr_pf, Pr_pf = self.Fr_pf, self.Kr_pf, self.Pr_pf
Fs, Ks, Ps = rblq.robust_rule_simple(P_init=Pr, tol=1e-12)
Fs_pf, Ks_pf, Ps_pf = rblq_pf.robust_rule_simple(P_init=Pr_pf, tol=1e-12)
assert_allclose(Fr, Fs, rtol=1e-4)
assert_allclose(Kr, Ks, rtol=1e-4)
assert_allclose(Pr, Ps, rtol=1e-4)
assert_allclose(Fr_pf, Fs_pf, rtol=1e-4)
assert_allclose(Kr_pf, Ks_pf, rtol=1e-4)
assert_allclose(Pr_pf, Ps_pf, rtol=1e-4)
def test_f2k_and_k2f(self):
rblq = self.rblq_test
Fr, Kr, Pr = self.Fr, self.Kr, self.Pr
K_f2k, P_f2k = rblq.F_to_K(Fr)
F_k2f, P_k2f = rblq.K_to_F(Kr)
assert_allclose(K_f2k, Kr, rtol=1e-4)
assert_allclose(F_k2f, Fr, rtol=1e-4)
assert_allclose(P_f2k, P_k2f, rtol=1e-4)
def test_evaluate_F(self):
rblq = self.rblq_test
Fr, Kr, Pr = self.Fr, self.Kr, self.Pr
Kf, Pf, df, Of, of = rblq.evaluate_F(Fr)
# In the future if we wanted, we could check more things, but I
# think the other pieces are basically just plugging these into
# equations so if these hold then the others should be correct
# as well.
assert_allclose(Pf, Pr)
assert_allclose(Kf, Kr)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestRBLQControl)
unittest.TextTestRunner(verbosity=2, stream=sys.stderr).run(suite)