
    "gB                     |    d Z ddl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
 Zd Zd Zy)zj
Hannan-Rissanen procedure for estimating ARMA(p,q) model parameters.

Author: Chad Fulton
License: BSD-3
    N)lfilter)Bunch)OLSyule_walker)lagmat)SARIMAXSpecification)SARIMAXParamsc                 V   t        | ||      }t        ||j                        }|j                  } |r| | j	                         z
  } t        |      }t        |       }	|j                  }
|j                  }|Ut        t        j                  t        j                  |	      dz        j                  t              dt        |
|      z        }t        | |      }t        ||j                   |j"                        }t%        | |
d      }d}|dk(  r3|
dk(  r.t        j&                  | d	      |_        | j+                         }nV|dk(  r.|dd|j,                  f   }|dd|j.                  f   }| |
d }|j0                  d
   dk7  r||j3                  |j4                        z
  }|j0                  d
   dk(  r?|j4                  |_        t        j&                  |d	      |_        |j+                         }nt9        ||      }|j;                         }|j<                  }|j>                  |_        tA        |jB                  |j4                  |jD                  |jF                  |j                         |_        n"tI        | |d      \  }}t%        | |d      }| |d }||j3                  |      z
  }t%        ||d      }||z   |
z
  }t        jJ                  ||d|j,                  f   |dd|jL                  f   f   }t        jJ                  ||d|j.                  f   |dd|jN                  f   f   }| ||z   d }|j0                  d
   dk7  r;||j3                  t        jP                  |j4                  |jR                  f         z
  }|j0                  d
   dk(  rO|j4                  |_        |jR                  |_*        t        j&                  |d	      |_        |j+                         }nt9        ||      }|j;                         }t        |jD                        }tA        |jB                  |j4                  |jD                  |jF                  d| |j                         |_        tA        |jV                  |jR                  |jX                  |jF                  |d |j"                        |_*        |j<                  }|j>                  |_        |du r<t        |      dk7  rt[        d      |j\                  r|j^                  s8ta        d      |+t        |      dk7  rd}n|j\                  xr |j^                  }|du r\|ta        d      t        jb                  |       }|jd                  jf                  }|jh                  jf                  }tk        |	      D ]k  }|t        |
|      k\  st        j2                  |d
d  | ||
z
  | ddd         }t        j2                  |d
d |||z
  | ddd         }| |   |z
  |z
  ||<   m tm        d
g||      } tm        t        jP                  d
|d
d  f   d
g|      }!t%        | |
d      }"t%        |!|d      }#t        jJ                  |"t        ||
z
  d      d|j,                  f   |#t        |
|z
  d      d|jL                  f   f   }$t9        |t        |
