U
    Kvf:&                     @   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_innovationszThe 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rt| ||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 | sttg }t|D ]0}t || dd|f |||}|| qt |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.   P/tmp/pip-unpacked-wheel-2v6byqio/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)Z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  