Skip to content

Commit b23e43d

Browse files
authored
LGTM code quality suggestions (#588)
* fix: removes from np import in lqcontrol * fix: removes from np import in lqnash * fix: removes import star in game_theory init * fix: removes from np import in robustlq * fix: removes from np import in matrix_eqn
1 parent 7b6b4a7 commit b23e43d

File tree

5 files changed

+66
-68
lines changed

5 files changed

+66
-68
lines changed

quantecon/game_theory/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@
1313
from .lemke_howson import lemke_howson
1414
from .mclennan_tourky import mclennan_tourky
1515
from .vertex_enumeration import vertex_enumeration, vertex_enumeration_gen
16-
from .game_generators import *
16+
from .game_generators import (
17+
blotto_game, ranking_game, sgc_game, tournament_game, unit_vector_game
18+
)
1719
from .repeated_game import RepeatedGame

quantecon/lqcontrol.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77
from textwrap import dedent
88
import numpy as np
9-
from numpy import dot
109
from scipy.linalg import solve
1110
from .matrix_eqn import solve_discrete_riccati, solve_discrete_riccati_system
1211
from .util import check_random_state
@@ -186,15 +185,15 @@ def update_values(self):
186185
Q, R, A, B, N, C = self.Q, self.R, self.A, self.B, self.N, self.C
187186
P, d = self.P, self.d
188187
# == Some useful matrices == #
189-
S1 = Q + self.beta * dot(B.T, dot(P, B))
190-
S2 = self.beta * dot(B.T, dot(P, A)) + N
191-
S3 = self.beta * dot(A.T, dot(P, A))
188+
S1 = Q + self.beta * np.dot(B.T, np.dot(P, B))
189+
S2 = self.beta * np.dot(B.T, np.dot(P, A)) + N
190+
S3 = self.beta * np.dot(A.T, np.dot(P, A))
192191
# == Compute F as (Q + B'PB)^{-1} (beta B'PA + N) == #
193192
self.F = solve(S1, S2)
194193
# === Shift P back in time one step == #
195-
new_P = R - dot(S2.T, self.F) + S3
194+
new_P = R - np.dot(S2.T, self.F) + S3
196195
# == Recalling that trace(AB) = trace(BA) == #
197-
new_d = self.beta * (d + np.trace(dot(P, dot(C, C.T))))
196+
new_d = self.beta * (d + np.trace(np.dot(P, np.dot(C, C.T))))
198197
# == Set new state == #
199198
self.P, self.d = new_P, new_d
200199

@@ -240,15 +239,15 @@ def stationary_values(self, method='doubling'):
240239
P = solve_discrete_riccati(A0, B0, R, Q, N, method=method)
241240

242241
# == Compute F == #
243-
S1 = Q + self.beta * dot(B.T, dot(P, B))
244-
S2 = self.beta * dot(B.T, dot(P, A)) + N
242+
S1 = Q + self.beta * np.dot(B.T, np.dot(P, B))
243+
S2 = self.beta * np.dot(B.T, np.dot(P, A)) + N
245244
F = solve(S1, S2)
246245

247246
# == Compute d == #
248247
if self.beta == 1:
249248
d = 0
250249
else:
251-
d = self.beta * np.trace(dot(P, dot(C, C.T))) / (1 - self.beta)
250+
d = self.beta * np.trace(np.dot(P, np.dot(C, C.T))) / (1 - self.beta)
252251

253252
# == Bind states and return values == #
254253
self.P, self.F, self.d = P, F, d
@@ -315,7 +314,7 @@ def compute_sequence(self, x0, ts_length=None, method='doubling',
315314
x_path = np.empty((self.n, T+1))
316315
u_path = np.empty((self.k, T))
317316
w_path = random_state.randn(self.j, T+1)
318-
Cw_path = dot(C, w_path)
317+
Cw_path = np.dot(C, w_path)
319318

320319
# == Compute and record the sequence of policies == #
321320
policies = []
@@ -327,13 +326,13 @@ def compute_sequence(self, x0, ts_length=None, method='doubling',
327326
# == Use policy sequence to generate states and controls == #
328327
F = policies.pop()
329328
x_path[:, 0] = x0.flatten()
330-
u_path[:, 0] = - dot(F, x0).flatten()
329+
u_path[:, 0] = - np.dot(F, x0).flatten()
331330
for t in range(1, T):
332331
F = policies.pop()
333-
Ax, Bu = dot(A, x_path[:, t-1]), dot(B, u_path[:, t-1])
332+
Ax, Bu = np.dot(A, x_path[:, t-1]), np.dot(B, u_path[:, t-1])
334333
x_path[:, t] = Ax + Bu + Cw_path[:, t]
335-
u_path[:, t] = - dot(F, x_path[:, t])
336-
Ax, Bu = dot(A, x_path[:, T-1]), dot(B, u_path[:, T-1])
334+
u_path[:, t] = - np.dot(F, x_path[:, t])
335+
Ax, Bu = np.dot(A, x_path[:, T-1]), np.dot(B, u_path[:, T-1])
337336
x_path[:, T] = Ax + Bu + Cw_path[:, T]
338337

339338
return x_path, u_path, w_path

quantecon/lqnash.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
from numpy import dot, eye
32
from scipy.linalg import solve
43
from .util import check_random_state
54

@@ -107,8 +106,8 @@ def nnash(A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2,
107106
k_2 = B2.shape[1]
108107

109108
random_state = check_random_state(random_state)
110-
v1 = eye(k_1)
111-
v2 = eye(k_2)
109+
v1 = np.eye(k_1)
110+
v2 = np.eye(k_2)
112111
P1 = np.zeros((n, n))
113112
P2 = np.zeros((n, n))
114113
F1 = random_state.randn(k_1, n)
@@ -119,28 +118,28 @@ def nnash(A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2,
119118
F10 = F1
120119
F20 = F2
121120

122-
G2 = solve(dot(B2.T, P2.dot(B2))+Q2, v2)
123-
G1 = solve(dot(B1.T, P1.dot(B1))+Q1, v1)
124-
H2 = dot(G2, B2.T.dot(P2))
125-
H1 = dot(G1, B1.T.dot(P1))
121+
G2 = solve(np.dot(B2.T, P2.dot(B2))+Q2, v2)
122+
G1 = solve(np.dot(B1.T, P1.dot(B1))+Q1, v1)
123+
H2 = np.dot(G2, B2.T.dot(P2))
124+
H1 = np.dot(G1, B1.T.dot(P1))
126125

127126
# break up the computation of F1, F2
128-
F1_left = v1 - dot(H1.dot(B2)+G1.dot(M1.T),
127+
F1_left = v1 - np.dot(H1.dot(B2)+G1.dot(M1.T),
129128
H2.dot(B1)+G2.dot(M2.T))
130-
F1_right = H1.dot(A)+G1.dot(W1.T) - dot(H1.dot(B2)+G1.dot(M1.T),
129+
F1_right = H1.dot(A)+G1.dot(W1.T) - np.dot(H1.dot(B2)+G1.dot(M1.T),
131130
H2.dot(A)+G2.dot(W2.T))
132131
F1 = solve(F1_left, F1_right)
133-
F2 = H2.dot(A)+G2.dot(W2.T) - dot(H2.dot(B1)+G2.dot(M2.T), F1)
132+
F2 = H2.dot(A)+G2.dot(W2.T) - np.dot(H2.dot(B1)+G2.dot(M2.T), F1)
134133

135134
Lambda1 = A - B2.dot(F2)
136135
Lambda2 = A - B1.dot(F1)
137-
Pi1 = R1 + dot(F2.T, S1.dot(F2))
138-
Pi2 = R2 + dot(F1.T, S2.dot(F1))
136+
Pi1 = R1 + np.dot(F2.T, S1.dot(F2))
137+
Pi2 = R2 + np.dot(F1.T, S2.dot(F1))
139138

140-
P1 = dot(Lambda1.T, P1.dot(Lambda1)) + Pi1 - \
141-
dot(dot(Lambda1.T, P1.dot(B1)) + W1 - F2.T.dot(M1), F1)
142-
P2 = dot(Lambda2.T, P2.dot(Lambda2)) + Pi2 - \
143-
dot(dot(Lambda2.T, P2.dot(B2)) + W2 - F1.T.dot(M2), F2)
139+
P1 = np.dot(Lambda1.T, P1.dot(Lambda1)) + Pi1 - \
140+
np.dot(np.dot(Lambda1.T, P1.dot(B1)) + W1 - F2.T.dot(M1), F1)
141+
P2 = np.dot(Lambda2.T, P2.dot(Lambda2)) + Pi2 - \
142+
np.dot(np.dot(Lambda2.T, P2.dot(B2)) + W2 - F1.T.dot(M2), F2)
144143

145144
dd = np.max(np.abs(F10 - F1)) + np.max(np.abs(F20 - F2))
146145

quantecon/matrix_eqn.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
2. Fix warnings from checking conditioning of matrices
1111
"""
1212
import numpy as np
13-
from numpy import dot
1413
from numpy.linalg import solve
1514
from scipy.linalg import solve_discrete_lyapunov as sp_solve_discrete_lyapunov
1615
from scipy.linalg import solve_discrete_are as sp_solve_discrete_are
@@ -172,19 +171,19 @@ def solve_discrete_riccati(A, B, Q, R, N=None, tolerance=1e-10, max_iter=500,
172171
# == Choose optimal value of gamma in R_hat = R + gamma B'B == #
173172
current_min = np.inf
174173
candidates = (0.01, 0.1, 0.25, 0.5, 1.0, 2.0, 10.0, 100.0, 10e5)
175-
BB = dot(B.T, B)
176-
BTA = dot(B.T, A)
174+
BB = np.dot(B.T, B)
175+
BTA = np.dot(B.T, A)
177176
for gamma in candidates:
178177
Z = R + gamma * BB
179178
cn = np.linalg.cond(Z)
180179
if cn * EPS < 1:
181-
Q_tilde = - Q + dot(N.T, solve(Z, N + gamma * BTA)) + gamma * I
182-
G0 = dot(B, solve(Z, B.T))
183-
A0 = dot(I - gamma * G0, A) - dot(B, solve(Z, N))
184-
H0 = gamma * dot(A.T, A0) - Q_tilde
180+
Q_tilde = - Q + np.dot(N.T, solve(Z, N + gamma * BTA)) + gamma * I
181+
G0 = np.dot(B, solve(Z, B.T))
182+
A0 = np.dot(I - gamma * G0, A) - np.dot(B, solve(Z, N))
183+
H0 = gamma * np.dot(A.T, A0) - Q_tilde
185184
f1 = np.linalg.cond(Z, np.inf)
186185
f2 = gamma * f1
187-
f3 = np.linalg.cond(I + dot(G0, H0))
186+
f3 = np.linalg.cond(I + np.dot(G0, H0))
188187
f_gamma = max(f1, f2, f3)
189188
if f_gamma < current_min:
190189
best_gamma = gamma
@@ -199,10 +198,10 @@ def solve_discrete_riccati(A, B, Q, R, N=None, tolerance=1e-10, max_iter=500,
199198
R_hat = R + gamma * BB
200199

201200
# == Initial conditions == #
202-
Q_tilde = - Q + dot(N.T, solve(R_hat, N + gamma * BTA)) + gamma * I
203-
G0 = dot(B, solve(R_hat, B.T))
204-
A0 = dot(I - gamma * G0, A) - dot(B, solve(R_hat, N))
205-
H0 = gamma * dot(A.T, A0) - Q_tilde
201+
Q_tilde = - Q + np.dot(N.T, solve(R_hat, N + gamma * BTA)) + gamma * I
202+
G0 = np.dot(B, solve(R_hat, B.T))
203+
A0 = np.dot(I - gamma * G0, A) - np.dot(B, solve(R_hat, N))
204+
H0 = gamma * np.dot(A.T, A0) - Q_tilde
206205
i = 1
207206

208207
# == Main loop == #
@@ -212,9 +211,9 @@ def solve_discrete_riccati(A, B, Q, R, N=None, tolerance=1e-10, max_iter=500,
212211
raise ValueError(fail_msg.format(i))
213212

214213
else:
215-
A1 = dot(A0, solve(I + dot(G0, H0), A0))
216-
G1 = G0 + dot(dot(A0, G0), solve(I + dot(H0, G0), A0.T))
217-
H1 = H0 + dot(A0.T, solve(I + dot(H0, G0), dot(H0, A0)))
214+
A1 = np.dot(A0, solve(I + np.dot(G0, H0), A0))
215+
G1 = G0 + np.dot(np.dot(A0, G0), solve(I + np.dot(H0, G0), A0.T))
216+
H1 = H0 + np.dot(A0.T, solve(I + np.dot(H0, G0), np.dot(H0, A0)))
218217

219218
error = np.max(np.abs(H1 - H0))
220219
A0 = A1

quantecon/robustlq.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numpy as np
77
from .lqcontrol import LQ
88
from .quadsums import var_quadratic_sum
9-
from numpy import dot, log, sqrt, identity, hstack, vstack, trace
109
from scipy.linalg import solve, inv, det
1110
from .matrix_eqn import solve_discrete_lyapunov
1211

@@ -108,10 +107,10 @@ def d_operator(self, P):
108107
"""
109108
C, theta = self.C, self.theta
110109
I = np.identity(self.j)
111-
S1 = dot(P, C)
112-
S2 = dot(C.T, S1)
110+
S1 = np.dot(P, C)
111+
S2 = np.dot(C.T, S1)
113112

114-
dP = P + dot(S1, solve(theta * I - S2, S1.T))
113+
dP = P + np.dot(S1, solve(theta * I - S2, S1.T))
115114

116115
return dP
117116

@@ -143,12 +142,12 @@ def b_operator(self, P):
143142
144143
"""
145144
A, B, Q, R, beta = self.A, self.B, self.Q, self.R, self.beta
146-
S1 = Q + beta * dot(B.T, dot(P, B))
147-
S2 = beta * dot(B.T, dot(P, A))
148-
S3 = beta * dot(A.T, dot(P, A))
145+
S1 = Q + beta * np.dot(B.T, np.dot(P, B))
146+
S2 = beta * np.dot(B.T, np.dot(P, A))
147+
S3 = beta * np.dot(A.T, np.dot(P, A))
149148
F = solve(S1, S2) if not self.pure_forecasting else np.zeros(
150149
(self.k, self.n))
151-
new_P = R - dot(S2.T, F) + S3
150+
new_P = R - np.dot(S2.T, F) + S3
152151

153152
return F, new_P
154153

@@ -188,7 +187,7 @@ def robust_rule(self, method='doubling'):
188187
beta, theta = self.beta, self.theta
189188
k, j = self.k, self.j
190189
# == Set up LQ version == #
191-
I = identity(j)
190+
I = np.identity(j)
192191
Z = np.zeros((k, j))
193192

194193
if self.pure_forecasting:
@@ -200,8 +199,8 @@ def robust_rule(self, method='doubling'):
200199
K = -f[:k, :]
201200

202201
else:
203-
Ba = hstack([B, C])
204-
Qa = vstack([hstack([Q, Z]), hstack([Z.T, -beta*I*theta])])
202+
Ba = np.hstack([B, C])
203+
Qa = np.vstack([np.hstack([Q, Z]), np.hstack([Z.T, -beta*I*theta])])
205204
lq = LQ(Qa, R, A, Ba, beta=beta)
206205

207206
# == Solve and convert back to robust problem == #
@@ -281,8 +280,8 @@ def F_to_K(self, F, method='doubling'):
281280
282281
"""
283282
Q2 = self.beta * self.theta
284-
R2 = - self.R - dot(F.T, dot(self.Q, F))
285-
A2 = self.A - dot(self.B, F)
283+
R2 = - self.R - np.dot(F.T, np.dot(self.Q, F))
284+
A2 = self.A - np.dot(self.B, F)
286285
B2 = self.C
287286
lq = LQ(Q2, R2, A2, B2, beta=self.beta)
288287
neg_P, neg_K, d = lq.stationary_values(method=method)
@@ -309,10 +308,10 @@ def K_to_F(self, K, method='doubling'):
309308
The value function for a given K
310309
311310
"""
312-
A1 = self.A + dot(self.C, K)
311+
A1 = self.A + np.dot(self.C, K)
313312
B1 = self.B
314313
Q1 = self.Q
315-
R1 = self.R - self.beta * self.theta * dot(K.T, K)
314+
R1 = self.R - self.beta * self.theta * np.dot(K.T, K)
316315
lq = LQ(Q1, R1, A1, B1, beta=self.beta)
317316
P, F, d = lq.stationary_values(method=method)
318317

@@ -349,9 +348,9 @@ def compute_deterministic_entropy(self, F, K, x0):
349348
The deterministic entropy
350349
351350
"""
352-
H0 = dot(K.T, K)
351+
H0 = np.dot(K.T, K)
353352
C0 = np.zeros((self.n, 1))
354-
A0 = self.A - dot(self.B, F) + dot(self.C, K)
353+
A0 = self.A - np.dot(self.B, F) + np.dot(self.C, K)
355354
e = var_quadratic_sum(A0, C0, H0, self.beta, x0)
356355

357356
return e
@@ -389,13 +388,13 @@ def evaluate_F(self, F):
389388
K_F, P_F = self.F_to_K(F)
390389
I = np.identity(self.j)
391390
H = inv(I - C.T.dot(P_F.dot(C)) / theta)
392-
d_F = log(det(H))
391+
d_F = np.log(det(H))
393392

394393
# == Compute O_F and o_F == #
395-
AO = sqrt(beta) * (A - dot(B, F) + dot(C, K_F))
396-
O_F = solve_discrete_lyapunov(AO.T, beta * dot(K_F.T, K_F))
397-
ho = (trace(H - 1) - d_F) / 2.0
398-
tr = trace(dot(O_F, C.dot(H.dot(C.T))))
394+
AO = np.sqrt(beta) * (A - np.dot(B, F) + np.dot(C, K_F))
395+
O_F = solve_discrete_lyapunov(AO.T, beta * np.dot(K_F.T, K_F))
396+
ho = (np.trace(H - 1) - d_F) / 2.0
397+
tr = np.trace(np.dot(O_F, C.dot(H.dot(C.T))))
399398
o_F = (ho + beta * tr) / (1 - beta)
400399

401400
return K_F, P_F, d_F, O_F, o_F

0 commit comments

Comments
 (0)