ó
áp7]c        !   @   s”  d  Z  d d l m Z m Z m Z d d l 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
 „  Z d d d e d d „ Z d e f d „  ƒ  YZ e d k re j d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 g  ƒ Z e e d1 d2 ƒZ d d3 l m Z m Z e e e d4 d5 d6 e e ƒ d1 d7 ƒ ƒ Z  e e  ƒ Z! n  d S(8   s+   
Seasonal Decomposition by Moving Averages
iÿÿÿÿ(   t   lmapt   ranget	   iteritemsN(   t   nanmeani   (   t   _maybe_get_pandas_wrapper_freqt   _maybe_get_pandas_wrapper(   t   convolution_filter(   t   freq_to_periodc         C   s?   t  j g  t | ƒ D]% } t |  | d | … d d ƒ^ q ƒ S(   s£   
    Return means for each period in x. freq is an int that gives the
    number of periods per cycle. E.g., 12 for monthly. NaNs are ignored
    in the mean.
    Nt   axisi    (   t   npt   arrayR   t
   pd_nanmean(   t   xt   freqt   i(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/seasonal.pyt   seasonal_mean   s    c   	      C   s×  t  d „  t |  ƒ Dƒ ƒ } |  j d d t  d „  t |  d d d … ƒ Dƒ ƒ } t | | | ƒ } t | | | ƒ } t j j t j t j	 | | ƒ t j
 | | ƒ f |  | | !d d ƒd \ } } t j	 d | ƒ t j | t j | j } |  j d k r| j ƒ  } n  | |  | *t j j t j t j	 | | ƒ t j
 | | ƒ f |  | | !d d ƒd \ } } t j	 | d |  j d ƒ t j | t j | j } |  j d k rÅ| j ƒ  } n  | |  | d )|  S(   s™   
    Replace nan values on trend's end-points with least-squares extrapolated
    values with regression considering npoints closest defined points.
    c         s   s3   |  ]) \ } } t  j t  j | ƒ ƒ s | Vq d  S(   N(   R	   t   anyt   isnan(   t   .0R   t   vals(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/seasonal.pys	   <genexpr>   s    	i    i   c         s   s3   |  ]) \ } } t  j t  j | ƒ ƒ s | Vq d  S(   N(   R	   R   R   (   R   R   R   (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/seasonal.pys	   <genexpr>   s    	Niÿÿÿÿt   rcond(   t   nextt	   enumeratet   shapet   mint   maxR	   t   linalgt   lstsqt   c_t   aranget   onest   Tt   ndimt   squeeze(	   t   trendt   npointst   frontt   backt
   front_lastt
   back_firstt   kt   nt   extra(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/seasonal.pyt   _extrapolate_trend   s&    8	) +
	) 6t   additivei    c      
   C   s¿  | d k r! t |  ƒ \ } } n t |  ƒ } d } t j |  ƒ j ƒ  }  t |  ƒ } t j t j |  ƒ ƒ s{ t	 d ƒ ‚ n  | j
 d ƒ r± t j |  d k ƒ r± t	 d ƒ ‚ q± n  | d k rí | d k	 rÞ t | ƒ } | } qí t	 d ƒ ‚ n  | d k rQ| d d k r8t j d g d g | d d g ƒ | } qQt j d	 | | ƒ } n  t | ƒ d }	 t |  | |	 ƒ }
 | d
 k rŒ| d } n  | d k r®t |
 | d ƒ }
 n  | j
 d ƒ rÊ|  |
 } n
 |  |
 } t | | ƒ } | j
 d ƒ r| t j | d d ƒ:} n | t j | d d ƒ8} t j | j | | d ƒ j |  } | j
 d ƒ rk|  | |
 } n
 | | } t | | |
 | |  g ƒ } t d | d d | d d | d d | d ƒ S(   sÝ  
    Seasonal decomposition using moving averages

    Parameters
    ----------
    x : array-like
        Time series. If 2d, individual series are in columns.
    model : str {"additive", "multiplicative"}
        Type of seasonal component. Abbreviations are accepted.
    filt : array-like
        The filter coefficients for filtering out the seasonal component.
        The concrete moving average method used in filtering is determined by two_sided.
    freq : int, optional
        Frequency of the series. Must be used if x is not a pandas object.
        Overrides default periodicity of x if x is a pandas
        object with a timeseries index.
    two_sided : bool
        The moving average method used in filtering.
        If True (default), a centered moving average is computed using the filt.
        If False, the filter coefficients are for past values only.
    extrapolate_trend : int or 'freq', optional
        If set to > 0, the trend resulting from the convolution is
        linear least-squares extrapolated on both ends (or the single one
        if two_sided is False) considering this many (+1) closest points.
        If set to 'freq', use `freq` closest points. Setting this parameter
        results in no NaN values in trend or resid components.

    Returns
    -------
    results : obj
        A object with seasonal, trend, and resid attributes.

    Notes
    -----
    This is a naive decomposition. More sophisticated methods should
    be preferred.

    The additive model is Y[t] = T[t] + S[t] + e[t]

    The multiplicative model is Y[t] = T[t] * S[t] * e[t]

    The seasonal component is first removed by applying a convolution
    filter to the data. The average of this smoothed series for each
    period is the returned seasonal component.

    See Also
    --------
    statsmodels.tsa.filters.bk_filter.bkfilter
    statsmodels.tsa.filters.cf_filter.xffilter
    statsmodels.tsa.filters.hp_filter.hpfilter
    statsmodels.tsa.filters.convolution_filter
    s,   This function does not handle missing valuest   mi    sJ   Multiplicative seasonality is not appropriate for zero and negative valuessh   You must specify a freq or x must be a pandas object with a timeseries index with a freq not set to Nonei   g      à?i   g      ð?R   R   t   seasonalR"   t   residt   observedi   N(   t   NoneR   R   R	   t
   asanyarrayR!   t   lent   allt   isfinitet
   ValueErrort
   startswithR   R   R
   t   repeatt   intR   R+   R   t   meant   tileR   R    t   DecomposeResult(   R   t   modelt   filtR   t	   two_sidedt   extrapolate_trendt   _pandas_wrappert   pfreqt   nobst   nsidesR"   t	   detrendedt   period_averagesR.   R/   t   results(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/seasonal.pyt   seasonal_decompose5   sP    6	/
$
R<   c           B   s   e  Z d  „  Z d „  Z RS(   c         K   sC   x* t  | ƒ D] \ } } t |  | | ƒ q Wt |  j ƒ |  _ d  S(   N(   R   t   setattrR3   R0   RC   (   t   selft   kwargst   keyt   value(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/seasonal.pyt   __init__«   s    c         C   sÎ  d d l  m } | ƒ  } | j d d d t ƒ\ } } t |  j d ƒ r|  j j d | d d	 t ƒ | d j d
 ƒ |  j	 j d | d d	 t ƒ | d j d ƒ |  j
 j d | d d	 t ƒ | d j d ƒ |  j j d | d d	 t ƒ | d j d ƒ n¼ | d j |  j ƒ | d j d
 ƒ | d j |  j	 ƒ | d j d ƒ | d j |  j
 ƒ | d j d ƒ | d j |  j ƒ | d j d ƒ | d j d ƒ | d j d |  j ƒ | j ƒ  | S(   Niÿÿÿÿ(   t   _import_mpli   i   t   sharext   plott   axi    t   legendt   Observedt   Trendi   t   Seasonali   t   Residualt   Time(   t   statsmodels.graphics.utilsRO   t   subplotst   Truet   hasattrR0   RQ   t   Falset
   set_ylabelR"   R.   R/   t
   set_xlabelt   set_xlimRC   t   tight_layout(   RJ   RO   t   pltt   figt   axes(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/seasonal.pyRQ   °   s0    	
(   t   __name__t
   __module__RN   RQ   (    (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/seasonal.pyR<   ª   s   	t   __main__iÎÿÿÿi¯   i•   iÖ   i÷   ií   iá   iI  iÙ  i)  i  ié  i  iÉ  iÃ   i°   iQ  iï   i€   if   iè   i­  i   ib   i+   isÿÿÿi³ÿÿÿióÿÿÿi}   ii  iÓÿÿÿi¸   R   i   (   t	   DataFramet
   date_ranget   starts   1/1/1951t   periodst   Q("   t   __doc__t   statsmodels.compat.pythonR    R   R   t   numpyR	   t   pandas.core.nanopsR   R   t   filters._utilsR   R   t   filters.filtertoolsR   t   statsmodels.tsa.tsatoolsR   R   R+   R1   R[   RH   t   objectR<   Re   R
   R   RG   t   pandasRh   Ri   R3   t   datat   res(    (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/seasonal.pyt   <module>   s*   			t#$-