<s> """<STR_LIT>""" <EOL> from textwrap import dedent <EOL> import numpy as np <EOL> from numpy import dot <EOL> from scipy . linalg import inv <EOL> from quantecon . lss import LinearStateSpace <EOL> from quantecon . matrix_eqn import solve_discrete_riccati <EOL> class Kalman ( object ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , ss , x_hat = None , Sigma = None ) : <EOL> self . ss = ss <EOL> self . set_state ( x_hat , Sigma ) <EOL> self . K_infinity = None <EOL> self . Sigma_infinity = None <EOL> def set_state ( self , x_hat , Sigma ) : <EOL> if Sigma is None : <EOL> Sigma = np . identity ( self . ss . n ) <EOL> else : <EOL> self . Sigma = np . atleast_2d ( Sigma ) <EOL> if x_hat is None : <EOL> x_hat = np . zeros ( ( self . ss . n , <NUM_LIT:1> ) ) <EOL> else : <EOL> self . x_hat = np . atleast_2d ( x_hat ) <EOL> self . x_hat . shape = self . ss . n , <NUM_LIT:1> <EOL> def __repr__ ( self ) : <EOL> return self . __str__ ( ) <EOL> def __str__ ( self ) : <EOL> m = """<STR_LIT>""" <EOL> return dedent ( m . format ( n = self . ss . n , k = self . ss . k ) ) <EOL> def whitener_lss ( self ) : <EOL> r"""<STR_LIT>""" <EOL> if self . K_infinity is None : <EOL> Sig , K = self . stationary_values ( ) <EOL> self . Sigma_infinity = Sig <EOL> self . K_infinity = K <EOL> else : <EOL> K = self . K_infinity <EOL> n , k , m , l = self . ss . n , self . ss . k , self . ss . m , self . ss . l <EOL> A , C , G , H = self . ss . A , self . ss . C , self . ss . G , self . ss . H <EOL> Atil = np . vstack ( [ np . hstack ( [ A , np . zeros ( ( n , n ) ) , np . zeros ( ( n , l ) ) ] ) , <EOL> np . hstack ( [ dot ( K , G ) , A - dot ( K , G ) , dot ( K , H ) ] ) , <EOL> np . zeros ( ( l , <NUM_LIT:2> * n + l ) ) ] ) <EOL> Ctil = np . vstack ( [ np . hstack ( [ C , np . zeros ( ( n , l ) ) ] ) , <EOL> np . zeros ( ( n , m + l ) ) , <EOL> np . hstack ( [ np . zeros ( ( l , m ) ) , np . eye ( l ) ] ) ] ) <EOL> Gtil = np . hstack ( [ G , - G , H ] ) <EOL> whitened_lss = LinearStateSpace ( Atil , Ctil , Gtil ) <EOL> self . whitened_lss = whitened_lss <EOL> return whitened_lss <EOL> def prior_to_filtered ( self , y ) : <EOL> r"""<STR_LIT>""" <EOL> G , H = self . ss . G , self . ss . H <EOL> R = np . dot ( H , H . T ) <EOL> y = np . atleast_2d ( y ) <EOL> y . shape = self . ss . k , <NUM_LIT:1> <EOL> E = dot ( self . Sigma , G . T ) <EOL> F = dot ( dot ( G , self . Sigma ) , G . T ) + R <EOL> M = dot ( E , inv ( F ) ) <EOL> self . x_hat = self . x_hat + dot ( M , ( y - dot ( G , self . x_hat ) ) ) <EOL> self . Sigma = self . Sigma - dot ( M , dot ( G , self . Sigma ) ) <EOL> def filtered_to_forecast ( self ) : <EOL> """<STR_LIT>""" <EOL> A , C = self . ss . A , self . ss . C <EOL> Q = np . dot ( C , C . T ) <EOL> self . x_hat = dot ( A , self . x_hat ) <EOL> self . Sigma = dot ( A , dot ( self . Sigma , A . T ) ) + Q <EOL> def update ( self , y ) : <EOL> """<STR_LIT>""" <EOL> self . prior_to_filtered ( y ) <EOL> self . filtered_to_forecast ( ) <EOL> def stationary_values ( self ) : <EOL> """<STR_LIT>""" <EOL> A , C , G , H = self . ss . A , self . ss . C , self . ss . G , self . ss . H <EOL> Q , R = np . dot ( C , C . T ) , np . dot ( H , H . T ) <EOL> Sigma_infinity = solve_discrete_riccati ( A . T , G . T , Q , R ) <EOL> temp1 = dot ( dot ( A , Sigma_infinity ) , G . T ) <EOL> temp2 = inv ( dot ( G , dot ( Sigma_infinity , G . T ) ) + R ) <EOL> K_infinity = dot ( temp1 , temp2 ) <EOL> self . Sigma_infinity , self . K_infinity = Sigma_infinity , K_infinity <EOL> return Sigma_infinity , K_infinity <EOL> def stationary_coefficients ( self , j , coeff_type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> A , G = self . ss . A , self . ss . G <EOL> K_infinity = self . K_infinity <EOL> if K_infinity is None : <EOL> S , K_infinity = self . stationary_values ( ) <EOL> coeffs = [ ] <EOL> i = <NUM_LIT:1> <EOL> if coeff_type == '<STR_LIT>' : <EOL> coeffs . append ( np . identity ( self . ss . k ) ) <EOL> P_mat = A <EOL> P = np . identity ( self . ss . n ) <EOL> elif coeff_type == '<STR_LIT>' : <EOL> coeffs . append ( dot ( G , K_infinity ) ) <EOL> P_mat = A - dot ( K_infinity , G ) <EOL> P = np . copy ( P_mat ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> while i <= j : <EOL> coeffs . append ( dot ( dot ( G , P ) , K_infinity ) ) <EOL> P = dot ( P , P_mat ) <EOL> i += <NUM_LIT:1> <EOL> return coeffs <EOL> def stationary_innovation_covar ( self ) : <EOL> H , G = self . ss . H , self . ss . G <EOL> R = np . dot ( H , H . T ) <EOL> Sigma_infinity = self . Sigma_infinity <EOL> if Sigma_infinity is None : <EOL> Sigma_infinity , K = self . stationary_values ( ) <EOL> return dot ( G , dot ( Sigma_infinity , G . T ) ) + R </s>
