
    M/Ph44                         d Z ddl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lmZ ddlmZ dd	lm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dS )zk
Feasible generalized least squares for regression with SARIMA errors.

Author: Chad Fulton
License: BSD-3
    N)add_constantBunch)OLS)arma_innovations)diff)yule_walker)burg)hannan_rissanen)innovationsinnovations_mle)
statespace)SARIMAXSpecification)SARIMAXParamsr   r   r   r   r   r   r   2   :0yE>r   c
                 
   ||}t           j        }|d         dk    p|d         dk    }
||
 }n|r|
rt          d          |r%|t          j        |           nt	          |          }t          | |||          }|j        } |j        }|j        rXt          j
        d           t          | |j        |j        |j                  } t          ||j        |j        |j                  }t           j        | |f         }|                    |           |	i }	t!          | |          }|                                }|j        }|j        }t)          |          }||_        |j        dk    rt          j        |j                  |_        |j        dk    rt          j        |j                  |_        |j        dk    rt          j        |j                  |_        |j         dk    rt          j        |j!                  |_"        |j#        |_$        |j        }|j        }|j        }|j"        }|j$        }dg}dg}|g}|d	nd}d}d
 }tK          d|dz             D ]}|}|dk    rtM          |f|j'        d	d|	\  }}n|dk    r' ||	d           tQ          ||j'        d	          \  }}n|dk    r/ ||	d           tS          ||j*        d	          \  } }| d         }n|dk    rtW          |f|j'        |j*        d	d|	\  }}n|dk    rdnt           j,        |||||f         }!|j-        d         d|j-        d         f}"|j.        d         d|j.        d         |j.        d         f}#|dk    rt_          |f|"|#d	|!d|	\  }}nta          |f|"|#d	|!d|	\  }}|j        }|j        }|j        }|j"        }|j$        }|1                    |           |j2        st          d          tg          j3        |||d          \  }$}%|$dddf         }&|$ddddf         }'t!          |&|'          }(|(                                })|)j        }| t          j4        ||          z
  }t)          |          }||_        |j        dk    r||_        |j        dk    r||_        |j        dk    r||_        |j         dk    r||_"        ||_$        |1                    |           t          j5        ||z
            }*|1                    |*           |t          j6        |*|k               rd} n|t          j
        d|z             |d         }to          |||||||	|d          }+||+fS )a  
    Estimate ARMAX parameters by GLS.

    Parameters
    ----------
    endog : array_like
        Input time series array.
    exog : array_like, optional
        Array of exogenous regressors. If not included, then `include_constant`
        must be True, and then `exog` will only include the constant column.
    order : tuple, optional
        The (p,d,q) order of the ARIMA model. Default is (0, 0, 0).
    seasonal_order : tuple, optional
        The (P,D,Q,s) order of the seasonal ARIMA model.
        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. Default is True if the specified model does not
        include integration and False otherwise.
    n_iter : int, optional
        Optionally iterate feasible GSL a specific number of times. Default is
        to iterate to convergence. If set, this argument overrides the
        `max_iter` and `tolerance` arguments.
    max_iter : int, optional
        Maximum number of feasible GLS iterations. Default is 50. If `n_iter`
        is set, it overrides this argument.
    tolerance : float, optional
        Tolerance for determining convergence of feasible GSL iterations. If
        `iter` is set, this argument has no effect.
        Default is 1e-8.
    arma_estimator : str, optional
        The estimator used for estimating the ARMA model. This option should
        not generally be used, unless the default method is failing or is
        otherwise unsuitable. Not all values will be valid, depending on the
        specified model orders (`order` and `seasonal_order`). Possible values
        are:
        * 'innovations_mle' - can be used with any specification
        * 'statespace' - can be used with any specification
        * 'hannan_rissanen' - can be used with any ARMA non-seasonal model
        * 'yule_walker' - only non-seasonal consecutive
          autoregressive (AR) models
        * 'burg' - only non-seasonal, consecutive autoregressive (AR) models
        * 'innovations' - only non-seasonal, consecutive moving
          average (MA) models.
        The default is 'innovations_mle'.
    arma_estimator_kwargs : dict, optional
        Arguments to pass to the ARMA estimator.

    Returns
    -------
    parameters : SARIMAXParams object
        Contains the parameter estimates from the final iteration.
    other_results : Bunch
        Includes eight components: `spec`, `params`, `converged`,
        `differences`, `iterations`, `arma_estimator`, 'arma_estimator_kwargs',
        and `arma_results`.

    Notes
    -----
    The primary reference is [1]_, section 6.6. In particular, the
    implementation follows the iterative procedure described in section 6.6.2.
    Construction of the transformed variables used to compute the GLS estimator
    described in section 6.6.1 is done via an application of the innovations
    algorithm (rather than explicit construction of the transformation matrix).

    Note that if the specified model includes integration, both the `endog` and
    `exog` series will be differenced prior to estimation and a warning will
    be issued to alert the user.

    References
    ----------
    .. [1] Brockwell, Peter J., and Richard A. Davis. 2016.
       Introduction to Time Series and Forecasting. Springer.
    N   r   z1Cannot include a constant in an integrated model.)exogorderseasonal_orderztProvided `endog` and `exog` series have been differenced to eliminate integration prior to GLS parameter estimation.)k_diffk_seasonal_diffseasonal_periods)specFc                 .    | rt          d|           d S )Nz/arma_estimator_kwargs not supported for method )
