U
    Ãmœd:&  ã                   @   s‚   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Zdd
d„Zddd„Zddd„Zddd„Zddd„ZdS )é    N)Úarima_process)Úprefix_dtype_map)Ú_get_epsilonÚapprox_fprime_cs)Úfind_best_blas_typeé   )Ú_arma_innovationszŠThe model's autoregressive parameters (ar_params) indicate that the process
 is non-stationary. The innovations algorithm cannot be used.
Fc                 C   s  t  | ¡} | jdk}|r(| dd…df } t  |dkr8g n|¡}t  |dkrNg n|¡}| j\}}t jd| f }	t jd|f }
|dkr t| ||t  |¡gƒ\}}}t| }t j| |d�} t j||d�}t j||d�}||ƒ 	¡ }t
t|d ƒ}t
t|d ƒ}t
t|d ƒ}tj|	|
||d�| }||	|
|ƒ\}}||||||ƒ\}}t  |¡}t  |dk ¡�s|t  |¡ ¡ �r|t  |¡ ¡ �s„ttƒ‚g }t|ƒD ]0}t  || dd…|f |||ƒ¡}| |¡ �q�t  |¡j}|�rì||dd…df d	  }|�rú| ¡ }||fS )
ay  
    Compute innovations using a given ARMA process.

    Parameters
    ----------
    endog : ndarray
        The observed time-series process, may be univariate or multivariate.
    ar_params : ndarray, optional
        Autoregressive parameters.
    ma_params : ndarray, optional
        Moving average parameters.
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    normalize : bool, optional
        Whether or not to normalize the returned innovations. Default is False.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    innovations : ndarray
        Innovations (one-step-ahead prediction errors) for the given `endog`
        series with predictions based on the given ARMA process. If
        `normalize=True`, then the returned innovations have been "whitened" by
        dividing through by the square root of the mean square error.
    innovations_mse : ndarray
        Mean square error for the innovations.
    r   N©ÚdtypeÚarma_transformed_acovf_fastÚarma_innovations_algo_fastÚarma_innovations_filter)Úsigma2Únobsr   g      à?)ÚnpÚarrayÚndimÚ
atleast_1dÚshapeÚr_r   r   ÚasfortranarrayÚitemÚgetattrr   r   Ú
arma_acovfÚanyÚisfiniteÚallÚ
ValueErrorÚNON_STATIONARY_ERRORÚrangeÚappendZvstackÚTZsqueeze)ÚendogÚ	ar_paramsÚ	ma_paramsr   Ú	normalizeÚprefixZsqueezedr   Zk_endogÚarÚmar
   Ú_r   r   r   r   ZacovfZacovf2ÚthetaÚvÚuÚiZu_i© r.   úe/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/tsa/innovations/arma_innovations.pyÚarma_innovations   sz    !


ÿ
 ÿ ÿ ÿ ÿÿ ÿ
ÿþ ÿr0   c                 C   s   t | ||||d�}t |¡S )a›  
    Compute the log-likelihood of the given data assuming an ARMA process.

    Parameters
    ----------
    endog : ndarray
        The observed time-series process.
    ar_params : ndarray, optional
        Autoregressive parameters.
    ma_params : ndarray, optional
        Moving average parameters.
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    float
        The joint loglikelihood.
    )r#   r$   r   r&   )Úarma_loglikeobsr   Úsum)r"   r#   r$   r   r&   Zllf_obsr.   r.   r/   Úarma_logliken   s
     ÿr3   c                 C   s´   t  | ¡} t  |dkrg n|¡}t  |dkr0g n|¡}|dkrZt| ||t  |¡gƒ\}}}t| }t j| |d�} t j||d�}t j||d�}||ƒ ¡ }tt	|d ƒ}|| |||ƒS )a»  
    Compute the log-likelihood for each observation assuming an ARMA process.

    Parameters
    ----------
    endog : ndarray
        The observed time-series process.
    ar_params : ndarray, optional
        Autoregressive parameters.
    ma_params : ndarray, optional
        Moving average parameters.
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    ndarray
        Array of loglikelihood values for each observation.
    Nr	   Zarma_loglikeobs_fast)
r   r   r   r   r   Zascontiguousarrayr   r   r   r   )r"   r#   r$   r   r&   r
   r)   Úfuncr.   r.   r/   r1   ‹   s    
ÿ
r1   c                    sn   |dkrg n|}|dkrg n|}t |ƒ‰t |ƒ‰‡ ‡‡fdd„}tj|||f }t|ddt |ƒƒ}t|||ƒS )a«  
    Compute the score (gradient of the log-likelihood function).

    Parameters
    ----------
    endog : ndarray
        The observed time-series process.
    ar_params : ndarray, optional
        Autoregressive coefficients, not including the zero lag.
    ma_params : ndarray, optional
        Moving average coefficients, not including the zero lag, where the sign
        convention assumes the coefficients are part of the lag polynomial on
        the right-hand-side of the ARMA definition (i.e. they have the same
        sign from the usual econometrics convention in which the coefficients
        are on the right-hand-side of the ARMA definition).
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    ndarray
        Score, evaluated at the given parameters.

    Notes
    -----
    This is a numerical approximation, calculated using first-order complex
    step differentiation on the `arma_loglike` method.
    Nc                    s.   t ˆ | d ˆ… | ˆˆˆ … | ˆˆ d … ƒS ©N)r3   ©Úparams©r"   ÚpÚqr.   r/   r4   Þ   s    zarma_score.<locals>.funcç       @©Úlenr   r   r   r   ©r"   r#   r$   r   r&   r4   Zparams0Úepsilonr.   r8   r/   Ú
arma_score¶   s    "r@   c                    sn   |dkrg n|}|dkrg n|}t |ƒ‰t |ƒ‰‡ ‡‡fdd„}tj|||f }t|ddt |ƒƒ}t|||ƒS )a¬  
    Compute the score (gradient) per observation.

    Parameters
    ----------
    endog : ndarray
        The observed time-series process.
    ar_params : ndarray, optional
        Autoregressive coefficients, not including the zero lag.
    ma_params : ndarray, optional
        Moving average coefficients, not including the zero lag, where the sign
        convention assumes the coefficients are part of the lag polynomial on
        the right-hand-side of the ARMA definition (i.e. they have the same
        sign from the usual econometrics convention in which the coefficients
        are on the right-hand-side of the ARMA definition).
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    ndarray
        Score per observation, evaluated at the given parameters.

    Notes
    -----
    This is a numerical approximation, calculated using first-order complex
    step differentiation on the `arma_loglike` method.
    Nc                    s.   t ˆ | d ˆ… | ˆˆˆ … | ˆˆ d … ƒS r5   )r1   r6   r8   r.   r/   r4     s    ÿzarma_scoreobs.<locals>.funcr;   r<   r>   r.   r8   r/   Úarma_scoreobsæ   s    "rA   )NNr   FN)NNr   N)NNr   N)NNr   N)NNr   N)Únumpyr   Zstatsmodels.tsar   Z statsmodels.tsa.statespace.toolsr   Zstatsmodels.tools.numdiffr   r   Zscipy.linalg.blasr   Ú r   r   r0   r3   r1   r@   rA   r.   r.   r.   r/   Ú<module>   s(       ÿ
_
  ÿ
+  ÿ
0  ÿ