Ë
    £�DjÜ  ã                   óJ   — d Z ddlZddlmZ ddlmZ d
d„Zd„ Zd„ Z	d„ Z
dd	„Zy)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).
é    N)Ústats)Ú
array_likec                 óÜ   — t        | d«      } |�|dk  rt        d|z  «      ‚|dk  rt        d|z  «      ‚|€|| j                  d¬«      z  }t        j                  | dd…df   | z
  «      |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.
    ÚxNr   zCThreshold distance must be positive if specified. Got epsilon of %fz?Threshold distance must be positive. Got distance multiplier %fé   )Úddof)r   Ú
ValueErrorÚstdÚnpÚabs)r   ÚepsilonÚdistances      úXC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels\tsa\_bds.pyÚdistance_indicatorsr      s•   € ô0 	�1�cÓ€AàÐ˜w¨!š|Üð .Ø07ñ8ó 9ð 	9à�1‚}Üð 7Ø9AñBó Cð 	Cð €Ø˜QŸU™U¨˜U›]Ñ*ˆä�6‰6�!’A�t�G‘*˜q‘.Ó! GÑ+Ð+ó    c                 ób  — | j                   dk(  st        d«      ‚| j                  d   | j                  d   k(  st        d«      ‚|dk(  r| }n)t        | |dz
  «      \  }} | dd…dd…f   | dd…dd…f   z  }t	        |«      }t        j                  |t        j                  |d«         «      }||fS )a‰  
    Calculate a correlation sum

    Useful as an estimator of a correlation integral

    Parameters
    ----------
    indicators : ndarray
        2d array of distance threshold indicators
    embedding_dim : int
        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_sumÚlenr   ÚmeanÚtriu_indices)Ú
indicatorsÚembedding_dimÚindicators_jointÚcorrsumÚnobss        r   r   r   D   sÉ   € ð( �?‰?˜aÒÜÐ6Ó7Ð7Ø×Ñ˜AÑ *×"2Ñ"2°1Ñ"5Ò5ÜÐFÓGÐGà˜ÒØ%Ñä-¨j¸-È!Ñ:KÓLÑˆ�Ø% a¡b¨!©" fÑ-¨j¸¸"¸¸c¸r¸c¸Ñ.BÑBÐäÐÓ €DÜ�g‰gÐ&¤r§¡°t¸QÓ'?Ñ@ÓA€GØÐ$Ð$Ð$r   c                 ó¢   — t        j                  d|f«      }t        | d«      \  |d<   } t        d|«      D ]  }t        | d«      \  |d|f<   } Œ |S )a3  
    Calculate all correlation sums for embedding dimensions 1:max_dim

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

    Returns
    -------
    corrsums : ndarray
        Correlation sums
    r   )r   r   r   r   )r   Úzerosr   Úrange)r   Úmax_dimÚcorrsumsÚis       r   Úcorrelation_sumsr&   h   sc   € ô" �x‰x˜˜G˜Ó%€Hä!0°¸QÓ!?Ñ€HˆT�N�JÜ�1�gÓò DˆÜ%4°ZÀÓ%CÑ"ˆ��A�‰™
ðDð €Or   c                 óø  — t        | «      }t        | d«      \  }}| j                  d«      dz  j                  «       d| j                  «       z  z
  d|z  z   ||dz
  z  |dz
  z  z  }t        j                  d|dz
  f«      }t        d|dz   «      D ]f  }d}t        d|«      D ]  }	||||	z
  z  |d|	z  z  z  z  }Œ d||z  d|z  z   |dz
  dz  |d|z  z  z  z   |dz  |z  |d|z  dz
  z  z  z
  z  |d|dz
  f<   Œh ||fS )a  
    Calculate the variance of a BDS effect

    Parameters
    ----------
    indicators : ndarray
        2d array of distance threshold indicators
    max_dim : int
        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   ÚtmpÚjs
             r   Ú_varr1   ‚   sh  € ô  ˆz‹?€DÜ% j°!Ó4�O€L�!Ø
