ó
áp7]c           @   sF   d  d l  Z d  d l m Z d  d l m Z d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   mad(   t   minimize_scalart   BoxCoxc           B   sd   e  Z d  Z d d d „ Z d d „ Z d d d „ Z d d	 i d
 d 6d „ Z i d
 d 6d „ Z RS(   s<   
    Mixin class to allow for a Box-Cox transformation.
    t   guerreroc         K   s¢   t  j | ƒ } t  j | d k ƒ r3 t d ƒ ‚ n  | d k rZ |  j | d | | } n  t  j | d ƒ r~ t  j | ƒ } n t  j | | ƒ d | } | | f S(   s„  
        Performs a Box-Cox transformation on the data array x. If lmbda is None,
        the indicated method is used to estimate a suitable lambda parameter.

        Parameters
        ----------
        x : array_like
        lmbda : float
            The lambda parameter for the Box-Cox transform. If None, a value
            will be estimated by means of the specified method.
        method : {'guerrero', 'loglik'}
            The method to estimate the lambda parameter. Will only be used if
            lmbda is None, and defaults to 'guerrero', detailed in Guerrero
            (1993). 'loglik' maximizes the profile likelihood.
        **kwargs
            Options for the specified method.
            * For 'guerrero', this entails window_length, the grouping
              parameter, scale, the dispersion measure, and options, to be
              passed to the optimizer.
            * For 'loglik': options, to be passed to the optimizer.

        Returns
        -------
        y : array_like
            The transformed series.
        lmbda : float
            The lmbda parameter used to transform the series.

        References
        ----------
        Guerrero, Victor M. 1993. "Time-series analysis supported by power
        transformations". `Journal of Forecasting`. 12 (1): 37-48.

        Guerrero, Victor M. and Perera, Rafael. 2004. "Variance Stabilizing
        Power Transformation for Time Series," `Journal of Modern Applied
        Statistical Methods`. 3 (2): 357-369.

        Box, G. E. P., and D. R. Cox. 1964. "An Analysis of Transformations".
        `Journal of the Royal Statistical Society`. 26 (2): 211-252.
        i    s   Non-positive x.t   methodg        g      ð?N(	   t   npt   asarrayt   anyt
   ValueErrort   Nonet   _est_lambdat   iscloset   logt   power(   t   selft   xt   lmbdaR   t   kwargst   y(    (    s9   lib/python2.7/site-packages/statsmodels/base/transform.pyt   transform_boxcox   s    )t   naivec         C   s…   | j  ƒ  } t j | ƒ } | d k rl t j | d ƒ rK t j | ƒ } q t j | | d d | ƒ } n t d j | ƒ ƒ ‚ | S(   s  
        Back-transforms the Box-Cox transformed data array, by means of the
        indicated method. The provided argument lmbda should be the lambda
        parameter that was used to initially transform the data.

        Parameters
        ----------
        x : array_like
            The transformed series.
        lmbda : float
            The lambda parameter that was used to transform the series.
        method : {'naive'}
            Indicates the method to be used in the untransformation. Defaults
            to 'naive', which reverses the transformation.

            NOTE: 'naive' is implemented natively, while other methods may be
            available in subclasses!

        Returns
        -------
        y : array_like
            The untransformed series.
        R   g        i   g      ð?s   Method '{0}' not understood.(   t   lowerR   R   R   t   expR   R   t   format(   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/statsmodels/base/transform.pyt   untransform_boxcoxF   s    !iÿÿÿÿi   c         K   sÆ   | j  ƒ  } t | ƒ d k r< t d j t | ƒ ƒ ƒ ‚ n# | d | d k r_ t d ƒ ‚ n  | d k r† |  j | d | | } n< | d k r­ |  j | d | | } n t d	 j | ƒ ƒ ‚ | S(
   sÎ  
        Computes an estimate for the lambda parameter in the Box-Cox
        transformation using method.

        Parameters
        ----------
        x : array_like
            The untransformed data.
        bounds : tuple
            Numeric 2-tuple, that indicate the solution space for the lambda
            parameter. Default (-1, 2).
        method : {'guerrero', 'loglik'}
            The method by which to estimate lambda. Defaults to 'guerrero', but
            the profile likelihood ('loglik') is also available.
        **kwargs
            Options for the specified method.
            * For 'guerrero': window_length (int), the seasonality/grouping
              parameter. Scale ({'mad', 'sd'}), the dispersion measure. Options
              (dict), to be passed to the optimizer.
            * For 'loglik': Options (dict), to be passed to the optimizer.

        Returns
        -------
        lmbda : float
            The lambda parameter.
        i   s$   Bounds of length {0} not understood.i    i   s    Lower bound exceeds upper bound.R   t   boundst   logliks   Method '{0}' not understood.(   R   t   lenR   R   t   _guerrero_cvt   _loglik_boxcox(   R   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/statsmodels/base/transform.pyR
   k   s    	i   t   sdi   t   maxiterc            sõ   t  | ƒ } t | | ƒ } t j | | | | | !| | f ƒ } t j | d ƒ ‰ | j ƒ  } | d k rˆ t j | d d d ƒ‰  n6 | d k r© t | d d ƒ‰  n t d j	 | ƒ ƒ ‚ ‡  ‡ f d †  }	 t
 |	 d | d	 d
 d | ƒ}
 |
 j S(   sß  
        Computes lambda using guerrero's coefficient of variation. If no
        seasonality is present in the data, window_length is set to 4 (as
        per Guerrero and Perera, (2004)).

        NOTE: Seasonality-specific auxiliaries *should* provide their own
        seasonality parameter.

        Parameters
        ----------
        x : array_like
        bounds : tuple
            Numeric 2-tuple, that indicate the solution space for the lambda
            parameter.
        window_length : int
            Seasonality/grouping parameter. Default 4, as per Guerrero and
            Perera (2004). NOTE: this indicates the length of the individual
            groups, not the total number of groups!
        scale : {'sd', 'mad'}
            The dispersion measure to be used. 'sd' indicates the sample
            standard deviation, but the more robust 'mad' is also available.
        options : dict
            The options (as a dict) to be passed to the optimizer.
        i   R   t   ddofR    t   axiss   Scale '{0}' not understood.c            sB   t  j ˆ  t  j ˆ d |  ƒ ƒ } t  j | d d ƒt  j | ƒ S(   Ni   R    (   R   t   divideR   t   stdt   mean(   R   t   rat(   t
   dispersionR$   (    s9   lib/python2.7/site-packages/statsmodels/base/transform.pyt   optimÁ   s    "R   R   t   boundedt   options(   R   t   intR   t   reshapeR$   R   R#   R    R   R   R   R   (   R   R   R   t   window_lengtht   scaleR)   t   nobst   groupst   grouped_dataR'   t   res(    (   R&   R$   s9   lib/python2.7/site-packages/statsmodels/base/transform.pyR   —   s"    		c            sa   t  j t  j ˆ ƒ ƒ ‰ t ˆ ƒ ‰  ‡  ‡ ‡ ‡ f d †  } t | d | d d d | ƒ} | j S(   s~  
        Taken from the Stata manual on Box-Cox regressions, where this is the
        special case of 'lhs only'. As an estimator for the variance, the
        sample variance is used, by means of the well-known formula.

        Parameters
        ----------
        x : array_like
        options : dict
            The options (as a dict) to be passed to the optimizer.
        c            sB   ˆ j  ˆ |  ƒ \ } }  d |  ˆ ˆ  d t j t j | ƒ ƒ S(   Ni   g       @(   R   R   R   t   var(   R   R   (   R.   R   t   sum_xR   (    s9   lib/python2.7/site-packages/statsmodels/base/transform.pyR'   Ú   s    R   R   R(   R)   (   R   t   sumR   R   R   R   (   R   R   R   R)   R'   R1   (    (   R.   R   R3   R   s9   lib/python2.7/site-packages/statsmodels/base/transform.pyR   Ë   s    		N(   iÿÿÿÿi   (	   t   __name__t
   __module__t   __doc__R	   R   R   R
   R   R   (    (    (    s9   lib/python2.7/site-packages/statsmodels/base/transform.pyR      s   ;%,3(   t   numpyR   t   statsmodels.robustR    t   scipy.optimizeR   t   objectR   (    (    (    s9   lib/python2.7/site-packages/statsmodels/base/transform.pyt   <module>   s   