
p7]c           @   s  d  Z  d d l m Z d d l Z d d l Z d d l m Z m	 Z	 m
 Z
 d d l m Z d d l m Z d d l m Z d d l m Z d d	 l m Z m Z m Z d d
 l m Z d d l m Z d d l j j Z d   Z d   Z  d   Z! d   Z" d   Z# d   Z$ d   Z% d   Z& d   Z' d   Z( d   Z) i	 e j& d d f 6e j' d d f 6e j$ d e* f 6e j( d d f 6e j) d d f 6e j% d e* f 6e j" e* d f 6e j! e* d f 6e j  e* e* f 6Z+ i	 e& d d f 6e' d d f 6e$ d e* f 6e( d d f 6e) d d f 6e% d e* f 6e" e* d f 6e! e* d f 6e  e* e* f 6Z, d e f d     YZ- d e f d     YZ. e e. e-  d e f d     YZ/ d e/ f d      YZ0 d! e/ f d"     YZ1 d S(#   s$  
Notes
-----
Code written using below textbook as a reference.
Results are checked against the expected outcomes in the text book.

Properties:
Hyndman, Rob J., and George Athanasopoulos. Forecasting: principles and
practice. OTexts, 2014.

Author: Terence L van Zyl
Modified: Kevin Sheppard
i(   t   string_typesN(   t   basinhoppingt   brutet   minimize(   t   sqeuclidean(   t
   inv_boxcox(   t   boxcox(   t   Results(   t   populate_wrappert   union_dictst   ResultsWrapper(   t   TimeSeriesModel(   t   freq_to_periodc         C   s   |  | | j  t j  <| d  \ } } } }	 }
 } d | } d | } | | } d | (d | (|	 | d <|
 | d <| | | | | | f S(   s"   Initialization for the Holt Modelsi   i   i    (   t   astypet   npt   bool(   t   xt   xit   pt   yt   lt   bt   alphat   betat   _t   l0t   b0t   phit   alphact   betact   y_alpha(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt
   _holt_init   s    




c
         C   sv   t  |  | | | | |  \ }
 } } } } } x9 t d |  D]( } | | d | | | d | | <q= Wt | |  S(   sH   
    Simple Exponential Smoothing
    Minimization Function
    (,)
    i   (   R   t   rangeR   (   R   R   R   R   R   R   t   st   mt   nt   max_seenR   R   R   R   R   R   t   i(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt__,   s    -&c
         C   s   t  |  | | | | |  \ }
 } } } } } |
 d k r= |	 S| |
 k rM |	 Sx{ t d |  D]j } | | d | | | d | | d | | | <| | | | | d | | | d | | | <q] Wt | | | |  S(   s]   
    Multiplicative and Multiplicative Damped
    Minimization Function
    (M,) & (Md,)
    g        i   (   R   R    R   (   R   R   R   R   R   R   R!   R"   R#   R$   R   R   R   R   R   R   R%   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt_mul_dam8   s    -26c
         C   s   t  |  | | | | |  \ }
 } } } } } |
 d k r= |	 S| |
 k rM |	 Sx{ t d |  D]j } | | d | | | d | | | d | | <| | | | | d | | | | d | | <q] Wt | | | |  S(   sQ   
    Additive and Additive Damped
    Minimization Function
    (A,) & (Ad,)
    g        i   (   R   R    R   (   R   R   R   R   R   R   R!   R"   R#   R$   R   R   R   R   R   R   R%   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt_add_damI   s    -26c      	   C   s   |  | | j  t j  <| d  \ } }	 }
 } } } | d } d | } d |	 } d |
 } | | } |
 | } d | (d | (d | (| | d <| | d <| | | *| |	 |
 | | | | | | f	 S(   s3   Initialization for the Holt Winters Seasonal Modelsi   i   i    (   R   R   R   (   R   R   R   R   R   R   R!   R"   R   R   t   gammaR   R   R   t   s0R   R   t   gammacR   t   y_gamma(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt_win_initZ   s    








c
      	   C   s   t  |  | | | | | | |  \	 }
 } } } } } } } } |
 d k rL |	 S| d |
 k r` |	 Sx{ t d |  D]j } | | d | | d | | | d | | <| | d | | d | | | d | | | d <qp Wt | | | d  |  S(   sD   
    Multiplicative Seasonal
    Minimization Function
    (,M)
    g        i   (   R-   R    R   (   R   R   R   R   R   R   R!   R"   R#   R$   R   R   R)   R   R   R   R+   R   R,   R%   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt_win__mulm   s    9.:c
      	   C   s   t  |  | | | | | | |  \	 }
 } } } } } } } } |
 d k rL |	 S| d |
 k r` |	 Sx t d |  D]r } | | d |
 | | d | | | d | | <| | d | | | d | | | d | | | d <qp Wt | | | d  |  S(   s>   
    Additive Seasonal
    Minimization Function
    (,A)
    g        i   (   R-   R    R   (   R   R   R   R   R   R   R!   R"   R#   R$   R   R   R)   R   R   R   R+   R   R,   R%   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt_win__add   s    92>c
      	   C   sb  t  |  | | | | | | |  \	 }
 } } } } } } } } |
 | d k rP |	 S| |
 k sl | d |
 k rp |	 Sx t d |  D] } | | d | | d | | | d | | | d | | <| | | | | d | | | | d | | <| | d | | d | | | d | | | d | | | d <q Wt | | | | | d  |  S(   sp   
    Additive and Additive Damped with Multiplicative Seasonal
    Minimization Function
    (A,M) & (Ad,M)
    g        i   (   R-   R    R   (   R   R   R   R   R   R   R!   R"   R#   R$   R   R   R)   R   R   R   R+   R   R,   R%   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt_win_add_mul_dam   s    9'21c
      	   C   sb  t  |  | | | | | | |  \	 }
 } } } } } } } } |
 | d k rP |	 S| |
 k sl | d |
 k rp |	 Sx t d |  D] } | | d | | d | | | d | | d | | | <| | | | | d | | | d | | | <| | d | | d | | d | | | | d | | | d <q Wt | | | | | d  |  S(   s|   
    Multiplicative and Multiplicative Damped with Multiplicative Seasonal
    Minimization Function
    (M,M) & (Md,M)
    g        i   (   R-   R    R   (   R   R   R   R   R   R   R!   R"   R#   R$   R   R   R)   R   R   R   R+   R   R,   R%   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt_win_mul_mul_dam   s    9'24c
      	   C   sj  t  |  | | | | | | |  \	 }
 } } } } } } } } |
 | d k rP |	 S| |
 k sl | d |
 k rp |	 Sx t d |  D] } | | d |
 | | d | | | d | | | d | | <| | | | | d | | | | d | | <| | d | | | d | | | d | | | d | | | d <q Wt | | | | | d  |  S(   sj   
    Additive and Additive Damped with Additive Seasonal
    Minimization Function
    (A,A) & (Ad,A)
    g        i   (   R-   R    R   (   R   R   R   R   R   R   R!   R"   R#   R$   R   R   R)   R   R   R   R+   R   R,   R%   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt_win_add_add_dam   s    9'2Nc
      	   C   sj  t  |  | | | | | | |  \	 }
 } } } } } } } } |
 | d k rP |	 S| |
 k sl | d |
 k rp |	 Sx t d |  D] } | | d |
 | | d | | | d | | d | | | <| | | | | d | | | d | | | <| | d | | | d | | d | | | | d | | | d <q Wt | | | | | d  |  S(   sv   
    Multiplicative and Multiplicative Damped with Additive Seasonal
    Minimization Function
    (M,A) & (M,Ad)
    g        i   (   R-   R    R   (   R   R   R   R   R   R   R!   R"   R#   R$   R   R   R)   R   R   R   R+   R   R,   R%   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _holt_win_mul_add_dam   s    9'2Nt   mult   addt   HoltWintersResultsc           B   s;   e  Z d  Z d   Z d d d  Z d d  Z d   Z RS(   s  
    Holt Winter's Exponential Smoothing Results

    Parameters
    ----------
    model : ExponentialSmoothing instance
        The fitted model instance
    params : dict
        All the parameters for the Exponential Smoothing model.

    Attributes
    ----------
    params: dict
        All the parameters for the Exponential Smoothing model.
    params_formatted: pd.DataFrame
        DataFrame containing all parameters, their short names and a flag
        indicating whether the parameter's value was optimized to fit the data.
    fittedfcast: array
        An array of both the fitted values and forecast values.
    fittedvalues: array
        An array of the fitted values. Fitted by the Exponential Smoothing
        model.
    fcastvalues: array
        An array of the forecast values forecast by the Exponential Smoothing
        model.
    sse: float
        The sum of squared errors
    level: array
        An array of the levels values that make up the fitted values.
    slope: array
        An array of the slope values that make up the fitted values.
    season: array
        An array of the seasonal values that make up the fitted values.
    aic: float
        The Akaike information criterion.
    bic: float
        The Bayesian information criterion.
    aicc: float
        AIC with a correction for finite sample sizes.
    resid: array
        An array of the residuals of the fittedvalues and actual values.
    k: int
        the k parameter used to remove the bias in AIC, BIC etc.
    optimized: bool
        Flag indicating whether the model parameters were optimized to fit
        the data.
    mle_retvals:  {None, scipy.optimize.optimize.OptimizeResult}
        Optimization results if the parameters were optimized to fit the data.
    c         K   s,   | j  |  _  t t |   j | | |  d  S(   N(   t   datat   superR6   t   __init__(   t   selft   modelt   paramst   kwargs(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR9   ,  s    c         C   s   |  j  j |  j | |  S(   s  
        In-sample prediction and out-of-sample forecasting

        Parameters
        ----------
        start : int, str, or datetime, optional
            Zero-indexed observation number at which to start forecasting, ie.,
            the first forecast is start. Can also be a date string to
            parse or a datetime type. Default is the the zeroth observation.
        end : int, str, or datetime, optional
            Zero-indexed observation number at which to end forecasting, ie.,
            the first forecast is start. Can also be a date string to
            parse or a datetime type. However, if the dates index does not
            have a fixed frequency, end must be an integer index if you
            want out of sample prediction. Default is the last observation in
            the sample.

        Returns
        -------
        forecast : array
            Array of out of sample forecasts.
        (   R;   t   predictR<   (   R:   t   startt   end(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR>   0  s    i   c         C   s   yg t  |  j j d d  } |  j j d | } |  j j d | | } |  j j |  j d | d | SWn0 t t f k
 r |  j j d | |  j  j SXd S(   s%  
        Out-of-sample forecasts

        Parameters
        ----------
        steps : int
            The number of out of sample forecasts from the end of the
            sample.

        Returns
        -------
        forecast : array
            Array of out of sample forecasts
        t   freqi   iR?   R@   t   hN(	   t   getattrR;   t   _indexR>   R<   t   AttributeErrort
   ValueErrort   _predictt   fcastvalues(   R:   t   stepsRA   R?   R@   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   forecastI  s    #c      
   C   sd  d d l  m } d d l m } |  j } | j j d } d } t |  j j t	 j
  rm |  j j j d } n* t |  j j t	 j  r |  j j j } n  |  j j d. k r d. n	 |  j j } i d d 6d d	 6d
 d 6d
 d 6d d. 6} |  j d } | r t n t }	 t | t  r| n
 |  j d }
 t |
 t  rFd j |
  }
 n  d | g f d | j j g f d t t j |  j   g f d | |  j j g f d | |  j j g f d t |  g f d t |	  g f d t |
  g f g } d t t |  j j   g f d d j |  j  g f d d j |  j  g f d d j |  j  g f d d j |  j   g f d/ d0 g } |   } | j! |  d! | d" | d# | |  j" } d$   } g  } xi | j#   D][ \ } } | j$ | | j% d%  d& j | j% d  d& j t t& | j% d'    g  qW| | d( d) d* d+ g d# d, d- t' | j(  } | j) j$ |  | S(1   s6  
        Summarize the fitted Model

        Returns
        -------
        smry : Summary instance
            This holds the summary table and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary.Summary
        i(   t   Summary(   t   SimpleTables    Model Resultst   endogi    t   AdditiveR5   t   additivet   MultiplicativeR4   t   multiplicativet   Nonet
   use_boxcoxt   lamdas	   {:>10.5f}s   Dep. Variable:s   Model:s
   Optimized:s   Trend:s	   Seasonal:s   Seasonal Periods:s   Box-Cox:s   Box-Cox Coeff.:s   No. Observations:t   SSEs   {:5.3f}t   AICt   BICt   AICCs   Date:s   Time:t   gleftt   grightt   titlec         S   s   t  j |   } d } | d k r9 t t  j |   } n  | d k sQ | d k  r^ d j |   St d | d  } d j |  } | j |   S(   Ni   i    i   is	   {:>20.5g}i   s   {{:>20.{0}f}}(   R   t   abst   intt   log10t   formatt   min(   R   t   abs_xt   scalet   dect   fmt(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   _fmt  s    i   s   {0:>20}i   t   headerst   coefft   codet	   optimizedt    t   stubsN(   s   Date:N(   s   Time:N(*   t   statsmodels.iolib.summaryRK   t   statsmodels.iolib.tableRL   R;   t	   __class__t   __name__t
   isinstanceRM   t   pdt	   DataFramet   columnst   Seriest   namet   seasonalRR   t   seasonal_periodsR<   t   Truet   FalseR    t   floatR_   t   strR   t   anyRi   t   trendt   lent   sset   aict   bict   aicct   add_table_2colst   params_formattedt   iterrowst   appendt   ilocR   t   listt   indext   tables(   R:   RK   RL   R;   R[   t   dep_variableRw   t   lookupt	   transformt   box_cox_transformt   box_cox_coefft   top_leftt	   top_rightt   smryt	   formattedRe   t   tabR   t   valst   params_table(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   summarya  s^    	$"				*N(   Ro   t
   __module__t   __doc__R9   RR   R>   RJ   R   (    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR6      s
   1	t   HoltWintersResultsWrapperc           B   si   e  Z i d  d 6d  d 6d  d 6d  d 6d  d 6Z e e j e  Z i d d 6d d 6Z e e j e  Z RS(	   t   rowst   fittedvaluest   levelt   residt   seasont   slopet   datesR>   RJ   (   Ro   R   t   _attrsR	   R
   t   _wrap_attrst   _methodst   _wrap_methods(    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR     s   



t   ExponentialSmoothingc           B   s   e  Z d  Z d e d d d d d d  Z d d d  Z d d d d e e e e d d d e d  Z d   Z	 d d d d d d d d d d d d d  Z
 RS(   sY  
    Holt Winter's Exponential Smoothing

    Parameters
    ----------
    endog : array-like
        Time series
    trend : {"add", "mul", "additive", "multiplicative", None}, optional
        Type of trend component.
    damped : bool, optional
        Should the trend component be damped.
    seasonal : {"add", "mul", "additive", "multiplicative", None}, optional
        Type of seasonal component.
    seasonal_periods : int, optional
        The number of periods in a complete seasonal cycle, e.g., 4 for
        quarterly data or 7 for daily data with a weekly cycle.

    Returns
    -------
    results : ExponentialSmoothing class

    Notes
    -----
    This is a full implementation of the holt winters exponential smoothing as
    per [1]_. This includes all the unstable methods as well as the stable
    methods. The implementation of the library covers the functionality of the
    R library as much as possible whilst still being Pythonic.

    References
    ----------
    .. [1] Hyndman, Rob J., and George Athanasopoulos. Forecasting: principles
        and practice. OTexts, 2014.
    t   nonec	   	      C   s  t  t |   j | d  | | d | |  j j t j  |  _ | d k rd i d d 6d d 6| } n  | |  _ | |  _	 | d k r i d d 6d d 6| } n  | |  _
 | d k |  _ | d k |  _ |  j d k s |  j
 d k rt j | d k  rt d   n  |  j	 r(|  j r(t d   n  |  j r|| |  _ | d  k r[t |  j  |  _ n  |  j d	 k rt d
   qn	 d |  _ t |  j  |  _ d  S(   Nt   missingRO   RQ   R5   R4   g        sW   endog must be strictly positive when using multiplicative trend or seasonal components.s#   Can only dampen the trend componenti   s'   seasonal_periods must be larger than 1.i    (   RO   RQ   (   RO   RQ   (   R4   R5   (   R4   R5   (   R8   R   R9   RR   RM   R   R   t   doubleR}   t   dampedRv   t   trendingt	   seasoningR|   RF   Rw   R   t   _index_freqR~   t   nobs(	   R:   RM   R}   R   Rv   Rw   R   RA   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR9     s0    			3			c         C   s   | d k r5 t |  j d d  } |  j d | } n  |  j d | d |  \ } } } } | d k r} |  j d | |  } n |  j d d |  } | j | | | d !S(	   s  
        Returns in-sample and out-of-sample prediction.

        Parameters
        ----------
        params : array
            The fitted model parameters.
        start : int, str, or datetime
            Zero-indexed observation number at which to start forecasting, ie.,
            the first forecast is start. Can also be a date string to
            parse or a datetime type.
        end : int, str, or datetime
            Zero-indexed observation number at which to end forecasting, ie.,
            the first forecast is start. Can also be a date string to
            parse or a datetime type.

        Returns
        -------
        predicted values : array
        RA   i   iR?   R@   i    RB   N(   RR   RC   RD   t   _get_prediction_indexRG   t   fittedfcast(   R:   R<   R?   R@   RA   t   out_of_samplet   prediction_indext   res(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR>      s    	c   4      C   s  | d( k	 r t |  n d( } | d( k	 r6 t |  n d( } | d( k	 rT t |  n d( } | d( k	 rr t |  n d( } |
 d( k	 r t |
  n d( |  _ | d( k	 r t |  n d( |  _ |  j } |  j } |  j } |  j } |  j } |  j	 } |  j
 } d( } | r| n d } | d k r5d } t | |  } nT t | t  r\| } t | |  } n- | rwt |  \ } } n d( } | j   } t j |  d k rt d   n  t j | d t j |  _ } t j |  j  } t j |  j  } t j |  j | d  } t j d |  } t j t j  j } |  j   \ }  }! }" t j | d t j }# | rH| d( k	 rw| n d t | d  }$ | d( k	 r| n | rd	 |$ n | }% d( }& | d( k	 r| n d
 }' | rk| d( k	 r| n d d |$ }& t j | d( k | o| d( k | d( k |
 d( k | o5| d( k | d( k oD| g t g |  }# t | | f }( n | rt j | d( k | d( k t |
 d( k | d( k | d( k o| g t g |  }# t d( | f }( nB t j | d( k t t |
 d( k t t g t g |  }# t d) }( |$ |% |& |  |! |' g |" | (t j |#  r|# t j t t t t t t g t g |  @}) |) j  t j  }) t j d* d+ d, d- d. d/ g d0 g |  }* |) j  t j!  | | | | | | |  j | f	 }+ |	 d( k rBt j |)  rB| rBt" |( |* |) |+ d d d t d d( }, |, \ | |) <} }- }- n |	 d( k	 rt j# t j |	   }	 t$ |	  |# j%   k rt d j& t$ |#  t$ |	     n  |	 | |# <n  |# j  t j!  | | | | | | |  j | f	 }+ |( t j | |#  |+  } |# j  t j!  | | | | | | |  j | f	 }+ | rtt' |( | |# d i |+ d 6|* |# d 6d d }, |, j( j) }. n, t* |( | |# d |+ d |* |# }, |, j) }. |. sd d l+ m, }/ d d l- m. }0 |/ d |0  n  |, j/ | |# <|, } n3 d d l+ m, }/ d d l- m0 }1 d }2 |/ |2 |1  | d  \ } } } }  }! } | d }" n  |  j1 d d d | d | d  | d! | d" |  d# |! d$ |" d% | d& | d' |#  }3 | |3 j2 _3 |3 S(1   sd
  
        Fit the model

        Parameters
        ----------
        smoothing_level : float, optional
            The alpha value of the simple exponential smoothing, if the value
            is set then this value will be used as the value.
        smoothing_slope :  float, optional
            The beta value of the Holt's trend method, if the value is
            set then this value will be used as the value.
        smoothing_seasonal : float, optional
            The gamma value of the holt winters seasonal method, if the value
            is set then this value will be used as the value.
        damping_slope : float, optional
            The phi value of the damped method, if the value is
            set then this value will be used as the value.
        optimized : bool, optional
            Estimate model parameters by maximizing the log-likelihood
        use_boxcox : {True, False, 'log', float}, optional
            Should the Box-Cox transform be applied to the data first? If 'log'
            then apply the log. If float then use lambda equal to float.
        remove_bias : bool, optional
            Remove bias from forecast values and fitted values by enforcing
            that the average residual is equal to zero.
        use_basinhopping : bool, optional
            Using Basin Hopping optimizer to find optimal values
        start_params: array, optional
            Starting values to used when optimizing the fit.  If not provided,
            starting values are determined using a combination of grid search
            and reasonable values based on the initial values of the data
        initial_level: float, optional
            Value to use when initializing the fitted level.
        initial_slope: float, optional
            Value to use when initializing the fitted slope.
        use_brute: bool, optional
            Search for good starting values using a brute force (grid)
            optimizer. If False, a naive set of starting values is used.

        Returns
        -------
        results : HoltWintersResults class
            See statsmodels.tsa.holtwinters.HoltWintersResults

        Notes
        -----
        This is a full implementation of the holt winters exponential smoothing
        as per [1]. This includes all the unstable methods as well as the
        stable methods. The implementation of the library covers the
        functionality of the R library as much as possible whilst still
        being Pythonic.

        References
        ----------
        [1] Hyndman, Rob J., and George Athanasopoulos. Forecasting: principles
            and practice. OTexts, 2014.
        g      ?t   logg        i   s!   Only 1 dimensional data supportedt   dtypei   g      ?g?gGz?g?t   Nsi   t   full_outputt   finishs5   start_params must have {0} values but has {1} insteadt   minimizer_kwargst   argst   boundst   stepsizeg{Gz?i(   t   warn(   t   ConvergenceWarnings3   Optimization failed to converge. Check mle_retvals.(   t   EstimationWarningsV   Model has no free parameters to estimate. Set optimized=False to suppress this warningRB   i    t   smoothing_levelt   smoothing_slopet   smoothing_seasonalt   damping_slopet   initial_levelt   initial_slopet   initial_seasonsRS   t   remove_biast   is_optimizedN(   NN(   g        g      ?(   g        g      ?(   g        g      ?(   g        N(   g        N(   g        g      ?(   NN(4   RR   Rz   t   _l0t   _b0RM   R   R   R   R}   Rv   Rw   R   Rp   t   squeezeR   t   ndimRF   t   ascontiguousarrayR   t   _yt   zerosR   t   finfot   maxt   initial_valuest
   zeros_likeR   t   arrayRx   t	   SMOOTHERSRy   R|   R   t   uint8R   t
   atleast_1dR~   t   sumR_   R   t   lowest_optimization_resultt   successR   t   warningsR   t   statsmodels.tools.sm_exceptionsR   R   R   RG   t   _resultst   mle_retvals(4   R:   R   R   R   R   Ri   RS   R   t   use_basinhoppingt   start_paramsR   R   t	   use_bruteR   R   R)   R   R7   R   R   R   R}   Rv   R"   t   optRT   R   t   lvlsR   R!   R   R$   R   R   R*   R   t
   init_alphat	   init_betat
   init_gammat   init_phit   funct   txiR   R   R   R   R   R   R   R   t   messaget   hwfit(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   fit   s    ?!!							%('###
00!(	00	#	
	c         C   s  |  j  } |  j } |  j } |  j } |  j } |  j } |  j } |  j } | rF| d k r | t	 j
 |  j  | d k j   n | } | d k r| r| | | | !| |  }	 }
 | d k r t	 j t	 j |	 j    t	 j |
 j    |  } q|	 |
 | j   } n  | d k r/t | |  |  n t | |  |  } n | r| d k rb| d n | } | d k r| d k r| d | d n | d | d } n  g  } n% | d k r| d } n  d } g  } | | | f S(   s<  
        Compute initial values used in the exponential smoothing recursions

        Returns
        -------
        initial_level : float
            The initial value used for the level component
        initial_slope : {float, None}
            The initial value used for the trend component
        initial_seasons : list
            The initial values used for the seasonal components

        Notes
        -----
        Convenience function the exposes the values used to initialize the
        recursions. When optimizing parameters these are used as starting
        values.

        Method used to compute the initial value depends on when components
        are included in the model.  In a simple exponential smoothing model
        without trend or a seasonal components, the initial value is set to the
        first observation. When a trend is added, the trend is initialized
        either using y[1]/y[0], if multiplicative, or y[1]-y[0]. When the
        seasonal component is added the initialization adapts to account for
        the modified structure.
        i    R4   i   N(   R   R}   Rv   R   R   Rw   R   R   RR   R   t   arangeR   t   meant   expR   R   (   R:   R   R}   Rv   R   R   R"   R   R   t   leadt   lagR*   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR     s4    								8873	c   <      C   s  | } | } | } | } |  j  } |  j } |  j } |  j } |  j } |  j } |  j } | rc | n d } |	 d k r d }
 t | d  } nx t |	 t	  r |	 }
 t | |
  } nQ |	 r t |  \ } }
 n6 d4 }
 | j   } t j |  d k rt d   n  t j |  j f  } t j |  j f  } d | } | | | (| rWd | } n  | rud | } | | | (n  t j |  j | d f  } t j |  j | d f  } t j |  j | | d f  }  | | d <| | d <| |  | *| r+t j t j | | d  t j d | d d   n t j d | d d  }! i t j d 6t j d 6d	   d4 6| }" i t j d 6t j d 6d
   d4 6| }# i t j d 6t j d 6d   d4 6| }$ |  j }% | d k rx t d |% d  D] }& | |& d |  |& d | |" | |& d |$ | |& d |   | |& <| ry| |# | |& | |& d  | |$ | |& d |  | |& <n  | |& d |" | |& d |$ | |& d |   | |  |& d |  |& | d <qW| d |% d !j   }' |  | |% | !j   }( | |% | |% )| r@|$ | |%  |  | |% *|$ | |% |!  | |% )n  |" | |  } g  t | d d  D] }) |  |% d |) | ^ qd|  |% | d )| |  |  }* n| d k rx t d |% d  D] }& | |& d | |  |& d | |" | |& d |$ | |& d |   | |& <| r[| |# | |& | |& d  | |$ | |& d |  | |& <n  | |& d | |" | |& d |$ | |& d |   | |  |& d |  |& | d <qW| d |% d !j   }' |  | |% | !j   }( | |% | |% )| r&|$ | |%  |  | |% *|$ | |% |!  | |% )n  |" | |  } g  t | d d  D] }) |  |% d |) | ^ qJ|  |% | d )| |  |  }* n'x t d |% d  D] }& | |& d | |" | |& d |$ | |& d |   | |& <| r| |# | |& | |& d  | |$ | |& d |  | |& <qqW| d |% d !j   }' |  | |% | !j   }( | |% | |% )| r|$ | |%  |  | |% *|$ | |% |!  | |% )n  |" | |  } | }* | d |% d !j   }+ |	 s|	 d k st |	 t	  rZt |* |
  }* t |+ |
  }+ |# | |%  |+  }' | d k r@|* t | |
  |%  }( qZ|* t | |
  |%  }( n  t |* | d  |  }, | | d | d d | }- |  j t j |, |  j  |- d }. |  j |- d d k rd |- d |- d |  j |- d }/ n	 t j }/ |. |/ }0 |  j t j |, |  j  |- t j |  j  }1 | |* | d  }2 | rd	|* |2 j   7}* n  i
 | d 6| d 6| d 6| r	| n t j  d 6| d d 6| d | d 6|  |  d 6|	 d 6|
 d 6| d 6|  _! d d d d d d g }3 |3 g  t |  D] }& d j" |&  ^ q	7}3 d d d d d d g }4 |4 g  t |  D] }& d j" |&  ^ q>
