#NEWLINE# def univariate_kalman( y , Z , H , T , Q , R ) : #NEWLINE# #INDENT# a = np . zeros ( ( T . shape [ 0 ] , ( y . shape [ 0 ] + 1 ) ) ) #NEWLINE# a [ 0 ] [ 0 ] = y [ 0 ] #NEWLINE# P = ( np . ones ( ( a . shape [ 0 ] , a . shape [ 0 ] , ( y . shape [ 0 ] + 1 ) ) ) * ( 10 ** 7 ) ) #NEWLINE# K = np . zeros ( a . shape [ 0 ] ) #NEWLINE# v = np . zeros ( y . shape [ 0 ] ) #NEWLINE# F = np . zeros ( ( H . shape [ 0 ] , H . shape [ 1 ] , y . shape [ 0 ] ) ) #NEWLINE# for t in xrange ( 0 , H . shape [ 0 ] ) : #NEWLINE# #INDENT# v [ t ] = ( y [ t ] - np . dot ( Z , a [ : , t ] ) ) #NEWLINE# F [ : , : , t ] = ( np . dot ( np . dot ( Z , P [ : , : , t ] ) , Z . T ) + H . flatten ( ) [ 0 ] ) #NEWLINE# K = ( np . dot ( np . dot ( T , P [ : , : , t ] ) , Z . T ) / F [ : , : , t ] . flatten ( ) [ 0 ] ) #NEWLINE# a [ : , ( t + 1 ) ] = ( np . dot ( T , a [ : , t ] ) + np . dot ( K , v [ t ] ) ) #NEWLINE# P [ : , : , ( t + 1 ) ] = ( ( np . dot ( np . dot ( T , P [ : , : , t ] ) , T . T ) + np . dot ( np . dot ( R , Q ) , R . T ) ) - ( F [ : , : , t ] . flatten ( ) [ 0 ] * np . dot ( np . array ( [ K ] ) . T , np . array ( [ K ] ) ) ) ) #NEWLINE# #UNINDENT# return ( a , F , v )