|      d |$      }%|%j;                         }&|j6                  |&jF                  d|jn                   z   |_        |jT                  |&jF                  |jn                  d z   |_*        |j                  |jp                  j3                  t        jP                  |j6                  |jT                  f         z
  }t        jr                  ||      t        |      z  |_        tu        |||d      }'||'fS )u[  
    Estimate ARMA parameters using Hannan-Rissanen procedure.

    Parameters
    ----------
    endog : array_like
        Input time series array, assumed to be stationary.
    ar_order : int or list of int
        Autoregressive order
    ma_order : int or list of int
        Moving average order
    demean : bool, optional
        Whether to estimate and remove the mean from the process prior to
        fitting the ARMA coefficients. Default is True.
    initial_ar_order : int, optional
        Order of long autoregressive process used for initial computation of
        residuals.
    unbiased : bool, optional
        Whether or not to apply the bias correction step. Default is True if
        the estimated coefficients from the previous step imply a stationary
        and invertible process and False otherwise.
    fixed_params : dict, optional
        Dictionary with names of fixed parameters as keys (e.g. 'ar.L1',
        'ma.L2'), which correspond to SARIMAXSpecification.param_names.
        Dictionary values are the values of the associated fixed parameters.

    Returns
    -------
    parameters : SARIMAXParams object
    other_results : Bunch
        Includes three components: `spec`, containing the
        `SARIMAXSpecification` instance corresponding to the input arguments;
        `initial_ar_order`, containing the autoregressive lag order used in the
        first step; and `resid`, which contains the computed residuals from the
        last step.

    Notes
    -----
    The primary reference is [1]_, section 5.1.4, which describes a three-step
    procedure that we implement here.

    1. Fit a large-order AR model via Yule-Walker to estimate residuals
    2. Compute AR and MA estimates via least squares
    3. (Unless the estimated coefficients from step (2) are non-stationary /
       non-invertible or `unbiased=False`) Perform bias correction

    The order used for the AR model in the first step may be given as an
    argument. If it is not, we compute it as suggested by [2]_.

    The estimate of the variance that we use is computed from the residuals
    of the least-squares regression and not from the innovations algorithm.
    This is because our fast implementation of the innovations algorithm is
    only valid for stationary processes, and the Hannan-Rissanen procedure may
    produce estimates that imply non-stationary processes. To avoid
    inconsistency, we never compute this latter variance here, even if it is
    possible. See test_hannan_rissanen::test_brockwell_davis_example_517 for
    an example of how to compute this variance manually.

    This procedure assumes that the series is stationary, but if this is not
    true, it is still possible that this procedure will return parameters that
    imply a non-stationary / non-invertible process.

    Note that the third stage will only be applied if the parameters from the
    second stage imply a stationary / invertible model. If `unbiased=True` is
    given, then non-stationary / non-invertible parameters in the second stage
    will throw an exception.

    References
    ----------
    .. [1] Brockwell, Peter J., and Richard A. Davis. 2016.
       Introduction to Time Series and Forecasting. Springer.
    .. [2] Gomez, Victor, and Agustin Maravall. 2001.
       "Automatic Modeling Methods for Univariate Series."
       A Course in Time Series Analysis, 171–201.
    )ar_orderma_order)specN   )r   both)trimr   )ddof   )fixed_ar_or_ma_lagsfixed_ar_or_ma_paramsfree_ar_or_ma_lagsfree_ar_or_ma_paramsspec_ar_or_ma_lagsmle)ordermethodTz|Third step of Hannan-Rissanen estimation to remove parameter bias is not yet implemented for the case with fixed parameters.zCannot perform third step of Hannan-Rissanen estimation to remove parameter bias, because parameters estimated from the second step are non-stationary or non-invertible.Fz)Must have free parameters to use unbiased)r   initial_ar_orderresid);r   _validate_fixed_paramsparam_namesendogmeanr	   lenmax_ar_ordermax_ma_ordermaxnpfloorlogastypeint#_package_fixed_and_free_params_infoar_lagsma_lagsr   varsigma2copy
free_ar_ixfixed_ar_ixshapedotfixed_ar_params	ar_paramsr   fitr   scale_stitch_fixed_and_free_paramsfixed_ar_lagsfree_ar_lagsparamsr   c_
free_ma_ixfixed_ma_ixr_fixed_ma_params	ma_paramsfixed_ma_lagsfree_ma_lagsNotImplementedErroris_stationaryis_invertible
ValueError
zeros_likear_polycoefma_polyranger   k_ar_paramsexoginnerr   )(r    r   r   demeanr   unbiasedfixed_paramsr   pnobsr#   r$   _params_infolagged_endogmodr   X_with_free_paramsX_with_fixed_paramsyresinitial_ar_paramsXlagged_residixk_free_ar_paramsZar_coefma_coefttmp_artmp_maVWlagged_Vlagged_WrO   
mod_unbias
res_unbiasother_resultss(                                           m/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/tsa/arima/estimators/hannan_rissanen.pyhannan_rissanenrq      s~   \  8LD),8H8HILJJE

$4 Au:D$$L$$L rxxta8??D 3|\#BBD 	U-=>A 6dllDLLK
 %F;L Cq\Q.66%a(

		 *![-C-C*CD*1k.E.E+EF,- $$Q'1,'++K,G,GHHA ##A&!+%55AKvvaa(AHFFHE a+,C'')CIIEyyAH7$/$=$=&1&A&A#.#;#;%(ZZ#'<<AK  +)% 915*8"#$AEE+,, e\? ,|;UUk4445K22234
 !eek5556K33345
 "\123$$Q'1,'++k11;3N3NNO A ##A&!+%55AK%55AKvvaa(AHFFHE a+,C'')C";#;#;<7$/$=$=&1&A&A#.#;#;%(ZZ0A1A%B#'<<AK 8$/$=$=&1&A&A#.#;#;%(ZZ0@0A%B#'<<AK IIEyyAH
 t< A%)- 
 oo!// &  < A% ??>q t{ !LMMe$AiinnGiinnG4[ 
6L,77
  VV eA,<Q&?"&EGFVVGABK$%a,&6q$9$B$$?AF 8f,v5AaD
6 Wa(Aa'!"+o.Q7AaF;HaF;H55|3Q78**+ |3Q78**+		D Qs<>?@$GJ#)J j//0A1A1ABB K j//0@0@0ABB K IIakk1;;./!1 1Exxu-E
:AH , M
 m    c           
         | i } t        | t              sJ t        | j                               }t        |      dhz
  }||z
  }t	        |      dkD  r6t        dt        t        |             dt        t        |             d      | S )z
    Check that keys in fixed_params are a subset of spec.param_names except
    "sigma2"

    Parameters
    ----------
    fixed_params : dict
    spec_param_names : list of string
        SARIMAXSpecification.param_names
    r/   r   zInvalid fixed parameter(s): z. Please select among .)
