Skip to content

Commit

Permalink
Remove numpy 'import from' (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Jan 6, 2021
1 parent 128ed39 commit 6695249
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
5 changes: 2 additions & 3 deletions quantecon/discrete_rv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import numpy as np
from numpy import cumsum
from .util import check_random_state


Expand All @@ -29,7 +28,7 @@ class DiscreteRV:

def __init__(self, q):
self._q = np.asarray(q)
self.Q = cumsum(q)
self.Q = np.cumsum(q)

def __repr__(self):
return "DiscreteRV with {n} elements".format(n=self._q.size)
Expand All @@ -52,7 +51,7 @@ def q(self, val):
"""
self._q = np.asarray(val)
self.Q = cumsum(val)
self.Q = np.cumsum(val)

def draw(self, k=1, random_state=None):
"""
Expand Down
35 changes: 18 additions & 17 deletions quantecon/kalman.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""
from textwrap import dedent
import numpy as np
from numpy import dot
from scipy.linalg import inv
from quantecon.lss import LinearStateSpace
from quantecon.matrix_eqn import solve_discrete_riccati
Expand Down Expand Up @@ -163,7 +162,9 @@ def whitener_lss(self):
A, C, G, H = self.ss.A, self.ss.C, self.ss.G, self.ss.H

Atil = np.vstack([np.hstack([A, np.zeros((n, n)), np.zeros((n, l))]),
np.hstack([dot(K, G), A-dot(K, G), dot(K, H)]),
np.hstack([np.dot(K, G),
A-np.dot(K, G),
np.dot(K, H)]),
np.zeros((l, 2*n + l))])

Ctil = np.vstack([np.hstack([C, np.zeros((n, l))]),
Expand Down Expand Up @@ -204,11 +205,11 @@ def prior_to_filtered(self, y):
# === and then update === #
y = np.atleast_2d(y)
y.shape = self.ss.k, 1
E = dot(self.Sigma, G.T)
F = dot(dot(G, self.Sigma), G.T) + R
M = dot(E, inv(F))
self.x_hat = self.x_hat + dot(M, (y - dot(G, self.x_hat)))
self.Sigma = self.Sigma - dot(M, dot(G, self.Sigma))
E = np.dot(self.Sigma, G.T)
F = np.dot(np.dot(G, self.Sigma), G.T) + R
M = np.dot(E, inv(F))
self.x_hat = self.x_hat + np.dot(M, (y - np.dot(G, self.x_hat)))
self.Sigma = self.Sigma - np.dot(M, np.dot(G, self.Sigma))

def filtered_to_forecast(self):
"""
Expand All @@ -222,8 +223,8 @@ def filtered_to_forecast(self):
Q = np.dot(C, C.T)

# === and then update === #
self.x_hat = dot(A, self.x_hat)
self.Sigma = dot(A, dot(self.Sigma, A.T)) + Q
self.x_hat = np.dot(A, self.x_hat)
self.Sigma = np.dot(A, np.dot(self.Sigma, A.T)) + Q

def update(self, y):
"""
Expand Down Expand Up @@ -268,9 +269,9 @@ def stationary_values(self, method='doubling'):

# === solve Riccati equation, obtain Kalman gain === #
Sigma_infinity = solve_discrete_riccati(A.T, G.T, Q, R, method=method)
temp1 = dot(dot(A, Sigma_infinity), G.T)
temp2 = inv(dot(G, dot(Sigma_infinity, G.T)) + R)
K_infinity = dot(temp1, temp2)
temp1 = np.dot(np.dot(A, Sigma_infinity), G.T)
temp2 = inv(np.dot(G, np.dot(Sigma_infinity, G.T)) + R)
K_infinity = np.dot(temp1, temp2)

# == record as attributes and return == #
self._Sigma_infinity, self._K_infinity = Sigma_infinity, K_infinity
Expand Down Expand Up @@ -300,14 +301,14 @@ def stationary_coefficients(self, j, coeff_type='ma'):
P_mat = A
P = np.identity(self.ss.n) # Create a copy
elif coeff_type == 'var':
coeffs.append(dot(G, K_infinity))
P_mat = A - dot(K_infinity, G)
coeffs.append(np.dot(G, K_infinity))
P_mat = A - np.dot(K_infinity, G)
P = np.copy(P_mat) # Create a copy
else:
raise ValueError("Unknown coefficient type")
while i <= j:
coeffs.append(dot(dot(G, P), K_infinity))
P = dot(P, P_mat)
coeffs.append(np.dot(np.dot(G, P), K_infinity))
P = np.dot(P, P_mat)
i += 1
return coeffs

Expand All @@ -317,4 +318,4 @@ def stationary_innovation_covar(self):
R = np.dot(H, H.T)
Sigma_infinity = self.Sigma_infinity

return dot(G, dot(Sigma_infinity, G.T)) + R
return np.dot(G, np.dot(Sigma_infinity, G.T)) + R

0 comments on commit 6695249

Please sign in to comment.