ó
áp7]c           @  s-  d  Z  d d l m Z d d l m Z d d l Z d d l m Z d d l	 j
 Z d d l j Z d d l m Z m Z d d l m Z m Z m Z d d l m Z m Z d d	 l m Z d
 d g Z d
 d g Z d Z e e e d ƒ Z e e ƒ d  Z e e e ƒ Z d d „ Z! d d „ Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d „  Z) d
 d g Z* d
 d g Z+ d
 d g Z, d
 d d g Z- d
 d g Z. e/ d e) f d e( f d e' f d  e' f g ƒ Z0 d e+ e, f f d e+ e- f f d e* e, f f d  e* e. f f g Z1 xÞ e1 D]Ö \ Z2 Z3 e3 \ Z Z e4 d ƒ e4 e2 e e ƒ e e e d! d ƒZ5 e e e d! d ƒZ6 e2 d"  d# k r•e0 e2 e ƒ Z7 n e0 e2 e e ƒ Z7 e4 e5 d$  ƒ e4 e7 d$  ƒ e e5 e7 d ƒ e e6 e7 e7 d d ƒ qWd% d
 d& „ Z8 d d' „ Z9 d( d) „ Z: d* „  Z; e< e$ e< d d+ „ Z= e8 ƒ  Z> e e> d ƒ Z? e? j@ d3 ƒ ZA e4 eA d ƒ e e> d ƒ ZB e e> d d- eC ƒZD e9 e> d ƒ ZE e9 e> d e> d jF ƒ  ƒ ZG e4 eB d  ƒ e4 eD d  ƒ e4 eE d  ƒ e4 eG d  ƒ e d
 d g d
 g d. ƒ Z e4 e e ƒ d/  ƒ e4 e jH jI e d ƒ ƒ e jJ e ƒ e jK ƒ  e= e e e ƒ e jK ƒ  e= e e e d0 eC ƒe jK ƒ  e e eB d/  e jL eM eB d/  ƒ ƒ d0 e< ƒe jK ƒ  e jN d1 ƒ ZO e eO eB d/  d0 e< ƒe jN d2 ƒ ZO e eO eB d/  e jL eM eB d/  ƒ ƒ d0 eC ƒd S(4   sš   trying to verify theoretical acf of arma

explicit functions for autocovariance functions of ARIMA(1,1), MA(1), MA(2)
plus 3 functions from nitime.utils

iÿÿÿÿ(   t   print_function(   t   rangeN(   t   assert_array_almost_equal(   t   arma_generate_samplet   arma_impulse_response(   t
   arma_acovft   arma_acft   ARIMA(   t   acft   acovf(   t   plotacfg      ð?g333333ã¿gš™™™™™Ù?t    iˆ  i
   c         C  s<   | d  k s | d k r" t |  ƒ S| d k r8 t |  ƒ Sd  S(   Nt   constantt   linear(   t   Nonet   detrend_meant   detrend_linear(   t   xt   key(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   detrend&   s    
i    c         C  s^   t  j |  ƒ }  | rM t d ƒ g | } | j t  j ƒ |  |  j | ƒ | S|  |  j | ƒ S(   s0   Return x minus its mean along the specified axisN(   t   npt   asarrayt   sliceR   t   appendt   newaxist   mean(   R   t   axist   ind(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   demean,   s    c         C  s   |  |  j  ƒ  S(   s   Return x minus the mean(x)(   R   (   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyR   5   s    c         C  s   |  S(   s   Return x: no detrending(    (   R   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   detrend_none9   s    c         C  sr   t  j t |  ƒ d t  j ƒ} t  j | |  d d ƒ} | d | d } |  j ƒ  | | j ƒ  } |  | | | S(   s2   Return y minus best fit line; 'linear' detrending t   dtypet   biasi   i    (   i    i   (   i    i    (   R   t   aranget   lent   float_t   covR   (   t   yR   t   Ct   bt   a(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyR   =   s
    c         C  sM   t  |  | ƒ } g  t d ƒ D]' } t j | | |  | | | !ƒ ^ q } | S(   s6   add correlation of MA representation explicitely

    i
   (   R   R   R   t   dot(   t   art   mat   nobst   irt   tt	   acovfexpl(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   acovf_explicitF   s    :c         C  s¦   |  d } | d } d | d d | | d | d g } | j  d | | | | d | d ƒ x/ t d ƒ D]! } | d } | j  | | ƒ qt Wt j | ƒ S(   Ni   g      ð?i   i   iÿÿÿÿ(   R   R   R   t   array(   R)   R*   R'   R&   t   rhot   _t   last(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   acovf_arma11N   s    
))
c         C  sa   |  d } |  d } t  j d ƒ } d | d | d | d <| | | | d <| | d <| S(   Ni   i   i
   i    (   R   t   zeros(   R*   t   b1t   b2R1   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt	   acovf_ma2a   s    c         C  s;   |  d } t  j d ƒ } d | d | d <| | d <| S(   Ni   i
   i   i    (   R   R5   (   R*   R&   R1   (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt	   acovf_ma1p   s
    gš™™™™™é¿g        g333333ã?t   ma1t   ma2t   arma11t   ar1R+   i   R*   i   i   c         C  sü   t  j d d d d g ƒ } t  j j d |  d | d ƒ } t  j |  ƒ } t | ƒ } xG t | ƒ D]9 } | | t  j | |  d  d  d … | |  ƒ | | <qb WxM t | |  ƒ D]< } | | t  j | | | | !d  d  d … | ƒ | | <q¯ W| | | f S(	   Ng ‰°áé@gÎQÚ|Àgî|?5^:@g oÅí¿t   sizet   scaleg      à?iÿÿÿÿ(   R   R0   t   randomt   normalR5   R!   R   R(   (   t   Nt   sigmat   tapst   vt   ut   Pt   l(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   ar_generator¤   s    7:c         C  sd   |  j  | } t j j |  d d | d d | ƒ} t j j | | j ƒ  d | ƒj |  } | | S(   sŒ   Returns the autocorrelation of signal s at all lags. Adheres to the
definition r(k) = E{s(n)s*(n-k)} where E{} is the expectation operator.
t   ni   i   R   (   t   shapeR   t   fftt   ifftt	   conjugatet   real(   t   sR   RB   t   St   sxx(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   autocorr·   s    &)t   validc         C  s8   t  j |  | | ƒ t  j |  ƒ t  j | ƒ |  j d S(   sØ   Returns the correlation between two ndarrays, by calling np.correlate in
'same' mode and normalizing the result by the std of the arrays and by
their lengths. This results in a correlation = 1 for an auto-correlationiÿÿÿÿ(   R   t	   correlatet   stdRK   (   R   R$   t   mode(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt	   norm_corrÅ   s    c         K  s   |  j  | | |  S(   s9  
    call signature::

        acorr(x, normed=True, detrend=detrend_none, usevlines=True,
              maxlags=10, **kwargs)

    Plot the autocorrelation of *x*.  If *normed* = *True*,
    normalize the data by the autocorrelation at 0-th lag.  *x* is
    detrended by the *detrend* callable (default no normalization).

    Data are plotted as ``plot(lags, c, **kwargs)``

    Return value is a tuple (*lags*, *c*, *line*) where:

      - *lags* are a length 2*maxlags+1 lag vector

      - *c* is the 2*maxlags+1 auto correlation vector

      - *line* is a :class:`~matplotlib.lines.Line2D` instance
        returned by :meth:`plot`

    The default *linestyle* is None and the default *marker* is
    ``'o'``, though these can be overridden with keyword args.
    The cross correlation is performed with
    :func:`numpy.correlate` with *mode* = 2.

    If *usevlines* is *True*, :meth:`~matplotlib.axes.Axes.vlines`
    rather than :meth:`~matplotlib.axes.Axes.plot` is used to draw
    vertical lines from the origin to the acorr.  Otherwise, the
    plot style is determined by the kwargs, which are
    :class:`~matplotlib.lines.Line2D` properties.

    *maxlags* is a positive integer detailing the number of lags
    to show.  The default value of *None* will return all
    :math:`2 \mathrm{len}(x) - 1` lags.

    The return value is a tuple (*lags*, *c*, *linecol*, *b*)
    where

    - *linecol* is the
      :class:`~matplotlib.collections.LineCollection`

    - *b* is the *x*-axis.

    .. seealso::

        :meth:`~matplotlib.axes.Axes.plot` or
        :meth:`~matplotlib.axes.Axes.vlines`
           For documentation on valid kwargs.

    **Example:**

    :func:`~matplotlib.pyplot.xcorr` above, and
    :func:`~matplotlib.pyplot.acorr` below.

    **Example:**

    .. plot:: mpl_examples/pylab_examples/xcorr_demo.py
    (   t   xcorr(   t   selfR   t   kwargs(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   pltacorrÑ   s    <c         K  sÑ  t  | ƒ } | t  | ƒ k r- t d ƒ ‚ n  | t j | ƒ ƒ } | t j | ƒ ƒ } t j | | d d ƒ}	 | r§ |	 t j t j | | ƒ t j | | ƒ ƒ :}	 n  | d k rÀ | d } n  | | k sØ | d k  rë t d | ƒ ‚ n  t j | | d ƒ }
 |	 | d | | | !}	 | rƒ|  j	 |
 d g |	 |  } |  j
 |   } | j d d ƒ | j d	 d
 ƒ |  j |
 |	 |  } n> | j d d ƒ | j d	 d
 ƒ |  j |
 |	 |  \ } d } |
 |	 | | f S(   s  
    call signature::

        def xcorr(self, x, y, normed=True, detrend=detrend_none,
          usevlines=True, maxlags=10, **kwargs):

    Plot the cross correlation between *x* and *y*.  If *normed* =
    *True*, normalize the data by the cross correlation at 0-th
    lag.  *x* and y are detrended by the *detrend* callable
    (default no normalization).  *x* and *y* must be equal length.

    Data are plotted as ``plot(lags, c, **kwargs)``

    Return value is a tuple (*lags*, *c*, *line*) where:

      - *lags* are a length ``2*maxlags+1`` lag vector

      - *c* is the ``2*maxlags+1`` auto correlation vector

      - *line* is a :class:`~matplotlib.lines.Line2D` instance
         returned by :func:`~matplotlib.pyplot.plot`.

    The default *linestyle* is *None* and the default *marker* is
    'o', though these can be overridden with keyword args.  The
    cross correlation is performed with :func:`numpy.correlate`
    with *mode* = 2.

    If *usevlines* is *True*:

       :func:`~matplotlib.pyplot.vlines`
       rather than :func:`~matplotlib.pyplot.plot` is used to draw
       vertical lines from the origin to the xcorr.  Otherwise the
       plotstyle is determined by the kwargs, which are
       :class:`~matplotlib.lines.Line2D` properties.

       The return value is a tuple (*lags*, *c*, *linecol*, *b*)
       where *linecol* is the
       :class:`matplotlib.collections.LineCollection` instance and
       *b* is the *x*-axis.

    *maxlags* is a positive integer detailing the number of lags to show.
    The default value of *None* will return all ``(2*len(x)-1)`` lags.

    **Example:**

    :func:`~matplotlib.pyplot.xcorr` above, and
    :func:`~matplotlib.pyplot.acorr` below.

    **Example:**

    .. plot:: mpl_examples/pylab_examples/xcorr_demo.py
    s   x and y must be equal lengthRW   i   i   s.   maxlags must be None or strictly positive < %di    t   markert   ot	   linestyleR   N(   R!   t
   ValueErrorR   R   RU   t   sqrtR(   R   R    t   vlinest   axhlinet
   setdefaultt   plot(   RZ   R   R$   t   normedR   t	   usevlinest   maxlagsR[   t   Nxt   ct   lagsR'   R&   t   d(    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   pltxcorr  s4    8 2 i   t   unbiasediô  i   Rg   iÓ   iÔ   (   i   i    i    (P   t   __doc__t
   __future__R    t   statsmodels.compat.pythonR   t   numpyR   t   numpy.testingR   t   matplotlib.pyplott   pyplott   pltt   statsmodels.apit   apit   smt   statsmodels.tsa.arima_processR   R   R   R   R   t   statsmodels.tsa.stattoolsR   R	   t   statsmodels.graphics.tsaplotsR
   R)   R*   t   modR   t   x_acft   x_irR   R   R   R   R   R   R/   R4   R8   R9   R=   t   ar0R:   R;   t   ma0t   dictt	   comparefnt   casesRj   t   argst   printt   myacovft   myacft   othacovfRI   RS   RX   R\   t   TrueRm   t   arrvst   armat   fitt   rest   acf1t   Falset   acovf1bt   acf2R   t   acf2mt
   regressiont   yule_walkerRe   t   figureR    R!   t   subplott   ax(    (    (    sC   lib/python2.7/site-packages/statsmodels/sandbox/tsa/example_arma.pyt   <module>   sª   												
	>_	


-
