
    >hZ9                     ~    S r SSKJr  SSKrSSKJr  SS jrSS jrSS jr	SS jr
S
S	 jrSS jrSS jrS rSS jrg)zy
Statistical tests to be used in conjunction with the models

Notes
-----
These functions have not been formally tested.
    )statsN)ValueWarningc                     [         R                  " U 5      n [         R                  " U SUS9n[         R                  " US-  US9[         R                  " U S-  US9-  nU$ )a  
Calculates the Durbin-Watson statistic.

Parameters
----------
resids : array_like
    Data for which to compute the Durbin-Watson statistic. Usually
    regression model residuals.
axis : int, optional
    Axis to use if data has more than 1 dimension. Default is 0.

Returns
-------
dw : float, array_like
    The Durbin-Watson statistic.

Notes
-----
The null hypothesis of the test is that there is no serial correlation
in the residuals.
The Durbin-Watson test statistic is defined as:

.. math::

   \sum_{t=2}^T((e_t - e_{t-1})^2)/\sum_{t=1}^Te_t^2

The test statistic is approximately equal to 2*(1-r) where ``r`` is the
sample autocorrelation of the residuals. Thus, for r == 0, indicating no
serial correlation, the test statistic equals 2. This statistic will
always be between 0 and 4. The closer to 0 the statistic, the more
evidence for positive serial correlation. The closer to 4, the more
evidence for negative serial correlation.
   axis   )npasarraydiffsum)residsr   diff_residsdws       nC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/stats/stattools.pydurbin_watsonr      sR    D ZZF''&!$/K	QT	*RVVFAID-I	IBI    c                     [         R                  " U 5      n U R                  U   nUS:  a?  SSKJn  U" S[        U5      -  [        5        [         R                  [         R                  4$ [        R                  " XS9$ )z
Omnibus test for normality

Parameters
----------
resid : array_like
axis : int, optional
    Default is 0

Returns
-------
Chi^2 score, two-tail probability
   r   )warnzPomni_normtest is not valid with less than 8 observations; %i samples were given.r   )
r
   r   shapewarningsr   intr   nanr   
normaltest)r   r   nr   s       r   omni_normtestr   6   si      ZZFTA1u! #%(V,-9	;vvrvv~F..r   c                 |   [         R                  " [         R                  " U [        S95      n U R                  S:  a  [        S5      e[        R                  " XS9nS[        R                  " XS9-   nU R                  U   nUS-  US-  SUS-
  S-  -  -   -  n[        R                  R                  US5      nXVX#4$ )a  
The Jarque-Bera test of normality.

Parameters
----------
resids : array_like
    Data to test for normality. Usually regression model residuals that
    are mean 0.
axis : int, optional
    Axis to use if data has more than 1 dimension. Default is 0.

Returns
-------
JB : {float, ndarray}
    The Jarque-Bera test statistic.
JBpv : {float, ndarray}
    The pvalue of the test statistic.
skew : {float, ndarray}
    Estimated skewness of the data.
kurtosis : {float, ndarray}
    Estimated kurtosis of the data.

Notes
-----
Each output returned has 1 dimension fewer than data

The Jarque-Bera test statistic tests the null that the data is normally
distributed against an alternative that the data follow some other
distribution. The test statistic is based on two moments of the data,
the skewness, and the kurtosis, and has an asymptotic :math:`\chi^2_2`
distribution.

The test statistic is defined

.. math:: JB = n(S^2/6+(K-3)^2/24)

where n is the number of data points, S is the sample skewness, and K is
the sample kurtosis of the data.
dtyper	   z'resids must contain at least 2 elementsr            @g      ?)r
   
atleast_1dr   floatsize
ValueErrorr   skewkurtosisr   chi2sf)r   r   r'   r(   r   jbjb_pvs          r   jarque_berar-   Q   s    P ]]2::fE:;F{{QBCC::f(D5>>&44H 	TA
b&TQY&X\a,?!??	@BJJMM"a Ed$$r   c                    Uc  U R                  5       n Sn[        R                  " X5      n [        R                  " U / SQUS9u  p#nU R	                  U5      nU R
                  4nUb2  [        UR                  5      nUR                  US5        [        U5      n[        R                  " XV5      n[        R                  " X65      n[        R                  " [        R                  " X-
  S-  U5      5      n	[        R                  " XS9n
X$-   SU-  -
  XB-
  -  nXS-
  [        R                  " [        X-
  5      US9-  nXS-
  U	-  nXX4$ )aK  
Calculates the four skewness measures in Kim & White

Parameters
----------
y : array_like
    Data to compute use in the estimator.
axis : int or None, optional
    Axis along which the skewness measures are computed.  If `None`, the
    entire array is used.

Returns
-------
sk1 : ndarray
      The standard skewness estimator.
sk2 : ndarray
      Skewness estimator based on quartiles.
sk3 : ndarray
      Skewness estimator based on mean-median difference, standardized by
      absolute deviation.
sk4 : ndarray
      Skewness estimator based on mean-median difference, standardized by
      standard deviation.

Notes
-----
The robust skewness measures are defined

.. math::

    SK_{2}=\frac{\left(q_{.75}-q_{.5}\right)
    -\left(q_{.5}-q_{.25}\right)}{q_{.75}-q_{.25}}

.. math::

    SK_{3}=\frac{\mu-\hat{q}_{0.5}}
    {\hat{E}\left[\left|y-\hat{\mu}\right|\right]}

.. math::

    SK_{4}=\frac{\mu-\hat{q}_{0.5}}{\hat{\sigma}}

.. [*] Tae-Hwan Kim and Halbert White, "On more robust estimation of
   skewness and kurtosis," Finance Research Letters, vol. 1, pp. 56-73,
   March 2004.
r   )      9@      I@     R@r   r   r	          @)ravelr
   sort
percentilemeanr%   listr   inserttuplereshapesqrtr   r'   abs)yr   q1q2q3mur   mu_bq2_bsigmask1sk2sk3sk4s                 r   robust_skewnessrI      s   ` |GGI
Aq"44@JBB	
BVVIERXXT1e::b D::b DGGBGGah]T23E
**Q
"C7S2X"'
*C7bggc!(m$7
7C7e
CSr   c                 6   USU-
  USU-
  4n[         R                  " X5      u  pEpg[         R                  " X U:     5      n[         R                  " X U:     5      n	[         R                  " X U:     5      n
[         R                  " X U:     5      nX-
  X-
  -  $ )a5  
KR3 estimator from Kim & White

Parameters
----------
y : array_like, 1-d
    Data to compute use in the estimator.
alpha : float, optional
    Lower cut-off for measuring expectation in tail.
beta :  float, optional
    Lower cut-off for measuring expectation in center.

Returns
-------
kr3 : float
    Robust kurtosis estimator based on standardized lower- and upper-tail
    expected values

Notes
-----
.. [*] Tae-Hwan Kim and Halbert White, "On more robust estimation of
   skewness and kurtosis," Finance Research Letters, vol. 1, pp. 56-73,
   March 2004.
      Y@)r
   r5   r6   )r=   alphabetaperclower_alphaupper_alpha
lower_beta
upper_betal_alphau_alphal_betau_betas               r   _kr3rW      s    2 55=$5D79}}Q7M4KjggaK()GggaK()GWWQ:~&'FWWQ:~&'F&/22r         @r0   c                    U u  p#Uu  pE[         R                  " S5      n[        R                  R                  n[        R                  R
                  nU" [         R                  " S5      S-  5      u  pppSUS'   X-
  X-
  -   X-
  -  US'   U" [         R                  " US-  US-  45      5      u  nnSU" U5      -  U-  SU" U5      -  U-  -  US'   U" [         R                  " US-  US-  45      5      u  nnS	U-  S	U-  -  US'   U$ )
a  
Calculates the expected value of the robust kurtosis measures in Kim and
White assuming the data are normally distributed.

Parameters
----------
ab : iterable, optional
    Contains 100*(alpha, beta) in the kr3 measure where alpha is the tail
    quantile cut-off for measuring the extreme tail and beta is the central
    quantile cutoff for the standardization of the measure
db : iterable, optional
    Contains 100*(delta, gamma) in the kr4 measure where delta is the tail
    quantile for measuring extreme values and gamma is the central quantile
    used in the the standardization of the measure

Returns
-------
ekr : ndarray, 4-element
    Contains the expected values of the 4 robust kurtosis measures

Notes
-----
See `robust_kurtosis` for definitions of the robust kurtosis measures
   )g      ?r2   g      @rY   r"   g      @r   r!   r   r   rK   r	   g       )r
   zerosr   normppfpdfarray)abdgrL   rM   deltagammaexpected_valuer^   r_   r>   r?   r@   q5q6q7q_alphaq_betaq_deltaq_gammas                      r   expected_robust_kurtosisrm      s   4 KELEXXa[N
**..C
**..C *H!IA!MNBBBN1'bg.27;N1"((EEM4%<#@ABOGVS\)E1a#f+o6LMN1288UU]EEM$BCDGWD7N;N1r   c                 X   Ub.  U R                  5       R                  S:X  a"  U R                  S:w  a  U R                  5       n SnUu  pVUu  pxSSSSSSUS	U-
  US	U-
  4
n	[        R                  " X	US
9u
  pppnnnnU(       a  [        X#5      O[        R                  " S5      n[        R                  " XS5      US   -
  nX-
  X-
  -   X-
  -  US   -
  nU R                  S:X  a  [        XU5      nO[        R                  " [        XXV5      nUUS   -  nUU-
  UU-
  -  US   -
  nUUUU4$ )aS  
Calculates the four kurtosis measures in Kim & White

Parameters
----------
y : array_like
    Data to compute use in the estimator.
axis : int or None, optional
    Axis along which the kurtosis are computed.  If `None`, the
    entire array is used.
a iterable, optional
    Contains 100*(alpha, beta) in the kr3 measure where alpha is the tail
    quantile cut-off for measuring the extreme tail and beta is the central
    quantile cutoff for the standardization of the measure
db : iterable, optional
    Contains 100*(delta, gamma) in the kr4 measure where delta is the tail
    quantile for measuring extreme values and gamma is the central quantile
    used in the the standardization of the measure
excess : bool, optional
    If true (default), computed values are excess of those for a standard
    normal distribution.

Returns
-------
kr1 : ndarray
      The standard kurtosis estimator.
kr2 : ndarray
      Kurtosis estimator based on octiles.
kr3 : ndarray
      Kurtosis estimators based on exceedance expectations.
kr4 : ndarray
      Kurtosis measure based on the spread between high and low quantiles.

Notes
-----
The robust kurtosis measures are defined

.. math::

    KR_{2}=\frac{\left(\hat{q}_{.875}-\hat{q}_{.625}\right)
    +\left(\hat{q}_{.375}-\hat{q}_{.125}\right)}
    {\hat{q}_{.75}-\hat{q}_{.25}}

.. math::

    KR_{3}=\frac{\hat{E}\left(y|y>\hat{q}_{1-\alpha}\right)
    -\hat{E}\left(y|y<\hat{q}_{\alpha}\right)}
    {\hat{E}\left(y|y>\hat{q}_{1-\beta}\right)
    -\hat{E}\left(y|y<\hat{q}_{\beta}\right)}

.. math::

    KR_{4}=\frac{\hat{q}_{1-\delta}-\hat{q}_{\delta}}
    {\hat{q}_{1-\gamma}-\hat{q}_{\gamma}}

where :math:`\hat{q}_{p}` is the estimated quantile at :math:`p`.

.. [*] Tae-Hwan Kim and Halbert White, "On more robust estimation of
   skewness and kurtosis," Finance Research Letters, vol. 1, pp. 56-73,
   March 2004.
r   r   g      )@r/   g     B@g     @O@r1   g     U@rK   r   r[   Fr	   r!   )squeezendimr3   r
   r5   rm   r\   r   r(   rW   apply_along_axis)r=   r   ra   rb   excessrL   rM   rc   rd   rN   e1e2e3e5e6e7fdf1mdfgf1mgre   kr1kr2kr3kr4s                            r   robust_kurtosisr   %  sM   | 	YY["qvv{GGIKELE$dD$55=%8D13qDH2J.BBBBb$   /r6%'XXa[  ..%
(>!+<
<CG RW
-q0A
ACvv{1T"!!$=>!C"9
#nQ&7
7CS#r   c                    [         R                  " [         R                  " U 5      5      n U R                  S:w  a  [	        S5      e[         R
                  " U 5      n U R                  S   nUS-  S:X  a  XS-  S-
     XS-     -   S-  nO
XS-
  S-     nX-
  nX3S:*     nX3S:     nUSS2S4   nXT-
  n[         R                  " US:H  US:H  5      n[         R                  Xg'   XT-   nX-  n	[         R                  " US:H  5      n
U
(       ak  [         R                  " X45      [         R                  " U
5      -
  nUS[         R                  " U5      -  -  n[         R                  " U5      nXSU
2U
* S24'   [         R                  " U	5      $ )a  
Calculates the medcouple robust measure of skew.

Parameters
----------
y : array_like, 1-d
    Data to compute use in the estimator.

Returns
-------
mc : float
    The medcouple statistic

Notes
-----
The current algorithm requires a O(N**2) memory allocations, and so may
not work for very large arrays (N>10000).

.. [*] M. Hubert and E. Vandervieren, "An adjusted boxplot for skewed
   distributions" Computational Statistics & Data Analysis, vol. 52, pp.
   5186-5201, August 2008.
r   z#y must be squeezable to a 1-d arrayr   r	   g        N)r
   ro   r   rp   r&   r4   r   logical_andinfr   oneseyetriufliplrmedian)r=   r   mfzlowerupperstandardizationis_zerospreadhnum_tiesreplacementss               r   _medcouple_1dr   ~  sg   4 	

2::a=!Avv{>??

A	
A1uzQ
maQi'1,A!|_	A3hKE3hKE!T'NEmOnnUc\5C<8G!vvO]F Avvesl#H ww34rvvh7GGBGGL111yy.#/)8)hYZ
 99Q<r   c                     [         R                  " U [         R                  S9n Uc  [        U R	                  5       5      $ [         R
                  " [        X5      $ )a  
Calculate the medcouple robust measure of skew.

Parameters
----------
y : array_like
    Data to compute use in the estimator.
axis : {int, None}
    Axis along which the medcouple statistic is computed.  If `None`, the
    entire array is used.

Returns
-------
mc : ndarray
    The medcouple statistic with the same shape as `y`, with the specified
    axis removed.

Notes
-----
The current algorithm requires a O(N**2) memory allocations, and so may
not work for very large arrays (N>10000).

.. [*] M. Hubert and E. Vandervieren, "An adjusted boxplot for skewed
   distributions" Computational Statistics & Data Analysis, vol. 52, pp.
   5186-5201, August 2008.
r   )r
   r   doubler   r3   rq   )r=   r   s     r   	medcoupler     sA    6 	

1BII&A|QWWY''}d66r   )r   )rX   g      @r/   )r   rX   r   T)__doc__scipyr   numpyr
   statsmodels.tools.sm_exceptionsr   r   r   r-   rI   rW   rm   r   r   r    r   r   <module>r      sK      8%P/64%nIX!3H*ZVr;|7r   