�.‰.˜Ó
˜QÑ
×	#Ñ	#Ó	%¨¨*¯.©.Ó*:Ñ(:Ñ	:Ø	
ˆ4‰ñ
Ø˜D 1™HÑ%¨°©Ñ2ñ	4€Aô —‘˜!˜W q™[Ð)Ó*€Iä˜q '¨A¡+Ó.ò NˆØˆÜ�q˜-Ó(ò 	DˆAØ�A˜¨Ñ)Ñ*¨\¸AÀ¹EÑ-BÑCÑC‰Cð	Dà()Øˆ}ÑØ�‰Gñà˜aÑ !Ñ#¨°q¸=Ñ7HÑ(IÑJñKð ˜AÑ Ñ" l°Q¸Ñ5FÈÑ5JÑ&KÑLñMñ)Nˆ	�!�] 1‘_Ð$Ò%ð	Nð �aˆ<Ðr   c           
      ó  — t        | dd¬«      } t        | «      }|dk  s||k\  rt        d|z  «      ‚t        | ||«      }t	        ||«      }t        ||«      \  }}t        j                  |«      }	t        j                  d|dz
  f«      }
t        j                  d|dz
  f«      }t        d|dz   «      D ]ª  }|dz
  }||z
  }t        ||d…|d…f   d«      \  }}|d|dz
  f   }|||z  z
  }|	d|dz
  f   }t        j                  |«      |z  |z  |
d|dz
  f<   dt        j                  j                  t        j                  |
d|dz
  f   «      «      z  }||d|dz
  f<   Œ¬ t        j                  |
«      t        j                  |«      fS )a”  
    BDS Test Statistic for Independence of a Time Series

    Parameters
    ----------
    x : ndarray
        Observations of time series for which bds statistics is calculated.
    max_dim : int
        The maximum embedding dimension.
    epsilon : {float, None}, optional
        The threshold distance to use in calculating the correlation sum.
    distance : float, optional
        Specifies the distance multiplier to use when computing the test
        statistic if epsilon is omitted.

    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   r   )r   r   zFMaximum embedding dimension must be in the range [2,len(x)-1]. Got %d.Nr   )r   r   r	   r   r&   r1   r   Úsqrtr!   r"   r   r   ÚnormÚsfr   Úsqueeze)r   r#   r   r   Ú	nobs_fullr   Úcorrsum_mdimsr.   r-   ÚstddevsÚ	bds_statsÚpvaluesr   Úninitialr   r+   r,   Úcorrsum_mdimÚeffectÚsdÚpvalues                        r   ÚbdsrA   ¦   sÃ  € ôL 	�1�c Ô"€AÜ�A“€Ià�‚{�g Ò*Üð 2Ø4;ñ<ó =ð 	=ô % Q¨°Ó:€Jô % Z°Ó9€Mô ˜
 GÓ,�L€IˆqÜ�g‰g�iÓ €Gä—‘˜!˜W q™[Ð)Ó*€IÜ�h‰h˜˜7 Q™;Ð'Ó(€GÜ˜q '¨!¡)Ó,ò /ˆØ! AÑ%ˆØ˜8Ñ#ˆô *¨*°X±YÀÁ	Ð5IÑ*JÈAÓN‰ˆ�aØ$ Q¨¸Ñ(9Ð%9Ñ:ˆð  ¨}Ñ!<Ñ=ˆØ�Q˜¨Ñ)Ð)Ñ*ˆô +-¯'©'°$«-¸&Ñ*@À2Ñ*Eˆ	�!�] QÑ&Ð&Ñ'ð ”5—:‘:—=‘=¤§¡¨	°!°]ÀQÑ5FÐ2FÑ(GÓ!HÓIÑIˆØ(.ˆ��= 1Ñ$Ð$Ò%ð%/ô( �:‰:�iÓ ¤"§*¡*¨WÓ"5Ð5Ð5r   )Nç      ø?)r   NrB   )Ú__doc__Únumpyr   Úscipyr   Ústatsmodels.tools.validationr   r   r   r&   r1   rA   © r   r   ú<module>rH      s2   ðñó& Ý å 3ó',òT!%òHò4!ôHM6r   