B
    Z                 @   sT   d Z ddlmZ ddlZddlmZ dddZdd	 Zd
d Z	dd Z
dddZdS )a	  
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).
    )divisionN)stats      ?c             C   sx   t | } t| }|dk	r.|dkr.td| |dkrBtd| |dkrZ|| jdd }t | dddf |  |k S )a  
    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.

    Nr   zCThreshold distance must be positive if specified. Got epsilon of %fz?Threshold distance must be positive. Got distance multiplier %f   )Zddof)npasarraylen
ValueErrorZstdabs)xepsilondistancenobs r   3lib/python3.7/site-packages/statsmodels/tsa/_bds.pydistance_indicators   s    
r   c             C   s   | j dkstd| jd | jd ks.td|dkr<| }n:t| |d \}} | ddddf | ddddf  }t|}t|t|d }||fS )a  
    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

       zIndicators must be a matrixr   r   z+Indicator matrix must be symmetric (square)N)ndimr	   shapecorrelation_sumr   r   ZmeanZtriu_indices)
indicatorsembedding_dimZindicators_jointZcorrsumr   r   r   r   r   E   s    
(r   c             C   sN   t d|f}t| d\|d< } x(td|D ]}t| d\|d|f< } q,W |S )a9  
    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

    r   )r   r   r   r   )r   zerosr   range)r   max_dimZcorrsumsir   r   r   correlation_sumsj   s
    r   c       
      C   s
  t | }t| d\}}| dd  d|    d|  ||d  |d   }td|d f}xtd|d D ]}d}x.td|D ] }	||||	  |d|	   7 }qW d|| d|  |d d |d|    |d | |d| d     |d|d f< qrW ||fS )a"  
    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

    r   r      r      )r   r   sumr   r   r   )
r   r   r   corrsum_1dim_k	variancesr   Ztmpjr   r   r   _var   s     &0r&   r   c          
   C   sV  t | } t| }|dk s"||kr.td| t| ||}t||}t||\}}t |}	t d|d f}
t d|d f}xt	d|d D ]}|d }|| }t
||d|df d\}}|d|d f }|||  }|	d|d f }t || | |
d|d f< dtjt |
d|d f  }||d|d f< qW t |
t |fS )a  
    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)})

    r   zFMaximum embedding dimension must be in the range [2,len(x)-1]. Got %d.r   Nr   )r   r   r   r	   r   r   r&   Zsqrtr   r   r   r   ZnormZsfr
   Zsqueeze)r   r   r   r   Z	nobs_fullr   Zcorrsum_mdimsr$   r#   ZstddevsZ	bds_statsZpvaluesr   Zninitialr   r!   r"   Zcorrsum_mdimZeffectZsdZpvaluer   r   r   bds   s,    '


"r'   )Nr   )r   Nr   )__doc__Z
__future__r   Znumpyr   Zscipyr   r   r   r   r&   r'   r   r   r   r   <module>   s   
,%%