
    "gV                     T    d Z ddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ 	 	 	 	 ddZy)	zX
State space approach to estimating SARIMAX models.

Author: Chad Fulton
License: BSD-3
    N)add_constantBunch)SARIMAX)SARIMAXSpecification)SARIMAXParamsc
           	         |r"|t        j                  |       n
t        |      }t        | ||||||      }
|
j                  } |
j
                  }t        |
      }|Yt        |
      }||_        |
j                  r|j                  st        d      |
j                  r|j                  st        d      t        | ||
j                  |
j                  |
j                  |
j                  |
j                         }|	i }	|	j#                  dd        |j$                  d	d|i|	}|j                  |_        t'        |
|d      }||fS )
a	  
    Estimate SARIMAX parameters using state space methods.

    Parameters
    ----------
    endog : array_like
        Input time series array.
    order : tuple, optional
        The (p,d,q) order of the model for the number of AR parameters,
        differences, and MA parameters. Default is (0, 0, 0).
    seasonal_order : tuple, optional
        The (P,D,Q,s) order of the seasonal component of the model for the
        AR parameters, differences, MA parameters, and periodicity. Default
        is (0, 0, 0, 0).
    include_constant : bool, optional
        Whether to add a constant term in `exog` if it's not already there.
        The estimate of the constant will then appear as one of the `exog`
        parameters. If `exog` is None, then the constant will represent the
        mean of the process.
    enforce_stationarity : bool, optional
        Whether or not to transform the AR parameters to enforce stationarity
        in the autoregressive component of the model. Default is True.
    enforce_invertibility : bool, optional
        Whether or not to transform the MA parameters to enforce invertibility
        in the moving average component of the model. Default is True.
    concentrate_scale : bool, optional
        Whether or not to concentrate the scale (variance of the error term)
        out of the likelihood. This reduces the number of parameters estimated
        by maximum likelihood by one.
    start_params : array_like, optional
        Initial guess of the solution for the loglikelihood maximization. The
        AR polynomial must be stationary. If `enforce_invertibility=True` the
        MA poylnomial must be invertible. If not provided, default starting
        parameters are computed using the Hannan-Rissanen method.
    fit_kwargs : dict, optional
        Arguments to pass to the state space model's `fit` method.

    Returns
    -------
    parameters : SARIMAXParams object
    other_results : Bunch
        Includes two components, `spec`, containing the `SARIMAXSpecification`
        instance corresponding to the input arguments; and
        `state_space_results`, corresponding to the results from the underlying
        state space model and Kalman filter / smoother.

    Notes
    -----
    The primary reference is [1]_.

    References
    ----------
    .. [1] Durbin, James, and Siem Jan Koopman. 2012.
       Time Series Analysis by State Space Methods: Second Edition.
       Oxford University Press.
    )exogorderseasonal_orderenforce_stationarityenforce_invertibilityconcentrate_scale)specz]Given starting parameters imply a non-stationary AR process with `enforce_stationarity=True`.z^Given starting parameters imply a non-invertible MA process with `enforce_invertibility=True`.dispr   start_params)r   statespace_results )np	ones_liker   r   endogr	   r   paramsr   is_stationary
ValueErrorr   is_invertibler   r
   r   r   
setdefaultfitr   )r   r	   r
   r   include_constantr   r   r   r   
fit_kwargsr   pspmodres_ssress                   h/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/tsa/arima/estimators/statespace.py
statespacer%      s`   | &*lr||E"T8J  Dn13+	-D
 JJE99D4 A % 	$$R-=-= M N N %%b.>.> N O O %d$**!%!4!4'+'@'@(,(B(B$($:$:	<C
 
&!$SWW=,=*=F }}AH
$ C
 c6M    )	N)r   r   r   )r   r   r   r   TTTFNN)__doc__numpyr   statsmodels.tools.toolsr   r   "statsmodels.tsa.statespace.sarimaxr   #statsmodels.tsa.arima.specificationr   statsmodels.tsa.arima.paramsr   r%   r   r&   r$   <module>r-      s/     7 6 D 6 (1=A@DFJjr&   