ó
áp7]c           @   sr   d  Z  d d l m Z d d l Z d d l m Z d d d „ Z d „  Z	 d „  Z
 d	 „  Z d
 d d d „ Z d S(   s	  
BDS test for IID time series

References
----------

Broock, W. A., J. A. Scheinkman, W. D. Dechert, and B. LeBaron. 1996.
"A Test for Independence Based on the Correlation Dimension."
Econometric Reviews 15 (3): 197-235.

Kanzler, Ludwig. 1999.
"Very Fast and Correctly Sized Estimation of the BDS Statistic".
SSRN Scholarly Paper ID 151669. Rochester, NY: Social Science Research Network.

LeBaron, Blake. 1997.
"A Fast Algorithm for the BDS Statistic."
Studies in Nonlinear Dynamics & Econometrics 2 (2) (January 1).
iÿÿÿÿ(   t   divisionN(   t   statsg      ø?c         C   s±   t  j |  ƒ }  t |  ƒ } | d k	 rF | d k rF t d | ƒ ‚ n  | d k re t d | ƒ ‚ n  | d k rŠ | |  j d d ƒ } n  t  j |  d d … d f |  ƒ | k  S(   s–  
    Calculate all pairwise threshold distance indicators for a time series

    Parameters
    ----------
    x : 1d array
        observations of time series for which heaviside distance indicators
        are calculated
    epsilon : scalar, optional
        the threshold distance to use in calculating the heaviside indicators
    distance : scalar, optional
        if epsilon is omitted, specifies the distance multiplier to use when
        computing it

    Returns
    -------
    indicators : 2d array
        matrix of distance threshold indicators

    Notes
    -----
    Since this can be a very large matrix, use np.int8 to save some space.

    i    sC   Threshold distance must be positive if specified. Got epsilon of %fs?   Threshold distance must be positive. Got distance multiplier %ft   ddofi   N(   t   npt   asarrayt   lent   Nonet
   ValueErrort   stdt   abs(   t   xt   epsilont   distancet   nobs(    (    s3   lib/python2.7/site-packages/statsmodels/tsa/_bds.pyt   distance_indicators   s    c         C   sà   |  j  d k s t d ƒ ‚ n  |  j d |  j d k sG t d ƒ ‚ n  | d k r\ |  } nO t |  | d ƒ \ } }  |  d d … d d … f |  d d … d d … f } t | ƒ } t j | t j | d ƒ ƒ } | | f S(   s  
    Calculate a correlation sum

    Useful as an estimator of a correlation integral

    Parameters
    ----------
    indicators : 2d array
        matrix of distance threshold indicators
    embedding_dim : integer
        embedding dimension

    Returns
    -------
    corrsum : float
        Correlation sum
    indicators_joint
        matrix of joint-distance-threshold indicators

    i   s   Indicators must be a matrixi    i   s+   Indicator matrix must be symmetric (square)Niÿÿÿÿ(   t   ndimR   t   shapet   correlation_sumR   R   t   meant   triu_indices(   t
   indicatorst   embedding_dimt   indicators_jointt   corrsumR   (    (    s3   lib/python2.7/site-packages/statsmodels/tsa/_bds.pyR   E   s    	6c         C   sk   t  j d | f ƒ } t |  d ƒ \ | d <}  x6 t d | ƒ D]% } t |  d ƒ \ | d | f <}  q> W| S(   s9  
    Calculate all correlation sums for embedding dimensions 1:max_dim

    Parameters
    ----------
    indicators : 2d array
        matrix of distance threshold indicators
    max_dim : integer
        maximum embedding dimension

    Returns
    -------
    corrsums : 1d array
        Correlation sums

    i   i    i   (   i    i    (   R   t   zerosR   t   range(   R   t   max_dimt   corrsumst   i(    (    s3   lib/python2.7/site-packages/statsmodels/tsa/_bds.pyt   correlation_sumsj   s
    #c   
      C   s;  t  |  ƒ } t |  d ƒ \ } } |  j d ƒ d j ƒ  d |  j ƒ  d | | | d | d } t j d | d f ƒ } x± t d | d ƒ D]œ } d } x5 t d | ƒ D]$ }	 | | | |	 | d |	 7} q­ Wd | | d | | d d | d | | d | | d | d | d | d f <q‘ W| | f S(   s"  
    Calculate the variance of a BDS effect

    Parameters
    ----------
    indicators : 2d array
        matrix of distance threshold indicators
    max_dim : integer
        maximum embedding dimension

    Returns
    -------
    variances : float
        Variance of BDS effect

    i   i   i   i    i   (   R   R   t   sumR   R   R   (
   R   R   R   t   corrsum_1dimt   _t   kt	   variancesR   t   tmpt   j(    (    s3   lib/python2.7/site-packages/statsmodels/tsa/_bds.pyt   _var…   s    $"'2i   c         C   sÒ  t  j |  ƒ }  t |  ƒ } | d k  s3 | | k rF t d | ƒ ‚ n  t |  | | ƒ } t | | ƒ } t | | ƒ \ } } t  j | ƒ }	 t  j d | d f ƒ }
 t  j d | d f ƒ } xö t	 d | d ƒ D]á } | d } | | } t
 | | d … | d … f d ƒ \ } } | d | d f } | | | } |	 d | d f } t  j | ƒ | | |
 d | d f <d t j j t  j |
 d | d f ƒ ƒ } | | d | d f <qÑ Wt  j |
 ƒ t  j | ƒ f S(   s‰  
    Calculate the BDS test statistic for independence of a time series

    Parameters
    ----------
    x : 1d array
        observations of time series for which bds statistics is calculated
    max_dim : integer
        maximum embedding dimension
    epsilon : scalar, optional
        the threshold distance to use in calculating the correlation sum
    distance : scalar, optional
        if epsilon is omitted, specifies the distance multiplier to use when
        computing it

    Returns
    -------
    bds_stat : float
        The BDS statistic
    pvalue : float
        The p-values associated with the BDS statistic

    Notes
    -----
    The null hypothesis of the test statistic is for an independent and
    identically distributed (i.i.d.) time series, and an unspecified
    alternative hypothesis.

    This test is often used as a residual diagnostic.

    The calculation involves matrices of size (nobs, nobs), so this test
    will not work with very long datasets.

    Implementation conditions on the first m-1 initial values, which are
    required to calculate the m-histories:
    x_t^m = (x_t, x_{t-1}, ... x_{t-(m-1)})

    i   sF   Maximum embedding dimension must be in the range [2,len(x)-1]. Got %d.i   Ni    (   R   R   R   R   R   R   R%   t   sqrtR   R   R   R   t   normt   sfR	   t   squeeze(   R
   R   R   R   t	   nobs_fullR   t   corrsum_mdimsR"   R!   t   stddevst	   bds_statst   pvaluesR   t   ninitialR   R   R    t   corrsum_mdimt   effectt   sdt   pvalue(    (    s3   lib/python2.7/site-packages/statsmodels/tsa/_bds.pyt   bdsª   s,    '

+%-(   t   __doc__t
   __future__R    t   numpyR   t   scipyR   R   R   R   R   R%   R4   (    (    (    s3   lib/python2.7/site-packages/statsmodels/tsa/_bds.pyt   <module>   s   ,	%		%