isinstancedictsetkeysr"   rH   sortedlist)rS   spec_param_namesfixed_param_namesvalid_param_namesinvalid_param_namess        rp   r   r   /  s     lD)))L--/0,-
:+.??
!#*6$7J2K+L*M N$$*40A+B$C#DAG
 	

 rr   c                 $   g }g }| j                         D ]{  \  }}t        |j                  d      d   j                  d            }|j	                  d      r|j                  ||f       W|j	                  d      si|j                  ||f       } |j                          |j                          |D cg c]  \  }}|	 }	}}t        j                  |D cg c]  \  }}|	 c}}      }
|D cg c]  \  }}|	 }}}t        j                  |D cg c]  \  }}|	 c}}      }|D cg c]  }|t        |	      vr| }}|D cg c]  }|t        |      vr| }}t        j                  |t              dz
  }t        j                  |t              dz
  }t        j                  |	t              dz
  }t        j                  |t              dz
  }t        |	||||||||
|
      S c c}}w c c}}w c c}}w c c}}w c c}w c c}w )	a  
    Parameters
    ----------
    fixed_params : dict
    spec_ar_lags : list of int
        SARIMAXSpecification.ar_lags
    spec_ma_lags : list of int
        SARIMAXSpecification.ma_lags

    Returns
    -------
    Bunch with
    (lags) fixed_ar_lags, fixed_ma_lags, free_ar_lags, free_ma_lags;
    (ix) fixed_ar_ix, fixed_ma_ix, free_ar_ix, free_ma_ix;
    (params) fixed_ar_params, free_ma_params
    rt   r   Larma)dtyper   )
r:   rC   r;   rD   r2   r?   r1   r>   r5   rA   )itemsr*   splitlstrip
startswithappendsortr&   arrayrw   r   )rS   spec_ar_lagsspec_ma_lagsfixed_ar_lags_and_paramsfixed_ma_lags_and_paramskeyvallagrV   r:   r5   rC   rA   r;   rD   r1   r>   r2   r?   s                      rp   r+   r+   M  s   &  "! &&( 8S#))C.$++C01>>$$++S#J7^^D!$++S#J78 !!#!!#'?@VS!S@M@hh2JK3KLO'?@VS!S@M@hh2JK3KLO $0 6C#m"44  6L 6#/ 6C#m"44  6L 6
 ,c2Q6J,c2Q6J((=4q8K((=4q8K#=![*'	 	' AK@K66s$   8G0G6
4G<H
/HHc                 H   t        |       t        |      k(  sJ t        |      t        |      k(  sJ t        j                  | |f   }t        j                  ||f   }t        |      t        |      k(  sJ t	        t        ||            }|D cg c]  }||   	 }	}|	S c c}w )a  
    Stitch together fixed and free params, by the order of lags, for setting
    SARIMAXParams.ma_params or SARIMAXParams.ar_params

    Parameters
    ----------
    fixed_ar_or_ma_lags : list or np.array
    fixed_ar_or_ma_params : list or np.array
        fixed_ar_or_ma_params corresponds with fixed_ar_or_ma_lags
    free_ar_or_ma_lags : list or np.array
    free_ar_or_ma_params : list or np.array
        free_ar_or_ma_params corresponds with free_ar_or_ma_lags
    spec_ar_or_ma_lags : list
        SARIMAXSpecification.ar_lags or SARIMAXSpecification.ma_lags

    Returns
    -------
    list of fixed and free params by the order of lags
    )r"   r&   r@   rw   rv   zip)
r   r   r   r   r   all_lags
all_paramslag_to_param_mapr   all_params_sorteds
             rp   r9   r9     s    , "#s+@'AAAA!"c*>&????uu(*<<=H,.BBCJx=C 23333C*56
 ;MM3)#.MM Ns   B)r   r   TNNN)__doc__numpyr&   scipy.signalr   statsmodels.tools.toolsr   #statsmodels.regression.linear_modelr   r   statsmodels.tsa.tsatoolsr   #statsmodels.tsa.arima.specificationr   statsmodels.tsa.arima.paramsr	   rq   r   r+   r9    rr   rp   <module>r      sC       ) @ + D 6 ;?48!%Zz<;|#rr   