Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Sigma_infinity and K_infinity as properties #396

Merged
merged 1 commit into from
Mar 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions quantecon/kalman.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Kalman:
def __init__(self, ss, x_hat=None, Sigma=None):
self.ss = ss
self.set_state(x_hat, Sigma)
self.K_infinity = None
self.Sigma_infinity = None
self._K_infinity = None
self._Sigma_infinity = None

def set_state(self, x_hat, Sigma):
if Sigma is None:
Expand All @@ -89,6 +89,18 @@ def __str__(self):
"""
return dedent(m.format(n=self.ss.n, k=self.ss.k))

@property
def Sigma_infinity(self):
if self._Sigma_infinity is None:
self.stationary_values()
return self._Sigma_infinity

@property
def K_infinity(self):
if self._K_infinity is None:
self.stationary_values()
return self._K_infinity

def whitener_lss(self):
r"""
This function takes the linear state space system
Expand Down Expand Up @@ -143,13 +155,7 @@ def whitener_lss(self):
This is the linear state space system that represents
the whitened system
"""
# Check for steady state Sigma and K
if self.K_infinity is None:
Sig, K = self.stationary_values()
self.Sigma_infinity = Sig
self.K_infinity = K
else:
K = self.K_infinity
K = self.K_infinity

# Get the matrix sizes
n, k, m, l = self.ss.n, self.ss.k, self.ss.m, self.ss.l
Expand Down Expand Up @@ -247,6 +253,7 @@ def stationary_values(self):
The stationary Kalman gain.

"""

# === simplify notation === #
A, C, G, H = self.ss.A, self.ss.C, self.ss.G, self.ss.H
Q, R = np.dot(C, C.T), np.dot(H, H.T)
Expand All @@ -258,7 +265,7 @@ def stationary_values(self):
K_infinity = dot(temp1, temp2)

# == record as attributes and return == #
self.Sigma_infinity, self.K_infinity = Sigma_infinity, K_infinity
self._Sigma_infinity, self._K_infinity = Sigma_infinity, K_infinity
return Sigma_infinity, K_infinity

def stationary_coefficients(self, j, coeff_type='ma'):
Expand All @@ -277,9 +284,6 @@ def stationary_coefficients(self, j, coeff_type='ma'):
# == simplify notation == #
A, G = self.ss.A, self.ss.G
K_infinity = self.K_infinity
# == make sure that K_infinity has actually been computed == #
if K_infinity is None:
S, K_infinity = self.stationary_values()
# == compute and return coefficients == #
coeffs = []
i = 1
Expand All @@ -305,7 +309,4 @@ def stationary_innovation_covar(self):
R = np.dot(H, H.T)
Sigma_infinity = self.Sigma_infinity

# == make sure that Sigma_infinity has been computed == #
if Sigma_infinity is None:
Sigma_infinity, K = self.stationary_values()
return dot(G, dot(Sigma_infinity, G.T)) + R