
p7]c        
   @   s  d  d l  m Z m Z m Z m Z m Z d  d l m Z d  d l Z	 d  d l
 j j Z d  d l 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 d	 e d
 d  Z d d e e d  Z d d d  Z d d e d  Z  d d d e d  Z! d   Z" d   Z# d   Z$ d   Z% d   Z& d   Z' d   Z( d   Z) d   Z* d   Z+ d   Z, d   Z- d    Z. d!   Z/ d"   Z0 d#   Z1 d$   Z2 d% d& d' d( d) d* d+ d, d- d. g
 Z3 d S(/   i(   t   ranget   lranget   lzipt   longt   PY3(   t   recarray_selectN(   t	   DataFrame(   t   offsets(   t	   to_offset(   t   ValueWarning(   t   _is_using_pandast   _is_recarrayt   ct   skipc         C   s!  | j    } d d d g } | d k r: | d  } d } nl | d k sR | d k r | d	  } | d k rx | d d	 !} n  d } n% | d
 k r d	 } n t d |   t |   } t |  d  p | } | s | r-| r |  j j } t j j	 |   }  q<t
 |  t j  rt j |   }  q<|  j   }  n t j |   }  t |   }	 t j t j d |	 d d t j | d  }
 t j |
  }
 | d k r|
 d d  d f }
 n  d | k r| s| rd   } |  j | d  } nD t j t j |   d d } | d k } | |  d d k @} | } t j |  r| d k r^d j |  } t |   q| d k r| d } |
 d d  d d  f }
 qqn  | rd n d } | s| rt j |
 d |  j d | }
 |
 |  g }  t j |  d d |  d  }  n( |
 |  g }  t j |  d d |   }  | r|  j d t  }  |  j j } t |  t |  } | r|| |  | } n | | | } t sg  | D] } | d ^ q} g  | D] } | d ^ q} g  | D] } t |  ^ q} t t  | |   } n  |  j! t j |   }  n  |  S(   s  
    Adds a trend and/or constant to an array.

    Parameters
    ----------
    X : array-like
        Original array of data.
    trend : str {"c","t","ct","ctt"}
        "c" add constant only
        "t" add trend only
        "ct" add constant and linear trend
        "ctt" add constant and linear and quadratic trend.
    prepend : bool
        If True, prepends the new data to the columns of X.
    has_constant : str {'raise', 'add', 'skip'}
        Controls what happens when trend is 'c' and a constant already
        exists in X. 'raise' will raise an error. 'add' will duplicate a
        constant. 'skip' will return the data without change. 'skip' is the
        default.

    Returns
    -------
    y : array, recarray or DataFrame
        The original data with the additional trend columns.  If x is a
        recarray or pandas Series or DataFrame, then the trend column names
        are 'const', 'trend' and 'trend_squared'.

    Notes
    -----
    Returns columns as ["ctt","ct","c"] whenever applicable. There is currently
    no checking for an existing trend.

    See Also
    --------
    statsmodels.tools.tools.add_constant
    t   constt   trendt   trend_squaredR   i   i    t   ctt   ti   t   ctts   trend %s not understoodt   dtypeNc         S   s;   y, t  j |   d k o* t  j |  d k  SWn t SXd  S(   Ng        (   t   npt   ptpt   anyt   False(   t   s(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   safe_is_const\   s    ,t   axist   raisesI   x contains a constant. Adding a constant with trend='{0}' is not allowed.R   it   indext   columns("   t   lowert
   ValueErrorR   R
   t   NoneR   t   descrt   pdR   t   from_recordst
   isinstancet   Seriest   copyR   t
   asanyarrayt   lent   vandert   aranget   float64t   fliplrt   applyR   R   t   formatR   t   concatt   column_stackt
   to_recordsR   R   t   bytest   listt   zipt   astype(   t   xR   t   prependt   has_constantR   t
   trendordert   is_recarrayt	   is_pandasR"   t   nobst   trendarrR   t	   col_constt   ptp0t   col_is_constt   nz_constt   msgt   ordert	   new_descrt	   extra_colt   entryt   namest   dtypest   name(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt	   add_trend   s    &
	
		/		
%"i   c         C   s  |  j  j r|  j  j } | rF t j |   j d k rF t d   n t |  d k re | d } n  t | t t	 f  r |  j  j | } n  t
 s g  | D]' } t | t  r t |  n | ^ q } | |  j  _ t | t  r t |  } q n  |  | } g  t d | d  D] } | d d | ^ q}	 t | d | d d }
 | t k rpt |  j |  d } nV | t k rt |  d } n7 | t |  k rd	 d
 l } | j d t  n  | } t | |   } t | |  } | r*| | k r| j | j |   q*| j | j |   n  | rdt |  |  } t j | | |	 |
 j d t } nm t j t |   | d t |	 |  | j  f |  } x4 t |	  D]& \ } } |
 d
 d
  | f | | <qW| rt j | | g  | D] } |  | | ^ qd t S| Sn|  j d k r:|  d
 d
  d
 f }  n  | d
 k rOd } n  | d k  ro|  j d | } n  |  d
 d
  | f } | t k r| d } n | t k r|  j d } ni | d k  r|  j d | d } n  | |  j d k r|  j d } d	 d
 l } | j d t  n  | } t | | d d }
 t  |  } t  | |  j d  } | r| | k r| j | j |   q| j | j |   n  t j! |  | d
  | f |
 |  | d
  | f f  Sd
 S(   s+  
    Returns an array with lags included given an array.

    Parameters
    ----------
    x : array
        An array or NumPy ndarray subclass. Can be either a 1d or 2d array with
        observations in columns.
    col : 'string', int, or None
        If data is a structured array or a recarray, `col` can be a string
        that is the name of the column containing the variable. Or `col` can
        be an int of the zero-based column index. If it's a 1d array `col`
        can be None.
    lags : int
        The number of lags desired.
    drop : bool
        Whether to keep the contemporaneous variable for the data.
    insert : bool or int
        If True, inserts the lagged values after `col`. If False, appends
        the data. If int inserts the lags at int.

    Returns
    -------
    array : ndarray
        Array with lags

    Examples
    --------

    >>> import statsmodels.api as sm
    >>> data = sm.datasets.macrodata.load(as_pandas=False)
    >>> data = data.data[['year','quarter','realgdp','cpi']]
    >>> data = sm.tsa.add_lag(data, 'realgdp', lags=2)

    Notes
    -----
    Trims the array both forward and backward, so that the array returned
    so that the length of the returned array is len(`X`) - lags. The lags are
    returned in increasing order, ie., t-1,t-2,...,t-lags
    i   s)   col is None and the input array is not 1di    t   _s   L(%i)t   maxlagt   trimt   BothiNs<   insert > number of variables, inserting at the last positiont   usemaskR   ("   R   RH   R   t   squeezet   ndimt
   IndexErrorR)   R%   t   intR   R   t   unicodeR3   R    t   lagmatt   TrueR4   R   R   t   warningst   warnR	   t   popR   t   nprft   append_fieldst   Tt   zerosR   t	   enumerateR!   t   shapeR   R1   (   R7   t   colt   lagst   dropt   insertRH   RJ   t   contempt   it	   tmp_namest   ndlagst   ins_idxRX   t   first_namest
   last_namest   _xt	   first_arrt
   first_colst	   last_cols(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   add_lag   s    )1
2	
(		
i    c         C   s  |  j  d k r- t |  d k r- |  j }  n |  j  d k rK t d   n  |  j d } | d k r} |  |  j d d  } nY t j t j t	 |   d | d } t j
 j |  j |   } |  t j | |  } |  j  d k rt |  d k r| j } n  | S(   s  
    Detrend an array with a trend of given order along axis 0 or 1

    Parameters
    ----------
    x : array_like, 1d or 2d
        data, if 2d, then each row or column is independently detrended with the
        same trendorder, but independent trend estimates
    order : int
        specifies the polynomial order of the trend, zero is constant, one is
        linear trend, two is quadratic trend
    axis : int
        axis can be either 0, observations by rows,
        or 1, observations by columns

    Returns
    -------
    detrended data series : ndarray
        The detrended series is the residual of the linear regression of the
        data on the trend of given order.
    i   i   s0   x.ndim > 2 is not implemented until it is neededi    R   t   N(   RR   RT   R]   t   NotImplementedErrorR`   t   meanR   R*   R+   t   floatt   linalgt   pinvt   dot(   R7   RD   R   R=   t   residt   trendst   beta(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   detrend  s    !(!t   forwardt   exc         C   s  t  |  d  o | } | d k r' d n | } | j   } | rZ | d k rZ t d   n  t j |   } d } | j d k r | d d  d f } n  | j \ } }	 | d k r |	 } n  | | k r t d	   n  t j | | |	 | d f  }
 x] t	 d t
 | d   D]B } | |
 | | | | |  |	 | | |	 | | d  f <qW| d k rld } n! | d k r| } n t d   | d k rt |
  } n | } | rt |  t  r|  j n	 |  j g } g  | D] } t |  ^ q} xR t	 |  D]D } t | d  } | j g  | D] } t |  d | ^ q' qWt |
 |  d |  j d | }
 |
 j | } | d k r| | } | j | d  } qnG |
 | |  | d  f } | d k r|
 | |  d |  f } n  | d k r| | f S| Sd S(   s  
    Create 2d array of lags

    Parameters
    ----------
    x : array_like, 1d or 2d
        data; if 2d, observation in rows and variables in columns
    maxlag : int
        all lags from zero to maxlag are included
    trim : str {'forward', 'backward', 'both', 'none'} or None
        * 'forward' : trim invalid observations in front
        * 'backward' : trim invalid initial observations
        * 'both' : trim invalid observations on both sides
        * 'none', None : no trimming of observations
    original : str {'ex','sep','in'}
        * 'ex' : drops the original array returning only the lagged values.
        * 'in' : returns the original array and the lagged values as a single
          array.
        * 'sep' : returns a tuple (original array, lagged values). The original
                  array is truncated to have the same number of rows as
                  the returned lagmat.
    use_pandas : bool, optional
        If true, returns a DataFrame when the input is a pandas
        Series or DataFrame.  If false, return numpy ndarrays.

    Returns
    -------
    lagmat : 2d array
        array with lagged observations
    y : 2d array, optional
        Only returned if original == 'sep'

    Examples
    --------
    >>> from statsmodels.tsa.tsatools import lagmat
    >>> import numpy as np
    >>> X = np.arange(1,7).reshape(-1,2)
    >>> lagmat(X, maxlag=2, trim="forward", original='in')
    array([[ 1.,  2.,  0.,  0.,  0.,  0.],
       [ 3.,  4.,  1.,  2.,  0.,  0.],
       [ 5.,  6.,  3.,  4.,  1.,  2.]])

    >>> lagmat(X, maxlag=2, trim="backward", original='in')
    array([[ 5.,  6.,  3.,  4.,  1.,  2.],
       [ 0.,  0.,  5.,  6.,  3.,  4.],
       [ 0.,  0.,  0.,  0.,  5.,  6.]])

    >>> lagmat(X, maxlag=2, trim="both", original='in')
    array([[ 5.,  6.,  3.,  4.,  1.,  2.]])

    >>> lagmat(X, maxlag=2, trim="none", original='in')
    array([[ 1.,  2.,  0.,  0.,  0.,  0.],
       [ 3.,  4.,  1.,  2.,  0.,  0.],
       [ 5.,  6.,  3.,  4.,  1.,  2.],
       [ 0.,  0.,  5.,  6.,  3.,  4.],
       [ 0.,  0.,  0.,  0.,  5.,  6.]])

    Notes
    -----
    When using a pandas DataFrame or Series with use_pandas=True, trim can only
    be 'forward' or 'both' since it is not possible to consistently extend index
    values.
    t   nonet   backwardsD   trim cannot be 'none' or 'forward' when used on Series or DataFramesi    i   NR}   t   seps   maxlag should be < nobsR|   t   boths   trim option not valids   .L.R   R   (   R~   R   (   R}   R   (   R~   R|   (   R   R   (   R~   R   (   R   R}   (   R
   R!   R   R    R   t   asarrayRR   R`   R^   R    RT   R)   R%   R   R   RJ   t   strt   extendR   t   ilocRc   (   R7   RM   RN   t   originalt
   use_pandasR<   t   xat   dropidxR=   t   nvart   lmt   kt   startobst   stopobst	   x_columnsRa   R   t   lagt   lag_strRb   t   leads(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyRV   H  sT    A	! @		$2

c      
   C   ss  | d k r | } n  t | |  } t |  d  } |  j d k rs | rZ t j |   }  q |  d d  d f }  n- |  j d k s |  j d k r t d   n  |  j \ } }	 | r| rt |  j	 d d  d f | d | d d d	 t
 }
 |
 j	 d d  d | d  f g } xu t d |	  D]d } t |  j	 d d  | f | d | d d d	 t
 }
 | j |
 j	 d d  | | d  f  q%Wt j | d
 d S| rt j |   }  n  t |  d d  d f | d | d d d d  d | d  f g } xc t d |	  D]R } | j t |  d d  | f | d | d d d d  | | d  f  qWt j |  S(   s  
    Generate lagmatrix for 2d array, columns arranged by variables

    Parameters
    ----------
    x : array_like, 2d
        2d data, observation in rows and variables in columns
    maxlag0 : int
        for first variable all lags from zero to maxlag are included
    maxlagex : None or int
        max lag for all other variables all lags from zero to maxlag are included
    dropex : int (default is 0)
        exclude first dropex lags from other variables
        for all variables, except the first, lags from dropex to maxlagex are
        included
    trim : string
        * 'forward' : trim invalid observations in front
        * 'backward' : trim invalid initial observations
        * 'both' : trim invalid observations on both sides
        * 'none' : no trimming of observations
    use_pandas : bool, optional
        If true, returns a DataFrame when the input is a pandas
        Series or DataFrame.  If false, return numpy ndarrays.

    Returns
    -------
    lagmat : 2d array
        array with lagged observations, columns ordered by variable

    Notes
    -----
    Inefficient implementation for unequal lags, implemented for convenience
    i   Ni    i   s'   Only supports 1 and 2-dimensional data.RN   R   t   inR   R   (   R!   t   maxR
   RR   R#   R   R    R`   RV   R   RW   R    t   appendR0   R   R(   R1   (   R7   t   maxlag0t   maxlagext   dropexRN   R   RM   R<   R=   R   Rb   t   lagsliR   (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt	   lagmat2ds  s4    $	%&%.HPc         C   s   |  j  d  S(   Nt   F(   t   ravel(   t   mat(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   vec  s    c         C   s   |  j  j t t |     S(   N(   R]   t   taket   _triu_indicesR)   (   R   (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   vech	  s    c         C   s!   t  j |   \ } } | |  | S(   N(   R   t   tril_indices(   t   nt   rowst   cols(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   _tril_indices  s    c         C   s!   t  j |   \ } } | |  | S(   N(   R   t   triu_indices(   R   R   R   (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyR     s    c         C   s!   t  j |   \ } } | |  | S(   N(   R   t   diag_indices(   R   R   R   (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   _diag_indices  s    c         C   sP   t  t j t |     } | | t |   k s7 t  |  j | | f d d S(   NRD   R   (   RT   R   t   sqrtR)   t   AssertionErrort   reshape(   t   vR   (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   unvec  s    c         C   s   d d t  j d d t |    } t t  j |   } t  j | | f  } |  | t  j |  <| | j } | t  j |  c d :<| S(   Ng      ?ii   i   i   (	   R   R   R)   RT   t   roundR^   R   R]   R   (   R   R   t   result(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   unvech%  s    %c         C   sJ   t  j |  |  d d  } t  j g  | D] } t |  j   ^ q(  j S(   s   
    Create duplication matrix D_n which satisfies vec(S) = D_n vech(S) for
    symmetric matrix S

    Returns
    -------
    D_n : ndarray
    i   i   (   R   t   eyet   arrayR   R   R]   (   R   t   tmpR7   (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   duplication_matrix4  s    	c         C   s?   t  t j t j |  |  f    } t j |  |   | d k S(   s   
    Create the elimination matrix L_n which satisfies vech(M) = L_n vec(M) for
    any matrix M

    Parameters
    ----------

    Returns
    -------

    i    (   R   R   t   trilt   onesR   (   R   t   vech_indices(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   elimination_matrixA  s    $c         C   sT   t  j |  |  } t  j |  |  j |  | f d d } | j | j   d d S(   s   
    Create the commutation matrix K_{p,q} satisfying vec(A') = K_{p,q} vec(A)

    Parameters
    ----------
    p : int
    q : int

    Returns
    -------
    K : ndarray (pq x pq)
    RD   R   R   i    (   R   R   R+   R   R   R   (   t   pt   qt   Kt   indices(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   commutation_matrixQ  s    (c      	   C   s   t  j |  d  } t  j |  d  } xl t d t |    D]U } | | } x4 t |  D]& } | | c | | | | d 8<qY W| |  | | *q< W| S(   s   
    Transforms params to induce stationarity/invertability.

    Parameters
    ----------
    params : array_like
        The AR coefficients

    Reference
    ---------
    Jones(1980)
    i   i   (   R   t   tanhR    R)   (   t   paramst	   newparamsR   t   jt   at   kiter(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   _ar_transparamsc  s    
$c         C   s   |  j    }  |  j    } x t t |   d d d  D]c } |  | } xB t |  D]4 } |  | | |  | | d d | d | | <qR W| |  |  | *q5 Wd t j |   } | S(   s   
    Inverse of the Jones reparameterization

    Parameters
    ----------
    params : array_like
        The transformed AR coefficients
    i   i    ii   (   R'   R    R)   R   t   arctanh(   R   R   R   R   R   t
   invarcoefs(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   _ar_invtransparamsz  s    	#
c      	   C   s   d t  j |   d t  j |   j   } d t  j |   d t  j |   j   } xl t d t |    D]U } | | } x4 t |  D]& } | | c | | | | d 7<q W| |  | | *qn W| S(   s   
    Transforms params to induce stationarity/invertability.

    Parameters
    ----------
    params : array
        The ma coeffecients of an (AR)MA model.

    Reference
    ---------
    Jones(1980)
    i   (   R   t   expR'   R    R)   (   R   R   R   R   t   bR   (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   _ma_transparams  s    ,,
$c         C   s   |  j    } x t t |   d d d  D]c } |  | } xB t |  D]4 } |  | | |  | | d d | d | | <qF W| |  |  | *q) Wt j d |  d |   } | S(   s   
    Inverse of the Jones reparameterization

    Parameters
    ----------
    params : array
        The transformed MA coefficients
    i   i    ii   (   R'   R    R)   R   t   log(   t   macoefsR   R   R   R   t
   invmacoefs(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   _ma_invtransparams  s    	#
2c         C   sJ   |  |  }  t  j g  t | d d  D]  } t  j |  | |  d ^ q#  S(   s  
    Returns the successive differences needed to unintegrate the series.

    Parameters
    ----------
    x : array-like
        The original series
    d : int
        The number of differences of the differenced series.

    Returns
    -------
    y : array-like
        The increasing differences from 0 to d-1 of the first d elements
        of x.

    See Also
    --------
    unintegrate
    i    i(   R   R   R    t   diff(   R7   t   dRf   (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   unintegrate_levels  s    
c         C   su   t  |  } t |  d k rQ | j d  } t t j t j | |  f  |  S| d } t j t j | |  f  S(   sy  
    After taking n-differences of a series, return the original series

    Parameters
    ----------
    x : array-like
        The n-th differenced series
    levels : list
        A list of the first-value in each differenced series, for
        [first-difference, second-difference, ..., n-th difference]

    Returns
    -------
    y : array-like
        The original series de-differenced

    Examples
    --------
    >>> x = np.array([1, 3, 9., 19, 8.])
    >>> levels = unintegrate_levels(x, 2)
    >>> levels
    array([ 1.,  2.])
    >>> unintegrate(np.diff(x, 2), levels)
    array([  1.,   3.,   9.,  19.,   8.])
    i   ii    (   R4   R)   RZ   t   unintegrateR   t   cumsumt   r_(   R7   t   levelst   x0(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyR     s    #
c         C   s   t  |  t j  s! t |   }  n  |  j j   }  |  d k sK |  j d  rO d S|  d k sj |  j d  rn d S|  d	 k s |  j d  r d S|  d k s |  j d  r d S|  d k r d S|  d k r d S|  d k r d St d j |     d S(   s-  
    Convert a pandas frequency to a periodicity

    Parameters
    ----------
    freq : str or offset
        Frequency to convert

    Returns
    -------
    period : int
        Periodicity of freq

    Notes
    -----
    Annual maps to 1, quarterly maps to 4, monthly to 12, weekly to 52.
    t   As   A-s   AS-i   t   Qs   Q-s   QS-i   t   Ms   M-t   MSi   t   Ws   W-i4   t   Di   t   Bi   t   Hi   sD   freq {} not understood. Please report if you think this is in error.N(   s   A-s   AS-(   s   Q-s   QS-(   s   M-R   (	   R%   R   t
   DateOffsetR   t	   rule_codet   uppert
   startswithR    R/   (   t   freq(    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   freq_to_period  s&    	RV   R   RK   R   R   R   R   R   R   R   (4   t   statsmodels.compat.pythonR    R   R   R   R   t   statsmodels.compat.numpyR   t   numpyR   t   numpy.lib.recfunctionst   libt   recfunctionsR[   t   pandasR#   R   t   pandas.tseriesR   t   pandas.tseries.frequenciesR   t   statsmodels.tools.sm_exceptionsR	   t   statsmodels.tools.dataR
   R   R   RK   R!   RW   Rp   R{   RV   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   __all__(    (    (    s7   lib/python2.7/site-packages/statsmodels/tsa/tsatools.pyt   <module>   sF   (*x	D																"	)