ValueError)kwargsmethods     d/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/arima/estimators/gls.py_check_arma_estimator_kwargsz)gls.<locals>._check_arma_estimator_kwargs   s1     	J&JJ  	 	    r   )ar_orderdemeanr	   r   )ma_orderr%   r
   )r$   r&   r%         r   )r   r   r%   start_params)r   r   include_constantr*   zRoots of the autoregressive parameters indicate that data isnon-stationary. GLS cannot be used with non-stationary parameters. You should consider differencing the model dataor applying a nonlinear transformation (e.g., natural log).T)	ar_params	ma_params	normalizezFeasible GLS failed to converge in %d iterations. Consider increasing the maximum number of iterations using the `max_iter` argument or reducing the required tolerance using the `tolerance` argument.)r   params	convergeddifferences
iterationsarma_estimatorarma_estimator_kwargsarma_results)8npinfr   	ones_liker   r   endogr   is_integratedwarningswarnr   seasonal_diffr   c_validate_estimatorr   fitr/   residr   exog_paramsmax_ar_orderzerosk_ar_paramsr,   max_seasonal_ar_orderk_seasonal_ar_paramsseasonal_ar_paramsmax_ma_orderk_ma_paramsr-   max_seasonal_ma_orderk_seasonal_ma_paramsseasonal_ma_paramsscalesigma2ranger   r$   r	   r   r&   r
   r_r   r   r   r   appendis_stationaryr   dotabsallr   ),r9   r   r   r   r+   n_itermax_iter	tolerancer3   r4   
integratedr   	augmentedmod_olsres_olsrB   rA   pr,   rH   r-   rM   rO   r5   r1   
parametersr0   ir"   prevp_armares_armaoutr*   	tmp_ordertmp_seasonal_ordertmp_uxmod_glsres_gls
differenceother_resultss,                                               r!   glsro      s    ` F	 qA6!2Q!6J)>	 Nj NLMMM  K&*lr|E"""T8J8J  D/=? ? ?DJE9D  < 	 / 	0 	0 	0 U49%)%7&*&;= = = D$($6%)%:< < < eTk"I 	N+++$ " %GkkmmG.KME 	4   AAM1ht/00!A%%!x(ABB1ht/00!A%%!x(ABB}AHI-I-XF 6L&KJTI	A   1hl## p? p? ]*** ) $e )  )' )  )FHH v%%(()>GGG#EDM+0 2  2  2FHH},,(()>NNN'/46 6 6MCWFF000. 7 $ 7  7 5 7  7FHH QBE)Y*<*<f+E %F  A4:a=9I"&"5a"8!"&"5a"8"&"5a"8": !222#2$-!*;M |$- $- ,$- $- 
 $.$-!*;M%*$- $- ,$- $- 
 $	#6$	#6H%%%  # 	N   "2i  Q 1I122J a))++--nt[111 t$$$#q  #AK%))#5A q  #AK%))#5A ! VK$.//
:&&&>bfZ)%;<<>IE>M 3 6>	> ? ? ? 	2A"(!6$	 	 	 	M mr#   )	Nr   r   NNr   r   r   N)__doc__numpyr6   r;   statsmodels.tools.toolsr   r   #statsmodels.regression.linear_modelr   statsmodels.tsa.innovationsr    statsmodels.tsa.statespace.toolsr   ,statsmodels.tsa.arima.estimators.yule_walkerr   %statsmodels.tsa.arima.estimators.burgr	   0statsmodels.tsa.arima.estimators.hannan_rissanenr
   ,statsmodels.tsa.arima.estimators.innovationsr   r   +statsmodels.tsa.arima.estimators.statespacer   #statsmodels.tsa.arima.specificationr   statsmodels.tsa.arima.paramsr   ro    r#   r!   <module>r~      s8         7 7 7 7 7 7 7 7 3 3 3 3 3 3 8 8 8 8 8 8 1 1 1 1 1 1 D D D D D D 6 6 6 6 6 6 L L L L L L" " " " " " " " B B B B B B D D D D D D 6 6 6 6 6 6 ;GCG@Da a a a a ar#   