<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import , print_function <EOL> from warnings import warn <EOL> import numpy as np <EOL> import pandas as pd <EOL> from . kalman_filter import KalmanFilter , FilterResults <EOL> from . mlemodel import MLEModel , MLEResults , MLEResultsWrapper <EOL> from . tools import ( <EOL> companion_matrix , diff , is_invertible , constrain_stationary_univariate , <EOL> unconstrain_stationary_univariate , solve_discrete_lyapunov <EOL> ) <EOL> from statsmodels . tools . tools import Bunch <EOL> from statsmodels . tools . data import _is_using_pandas <EOL> from statsmodels . tsa . tsatools import lagmat <EOL> from statsmodels . tools . decorators import cache_readonly <EOL> import statsmodels . base . wrapper as wrap <EOL> class SARIMAX ( MLEModel ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , endog , exog = None , order = ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> seasonal_order = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , trend = None , <EOL> measurement_error = False , time_varying_regression = False , <EOL> mle_regression = True , simple_differencing = False , <EOL> enforce_stationarity = True , enforce_invertibility = True , <EOL> hamilton_representation = False , ** kwargs ) : <EOL> self . k_seasons = seasonal_order [ <NUM_LIT:3> ] <EOL> self . measurement_error = measurement_error <EOL> self . time_varying_regression = time_varying_regression <EOL> self . mle_regression = mle_regression <EOL> self . simple_differencing = simple_differencing <EOL> self . enforce_stationarity = enforce_stationarity <EOL> self . enforce_invertibility = enforce_invertibility <EOL> self . hamilton_representation = hamilton_representation <EOL> self . order = order <EOL> self . seasonal_order = seasonal_order <EOL> if self . time_varying_regression and self . mle_regression : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if isinstance ( order [ <NUM_LIT:0> ] , int ) : <EOL> self . polynomial_ar = np . r_ [ <NUM_LIT:1.> , np . ones ( order [ <NUM_LIT:0> ] ) ] <EOL> else : <EOL> self . polynomial_ar = np . r_ [ <NUM_LIT:1.> , order [ <NUM_LIT:0> ] ] <EOL> if isinstance ( order [ <NUM_LIT:2> ] , int ) : <EOL> self . polynomial_ma = np . r_ [ <NUM_LIT:1.> , np . ones ( order [ <NUM_LIT:2> ] ) ] <EOL> else : <EOL> self . polynomial_ma = np . r_ [ <NUM_LIT:1.> , order [ <NUM_LIT:2> ] ] <EOL> if isinstance ( seasonal_order [ <NUM_LIT:0> ] , int ) : <EOL> self . polynomial_seasonal_ar = np . r_ [ <EOL> <NUM_LIT:1.> , <EOL> ( [ <NUM_LIT:0> ] * ( self . k_seasons - <NUM_LIT:1> ) + [ <NUM_LIT:1> ] ) * seasonal_order [ <NUM_LIT:0> ] <EOL> ] <EOL> else : <EOL> self . polynomial_seasonal_ar = np . r_ [ <EOL> <NUM_LIT:1.> , [ <NUM_LIT:0> ] * self . k_seasons * len ( seasonal_order [ <NUM_LIT:0> ] ) <EOL> ] <EOL> for i in range ( len ( seasonal_order [ <NUM_LIT:0> ] ) ) : <EOL> self . polynomial_seasonal_ar [ ( i + <NUM_LIT:1> ) * self . k_seasons ] = ( <EOL> seasonal_order [ <NUM_LIT:0> ] [ i ] <EOL> ) <EOL> if isinstance ( seasonal_order [ <NUM_LIT:2> ] , int ) : <EOL> self . polynomial_seasonal_ma = np . r_ [ <EOL> <NUM_LIT:1.> , <EOL> ( [ <NUM_LIT:0> ] * ( self . k_seasons - <NUM_LIT:1> ) + [ <NUM_LIT:1> ] ) * seasonal_order [ <NUM_LIT:2> ] <EOL> ] <EOL> else : <EOL> self . polynomial_seasonal_ma = np . r_ [ <EOL> <NUM_LIT:1.> , [ <NUM_LIT:0> ] * self . k_seasons * len ( seasonal_order [ <NUM_LIT:2> ] ) <EOL> ] <EOL> for i in range ( len ( seasonal_order [ <NUM_LIT:2> ] ) ) : <EOL> self . polynomial_seasonal_ma [ ( i + <NUM_LIT:1> ) * self . k_seasons ] = ( <EOL> seasonal_order [ <NUM_LIT:2> ] [ i ] <EOL> ) <EOL> self . trend = trend <EOL> if trend is None or trend == '<STR_LIT:n>' : <EOL> self . polynomial_trend = np . ones ( ( <NUM_LIT:0> ) ) <EOL> elif trend == '<STR_LIT:c>' : <EOL> self . polynomial_trend = np . r_ [ <NUM_LIT:1> ] <EOL> elif trend == '<STR_LIT:t>' : <EOL> self . polynomial_trend = np . r_ [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> elif trend == '<STR_LIT>' : <EOL> self . polynomial_trend = np . r_ [ <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> else : <EOL> self . polynomial_trend = ( np . array ( trend ) > <NUM_LIT:0> ) . astype ( int ) <EOL> self . k_ar = int ( self . polynomial_ar . shape [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> self . k_ar_params = int ( np . sum ( self . polynomial_ar ) - <NUM_LIT:1> ) <EOL> self . k_diff = int ( order [ <NUM_LIT:1> ] ) <EOL> self . k_ma = int ( self . polynomial_ma . shape [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> self . k_ma_params = int ( np . sum ( self . polynomial_ma ) - <NUM_LIT:1> ) <EOL> self . k_seasonal_ar = int ( self . polynomial_seasonal_ar . shape [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> self . k_seasonal_ar_params = ( <EOL> int ( np . sum ( self . polynomial_seasonal_ar ) - <NUM_LIT:1> ) <EOL> ) <EOL> self . k_seasonal_diff = int ( seasonal_order [ <NUM_LIT:1> ] ) <EOL> self . k_seasonal_ma = int ( self . polynomial_seasonal_ma . shape [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> self . k_seasonal_ma_params = ( <EOL> int ( np . sum ( self . polynomial_seasonal_ma ) - <NUM_LIT:1> ) <EOL> ) <EOL> self . _k_diff = self . k_diff <EOL> self . _k_seasonal_diff = self . k_seasonal_diff <EOL> if ( self . hamilton_representation and not ( self . simple_differencing or <EOL> self . _k_diff == self . _k_seasonal_diff == <NUM_LIT:0> ) ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . k_trend = int ( np . sum ( self . polynomial_trend ) ) <EOL> self . _k_order = max ( self . k_ar + self . k_seasonal_ar , <EOL> self . k_ma + self . k_seasonal_ma + <NUM_LIT:1> ) <EOL> if self . _k_order == <NUM_LIT:1> and self . k_ar + self . k_seasonal_ar == <NUM_LIT:0> : <EOL> self . _k_order = <NUM_LIT:0> <EOL> self . k_exog = <NUM_LIT:0> <EOL> if exog is not None : <EOL> exog_is_using_pandas = _is_using_pandas ( exog , None ) <EOL> if not exog_is_using_pandas : <EOL> exog = np . asarray ( exog ) <EOL> if exog . ndim == <NUM_LIT:1> : <EOL> if not exog_is_using_pandas : <EOL> exog = exog [ : , None ] <EOL> else : <EOL> exog = pd . DataFrame ( exog ) <EOL> self . k_exog = exog . shape [ <NUM_LIT:1> ] <EOL> self . mle_regression = ( <EOL> self . mle_regression and exog is not None and self . k_exog > <NUM_LIT:0> <EOL> ) <EOL> self . state_regression = ( <EOL> not self . mle_regression and exog is not None and self . k_exog > <NUM_LIT:0> <EOL> ) <EOL> if self . state_regression and self . _k_order == <NUM_LIT:0> : <EOL> self . measurement_error = True <EOL> k_states = self . _k_order <EOL> if not self . simple_differencing : <EOL> k_states += self . k_seasons * self . _k_seasonal_diff + self . _k_diff <EOL> if self . state_regression : <EOL> k_states += self . k_exog <EOL> k_diffuse_states = k_states <EOL> if self . enforce_stationarity : <EOL> k_diffuse_states -= self . _k_order <EOL> k_posdef = int ( self . _k_order > <NUM_LIT:0> ) <EOL> self . state_error = k_posdef > <NUM_LIT:0> <EOL> if self . state_regression and self . time_varying_regression : <EOL> k_posdef += self . k_exog <EOL> if self . state_regression : <EOL> kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . k_params = ( <EOL> self . k_ar_params + self . k_ma_params + <EOL> self . k_seasonal_ar_params + self . k_seasonal_ar_params + <EOL> self . k_trend + <EOL> self . measurement_error + <NUM_LIT:1> <EOL> ) <EOL> if self . mle_regression : <EOL> self . k_params += self . k_exog <EOL> self . orig_endog = endog <EOL> self . orig_exog = exog <EOL> if not _is_using_pandas ( endog , None ) : <EOL> endog = np . asanyarray ( endog ) <EOL> self . orig_k_diff = self . _k_diff <EOL> self . orig_k_seasonal_diff = self . _k_seasonal_diff <EOL> if ( self . simple_differencing and <EOL> ( self . _k_diff > <NUM_LIT:0> or self . _k_seasonal_diff > <NUM_LIT:0> ) ) : <EOL> self . _k_diff = <NUM_LIT:0> <EOL> self . _k_seasonal_diff = <NUM_LIT:0> <EOL> self . _k_states_diff = ( <EOL> self . _k_diff + self . k_seasons * self . _k_seasonal_diff <EOL> ) <EOL> self . nobs = len ( endog ) <EOL> self . k_states = k_states <EOL> self . k_posdef = k_posdef <EOL> kwargs . setdefault ( '<STR_LIT>' , k_diffuse_states ) <EOL> super ( SARIMAX , self ) . __init__ ( <EOL> endog , exog = exog , k_states = k_states , k_posdef = k_posdef , ** kwargs <EOL> ) <EOL> if self . ssm . initialization is not None : <EOL> self . _manual_initialization = True <EOL> self . ssm . design = self . initial_design <EOL> self . ssm . state_intercept = self . initial_state_intercept <EOL> self . ssm . transition = self . initial_transition <EOL> self . ssm . selection = self . initial_selection <EOL> if k_diffuse_states == <NUM_LIT:0> and not self . _manual_initialization : <EOL> self . initialize_stationary ( ) <EOL> self . _init_keys += [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] + list ( kwargs . keys ( ) ) <EOL> def _get_init_kwds ( self ) : <EOL> kwds = super ( SARIMAX , self ) . _get_init_kwds ( ) <EOL> for key , value in kwds . items ( ) : <EOL> if value is None and hasattr ( self . ssm , key ) : <EOL> kwds [ key ] = getattr ( self . ssm , key ) <EOL> return kwds <EOL> def prepare_data ( self ) : <EOL> endog , exog = super ( SARIMAX , self ) . prepare_data ( ) <EOL> if ( self . simple_differencing and <EOL> ( self . orig_k_diff > <NUM_LIT:0> or self . orig_k_seasonal_diff > <NUM_LIT:0> ) ) : <EOL> endog = diff ( endog . copy ( ) , self . orig_k_diff , <EOL> self . orig_k_seasonal_diff , self . k_seasons ) <EOL> if exog is not None : <EOL> exog = diff ( exog . copy ( ) , self . orig_k_diff , <EOL> self . orig_k_seasonal_diff , self . k_seasons ) <EOL> self . data . endog , self . data . exog = ( <EOL> self . data . _convert_endog_exog ( endog , exog ) ) <EOL> self . nobs = endog . shape [ <NUM_LIT:0> ] <EOL> time_trend = np . arange ( <NUM_LIT:1> , self . nobs + <NUM_LIT:1> ) <EOL> self . _trend_data = np . zeros ( ( self . nobs , self . k_trend ) ) <EOL> i = <NUM_LIT:0> <EOL> for k in self . polynomial_trend . nonzero ( ) [ <NUM_LIT:0> ] : <EOL> if k == <NUM_LIT:0> : <EOL> self . _trend_data [ : , i ] = np . ones ( self . nobs , ) <EOL> else : <EOL> self . _trend_data [ : , i ] = time_trend ** k <EOL> i += <NUM_LIT:1> <EOL> return endog , exog <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( SARIMAX , self ) . initialize ( ) <EOL> self . _manual_initialization = False <EOL> self . _polynomial_ar_idx = np . nonzero ( self . polynomial_ar ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] <EOL> self . _polynomial_ma_idx = np . nonzero ( self . polynomial_ma ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] <EOL> self . _polynomial_seasonal_ar_idx = np . nonzero ( <EOL> self . polynomial_seasonal_ar <EOL> ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] <EOL> self . _polynomial_seasonal_ma_idx = np . nonzero ( <EOL> self . polynomial_seasonal_ma <EOL> ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] <EOL> start_row = self . _k_states_diff <EOL> end_row = start_row + self . k_ar + self . k_seasonal_ar <EOL> col = self . _k_states_diff <EOL> if not self . hamilton_representation : <EOL> self . transition_ar_params_idx = ( <EOL> np . s_ [ '<STR_LIT>' , start_row : end_row , col ] <EOL> ) <EOL> else : <EOL> self . transition_ar_params_idx = ( <EOL> np . s_ [ '<STR_LIT>' , col , start_row : end_row ] <EOL> ) <EOL> start_row += <NUM_LIT:1> <EOL> end_row = start_row + self . k_ma + self . k_seasonal_ma <EOL> col = <NUM_LIT:0> <EOL> if not self . hamilton_representation : <EOL> self . selection_ma_params_idx = ( <EOL> np . s_ [ '<STR_LIT>' , start_row : end_row , col ] <EOL> ) <EOL> else : <EOL> self . design_ma_params_idx = ( <EOL> np . s_ [ '<STR_LIT>' , col , start_row : end_row ] <EOL> ) <EOL> if self . state_regression and self . time_varying_regression : <EOL> idx = np . diag_indices ( self . k_posdef ) <EOL> self . _exog_variance_idx = ( '<STR_LIT>' , idx [ <NUM_LIT:0> ] [ - self . k_exog : ] , <EOL> idx [ <NUM_LIT:1> ] [ - self . k_exog : ] ) <EOL> def initialize_known ( self , initial_state , initial_state_cov ) : <EOL> self . _manual_initialization = True <EOL> self . ssm . initialize_known ( initial_state , initial_state_cov ) <EOL> initialize_known . __doc__ = KalmanFilter . initialize_known . __doc__ <EOL> def initialize_approximate_diffuse ( self , variance = None ) : <EOL> self . _manual_initialization = True <EOL> self . ssm . initialize_approximate_diffuse ( variance ) <EOL> initialize_approximate_diffuse . __doc__ = ( <EOL> KalmanFilter . initialize_approximate_diffuse . __doc__ <EOL> ) <EOL> def initialize_stationary ( self ) : <EOL> self . _manual_initialization = True <EOL> self . ssm . initialize_stationary ( ) <EOL> initialize_stationary . __doc__ = ( <EOL> KalmanFilter . initialize_stationary . __doc__ <EOL> ) <EOL> def initialize_state ( self , variance = None , complex_step = False ) : <EOL> """<STR_LIT>""" <EOL> if self . _manual_initialization : <EOL> return <EOL> if not self . enforce_stationarity : <EOL> self . initialize_approximate_diffuse ( variance ) <EOL> return <EOL> if variance is None : <EOL> variance = self . ssm . initial_variance <EOL> dtype = self . ssm . transition . dtype <EOL> initial_state = np . zeros ( self . k_states , dtype = dtype ) <EOL> initial_state_cov = np . eye ( self . k_states , dtype = dtype ) * variance <EOL> if self . state_regression : <EOL> start = - ( self . k_exog + self . _k_order ) <EOL> end = - self . k_exog if self . k_exog > <NUM_LIT:0> else None <EOL> else : <EOL> start = - self . _k_order <EOL> end = None <EOL> if self . _k_order > <NUM_LIT:0> : <EOL> selection_stationary = self . ssm . selection [ start : end , : , <NUM_LIT:0> ] <EOL> selected_state_cov_stationary = np . dot ( <EOL> np . dot ( selection_stationary , self . ssm . state_cov [ : , : , <NUM_LIT:0> ] ) , <EOL> selection_stationary . T <EOL> ) <EOL> initial_state_cov_stationary = solve_discrete_lyapunov ( <EOL> self . ssm . transition [ start : end , start : end , <NUM_LIT:0> ] , <EOL> selected_state_cov_stationary , <EOL> complex_step = complex_step <EOL> ) <EOL> initial_state_cov [ start : end , start : end ] = ( <EOL> initial_state_cov_stationary <EOL> ) <EOL> self . ssm . initialize_known ( initial_state , initial_state_cov ) <EOL> @ property <EOL> def initial_design ( self ) : <EOL> """<STR_LIT>""" <EOL> design = np . r_ [ <EOL> [ <NUM_LIT:1> ] * self . _k_diff , <EOL> ( [ <NUM_LIT:0> ] * ( self . k_seasons - <NUM_LIT:1> ) + [ <NUM_LIT:1> ] ) * self . _k_seasonal_diff , <EOL> [ <NUM_LIT:1> ] * self . state_error , [ <NUM_LIT:0> ] * ( self . _k_order - <NUM_LIT:1> ) <EOL> ] <EOL> if self . state_regression : <EOL> if self . _k_order > <NUM_LIT:0> : <EOL> design = np . c_ [ <EOL> np . reshape ( <EOL> np . repeat ( design , self . nobs ) , <EOL> ( design . shape [ <NUM_LIT:0> ] , self . nobs ) <EOL> ) . T , <EOL> self . exog <EOL> ] . T [ None , : , : ] <EOL> else : <EOL> design = self . exog . T [ None , : , : ] <EOL> return design <EOL> @ property <EOL> def initial_state_intercept ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . k_trend > <NUM_LIT:0> : <EOL> state_intercept = np . zeros ( ( self . k_states , self . nobs ) ) <EOL> else : <EOL> state_intercept = np . zeros ( ( self . k_states , ) ) <EOL> return state_intercept <EOL> @ property <EOL> def initial_transition ( self ) : <EOL> """<STR_LIT>""" <EOL> transition = np . zeros ( ( self . k_states , self . k_states ) ) <EOL> if self . state_regression : <EOL> start = - self . k_exog <EOL> transition [ start : , start : ] = np . eye ( self . k_exog ) <EOL> start = - ( self . k_exog + self . _k_order ) <EOL> end = - self . k_exog if self . k_exog > <NUM_LIT:0> else None <EOL> else : <EOL> start = - self . _k_order <EOL> end = None <EOL> transition [ start : end , start : end ] = companion_matrix ( self . _k_order ) <EOL> if self . hamilton_representation : <EOL> transition [ start : end , start : end ] = np . transpose ( <EOL> companion_matrix ( self . _k_order ) <EOL> ) <EOL> if self . _k_seasonal_diff > <NUM_LIT:0> : <EOL> seasonal_companion = companion_matrix ( self . k_seasons ) . T <EOL> seasonal_companion [ <NUM_LIT:0> , - <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> for d in range ( self . _k_seasonal_diff ) : <EOL> start = self . _k_diff + d * self . k_seasons <EOL> end = self . _k_diff + ( d + <NUM_LIT:1> ) * self . k_seasons <EOL> transition [ start : end , start : end ] = seasonal_companion <EOL> for i in range ( d + <NUM_LIT:1> , self . _k_seasonal_diff ) : <EOL> transition [ start , end + self . k_seasons - <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> transition [ start , self . _k_states_diff ] = <NUM_LIT:1> <EOL> if self . _k_diff > <NUM_LIT:0> : <EOL> idx = np . triu_indices ( self . _k_diff ) <EOL> transition [ idx ] = <NUM_LIT:1> <EOL> if self . k_seasons > <NUM_LIT:0> : <EOL> start = self . _k_diff <EOL> end = self . _k_states_diff <EOL> transition [ : self . _k_diff , start : end ] = ( <EOL> ( [ <NUM_LIT:0> ] * ( self . k_seasons - <NUM_LIT:1> ) + [ <NUM_LIT:1> ] ) * self . _k_seasonal_diff <EOL> ) <EOL> column = self . _k_states_diff <EOL> transition [ : self . _k_diff , column ] = <NUM_LIT:1> <EOL> return transition <EOL> @ property <EOL> def initial_selection ( self ) : <EOL> """<STR_LIT>""" <EOL> if not ( self . state_regression and self . time_varying_regression ) : <EOL> if self . k_posdef > <NUM_LIT:0> : <EOL> selection = np . r_ [ <EOL> [ <NUM_LIT:0> ] * ( self . _k_states_diff ) , <EOL> [ <NUM_LIT:1> ] * ( self . _k_order > <NUM_LIT:0> ) , [ <NUM_LIT:0> ] * ( self . _k_order - <NUM_LIT:1> ) , <EOL> [ <NUM_LIT:0> ] * ( ( <NUM_LIT:1> - self . mle_regression ) * self . k_exog ) <EOL> ] [ : , None ] <EOL> else : <EOL> selection = np . zeros ( ( self . k_states , <NUM_LIT:0> ) ) <EOL> else : <EOL> selection = np . zeros ( ( self . k_states , self . k_posdef ) ) <EOL> if self . _k_order > <NUM_LIT:0> : <EOL> selection [ <NUM_LIT:0> , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> for i in range ( self . k_exog , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> selection [ - i , - i ] = <NUM_LIT:1> <EOL> return selection <EOL> def filter ( self , params , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , SARIMAXResults ) <EOL> kwargs . setdefault ( '<STR_LIT>' , SARIMAXResultsWrapper ) <EOL> return super ( SARIMAX , self ) . filter ( params , ** kwargs ) <EOL> def smooth ( self , params , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , SARIMAXResults ) <EOL> kwargs . setdefault ( '<STR_LIT>' , SARIMAXResultsWrapper ) <EOL> return super ( SARIMAX , self ) . smooth ( params , ** kwargs ) <EOL> @ staticmethod <EOL> def _conditional_sum_squares ( endog , k_ar , polynomial_ar , k_ma , <EOL> polynomial_ma , k_trend = <NUM_LIT:0> , trend_data = None ) : <EOL> k = <NUM_LIT:2> * k_ma <EOL> r = max ( k + k_ma , k_ar ) <EOL> k_params_ar = <NUM_LIT:0> if k_ar == <NUM_LIT:0> else len ( polynomial_ar . nonzero ( ) [ <NUM_LIT:0> ] ) - <NUM_LIT:1> <EOL> k_params_ma = <NUM_LIT:0> if k_ma == <NUM_LIT:0> else len ( polynomial_ma . nonzero ( ) [ <NUM_LIT:0> ] ) - <NUM_LIT:1> <EOL> residuals = None <EOL> if k_ar + k_ma + k_trend > <NUM_LIT:0> : <EOL> if k_ma > <NUM_LIT:0> : <EOL> Y = endog [ k : ] <EOL> X = lagmat ( endog , k , trim = '<STR_LIT>' ) <EOL> params_ar = np . linalg . pinv ( X ) . dot ( Y ) <EOL> residuals = Y - np . dot ( X , params_ar ) <EOL> Y = endog [ r : ] <EOL> X = np . empty ( ( Y . shape [ <NUM_LIT:0> ] , <NUM_LIT:0> ) ) <EOL> if k_trend > <NUM_LIT:0> : <EOL> if trend_data is None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> X = np . c_ [ X , trend_data [ : ( - r if r > <NUM_LIT:0> else None ) , : ] ] <EOL> if k_ar > <NUM_LIT:0> : <EOL> cols = polynomial_ar . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] - <NUM_LIT:1> <EOL> X = np . c_ [ X , lagmat ( endog , k_ar ) [ r : , cols ] ] <EOL> if k_ma > <NUM_LIT:0> : <EOL> cols = polynomial_ma . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] - <NUM_LIT:1> <EOL> X = np . c_ [ X , lagmat ( residuals , k_ma ) [ r - k : , cols ] ] <EOL> params = np . linalg . pinv ( X ) . dot ( Y ) <EOL> residuals = Y - np . dot ( X , params ) <EOL> params_trend = [ ] <EOL> params_ar = [ ] <EOL> params_ma = [ ] <EOL> params_variance = [ ] <EOL> offset = <NUM_LIT:0> <EOL> if k_trend > <NUM_LIT:0> : <EOL> params_trend = params [ offset : k_trend + offset ] <EOL> offset += k_trend <EOL> if k_ar > <NUM_LIT:0> : <EOL> params_ar = params [ offset : k_params_ar + offset ] <EOL> offset += k_params_ar <EOL> if k_ma > <NUM_LIT:0> : <EOL> params_ma = params [ offset : k_params_ma + offset ] <EOL> offset += k_params_ma <EOL> if residuals is not None : <EOL> params_variance = ( residuals [ k_params_ma : ] ** <NUM_LIT:2> ) . mean ( ) <EOL> return ( params_trend , params_ar , params_ma , <EOL> params_variance ) <EOL> @ property <EOL> def start_params ( self ) : <EOL> """<STR_LIT>""" <EOL> trend_data = self . _trend_data <EOL> if not self . simple_differencing and ( <EOL> self . _k_diff > <NUM_LIT:0> or self . _k_seasonal_diff > <NUM_LIT:0> ) : <EOL> endog = diff ( self . endog , self . _k_diff , <EOL> self . _k_seasonal_diff , self . k_seasons ) <EOL> if self . exog is not None : <EOL> exog = diff ( self . exog , self . _k_diff , <EOL> self . _k_seasonal_diff , self . k_seasons ) <EOL> else : <EOL> exog = None <EOL> trend_data = trend_data [ : endog . shape [ <NUM_LIT:0> ] , : ] <EOL> else : <EOL> endog = self . endog . copy ( ) <EOL> exog = self . exog . copy ( ) if self . exog is not None else None <EOL> endog = endog . squeeze ( ) <EOL> if np . any ( np . isnan ( endog ) ) : <EOL> mask = ~ np . isnan ( endog ) <EOL> endog = endog [ mask ] <EOL> if exog is not None : <EOL> exog = exog [ mask ] <EOL> if trend_data is not None : <EOL> trend_data = trend_data [ mask ] <EOL> params_exog = [ ] <EOL> if self . k_exog > <NUM_LIT:0> : <EOL> params_exog = np . linalg . pinv ( exog ) . dot ( endog ) <EOL> endog -= np . dot ( exog , params_exog ) <EOL> if self . state_regression : <EOL> params_exog = [ ] <EOL> ( params_trend , params_ar , params_ma , <EOL> params_variance ) = self . _conditional_sum_squares ( <EOL> endog , self . k_ar , self . polynomial_ar , self . k_ma , <EOL> self . polynomial_ma , self . k_trend , trend_data <EOL> ) <EOL> invalid_ar = ( <EOL> self . k_ar > <NUM_LIT:0> and <EOL> self . enforce_stationarity and <EOL> not is_invertible ( np . r_ [ <NUM_LIT:1> , - params_ar ] ) <EOL> ) <EOL> if invalid_ar : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> invalid_ma = ( <EOL> self . k_ma > <NUM_LIT:0> and <EOL> self . enforce_invertibility and <EOL> not is_invertible ( np . r_ [ <NUM_LIT:1> , params_ma ] ) <EOL> ) <EOL> if invalid_ma : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> _ , params_seasonal_ar , params_seasonal_ma , params_seasonal_variance = ( <EOL> self . _conditional_sum_squares ( <EOL> endog , self . k_seasonal_ar , self . polynomial_seasonal_ar , <EOL> self . k_seasonal_ma , self . polynomial_seasonal_ma <EOL> ) <EOL> ) <EOL> invalid_seasonal_ar = ( <EOL> self . k_seasonal_ar > <NUM_LIT:0> and <EOL> self . enforce_stationarity and <EOL> not is_invertible ( np . r_ [ <NUM_LIT:1> , - params_seasonal_ar ] ) <EOL> ) <EOL> if invalid_seasonal_ar : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> invalid_seasonal_ma = ( <EOL> self . k_seasonal_ma > <NUM_LIT:0> and <EOL> self . enforce_invertibility and <EOL> not is_invertible ( np . r_ [ <NUM_LIT:1> , params_seasonal_ma ] ) <EOL> ) <EOL> if invalid_seasonal_ma : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> params_exog_variance = [ ] <EOL> if self . state_regression and self . time_varying_regression : <EOL> params_exog_variance = [ <NUM_LIT:1> ] * self . k_exog <EOL> if self . state_error and params_variance == [ ] : <EOL> if not params_seasonal_variance == [ ] : <EOL> params_variance = params_seasonal_variance <EOL> elif self . k_exog > <NUM_LIT:0> : <EOL> params_variance = np . dot ( endog , endog ) <EOL> else : <EOL> params_variance = <NUM_LIT:1> <EOL> params_measurement_variance = <NUM_LIT:1> if self . measurement_error else [ ] <EOL> return np . r_ [ <EOL> params_trend , <EOL> params_exog , <EOL> params_ar , <EOL> params_ma , <EOL> params_seasonal_ar , <EOL> params_seasonal_ma , <EOL> params_exog_variance , <EOL> params_measurement_variance , <EOL> params_variance <EOL> ] <EOL> @ property <EOL> def endog_names ( self , latex = False ) : <EOL> """<STR_LIT>""" <EOL> diff = '<STR_LIT>' <EOL> if self . k_diff > <NUM_LIT:0> : <EOL> if self . k_diff == <NUM_LIT:1> : <EOL> diff = '<STR_LIT>' if latex else '<STR_LIT:D>' <EOL> else : <EOL> diff = ( '<STR_LIT>' if latex else '<STR_LIT>' ) % self . k_diff <EOL> seasonal_diff = '<STR_LIT>' <EOL> if self . k_seasonal_diff > <NUM_LIT:0> : <EOL> if self . k_seasonal_diff == <NUM_LIT:1> : <EOL> seasonal_diff = ( ( '<STR_LIT>' if latex else '<STR_LIT>' ) % <EOL> ( self . k_seasons ) ) <EOL> else : <EOL> seasonal_diff = ( ( '<STR_LIT>' if latex else '<STR_LIT>' ) % <EOL> ( self . k_seasonal_diff , self . k_seasons ) ) <EOL> endog_diff = self . simple_differencing <EOL> if endog_diff and self . k_diff > <NUM_LIT:0> and self . k_seasonal_diff > <NUM_LIT:0> : <EOL> return ( ( '<STR_LIT>' if latex else '<STR_LIT>' ) % <EOL> ( diff , seasonal_diff , self . data . ynames ) ) <EOL> elif endog_diff and self . k_diff > <NUM_LIT:0> : <EOL> return ( ( '<STR_LIT>' if latex else '<STR_LIT>' ) % <EOL> ( diff , self . data . ynames ) ) <EOL> elif endog_diff and self . k_seasonal_diff > <NUM_LIT:0> : <EOL> return ( ( '<STR_LIT>' if latex else '<STR_LIT>' ) % <EOL> ( seasonal_diff , self . data . ynames ) ) <EOL> else : <EOL> return self . data . ynames <EOL> params_complete = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> @ property <EOL> def param_terms ( self ) : <EOL> """<STR_LIT>""" <EOL> model_orders = self . model_orders <EOL> params = [ <EOL> order for order in self . params_complete <EOL> if model_orders [ order ] > <NUM_LIT:0> <EOL> ] <EOL> if '<STR_LIT>' in params and not self . mle_regression : <EOL> params . remove ( '<STR_LIT>' ) <EOL> return params <EOL> @ property <EOL> def param_names ( self ) : <EOL> """<STR_LIT>""" <EOL> params_sort_order = self . param_terms <EOL> model_names = self . model_names <EOL> return [ <EOL> name for param in params_sort_order for name in model_names [ param ] <EOL> ] <EOL> @ property <EOL> def model_orders ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : self . k_trend , <EOL> '<STR_LIT>' : self . k_exog , <EOL> '<STR_LIT>' : self . k_ar , <EOL> '<STR_LIT>' : self . k_ma , <EOL> '<STR_LIT>' : self . k_seasonal_ar , <EOL> '<STR_LIT>' : self . k_seasonal_ma , <EOL> '<STR_LIT>' : self . k_ar + self . k_seasonal_ar , <EOL> '<STR_LIT>' : self . k_ma + self . k_seasonal_ma , <EOL> '<STR_LIT>' : self . k_exog if ( <EOL> self . state_regression and self . time_varying_regression ) else <NUM_LIT:0> , <EOL> '<STR_LIT>' : int ( self . measurement_error ) , <EOL> '<STR_LIT>' : int ( self . state_error ) , <EOL> } <EOL> @ property <EOL> def model_names ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_model_names ( latex = False ) <EOL> @ property <EOL> def model_latex_names ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_model_names ( latex = True ) <EOL> def _get_model_names ( self , latex = False ) : <EOL> names = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> if self . k_trend > <NUM_LIT:0> : <EOL> trend_template = '<STR_LIT>' if latex else '<STR_LIT>' <EOL> names [ '<STR_LIT>' ] = [ ] <EOL> for i in self . polynomial_trend . nonzero ( ) [ <NUM_LIT:0> ] : <EOL> if i == <NUM_LIT:0> : <EOL> names [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> elif i == <NUM_LIT:1> : <EOL> names [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> else : <EOL> names [ '<STR_LIT>' ] . append ( trend_template % i ) <EOL> if self . k_exog > <NUM_LIT:0> : <EOL> names [ '<STR_LIT>' ] = self . exog_names <EOL> if self . k_ar > <NUM_LIT:0> : <EOL> ar_template = '<STR_LIT>' if latex else '<STR_LIT>' <EOL> names [ '<STR_LIT>' ] = [ ] <EOL> for i in self . polynomial_ar . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] : <EOL> names [ '<STR_LIT>' ] . append ( ar_template % i ) <EOL> if self . k_ma > <NUM_LIT:0> : <EOL> ma_template = '<STR_LIT>' if latex else '<STR_LIT>' <EOL> names [ '<STR_LIT>' ] = [ ] <EOL> for i in self . polynomial_ma . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] : <EOL> names [ '<STR_LIT>' ] . append ( ma_template % i ) <EOL> if self . k_seasonal_ar > <NUM_LIT:0> : <EOL> seasonal_ar_template = ( <EOL> '<STR_LIT>' if latex else '<STR_LIT>' <EOL> ) <EOL> names [ '<STR_LIT>' ] = [ ] <EOL> for i in self . polynomial_seasonal_ar . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] : <EOL> names [ '<STR_LIT>' ] . append ( seasonal_ar_template % i ) <EOL> if self . k_seasonal_ma > <NUM_LIT:0> : <EOL> seasonal_ma_template = ( <EOL> '<STR_LIT>' if latex else '<STR_LIT>' <EOL> ) <EOL> names [ '<STR_LIT>' ] = [ ] <EOL> for i in self . polynomial_seasonal_ma . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] : <EOL> names [ '<STR_LIT>' ] . append ( seasonal_ma_template % i ) <EOL> if self . k_ar > <NUM_LIT:0> or self . k_seasonal_ar > <NUM_LIT:0> : <EOL> reduced_polynomial_ar = reduced_polynomial_ar = - np . polymul ( <EOL> self . polynomial_ar , self . polynomial_seasonal_ar <EOL> ) <EOL> ar_template = '<STR_LIT>' if latex else '<STR_LIT>' <EOL> names [ '<STR_LIT>' ] = [ ] <EOL> for i in reduced_polynomial_ar . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] : <EOL> names [ '<STR_LIT>' ] . append ( ar_template % i ) <EOL> if self . k_ma > <NUM_LIT:0> or self . k_seasonal_ma > <NUM_LIT:0> : <EOL> reduced_polynomial_ma = np . polymul ( <EOL> self . polynomial_ma , self . polynomial_seasonal_ma <EOL> ) <EOL> ma_template = '<STR_LIT>' if latex else '<STR_LIT>' <EOL> names [ '<STR_LIT>' ] = [ ] <EOL> for i in reduced_polynomial_ma . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] : <EOL> names [ '<STR_LIT>' ] . append ( ma_template % i ) <EOL> if self . state_regression and self . time_varying_regression : <EOL> exog_var_template = '<STR_LIT>' if latex else '<STR_LIT>' <EOL> names [ '<STR_LIT>' ] = [ <EOL> exog_var_template % exog_name for exog_name in self . exog_names <EOL> ] <EOL> if self . measurement_error : <EOL> meas_var_tpl = ( <EOL> '<STR_LIT>' if latex else '<STR_LIT>' <EOL> ) <EOL> names [ '<STR_LIT>' ] = [ meas_var_tpl ] <EOL> if self . state_error : <EOL> var_tpl = '<STR_LIT>' if latex else '<STR_LIT>' <EOL> names [ '<STR_LIT>' ] = [ var_tpl ] <EOL> return names <EOL> def transform_params ( self , unconstrained ) : <EOL> """<STR_LIT>""" <EOL> unconstrained = np . array ( unconstrained , ndmin = <NUM_LIT:1> ) <EOL> constrained = np . zeros ( unconstrained . shape , unconstrained . dtype ) <EOL> start = end = <NUM_LIT:0> <EOL> if self . k_trend > <NUM_LIT:0> : <EOL> end += self . k_trend <EOL> constrained [ start : end ] = unconstrained [ start : end ] <EOL> start += self . k_trend <EOL> if self . mle_regression : <EOL> end += self . k_exog <EOL> constrained [ start : end ] = unconstrained [ start : end ] <EOL> start += self . k_exog <EOL> if self . k_ar_params > <NUM_LIT:0> : <EOL> end += self . k_ar_params <EOL> if self . enforce_stationarity : <EOL> constrained [ start : end ] = ( <EOL> constrain_stationary_univariate ( unconstrained [ start : end ] ) <EOL> ) <EOL> else : <EOL> constrained [ start : end ] = unconstrained [ start : end ] <EOL> start += self . k_ar_params <EOL> if self . k_ma_params > <NUM_LIT:0> : <EOL> end += self . k_ma_params <EOL> if self . enforce_invertibility : <EOL> constrained [ start : end ] = ( <EOL> constrain_stationary_univariate ( unconstrained [ start : end ] ) <EOL> ) <EOL> else : <EOL> constrained [ start : end ] = unconstrained [ start : end ] <EOL> start += self . k_ma_params <EOL> if self . k_seasonal_ar > <NUM_LIT:0> : <EOL> end += self . k_seasonal_ar_params <EOL> if self . enforce_stationarity : <EOL> constrained [ start : end ] = ( <EOL> constrain_stationary_univariate ( unconstrained [ start : end ] ) <EOL> ) <EOL> else : <EOL> constrained [ start : end ] = unconstrained [ start : end ] <EOL> start += self . k_seasonal_ar_params <EOL> if self . k_seasonal_ma_params > <NUM_LIT:0> : <EOL> end += self . k_seasonal_ma_params <EOL> if self . enforce_invertibility : <EOL> constrained [ start : end ] = ( <EOL> constrain_stationary_univariate ( unconstrained [ start : end ] ) <EOL> ) <EOL> else : <EOL> constrained [ start : end ] = unconstrained [ start : end ] <EOL> start += self . k_seasonal_ma_params <EOL> if self . state_regression and self . time_varying_regression : <EOL> end += self . k_exog <EOL> constrained [ start : end ] = unconstrained [ start : end ] ** <NUM_LIT:2> <EOL> start += self . k_exog <EOL> if self . measurement_error : <EOL> constrained [ start ] = unconstrained [ start ] ** <NUM_LIT:2> <EOL> start += <NUM_LIT:1> <EOL> end += <NUM_LIT:1> <EOL> if self . state_error : <EOL> constrained [ start ] = unconstrained [ start ] ** <NUM_LIT:2> <EOL> return constrained <EOL> def untransform_params ( self , constrained ) : <EOL> """<STR_LIT>""" <EOL> constrained = np . array ( constrained , ndmin = <NUM_LIT:1> ) <EOL> unconstrained = np . zeros ( constrained . shape , constrained . dtype ) <EOL> start = end = <NUM_LIT:0> <EOL> if self . k_trend > <NUM_LIT:0> : <EOL> end += self . k_trend <EOL> unconstrained [ start : end ] = constrained [ start : end ] <EOL> start += self . k_trend <EOL> if self . mle_regression : <EOL> end += self . k_exog <EOL> unconstrained [ start : end ] = constrained [ start : end ] <EOL> start += self . k_exog <EOL> if self . k_ar_params > <NUM_LIT:0> : <EOL> end += self . k_ar_params <EOL> if self . enforce_stationarity : <EOL> unconstrained [ start : end ] = ( <EOL> unconstrain_stationary_univariate ( constrained [ start : end ] ) <EOL> ) <EOL> else : <EOL> unconstrained [ start : end ] = constrained [ start : end ] <EOL> start += self . k_ar_params <EOL> if self . k_ma_params > <NUM_LIT:0> : <EOL> end += self . k_ma_params <EOL> if self . enforce_invertibility : <EOL> unconstrained [ start : end ] = ( <EOL> unconstrain_stationary_univariate ( constrained [ start : end ] ) <EOL> ) <EOL> else : <EOL> unconstrained [ start : end ] = constrained [ start : end ] <EOL> start += self . k_ma_params <EOL> if self . k_seasonal_ar > <NUM_LIT:0> : <EOL> end += self . k_seasonal_ar_params <EOL> if self . enforce_stationarity : <EOL> unconstrained [ start : end ] = ( <EOL> unconstrain_stationary_univariate ( constrained [ start : end ] ) <EOL> ) <EOL> else : <EOL> unconstrained [ start : end ] = constrained [ start : end ] <EOL> start += self . k_seasonal_ar_params <EOL> if self . k_seasonal_ma_params > <NUM_LIT:0> : <EOL> end += self . k_seasonal_ma_params <EOL> if self . enforce_invertibility : <EOL> unconstrained [ start : end ] = ( <EOL> unconstrain_stationary_univariate ( constrained [ start : end ] ) <EOL> ) <EOL> else : <EOL> unconstrained [ start : end ] = constrained [ start : end ] <EOL> start += self . k_seasonal_ma_params <EOL> if self . state_regression and self . time_varying_regression : <EOL> end += self . k_exog <EOL> unconstrained [ start : end ] = constrained [ start : end ] ** <NUM_LIT:0.5> <EOL> start += self . k_exog <EOL> if self . measurement_error : <EOL> unconstrained [ start ] = constrained [ start ] ** <NUM_LIT:0.5> <EOL> start += <NUM_LIT:1> <EOL> end += <NUM_LIT:1> <EOL> if self . state_error : <EOL> unconstrained [ start ] = constrained [ start ] ** <NUM_LIT:0.5> <EOL> return unconstrained <EOL> def update ( self , params , transformed = True , complex_step = False ) : <EOL> """<STR_LIT>""" <EOL> params = super ( SARIMAX , self ) . update ( params , transformed = transformed , <EOL> complex_step = False ) <EOL> params_trend = None <EOL> params_exog = None <EOL> params_ar = None <EOL> params_ma = None <EOL> params_seasonal_ar = None <EOL> params_seasonal_ma = None <EOL> params_exog_variance = None <EOL> params_measurement_variance = None <EOL> params_variance = None <EOL> start = end = <NUM_LIT:0> <EOL> end += self . k_trend <EOL> params_trend = params [ start : end ] <EOL> start += self . k_trend <EOL> if self . mle_regression : <EOL> end += self . k_exog <EOL> params_exog = params [ start : end ] <EOL> start += self . k_exog <EOL> end += self . k_ar_params <EOL> params_ar = params [ start : end ] <EOL> start += self . k_ar_params <EOL> end += self . k_ma_params <EOL> params_ma = params [ start : end ] <EOL> start += self . k_ma_params <EOL> end += self . k_seasonal_ar_params <EOL> params_seasonal_ar = params [ start : end ] <EOL> start += self . k_seasonal_ar_params <EOL> end += self . k_seasonal_ma_params <EOL> params_seasonal_ma = params [ start : end ] <EOL> start += self . k_seasonal_ma_params <EOL> if self . state_regression and self . time_varying_regression : <EOL> end += self . k_exog <EOL> params_exog_variance = params [ start : end ] <EOL> start += self . k_exog <EOL> if self . measurement_error : <EOL> params_measurement_variance = params [ start ] <EOL> start += <NUM_LIT:1> <EOL> end += <NUM_LIT:1> <EOL> if self . state_error : <EOL> params_variance = params [ start ] <EOL> if self . k_ar > <NUM_LIT:0> : <EOL> if self . polynomial_ar . dtype == params . dtype : <EOL> self . polynomial_ar [ self . _polynomial_ar_idx ] = - params_ar <EOL> else : <EOL> polynomial_ar = self . polynomial_ar . real . astype ( params . dtype ) <EOL> polynomial_ar [ self . _polynomial_ar_idx ] = - params_ar <EOL> self . polynomial_ar = polynomial_ar <EOL> if self . k_ma > <NUM_LIT:0> : <EOL> if self . polynomial_ma . dtype == params . dtype : <EOL> self . polynomial_ma [ self . _polynomial_ma_idx ] = params_ma <EOL> else : <EOL> polynomial_ma = self . polynomial_ma . real . astype ( params . dtype ) <EOL> polynomial_ma [ self . _polynomial_ma_idx ] = params_ma <EOL> self . polynomial_ma = polynomial_ma <EOL> if self . k_seasonal_ar > <NUM_LIT:0> : <EOL> idx = self . _polynomial_seasonal_ar_idx <EOL> if self . polynomial_seasonal_ar . dtype == params . dtype : <EOL> self . polynomial_seasonal_ar [ idx ] = - params_seasonal_ar <EOL> else : <EOL> polynomial_seasonal_ar = ( <EOL> self . polynomial_seasonal_ar . real . astype ( params . dtype ) <EOL> ) <EOL> polynomial_seasonal_ar [ idx ] = - params_seasonal_ar <EOL> self . polynomial_seasonal_ar = polynomial_seasonal_ar <EOL> if self . k_seasonal_ma > <NUM_LIT:0> : <EOL> idx = self . _polynomial_seasonal_ma_idx <EOL> if self . polynomial_seasonal_ma . dtype == params . dtype : <EOL> self . polynomial_seasonal_ma [ idx ] = params_seasonal_ma <EOL> else : <EOL> polynomial_seasonal_ma = ( <EOL> self . polynomial_seasonal_ma . real . astype ( params . dtype ) <EOL> ) <EOL> polynomial_seasonal_ma [ idx ] = params_seasonal_ma <EOL> self . polynomial_seasonal_ma = polynomial_seasonal_ma <EOL> if self . k_seasonal_ar > <NUM_LIT:0> : <EOL> reduced_polynomial_ar = - np . polymul ( <EOL> self . polynomial_ar , self . polynomial_seasonal_ar <EOL> ) <EOL> else : <EOL> reduced_polynomial_ar = - self . polynomial_ar <EOL> if self . k_seasonal_ma > <NUM_LIT:0> : <EOL> reduced_polynomial_ma = np . polymul ( <EOL> self . polynomial_ma , self . polynomial_seasonal_ma <EOL> ) <EOL> else : <EOL> reduced_polynomial_ma = self . polynomial_ma <EOL> if self . mle_regression : <EOL> self . ssm [ '<STR_LIT>' ] = np . dot ( self . exog , params_exog ) [ None , : ] <EOL> if self . k_trend > <NUM_LIT:0> : <EOL> data = np . dot ( self . _trend_data , params_trend ) . astype ( params . dtype ) <EOL> if not self . hamilton_representation : <EOL> self . ssm [ '<STR_LIT>' , self . _k_states_diff , : ] = data <EOL> else : <EOL> if self . hamilton_representation : <EOL> data /= np . sum ( - reduced_polynomial_ar ) <EOL> if self . mle_regression : <EOL> self . ssm . obs_intercept += data [ None , : ] <EOL> else : <EOL> self . ssm . obs_intercept = data [ None , : ] <EOL> if self . measurement_error : <EOL> self . ssm [ '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:0> ] = params_measurement_variance <EOL> if self . k_ar > <NUM_LIT:0> or self . k_seasonal_ar > <NUM_LIT:0> : <EOL> self . ssm [ self . transition_ar_params_idx ] = reduced_polynomial_ar [ <NUM_LIT:1> : ] <EOL> elif not self . ssm . transition . dtype == params . dtype : <EOL> self . ssm . transition = self . ssm . transition . real . astype ( params . dtype ) <EOL> if self . k_ma > <NUM_LIT:0> or self . k_seasonal_ma > <NUM_LIT:0> : <EOL> if not self . hamilton_representation : <EOL> self . ssm [ self . selection_ma_params_idx ] = ( <EOL> reduced_polynomial_ma [ <NUM_LIT:1> : ] <EOL> ) <EOL> else : <EOL> self . ssm [ self . design_ma_params_idx ] = reduced_polynomial_ma [ <NUM_LIT:1> : ] <EOL> if self . k_posdef > <NUM_LIT:0> : <EOL> self . ssm [ '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:0> ] = params_variance <EOL> if self . state_regression and self . time_varying_regression : <EOL> self . ssm [ self . _exog_variance_idx ] = params_exog_variance <EOL> if not self . _manual_initialization : <EOL> self . initialize_state ( complex_step = complex_step ) <EOL> return params <EOL> class SARIMAXResults ( MLEResults ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model , params , filter_results , cov_type = '<STR_LIT>' , ** kwargs ) : <EOL> super ( SARIMAXResults , self ) . __init__ ( model , params , filter_results , <EOL> cov_type , ** kwargs ) <EOL> self . df_resid = np . inf <EOL> self . _init_kwds = self . model . _get_init_kwds ( ) <EOL> self . specification = Bunch ( ** { <EOL> '<STR_LIT>' : self . model . k_seasons , <EOL> '<STR_LIT>' : self . model . measurement_error , <EOL> '<STR_LIT>' : self . model . time_varying_regression , <EOL> '<STR_LIT>' : self . model . mle_regression , <EOL> '<STR_LIT>' : self . model . simple_differencing , <EOL> '<STR_LIT>' : self . model . enforce_stationarity , <EOL> '<STR_LIT>' : self . model . enforce_invertibility , <EOL> '<STR_LIT>' : self . model . hamilton_representation , <EOL> '<STR_LIT>' : self . model . order , <EOL> '<STR_LIT>' : self . model . seasonal_order , <EOL> '<STR_LIT>' : self . model . k_diff , <EOL> '<STR_LIT>' : self . model . k_seasonal_diff , <EOL> '<STR_LIT>' : self . model . k_ar , <EOL> '<STR_LIT>' : self . model . k_ma , <EOL> '<STR_LIT>' : self . model . k_seasonal_ar , <EOL> '<STR_LIT>' : self . model . k_seasonal_ma , <EOL> '<STR_LIT>' : self . model . k_ar_params , <EOL> '<STR_LIT>' : self . model . k_ma_params , <EOL> '<STR_LIT>' : self . model . trend , <EOL> '<STR_LIT>' : self . model . k_trend , <EOL> '<STR_LIT>' : self . model . k_exog , <EOL> '<STR_LIT>' : self . model . mle_regression , <EOL> '<STR_LIT>' : self . model . state_regression , <EOL> } ) <EOL> self . polynomial_trend = self . model . polynomial_trend <EOL> self . polynomial_ar = self . model . polynomial_ar <EOL> self . polynomial_ma = self . model . polynomial_ma <EOL> self . polynomial_seasonal_ar = self . model . polynomial_seasonal_ar <EOL> self . polynomial_seasonal_ma = self . model . polynomial_seasonal_ma <EOL> self . polynomial_reduced_ar = np . polymul ( <EOL> self . polynomial_ar , self . polynomial_seasonal_ar <EOL> ) <EOL> self . polynomial_reduced_ma = np . polymul ( <EOL> self . polynomial_ma , self . polynomial_seasonal_ma <EOL> ) <EOL> self . model_orders = self . model . model_orders <EOL> self . param_terms = self . model . param_terms <EOL> start = end = <NUM_LIT:0> <EOL> for name in self . param_terms : <EOL> end += self . model_orders [ name ] <EOL> setattr ( self , '<STR_LIT>' % name , self . params [ start : end ] ) <EOL> start += self . model_orders [ name ] <EOL> @ cache_readonly <EOL> def arroots ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . roots ( self . polynomial_reduced_ar ) ** - <NUM_LIT:1> <EOL> @ cache_readonly <EOL> def maroots ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . roots ( self . polynomial_reduced_ma ) ** - <NUM_LIT:1> <EOL> @ cache_readonly <EOL> def arfreq ( self ) : <EOL> """<STR_LIT>""" <EOL> z = self . arroots <EOL> if not z . size : <EOL> return <EOL> return np . arctan2 ( z . imag , z . real ) / ( <NUM_LIT:2> * np . pi ) <EOL> @ cache_readonly <EOL> def mafreq ( self ) : <EOL> """<STR_LIT>""" <EOL> z = self . maroots <EOL> if not z . size : <EOL> return <EOL> return np . arctan2 ( z . imag , z . real ) / ( <NUM_LIT:2> * np . pi ) <EOL> @ cache_readonly <EOL> def arparams ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _params_ar <EOL> @ cache_readonly <EOL> def maparams ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _params_ma <EOL> def predict ( self , start = None , end = None , exog = None , dynamic = False , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if start is None : <EOL> start = <NUM_LIT:0> <EOL> _start = self . model . _get_predict_start ( start ) <EOL> _end , _out_of_sample = self . model . _get_predict_end ( end ) <EOL> if _out_of_sample and ( self . model . k_exog + self . model . k_trend > <NUM_LIT:0> ) : <EOL> nobs = self . model . data . orig_endog . shape [ <NUM_LIT:0> ] + _out_of_sample <EOL> endog = np . zeros ( ( nobs , self . model . k_endog ) ) <EOL> if self . model . k_exog > <NUM_LIT:0> : <EOL> if exog is None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> exog = np . array ( exog ) <EOL> required_exog_shape = ( _out_of_sample , self . model . k_exog ) <EOL> if not exog . shape == required_exog_shape : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( str ( required_exog_shape ) , <EOL> str ( exog . shape ) ) ) <EOL> exog = np . c_ [ self . model . data . orig_exog . T , exog . T ] . T <EOL> model_kwargs = self . _init_kwds . copy ( ) <EOL> model_kwargs [ '<STR_LIT>' ] = exog <EOL> model = SARIMAX ( endog , ** model_kwargs ) <EOL> model . update ( self . params ) <EOL> for name in self . filter_results . shapes . keys ( ) : <EOL> if name == '<STR_LIT>' : <EOL> continue <EOL> mat = getattr ( model . ssm , name ) <EOL> if mat . shape [ - <NUM_LIT:1> ] > <NUM_LIT:1> : <EOL> if len ( mat . shape ) == <NUM_LIT:2> : <EOL> kwargs [ name ] = mat [ : , - _out_of_sample : ] <EOL> else : <EOL> kwargs [ name ] = mat [ : , : , - _out_of_sample : ] <EOL> elif self . model . k_exog == <NUM_LIT:0> and exog is not None : <EOL> warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return super ( SARIMAXResults , self ) . predict ( <EOL> start = start , end = end , exog = exog , dynamic = dynamic , ** kwargs <EOL> ) <EOL> def forecast ( self , steps = <NUM_LIT:1> , exog = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( SARIMAXResults , self ) . forecast ( steps , exog = exog , ** kwargs ) <EOL> def summary ( self , alpha = <NUM_LIT> , start = None ) : <EOL> order = '<STR_LIT>' <EOL> if self . model . k_ar + self . model . k_diff + self . model . k_ma > <NUM_LIT:0> : <EOL> if self . model . k_ar == self . model . k_ar_params : <EOL> order_ar = self . model . k_ar <EOL> else : <EOL> order_ar = tuple ( self . polynomial_ar . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] ) <EOL> if self . model . k_ma == self . model . k_ma_params : <EOL> order_ma = self . model . k_ma <EOL> else : <EOL> order_ma = tuple ( self . polynomial_ma . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] ) <EOL> k_diff = <NUM_LIT:0> if self . model . simple_differencing else self . model . k_diff <EOL> order = '<STR_LIT>' % ( order_ar , k_diff , order_ma ) <EOL> seasonal_order = '<STR_LIT>' <EOL> has_seasonal = ( <EOL> self . model . k_seasonal_ar + <EOL> self . model . k_seasonal_diff + <EOL> self . model . k_seasonal_ma <EOL> ) > <NUM_LIT:0> <EOL> if has_seasonal : <EOL> if self . model . k_ar == self . model . k_ar_params : <EOL> order_seasonal_ar = ( <EOL> int ( self . model . k_seasonal_ar / self . model . k_seasons ) <EOL> ) <EOL> else : <EOL> order_seasonal_ar = ( <EOL> tuple ( self . polynomial_seasonal_ar . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] ) <EOL> ) <EOL> if self . model . k_ma == self . model . k_ma_params : <EOL> order_seasonal_ma = ( <EOL> int ( self . model . k_seasonal_ma / self . model . k_seasons ) <EOL> ) <EOL> else : <EOL> order_seasonal_ma = ( <EOL> tuple ( self . polynomial_seasonal_ma . nonzero ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] ) <EOL> ) <EOL> k_seasonal_diff = self . model . k_seasonal_diff <EOL> if self . model . simple_differencing : <EOL> k_seasonal_diff = <NUM_LIT:0> <EOL> seasonal_order = ( '<STR_LIT>' % <EOL> ( str ( order_seasonal_ar ) , k_seasonal_diff , <EOL> str ( order_seasonal_ma ) , self . model . k_seasons ) ) <EOL> if not order == '<STR_LIT>' : <EOL> order += '<STR_LIT:x>' <EOL> model_name = ( <EOL> '<STR_LIT>' % ( self . model . __class__ . __name__ , order , seasonal_order ) <EOL> ) <EOL> return super ( SARIMAXResults , self ) . summary ( <EOL> alpha = alpha , start = start , model_name = model_name <EOL> ) <EOL> summary . __doc__ = MLEResults . summary . __doc__ <EOL> class SARIMAXResultsWrapper ( MLEResultsWrapper ) : <EOL> _attrs = { } <EOL> _wrap_attrs = wrap . union_dicts ( MLEResultsWrapper . _wrap_attrs , <EOL> _attrs ) <EOL> _methods = { } <EOL> _wrap_methods = wrap . union_dicts ( MLEResultsWrapper . _wrap_methods , <EOL> _methods ) <EOL> wrap . populate_wrapper ( SARIMAXResultsWrapper , SARIMAXResults ) </s>
