<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import , print_function <EOL> import numpy as np <EOL> import pandas as pd <EOL> import os <EOL> try : <EOL> from scipy . linalg . blas import find_best_blas_type <EOL> except ImportError : <EOL> _type_conv = { '<STR_LIT:f>' : '<STR_LIT:s>' , '<STR_LIT:d>' : '<STR_LIT:d>' , '<STR_LIT:F>' : '<STR_LIT:c>' , '<STR_LIT:D>' : '<STR_LIT:z>' , '<STR_LIT>' : '<STR_LIT:z>' } <EOL> def find_best_blas_type ( arrays ) : <EOL> dtype , index = max ( <EOL> [ ( ar . dtype , i ) for i , ar in enumerate ( arrays ) ] ) <EOL> prefix = _type_conv . get ( dtype . char , '<STR_LIT:d>' ) <EOL> return ( prefix , dtype , None ) <EOL> from statsmodels . tsa . statespace . sarimax import SARIMAX <EOL> from statsmodels . tsa . statespace import _statespace as ss <EOL> from . results import results_kalman_filter <EOL> from numpy . testing import assert_almost_equal , assert_allclose <EOL> from nose . exc import SkipTest <EOL> prefix_statespace_map = { <EOL> '<STR_LIT:s>' : ss . sStatespace , '<STR_LIT:d>' : ss . dStatespace , <EOL> '<STR_LIT:c>' : ss . cStatespace , '<STR_LIT:z>' : ss . zStatespace <EOL> } <EOL> prefix_kalman_filter_map = { <EOL> '<STR_LIT:s>' : ss . sKalmanFilter , '<STR_LIT:d>' : ss . dKalmanFilter , <EOL> '<STR_LIT:c>' : ss . cKalmanFilter , '<STR_LIT:z>' : ss . zKalmanFilter <EOL> } <EOL> current_path = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> class Clark1987 ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls , dtype = float , conserve_memory = <NUM_LIT:0> , loglikelihood_burn = <NUM_LIT:0> ) : <EOL> cls . true = results_kalman_filter . uc_uni <EOL> cls . true_states = pd . DataFrame ( cls . true [ '<STR_LIT>' ] ) <EOL> data = pd . DataFrame ( <EOL> cls . true [ '<STR_LIT:data>' ] , <EOL> index = pd . date_range ( '<STR_LIT>' , '<STR_LIT>' , freq = '<STR_LIT>' ) , <EOL> columns = [ '<STR_LIT>' ] <EOL> ) <EOL> data [ '<STR_LIT>' ] = np . log ( data [ '<STR_LIT>' ] ) <EOL> cls . conserve_memory = conserve_memory <EOL> cls . loglikelihood_burn = loglikelihood_burn <EOL> cls . obs = np . array ( data [ '<STR_LIT>' ] , ndmin = <NUM_LIT:2> , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . k_endog = k_endog = <NUM_LIT:1> <EOL> cls . design = np . zeros ( ( k_endog , <NUM_LIT:4> , <NUM_LIT:1> ) , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . design [ : , : , <NUM_LIT:0> ] = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> cls . obs_intercept = np . zeros ( ( k_endog , <NUM_LIT:1> ) , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . obs_cov = np . zeros ( ( k_endog , k_endog , <NUM_LIT:1> ) , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . k_states = k_states = <NUM_LIT:4> <EOL> cls . transition = np . zeros ( ( k_states , k_states , <NUM_LIT:1> ) , <EOL> dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . transition [ ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) ] = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> cls . state_intercept = np . zeros ( ( k_states , <NUM_LIT:1> ) , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . selection = np . asfortranarray ( np . eye ( k_states ) [ : , : , None ] , <EOL> dtype = dtype ) <EOL> cls . state_cov = np . zeros ( ( k_states , k_states , <NUM_LIT:1> ) , <EOL> dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . initial_state = np . zeros ( ( k_states , ) , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . initial_state_cov = np . asfortranarray ( np . eye ( k_states ) * <NUM_LIT:100> , <EOL> dtype = dtype ) <EOL> ( sigma_v , sigma_e , sigma_w , phi_1 , phi_2 ) = np . array ( <EOL> cls . true [ '<STR_LIT>' ] , dtype = dtype <EOL> ) <EOL> cls . transition [ ( [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ] = [ phi_1 , phi_2 ] <EOL> cls . state_cov [ <EOL> np . diag_indices ( k_states ) + ( np . zeros ( k_states , dtype = int ) , ) ] = [ <EOL> sigma_v ** <NUM_LIT:2> , sigma_e ** <NUM_LIT:2> , <NUM_LIT:0> , sigma_w ** <NUM_LIT:2> <EOL> ] <EOL> cls . initial_state_cov = np . asfortranarray ( <EOL> np . dot ( <EOL> np . dot ( cls . transition [ : , : , <NUM_LIT:0> ] , cls . initial_state_cov ) , <EOL> cls . transition [ : , : , <NUM_LIT:0> ] . T <EOL> ) <EOL> ) <EOL> @ classmethod <EOL> def init_filter ( cls ) : <EOL> prefix = find_best_blas_type ( ( cls . obs , ) ) <EOL> klass = prefix_statespace_map [ prefix [ <NUM_LIT:0> ] ] <EOL> model = klass ( <EOL> cls . obs , cls . design , cls . obs_intercept , cls . obs_cov , <EOL> cls . transition , cls . state_intercept , cls . selection , <EOL> cls . state_cov <EOL> ) <EOL> model . initialize_known ( cls . initial_state , cls . initial_state_cov ) <EOL> klass = prefix_kalman_filter_map [ prefix [ <NUM_LIT:0> ] ] <EOL> kfilter = klass ( model , conserve_memory = cls . conserve_memory , <EOL> loglikelihood_burn = cls . loglikelihood_burn ) <EOL> return model , kfilter <EOL> @ classmethod <EOL> def run_filter ( cls ) : <EOL> cls . filter ( ) <EOL> return { <EOL> '<STR_LIT>' : lambda burn : np . sum ( cls . filter . loglikelihood [ burn : ] ) , <EOL> '<STR_LIT:state>' : np . array ( cls . filter . filtered_state ) , <EOL> } <EOL> def test_loglike ( self ) : <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT>' ] ( self . true [ '<STR_LIT:start>' ] ) , self . true [ '<STR_LIT>' ] , <NUM_LIT:5> <EOL> ) <EOL> def test_filtered_state ( self ) : <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:0> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:0> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:1> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:1> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:3> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:2> ] , <NUM_LIT:4> <EOL> ) <EOL> class TestClark1987Single ( Clark1987 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> super ( TestClark1987Single , cls ) . setup_class ( <EOL> dtype = np . float32 , conserve_memory = <NUM_LIT:0> <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> def test_loglike ( self ) : <EOL> assert_allclose ( <EOL> self . result [ '<STR_LIT>' ] ( self . true [ '<STR_LIT:start>' ] ) , self . true [ '<STR_LIT>' ] , <EOL> rtol = <NUM_LIT> <EOL> ) <EOL> def test_filtered_state ( self ) : <EOL> assert_allclose ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:0> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:0> ] , <EOL> atol = <NUM_LIT> <EOL> ) <EOL> assert_allclose ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:1> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:1> ] , <EOL> atol = <NUM_LIT> <EOL> ) <EOL> assert_allclose ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:3> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:2> ] , <EOL> atol = <NUM_LIT> <EOL> ) <EOL> class TestClark1987Double ( Clark1987 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1987Double , cls ) . setup_class ( <EOL> dtype = float , conserve_memory = <NUM_LIT:0> <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class TestClark1987SingleComplex ( Clark1987 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> super ( TestClark1987SingleComplex , cls ) . setup_class ( <EOL> dtype = np . complex64 , conserve_memory = <NUM_LIT:0> <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> def test_loglike ( self ) : <EOL> assert_allclose ( <EOL> self . result [ '<STR_LIT>' ] ( self . true [ '<STR_LIT:start>' ] ) , self . true [ '<STR_LIT>' ] , <EOL> rtol = <NUM_LIT> <EOL> ) <EOL> def test_filtered_state ( self ) : <EOL> assert_allclose ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:0> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:0> ] , <EOL> atol = <NUM_LIT> <EOL> ) <EOL> assert_allclose ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:1> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:1> ] , <EOL> atol = <NUM_LIT> <EOL> ) <EOL> assert_allclose ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:3> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:2> ] , <EOL> atol = <NUM_LIT> <EOL> ) <EOL> class TestClark1987DoubleComplex ( Clark1987 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1987DoubleComplex , cls ) . setup_class ( <EOL> dtype = complex , conserve_memory = <NUM_LIT:0> <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class TestClark1987Conserve ( Clark1987 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1987Conserve , cls ) . setup_class ( <EOL> dtype = float , conserve_memory = <NUM_LIT> | <NUM_LIT> <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class Clark1987Forecast ( Clark1987 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls , dtype = float , nforecast = <NUM_LIT:100> , conserve_memory = <NUM_LIT:0> ) : <EOL> super ( Clark1987Forecast , cls ) . setup_class ( <EOL> dtype , conserve_memory <EOL> ) <EOL> cls . nforecast = nforecast <EOL> cls . _obs = cls . obs <EOL> cls . obs = np . array ( np . r_ [ cls . obs [ <NUM_LIT:0> , : ] , [ np . nan ] * nforecast ] , <EOL> ndmin = <NUM_LIT:2> , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> def test_filtered_state ( self ) : <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:0> ] [ self . true [ '<STR_LIT:start>' ] : - self . nforecast ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:0> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:1> ] [ self . true [ '<STR_LIT:start>' ] : - self . nforecast ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:1> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:3> ] [ self . true [ '<STR_LIT:start>' ] : - self . nforecast ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:2> ] , <NUM_LIT:4> <EOL> ) <EOL> class TestClark1987ForecastDouble ( Clark1987Forecast ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1987ForecastDouble , cls ) . setup_class ( ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class TestClark1987ForecastDoubleComplex ( Clark1987Forecast ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1987ForecastDoubleComplex , cls ) . setup_class ( <EOL> dtype = complex <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class TestClark1987ForecastConserve ( Clark1987Forecast ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1987ForecastConserve , cls ) . setup_class ( <EOL> dtype = float , conserve_memory = <NUM_LIT> | <NUM_LIT> <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class TestClark1987ConserveAll ( Clark1987 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1987ConserveAll , cls ) . setup_class ( <EOL> dtype = float , conserve_memory = <NUM_LIT> | <NUM_LIT> | <NUM_LIT> | <NUM_LIT> <EOL> ) <EOL> cls . loglikelihood_burn = cls . true [ '<STR_LIT:start>' ] <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> def test_loglike ( self ) : <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT>' ] ( <NUM_LIT:0> ) , self . true [ '<STR_LIT>' ] , <NUM_LIT:5> <EOL> ) <EOL> def test_filtered_state ( self ) : <EOL> end = self . true_states . shape [ <NUM_LIT:0> ] <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] , <EOL> self . true_states . iloc [ end - <NUM_LIT:1> , <NUM_LIT:0> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:1> ] [ - <NUM_LIT:1> ] , <EOL> self . true_states . iloc [ end - <NUM_LIT:1> , <NUM_LIT:1> ] , <NUM_LIT:4> <EOL> ) <EOL> class Clark1989 ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls , dtype = float , conserve_memory = <NUM_LIT:0> , loglikelihood_burn = <NUM_LIT:0> ) : <EOL> cls . true = results_kalman_filter . uc_bi <EOL> cls . true_states = pd . DataFrame ( cls . true [ '<STR_LIT>' ] ) <EOL> data = pd . DataFrame ( <EOL> cls . true [ '<STR_LIT:data>' ] , <EOL> index = pd . date_range ( '<STR_LIT>' , '<STR_LIT>' , freq = '<STR_LIT>' ) , <EOL> columns = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) [ <NUM_LIT:4> : ] <EOL> data [ '<STR_LIT>' ] = np . log ( data [ '<STR_LIT>' ] ) <EOL> data [ '<STR_LIT>' ] = ( data [ '<STR_LIT>' ] / <NUM_LIT:100> ) <EOL> cls . obs = np . array ( data , ndmin = <NUM_LIT:2> , dtype = dtype , order = "<STR_LIT:C>" ) . T <EOL> cls . k_endog = k_endog = <NUM_LIT:2> <EOL> cls . k_states = k_states = <NUM_LIT:6> <EOL> cls . conserve_memory = conserve_memory <EOL> cls . loglikelihood_burn = loglikelihood_burn <EOL> cls . design = np . zeros ( ( k_endog , k_states , <NUM_LIT:1> ) , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . design [ : , : , <NUM_LIT:0> ] = [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] <EOL> cls . obs_intercept = np . zeros ( ( k_endog , <NUM_LIT:1> ) , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . obs_cov = np . zeros ( ( k_endog , k_endog , <NUM_LIT:1> ) , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . transition = np . zeros ( ( k_states , k_states , <NUM_LIT:1> ) , <EOL> dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . transition [ ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:5> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) ] = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> cls . state_intercept = np . zeros ( ( k_states , <NUM_LIT:1> ) , dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . selection = np . asfortranarray ( np . eye ( k_states ) [ : , : , None ] , <EOL> dtype = dtype ) <EOL> cls . state_cov = np . zeros ( ( k_states , k_states , <NUM_LIT:1> ) , <EOL> dtype = dtype , order = "<STR_LIT:F>" ) <EOL> cls . initial_state = np . zeros ( ( k_states , ) , dtype = dtype ) <EOL> cls . initial_state_cov = np . asfortranarray ( np . eye ( k_states ) * <NUM_LIT:100> , <EOL> dtype = dtype ) <EOL> ( sigma_v , sigma_e , sigma_w , sigma_vl , sigma_ec , <EOL> phi_1 , phi_2 , alpha_1 , alpha_2 , alpha_3 ) = np . array ( <EOL> cls . true [ '<STR_LIT>' ] , dtype = dtype <EOL> ) <EOL> cls . design [ ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) ] = [ <EOL> alpha_1 , alpha_2 , alpha_3 <EOL> ] <EOL> cls . transition [ ( [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ] = [ phi_1 , phi_2 ] <EOL> cls . obs_cov [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] = sigma_ec ** <NUM_LIT:2> <EOL> cls . state_cov [ <EOL> np . diag_indices ( k_states ) + ( np . zeros ( k_states , dtype = int ) , ) ] = [ <EOL> sigma_v ** <NUM_LIT:2> , sigma_e ** <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> , sigma_w ** <NUM_LIT:2> , sigma_vl ** <NUM_LIT:2> <EOL> ] <EOL> cls . initial_state_cov = np . asfortranarray ( <EOL> np . dot ( <EOL> np . dot ( cls . transition [ : , : , <NUM_LIT:0> ] , cls . initial_state_cov ) , <EOL> cls . transition [ : , : , <NUM_LIT:0> ] . T <EOL> ) <EOL> ) <EOL> @ classmethod <EOL> def init_filter ( cls ) : <EOL> prefix = find_best_blas_type ( ( cls . obs , ) ) <EOL> klass = prefix_statespace_map [ prefix [ <NUM_LIT:0> ] ] <EOL> model = klass ( <EOL> cls . obs , cls . design , cls . obs_intercept , cls . obs_cov , <EOL> cls . transition , cls . state_intercept , cls . selection , <EOL> cls . state_cov <EOL> ) <EOL> model . initialize_known ( cls . initial_state , cls . initial_state_cov ) <EOL> klass = prefix_kalman_filter_map [ prefix [ <NUM_LIT:0> ] ] <EOL> kfilter = klass ( model , conserve_memory = cls . conserve_memory , <EOL> loglikelihood_burn = cls . loglikelihood_burn ) <EOL> return model , kfilter <EOL> @ classmethod <EOL> def run_filter ( cls ) : <EOL> cls . filter ( ) <EOL> return { <EOL> '<STR_LIT>' : lambda burn : np . sum ( cls . filter . loglikelihood [ burn : ] ) , <EOL> '<STR_LIT:state>' : np . array ( cls . filter . filtered_state ) , <EOL> } <EOL> def test_loglike ( self ) : <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT>' ] ( <NUM_LIT:0> ) , <EOL> self . true [ '<STR_LIT>' ] , <NUM_LIT:2> <EOL> ) <EOL> def test_filtered_state ( self ) : <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:0> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:0> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:1> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:1> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:4> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:2> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:5> ] [ self . true [ '<STR_LIT:start>' ] : ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:3> ] , <NUM_LIT:4> <EOL> ) <EOL> class TestClark1989 ( Clark1989 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1989 , cls ) . setup_class ( dtype = float , conserve_memory = <NUM_LIT:0> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class TestClark1989Conserve ( Clark1989 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1989Conserve , cls ) . setup_class ( <EOL> dtype = float , conserve_memory = <NUM_LIT> | <NUM_LIT> <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class Clark1989Forecast ( Clark1989 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls , dtype = float , nforecast = <NUM_LIT:100> , conserve_memory = <NUM_LIT:0> ) : <EOL> super ( Clark1989Forecast , cls ) . setup_class ( dtype , conserve_memory ) <EOL> cls . nforecast = nforecast <EOL> cls . _obs = cls . obs <EOL> cls . obs = np . array ( <EOL> np . c_ [ <EOL> cls . _obs , <EOL> np . r_ [ [ np . nan , np . nan ] * nforecast ] . reshape ( <NUM_LIT:2> , nforecast ) <EOL> ] , <EOL> ndmin = <NUM_LIT:2> , dtype = dtype , order = "<STR_LIT:F>" <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> def test_filtered_state ( self ) : <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:0> ] [ self . true [ '<STR_LIT:start>' ] : - self . nforecast ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:0> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:1> ] [ self . true [ '<STR_LIT:start>' ] : - self . nforecast ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:1> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:4> ] [ self . true [ '<STR_LIT:start>' ] : - self . nforecast ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:2> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:5> ] [ self . true [ '<STR_LIT:start>' ] : - self . nforecast ] , <EOL> self . true_states . iloc [ : , <NUM_LIT:3> ] , <NUM_LIT:4> <EOL> ) <EOL> class TestClark1989ForecastDouble ( Clark1989Forecast ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1989ForecastDouble , cls ) . setup_class ( ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class TestClark1989ForecastDoubleComplex ( Clark1989Forecast ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1989ForecastDoubleComplex , cls ) . setup_class ( <EOL> dtype = complex <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class TestClark1989ForecastConserve ( Clark1989Forecast ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1989ForecastConserve , cls ) . setup_class ( <EOL> dtype = float , conserve_memory = <NUM_LIT> | <NUM_LIT> <EOL> ) <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> class TestClark1989ConserveAll ( Clark1989 ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> super ( TestClark1989ConserveAll , cls ) . setup_class ( <EOL> dtype = float , conserve_memory = <NUM_LIT> | <NUM_LIT> | <NUM_LIT> | <NUM_LIT> , <EOL> ) <EOL> cls . loglikelihood_burn = <NUM_LIT:0> <EOL> cls . model , cls . filter = cls . init_filter ( ) <EOL> cls . result = cls . run_filter ( ) <EOL> def test_loglike ( self ) : <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT>' ] ( <NUM_LIT:0> ) , self . true [ '<STR_LIT>' ] , <NUM_LIT:2> <EOL> ) <EOL> def test_filtered_state ( self ) : <EOL> end = self . true_states . shape [ <NUM_LIT:0> ] <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] , <EOL> self . true_states . iloc [ end - <NUM_LIT:1> , <NUM_LIT:0> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:1> ] [ - <NUM_LIT:1> ] , <EOL> self . true_states . iloc [ end - <NUM_LIT:1> , <NUM_LIT:1> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:4> ] [ - <NUM_LIT:1> ] , <EOL> self . true_states . iloc [ end - <NUM_LIT:1> , <NUM_LIT:2> ] , <NUM_LIT:4> <EOL> ) <EOL> assert_almost_equal ( <EOL> self . result [ '<STR_LIT:state>' ] [ <NUM_LIT:5> ] [ - <NUM_LIT:1> ] , <EOL> self . true_states . iloc [ end - <NUM_LIT:1> , <NUM_LIT:3> ] , <NUM_LIT:4> <EOL> ) </s>