7}4 | | | | d | d | g }5 |5 |  |  j#   7}5 t$ t% d    |5   }5 t j& |5  }5 | d4 k r
t j t' |3  d! t j( }6 n | j) t j(  }6 t* | | t* | | g }7 |7 t* g | 7}7 t+ j, g  t- |3 |5 |6  D] \ }8 }9 }: |8 |9 |: g ^ q6d" d# d$ d% g d& |4 }5 |5 j. |7 }5 t/ |  |  j! d' |* d( |* | d  d) |* | d d* |, d+ |+ d, |' d- |( d. |. d/ |1 d0 |0 d1 |2 d2 |- d3 |5 d% |6 }; t0 |;  S(5   s   
        Helper prediction function

        Parameters
        ----------
        h : int, optional
            The number of time steps to forecast ahead.
        g      ?R   g        i   s!   Only 1 dimensional data supportedi    R4   R5   c         S   s   |  S(   N(    (   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   <lambda>Q  Rj   c         S   s   d S(   Ni    (    (   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR   U  Rj   c         S   s   d S(   Ni    (    (   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR   Y  Rj   i   i   R   R   R   R   R   R   R   RS   RT   R   R   R   R)   s   l.0s   b.0R   s   s.{0}s   initial_seasons.{0}c         S   s   |  d  k r t j S|  S(   N(   RR   R   t   nan(   t   v(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR     Rj   R   Rs   Ru   t   paramRi   R   R   R   RH   R   R   R   R   R   R   R   R   t   kR   N(1   RM   R   R   R   R}   Rv   Rw   R   Rp   Rz   RR   R   R   R   t   NotImplementedErrorR   R   t   cumsumt   repeatR   t   multiplyR5   t   dividet   subtractt   powerR    t   copyR   R   R   t   infR   R   R<   R_   t   tolistR   t   mapR   R~   R   R   Rx   Rq   Rr   t   zipt   locR6   R   (<   R:   RB   R   R   R   R   R   R   R   RS   RT   R   R   R   R   R)   R   R7   R   R   R   R}   Rv   R"   R   R   R,   R   R   R+   R   R   R!   t   phi_ht   trendedt   detrendt   dampenR   R%   R   R   t   jt   fittedR   R   R   R   t   aicc_penaltyR   R   R   t   codest   idxR   Ri   t   includedt   ct   ft   oR   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyRG     s8   							

!


W





	1#1#C1#5#C1'!%(	
1
,	, !=	N(   Ro   R   R   RR   Ry   R9   R>   Rx   R   R   RG   (    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR     s   !	 		9		t   SimpleExpSmoothingc           B   s/   e  Z d  Z d   Z d e d d e d  Z RS(   s?  
    Simple Exponential Smoothing

    Parameters
    ----------
    endog : array-like
        Time series

    Returns
    -------
    results : SimpleExpSmoothing class

    Notes
    -----
    This is a full implementation of the simple exponential smoothing as
    per [1]_.  `SimpleExpSmoothing` is a restricted version of
    :class:`ExponentialSmoothing`.

    See Also
    --------
    ExponentialSmoothing
    Holt

    References
    ----------
    .. [1] Hyndman, Rob J., and George Athanasopoulos. Forecasting: principles
        and practice. OTexts, 2014.
    c         C   s   t  t |   j |  d  S(   N(   R8   R  R9   (   R:   RM   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR9     s    c         C   s1   t  t |   j d | d | d | d | d |  S(   s8  
        Fit the model

        Parameters
        ----------
        smoothing_level : float, optional
            The smoothing_level value of the simple exponential smoothing, if
            the value is set then this value will be used as the value.
        optimized : bool, optional
            Estimate model parameters by maximizing the log-likelihood
        start_params: array, optional
            Starting values to used when optimizing the fit.  If not provided,
            starting values are determined using a combination of grid search
            and reasonable values based on the initial values of the data
        initial_level: float, optional
            Value to use when initializing the fitted level.
        use_brute: bool, optional
            Search for good starting values using a brute force (grid)
            optimizer. If False, a naive set of starting values is used.

        Returns
        -------
        results : HoltWintersResults class
            See statsmodels.tsa.holtwinters.HoltWintersResults

        Notes
        -----
        This is a full implementation of the simple exponential smoothing as
        per [1].

        References
        ----------
        [1] Hyndman, Rob J., and George Athanasopoulos. Forecasting: principles
            and practice. OTexts, 2014.
        R   Ri   R   R   R   (   R8   R  R   (   R:   R   Ri   R   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR     s    %N(   Ro   R   R   R9   RR   Rx   R   (    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR    s   		t   Holtc        	   B   s>   e  Z d  Z e e d  Z d d d e d d d e d  Z RS(   s  
    Holt's Exponential Smoothing

    Parameters
    ----------
    endog : array-like
        Time series
    exponential : bool, optional
        Type of trend component.
    damped : bool, optional
        Should the trend component be damped.

    Returns
    -------
    results : Holt class

    Notes
    -----
    This is a full implementation of the Holt's exponential smoothing as
    per [1]_. `Holt` is a restricted version of :class:`ExponentialSmoothing`.

    See Also
    --------
    ExponentialSmoothing
    SimpleExpSmoothing

    References
    ----------
    .. [1] Hyndman, Rob J., and George Athanasopoulos. Forecasting: principles
        and practice. OTexts, 2014.
    c         C   s8   | r d n d } t  t |   j | d | d | d  S(   NR4   R5   R}   R   (   R8   R  R9   (   R:   RM   t   exponentialR   R}   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR9   <  s    c	   	      C   sC   t  t |   j d | d | d | d | d | d | d | d |  S(	   s  
        Fit the model

        Parameters
        ----------
        smoothing_level : float, optional
            The alpha value of the simple exponential smoothing, if the value
            is set then this value will be used as the value.
        smoothing_slope :  float, optional
            The beta value of the Holt's trend method, if the value is
            set then this value will be used as the value.
        damping_slope : float, optional
            The phi value of the damped method, if the value is
            set then this value will be used as the value.
        optimized : bool, optional
            Estimate model parameters by maximizing the log-likelihood
        start_params: array, optional
            Starting values to used when optimizing the fit.  If not provided,
            starting values are determined using a combination of grid search
            and reasonable values based on the initial values of the data
        initial_level: float, optional
            Value to use when initializing the fitted level.
        initial_slope: float, optional
            Value to use when initializing the fitted slope.
        use_brute: bool, optional
            Search for good starting values using a brute force (grid)
            optimizer. If False, a naive set of starting values is used.

        Returns
        -------
        results : HoltWintersResults class
            See statsmodels.tsa.holtwinters.HoltWintersResults

        Notes
        -----
        This is a full implementation of the Holt's exponential smoothing as
        per [1].

        References
        ----------
        [1] Hyndman, Rob J., and George Athanasopoulos. Forecasting: principles
            and practice. OTexts, 2014.
        R   R   R   Ri   R   R   R   R   (   R8   R  R   (	   R:   R   R   R   Ri   R   R   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR   @  s    .N(   Ro   R   R   Ry   R9   RR   Rx   R   (    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyR    s
   		(2   R   t   statsmodels.compat.pythonR    t   numpyR   t   pandasRq   t   scipy.optimizeR   R   R   t   scipy.spatial.distanceR   t   scipy.specialR   t   scipy.statsR   t   statsmodels.base.modelR   t   statsmodels.base.wrapperR   R	   R
   t   statsmodels.tsa.base.tsa_modelR   t   statsmodels.tsa.tsatoolsR   t&   statsmodels.tsa._exponential_smootherst   tsat   _exponential_smootherst	   smoothersR   R&   R'   R(   R-   R.   R/   R0   R1   R2   R3   RR   R   t   PY_SMOOTHERSR6   R   R   R  R  (    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/holtwinters.pyt   <module>   sb   											  L