ó
áp7]c           @   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 d d e d d „ Z d d d d d	 „ Z d d d d d
 „ Z d d d d d „ Z d d d d d „ Z d S(   iÿÿÿÿN(   t   arima_process(   t   prefix_dtype_map(   t   _get_epsilont   approx_fprime_cs(   t   find_best_blas_typei   (   t   _arma_innovationsc         C   s•  t  j |  ƒ }  |  j d k } | r= |  d d … d f }  n  t  j | d k rU g  n | ƒ } t  j | d k rv g  n | ƒ } |  j \ } } t  j d | f }	 t  j d | f }
 | d k rî t |  | | t  j | ƒ g ƒ \ } } } n  t | } t  j	 |  d | ƒ}  t  j	 | d | ƒ} t  j	 | d | ƒ} | | ƒ j
 ƒ  } t t | d ƒ } t t | d ƒ } t t | d ƒ } t j |	 |
 d | d | ƒ| } | |	 |
 | ƒ \ } } | | | | | | ƒ \ } } t  j | ƒ } | rü| d	 } n  g  } x_ t | ƒ D]Q } t  j | |  d d … | f | | | ƒ ƒ } | j | rY| | n | ƒ qWt  j | ƒ j } | r‹| j ƒ  } n  | | f S(
   s|  
    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 : boolean, 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.

    i   Nt   dtypet   arma_transformed_acovf_fastt   arma_innovations_algo_fastt   arma_innovations_filtert   sigma2t   nobsg      à?(   t   npt   arrayt   ndimt   Nonet
   atleast_1dt   shapet   r_R   R   t   asfortranarrayt   itemt   getattrR   R    t
   arma_acovft   ranget   appendt   vstackt   Tt   squeeze(   t   endogt	   ar_paramst	   ma_paramsR
   t	   normalizet   prefixt   squeezedR   t   k_endogt   art   maR   t   _R   R   R	   R   t   acovft   acovf2t   thetat   vt   v05t   ut   it   u_i(    (    sK   lib/python2.7/site-packages/statsmodels/tsa/innovations/arma_innovations.pyt   arma_innovations
   sP    "!!*
!c      
   C   s1   t  |  d | d | d | d | ƒ} t j | ƒ S(   s¢  
    Compute loglikelihood 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
    -------
    loglike : numeric
        The joint loglikelihood.

    R   R   R
   R    (   t   arma_loglikeobsR   t   sum(   R   R   R   R
   R    t   llf_obs(    (    sK   lib/python2.7/site-packages/statsmodels/tsa/innovations/arma_innovations.pyt   arma_loglikee   s    c         C   s  t  j |  ƒ }  t  j | d k r' g  n | ƒ } t  j | d k rH g  n | ƒ } | d k rŠ t |  | | t  j | ƒ g ƒ \ } } } n  t | } t  j |  d | ƒ}  t  j | d | ƒ} t  j | d | ƒ} | | ƒ j ƒ  } t	 t
 | d ƒ } | |  | | | ƒ S(   sÌ  
    Compute loglikelihood 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
    -------
    loglikeobs : array of numeric
        Array of loglikelihood values for each observation.

    R   t   arma_loglikeobs_fastN(   R   R   R   R   R   R   t   ascontiguousarrayR   R   R   R   (   R   R   R   R
   R    R   R%   t   func(    (    sK   lib/python2.7/site-packages/statsmodels/tsa/innovations/arma_innovations.pyR/   ƒ   s    !!*
c            sž   | d k r g  n | } | d k r* g  n | } t | ƒ ‰ t | ƒ ‰ ‡  ‡ ‡ f d †  } t j | | | f } t | d d t | ƒ ƒ } t | | | ƒ S(   s±  
    Compute the score (gradient of the loglikelihood 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
    ---------
    score : array
        Score, evaluated at the given parameters.

    Notes
    -----
    This is a numerical approximation, calculated using first-order complex
    step differentiation on the `arma_loglike` method.
    c            s*   t  ˆ  |  ˆ  |  ˆ ˆ ˆ !|  ˆ ˆ ƒ S(   N(   R2   (   t   params(   R   t   pt   q(    sK   lib/python2.7/site-packages/statsmodels/tsa/innovations/arma_innovations.pyR5   ×   s    g       @N(   R   t   lenR   R   R   R   (   R   R   R   R
   R    R5   t   params0t   epsilon(    (   R   R7   R8   sK   lib/python2.7/site-packages/statsmodels/tsa/innovations/arma_innovations.pyt
   arma_score¯   s    "c            sž   | d k r g  n | } | d k r* g  n | } t | ƒ ‰ t | ƒ ‰ ‡  ‡ ‡ f d †  } t j | | | f } t | d d t | ƒ ƒ } t | | | ƒ S(   sÔ  
    Compute the score per observation (gradient of the loglikelihood 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
    ---------
    scoreobs : array
        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.
    c            s*   t  ˆ  |  ˆ  |  ˆ ˆ ˆ !|  ˆ ˆ ƒ S(   N(   R/   (   R6   (   R   R7   R8   (    sK   lib/python2.7/site-packages/statsmodels/tsa/innovations/arma_innovations.pyR5     s    g       @N(   R   R9   R   R   R   R   (   R   R   R   R
   R    R5   R:   R;   (    (   R   R7   R8   sK   lib/python2.7/site-packages/statsmodels/tsa/innovations/arma_innovations.pyt   arma_scoreobsß   s    "(   t   numpyR   t   statsmodels.tsaR    t    statsmodels.tsa.statespace.toolsR   t   statsmodels.tools.numdiffR   R   t   scipy.linalg.blasR   t    R   R   t   FalseR.   R2   R/   R<   R=   (    (    (    sK   lib/python2.7/site-packages/statsmodels/tsa/innovations/arma_innovations.pyt   <module>   s   	Z	+	/	