ó
áp7]c        
   @   sÆ   d  Z  d d l Z d d l m Z d d l m Z m Z d „  Z d „  Z	 d d d e e e d e d d	 „	 Z d d d d
 e d e d d „ Z d d d „ Z d d d d „ Z d d d d „ Z d S(   s   Correlation plot functions.iÿÿÿÿN(   t   utils(   t   acft   pacfc         C   sì   t  | ƒ } | r t n t } | d  k r€ |  j d } t t t j d t j	 | ƒ ƒ ƒ | d ƒ } t j
 | | d ƒ } nP t j | ƒ r¯ t j
 | t | ƒ d ƒ } n! t } t j | ƒ j t j ƒ } | j d ƒ } | | | f S(   Ni    i
   i   (   t   boolt   Falset   Truet   Nonet   shapet   mint   intt   npt   ceilt   log10t   aranget   isscalart
   asanyarrayt   astypet   max(   t   xt   lagst   zerot	   irregulart   nobst   limt   nlags(    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyt   _prepare_data_corr_plot
   s    / c   	      K   s~  | r, | | } | d  k	 r, | | } q, n  | r[ |  j | d g | |  |  j |   n  | j d d ƒ | j d d ƒ d | k rš | j d d ƒ n  |  j d	 ƒ |  j | | |  |  j | ƒ | d  k	 rz| d d k r| d
 } | d
 } | d
 } n  | j t j	 ƒ } | d c d 8<| d c d 7<|  j
 | | d  d  … d f | | d  d  … d
 f | d d ƒn  d  S(   Ni    t   markert   ot
   markersizei   t   lst	   linestyleR   gš™™™™™©?i   g      à?iÿÿÿÿt   alphag      Ð?(   R   t   vlinest   axhlinet
   setdefaultt   marginst   plott	   set_titleR   R
   t   floatt   fill_between(	   t   axt   titlet   acf_xt   confintR   R   t
   use_vlinest   vlines_kwargst   kwargs(    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyt
   _plot_corr   s0    


 gš™™™™™©?t   Autocorrelationc
      
   K   sÑ   t  j | ƒ \ } } t |  | | ƒ \ } } } |	 d k rB i  n |	 }	 d } | d k r t |  d | d | d | d | ƒ} n* t |  d | d | d | d | ƒ\ } } t | | | | | | | |	 |
  | S(   sh  Plot the autocorrelation function

    Plots lags on the horizontal and the correlations on vertical axis.

    Parameters
    ----------
    x : array_like
        Array of time-series values
    ax : Matplotlib AxesSubplot instance, optional
        If given, this subplot is used to plot in instead of a new figure being
        created.
    lags : int or array_like, optional
        int or Array of lag values, used on horizontal axis. Uses
        np.arange(lags) when lags is an int.  If not provided,
        ``lags=np.arange(len(corr))`` is used.
    alpha : scalar, optional
        If a number is given, the confidence intervals for the given level are
        returned. For instance if alpha=.05, 95 % confidence intervals are
        returned where the standard deviation is computed according to
        Bartlett's formula. If None, no confidence intervals are plotted.
    use_vlines : bool, optional
        If True, vertical lines and markers are plotted.
        If False, only markers are plotted.  The default marker is 'o'; it can
        be overridden with a ``marker`` kwarg.
    unbiased : bool
        If True, then denominators for autocovariance are n-k, otherwise n
    fft : bool, optional
        If True, computes the ACF via FFT.
    title : str, optional
        Title to place on plot.  Default is 'Autocorrelation'
    zero : bool, optional
        Flag indicating whether to include the 0-lag autocorrelation.
        Default is True.
    vlines_kwargs : dict, optional
        Optional dictionary of keyword arguments that are passed to vlines.
    **kwargs : kwargs, optional
        Optional keyword arguments that are directly passed on to the
        Matplotlib ``plot`` and ``axhline`` functions.

    Returns
    -------
    fig : Matplotlib figure instance
        If `ax` is None, the created figure.  Otherwise the figure to which
        `ax` is connected.

    See Also
    --------
    matplotlib.pyplot.xcorr
    matplotlib.pyplot.acorr

    Notes
    -----
    Adapted from matplotlib's `xcorr`.

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

    kwargs is used to pass matplotlib optional arguments to both the line
    tracing the autocorrelations and for the horizontal line at 0. These
    options must be valid for a Line2D object.

    vlines_kwargs is used to pass additional optional arguments to the
    vertical lines connecting each autocorrelation to the axis.  These options
    must be valid for a LineCollection object.

    Examples
    --------
    >>> import pandas as pd
    >>> import matplotlib.pyplot as plt
    >>> import statsmodels.api as sm

    >>> dta = sm.datasets.sunspots.load_pandas().data
    >>> dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
    >>> del dta["YEAR"]
    >>> sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=40)
    >>> plt.show()

    .. plot:: plots/graphics_tsa_plot_acf.py
    R   R   t   fftt   unbiasedN(   R    t   create_mpl_axR   R   R   R/   (   R   R(   R   R   R,   R2   R1   R)   R   R-   R.   t   figR   R   R+   R*   (    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyt   plot_acf<   s    Q
t
   ywunbiaseds   Partial Autocorrelationc	      
   K   sÅ   t  j | ƒ \ }
 } | d k r' i  n | } t |  | | ƒ \ } } } d } | d k r{ t |  d | d | d | ƒ} n$ t |  d | d | d | ƒ\ } } t | | | | | | | | |	  |
 S(   sš  
    Plot the partial autocorrelation function

    Parameters
    ----------
    x : array_like
        Array of time-series values
    ax : Matplotlib AxesSubplot instance, optional
        If given, this subplot is used to plot in instead of a new figure being
        created.
    lags : int or array_like, optional
        int or Array of lag values, used on horizontal axis. Uses
        np.arange(lags) when lags is an int.  If not provided,
        ``lags=np.arange(len(corr))`` is used.
    alpha : float, optional
        If a number is given, the confidence intervals for the given level are
        returned. For instance if alpha=.05, 95 % confidence intervals are
        returned where the standard deviation is computed according to
        1/sqrt(len(x))
    method : {'ywunbiased', 'ywmle', 'ols'}
        Specifies which method for the calculations to use:

        - yw or ywunbiased : yule walker with bias correction in denominator
          for acovf. Default.
        - ywm or ywmle : yule walker without bias correction
        - ols - regression of time series on lags of it and on constant
        - ld or ldunbiased : Levinson-Durbin recursion with bias correction
        - ldb or ldbiased : Levinson-Durbin recursion without bias correction

    use_vlines : bool, optional
        If True, vertical lines and markers are plotted.
        If False, only markers are plotted.  The default marker is 'o'; it can
        be overridden with a ``marker`` kwarg.
    title : str, optional
        Title to place on plot.  Default is 'Partial Autocorrelation'
    zero : bool, optional
        Flag indicating whether to include the 0-lag autocorrelation.
        Default is True.
    vlines_kwargs : dict, optional
        Optional dictionary of keyword arguments that are passed to vlines.
    **kwargs : kwargs, optional
        Optional keyword arguments that are directly passed on to the
        Matplotlib ``plot`` and ``axhline`` functions.

    Returns
    -------
    fig : Matplotlib figure instance
        If `ax` is None, the created figure.  Otherwise the figure to which
        `ax` is connected.

    See Also
    --------
    matplotlib.pyplot.xcorr
    matplotlib.pyplot.acorr

    Notes
    -----
    Plots lags on the horizontal and the correlations on vertical axis.
    Adapted from matplotlib's `xcorr`.

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

    kwargs is used to pass matplotlib optional arguments to both the line
    tracing the autocorrelations and for the horizontal line at 0. These
    options must be valid for a Line2D object.

    vlines_kwargs is used to pass additional optional arguments to the
    vertical lines connecting each autocorrelation to the axis.  These options
    must be valid for a LineCollection object.

    Examples
    --------
    >>> import pandas as pd
    >>> import matplotlib.pyplot as plt
    >>> import statsmodels.api as sm

    >>> dta = sm.datasets.sunspots.load_pandas().data
    >>> dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
    >>> del dta["YEAR"]
    >>> sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=40)
    >>> plt.show()

    .. plot:: plots/graphics_tsa_plot_pacf.py
    R   R   t   methodN(   R    R3   R   R   R   R/   (   R   R(   R   R   R7   R,   R)   R   R-   R.   R4   R   R   R+   R*   (    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyt	   plot_pacf¡   s    W!$
c      	   C   s  t  j | ƒ \ } } d } g  } x¯ |  D]§ \ } } | j ƒ  } | j ƒ  t | ƒ }	 t j | | |	 ƒ }
 | j |
 j ƒ  ƒ | j	 |
 | j
 d ƒ | j | j
 j ƒ  |
 d |
 d d d d d ƒ| |	 7} q( W| j | ƒ | j | ƒ | j | ƒ | j d d	 ƒ | S(
   s@  
    Consider using one of month_plot or quarter_plot unless you need
    irregular plotting.

    Parameters
    ----------
    grouped_x : iterable of DataFrames
        Should be a GroupBy object (or similar pair of group_names and groups
        as DataFrames) with a DatetimeIndex or PeriodIndex
    xticklabels : list of str
        List of season labels, one for each group.
    ylabel : str
        Lable for y axis
    ax : Matplotlib AxesSubplot instance, optional
        If given, this subplot is used to plot in instead of a new figure being
        created.
    i    t   kiÿÿÿÿt   colorst   rt	   linewidthi   gš™™™™™¹?gš™™™™™©?(   R    R3   t   copyt
   sort_indext   lenR
   R   t   appendt   meanR$   t   valuest   hlinest
   set_xtickst   set_xticklabelst
   set_ylabelR#   (   t	   grouped_xt   xticklabelst   ylabelR(   R4   t   startt   tickst   seasont   dfR   t   x_plot(    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyt   seasonal_plot  s$    
)c         C   s²   | d k r/ d d l m } | |  d d ƒn4 d d l m } m } | |  d | | d d ƒƒ}  d d d	 d
 d	 d d d
 d d d d g } t |  j d „  ƒ | d | d | ƒS(   sM  
    Seasonal plot of monthly data

    Parameters
    ----------
    x : array-like
        Seasonal data to plot. If dates is None, x must be a pandas object
        with a PeriodIndex or DatetimeIndex with a monthly frequency.
    dates : array-like, optional
        If `x` is not a pandas object, then dates must be supplied.
    ylabel : str, optional
        The label for the y-axis. Will attempt to use the `name` attribute
        of the Series.
    ax : matplotlib.axes, optional
        Existing axes instance.

    Returns
    -------
    matplotlib.Figure

    Examples
    --------
    >>> import statsmodels.api as sm
    >>> import pandas as pd

    >>> dta = sm.datasets.elnino.load_pandas().data
    >>> dta['YEAR'] = dta.YEAR.astype(int).astype(str)
    >>> dta = dta.set_index('YEAR').T.unstack()
    >>> dates = pd.to_datetime(list(map(lambda x: '-'.join(x) + '-1',
    ...                                 dta.index.values)))
    >>> dta.index = pd.DatetimeIndex(dates, freq='MS')
    >>> fig = sm.graphics.tsa.month_plot(dta)

    .. plot:: plots/graphics_tsa_month_plot.py
    iÿÿÿÿ(   t   _check_period_indext   freqt   M(   t   Seriest   PeriodIndext   indext   jt   ft   mt   at   sR   t   nt   dc         S   s   |  j  S(   N(   t   month(   t   y(    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyt   <lambda>\  t    RI   R(   N(   R   t   statsmodels.tools.dataRP   t   pandasRS   RT   RO   t   groupby(   R   t   datesRI   R(   RP   RS   RT   RH   (    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyt
   month_plot/  s    %*c         C   sš   | d k r/ d d l m } | |  d d ƒn4 d d l m } m } | |  d | | d d ƒƒ}  d d d	 d
 g } t |  j d „  ƒ | d | d | ƒS(   sE  
    Seasonal plot of quarterly data

    Parameters
    ----------
    x : array-like
        Seasonal data to plot. If dates is None, x must be a pandas object
        with a PeriodIndex or DatetimeIndex with a monthly frequency.
    dates : array-like, optional
        If `x` is not a pandas object, then dates must be supplied.
    ylabel : str, optional
        The label for the y-axis. Will attempt to use the `name` attribute
        of the Series.
    ax : matplotlib.axes, optional
        Existing axes instance.

    Returns
    -------
    matplotlib.Figure

    Examples
    --------
    >>> import statsmodels.api as sm
    >>> import pandas as pd

    >>> dta = sm.datasets.elnino.load_pandas().data
    >>> dta['YEAR'] = dta.YEAR.astype(int).astype(str)
    >>> dta = dta.set_index('YEAR').T.unstack()
    >>> dates = pd.to_datetime(list(map(lambda x: '-'.join(x) + '-1',
    ...                                 dta.index.values)))
    >>> dta.index = dates.to_period('Q')
    >>> fig = sm.graphics.tsa.quarter_plot(dta)

    .. plot:: plots/graphics_tsa_quarter_plot.py
    iÿÿÿÿ(   RP   RQ   t   Q(   RS   RT   RU   t   q1t   q2t   q3t   q4c         S   s   |  j  S(   N(   t   quarter(   R^   (    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyR_     R`   RI   R(   N(   R   Ra   RP   Rb   RS   RT   RO   Rc   (   R   Rd   RI   R(   RP   RS   RT   RH   (    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyt   quarter_plot`  s    %(   t   __doc__t   numpyR
   t   statsmodels.graphicsR    t   statsmodels.tsa.stattoolsR   R   R   R/   R   R   R   R5   R8   RO   Re   Rl   (    (    (    s<   lib/python2.7/site-packages/statsmodels/graphics/tsaplots.pyt   <module>   s   		 	c	e'1