
    >hlM                         S r SSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  S rSS jrS	 rSS
 jr  SS jr S rS rS rS rS rS rg)zL
Created on Sun Nov  5 14:48:19 2017

Author: Josef Perktold
License: BSD-3
    N)stats)cov2corr)HolderTuple)
array_likec                 F    [         R                  R                  U 5      S   $ )N   )nplinalgslogdet)xs    qC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/stats/multivariate.py_logdetr      s    99Q""    c                    [         R                  " U 5      nUR                  u  pEUR                  S5      n[         R                  " USSS9nXa-
  nXHR                  [         R                  R                  Xx5      5      -  n	US-
  U-  XE-
  -  n
X-  nXTU-
  4n[        R                  R                  XS   US   5      nU(       a  [        UUUU	SS9nU$ X4$ )a[  Hotellings test for multivariate mean in one sample

Parameters
----------
data : array_like
    data with observations in rows and variables in columns
mean_null : array_like
    mean of the multivariate data under the null hypothesis
return_results : bool
    If true, then a results instance is returned. If False, then only
    the test statistic and pvalue are returned.

Returns
-------
results : instance of a results class with attributes
    statistic, pvalue, t2 and df
(statistic, pvalue) : tuple
    If return_results is false, then only the test statistic and the
    pvalue are returned.

r   Fr   rowvarddofF	statisticpvaluedft2distr)r	   asarrayshapemeancovdotr
   solver   fsfr   )data	mean_nullreturn_resultsr   nobsk_varsr   r   diffr   factorr   r   r   ress                  r   test_mvmeanr+      s    , 	

4A77LD66!9D
&&5q
)CD	34	4BQh& DM2FI
-	 BWWZZ	a5"Q%0FI!' #	%
 
  r   c                 \   [        U SSS9n[        USSS9nUR                  u  pEUR                  u  pgXu:w  a  Sn[        U5      eUR                  S5      n	UR                  S5      n
[        R
                  " USSS	9n[        R
                  " USSS	9nXF-   nUS-
  U-  US-
  U-  -   US-
  -  nX-
  nXF-  U-  U-  [        R                  R                  X5      -  nUS-
  U-  X-
  S-
  -  nUU-  nX]S-
  U-
  4n[        R                  R                  UUS   US   5      n[        UUUUS
S9$ )a  Hotellings test for multivariate mean in two independent samples

The null hypothesis is that both samples have the same mean.
The alternative hypothesis is that means differ.

Parameters
----------
data1 : array_like
    first sample data with observations in rows and variables in columns
data2 : array_like
    second sample data with observations in rows and variables in columns

Returns
-------
results : instance of a results class with attributes
    statistic, pvalue, t2 and df
x1   )ndimx2z4both samples need to have the same number of columnsr   Fr   r   r   r   )r   r   
ValueErrorr   r	   r   r
   r    r   r!   r"   r   )data1data2r-   r0   nobs1r'   nobs2k_vars2msgmean1mean2cov1cov2nobs_tcombined_covr(   r   r)   r   r   r   s                        r   test_mvmean_2indepr>   @   sL   $ 
E4a	(B	E4a	(BHHMEXXNEDoGGAJEGGAJE66"U+D66"U+D]FQY$&%!)t);;
KL=D
-6	!D	(299??<+N	NBzV#!(;<FVI
1*v%	&BWWZZ	2a5"Q%0F$ 	" "r   c           	          [         R                  " U 5      nUR                  u  pVUc  [         R                  " U5      nUR	                  S5      n[         R
                  " USSS9n[        XxXQX#S9n	U	$ )a2  Confidence interval for linear transformation of a multivariate mean

Either pointwise or simultaneous confidence intervals are returned.

Parameters
----------
data : array_like
    data with observations in rows and variables in columns
lin_transf : array_like or None
    The linear transformation or contrast matrix for transforming the
    vector of means. If this is None, then the identity matrix is used
    which specifies the means themselves.
alpha : float in (0, 1)
    confidence level for the confidence interval, commonly used is
    alpha=0.05.
simult : bool
    If ``simult`` is False (default), then the pointwise confidence
    interval is returned.
    Otherwise, a simultaneous confidence interval is returned.
    Warning: additional simultaneous confidence intervals might be added
    and the default for those might change.

Returns
-------
low : ndarray
    lower confidence bound on the linear transformed
upp : ndarray
    upper confidence bound on the linear transformed
values : ndarray
    mean or their linear transformation, center of the confidence region

Notes
-----
Pointwise confidence interval is based on Johnson and Wichern
equation (5-21) page 224.

Simultaneous confidence interval is based on Johnson and Wichern
Result 5.3 page 225.
This looks like Sheffe simultaneous confidence intervals.

Bonferroni corrected simultaneous confidence interval might be added in
future

References
----------
Johnson, Richard A., and Dean W. Wichern. 2007. Applied Multivariate
Statistical Analysis. 6th ed. Upper Saddle River, N.J: Pearson Prentice
Hall.
r   Fr   )
lin_transfalphasimult)r	   r   r   eyer   r   confint_mvmean_fromstats)
r#   r@   rA   rB   r   r&   r'   r   r   cis
             r   confint_mvmeanrF   l   sf    d 	

4A77LDVVF^
66!9D
&&5q
)C	!$T(-
>BIr   c                     [         R                  " U 5      n [         R                  " U5      n[         R                  " U5      n[        U 5      nUSL a  UR	                  U 5      nXaR	                  UR
                  5      R
                  -  R                  S5      n	US-
  n
[        R                  R                  US-  U
5      n[         R                  " X-  5      U-  nX-
  nX-   nOUR	                  U 5      nXaR	                  UR
                  5      R
                  -  R                  S5      n	US-
  U-  X'-
  -  U-  nXrU-
  4n
[        R                  R                  XJS   U
S   5      n[         R                  " X-  U-  5      nX-
  nX-   nXU4$ )aY  Confidence interval for linear transformation of a multivariate mean

Either pointwise or simultaneous confidence intervals are returned.
Data is provided in the form of summary statistics, mean, cov, nobs.

Parameters
----------
mean : ndarray
cov : ndarray
nobs : int
lin_transf : array_like or None
    The linear transformation or contrast matrix for transforming the
    vector of means. If this is None, then the identity matrix is used
    which specifies the means themselves.
alpha : float in (0, 1)
    confidence level for the confidence interval, commonly used is
    alpha=0.05.
simult : bool
    If simult is False (default), then pointwise confidence interval is
    returned.
    Otherwise, a simultaneous confidence interval is returned.
    Warning: additional simultaneous confidence intervals might be added
    and the default for those might change.

Notes
-----
Pointwise confidence interval is based on Johnson and Wichern
equation (5-21) page 224.

Simultaneous confidence interval is based on Johnson and Wichern
Result 5.3 page 225.
This looks like Sheffe simultaneous confidence intervals.

Bonferroni corrected simultaneous confidence interval might be added in
future

References
----------
Johnson, Richard A., and Dean W. Wichern. 2007. Applied Multivariate
Statistical Analysis. 6th ed. Upper Saddle River, N.J: Pearson Prentice
Hall.

Fr   r.   r   )r	   r   
atleast_2dlenr   Tsumr   tisfsqrtr!   )r   r   r&   r@   rA   rB   cr'   values	quad_formr   	t_critvalci_difflowuppr)   	f_critvals                    r   rD   rD      sW   Z ::dD
**S/C
j!AYFt',,Q/	AXGGKK	2.	'').)I5t',,Q/	(f$6=Vm$GGKK!ube4	''&,y89Vr   c           	         [         R                  " U 5      US-
  -  U-  n[         R                  " U5      nU R                  S   nUnUS-
  nUSSU-  S-   SUS-   -  -
  SUS-
  -  S-
  -  -
  -  n[        U5      [        XfS-
  -  U-  5      -
  nU[         R                  " XfS-
  -  [         R
                  R                  XC5      -  5      U-
  -  nXx-  n	XUS-   -  S-  n
[        R                  R                  X5      n[        U	UU
SSUS9$ )	u@  One sample hypothesis test for covariance equal to null covariance

The Null hypothesis is that cov = cov_null, against the alternative that
it is not equal to cov_null

Parameters
----------
cov : array_like
    Covariance matrix of the data, estimated with denominator ``(N - 1)``,
    i.e. `ddof=1`.
nobs : int
    number of observations used in the estimation of the covariance
cov_null : nd_array
    covariance under the null hypothesis

Returns
-------
res : instance of HolderTuple
    results with ``statistic, pvalue`` and other attributes like ``df``

References
----------
Bartlett, M. S. 1954. “A Note on the Multiplying Factors for Various Χ2
Approximations.” Journal of the Royal Statistical Society. Series B
(Methodological) 16 (2): 296–98.

Rencher, Alvin C., and William F. Christensen. 2012. Methods of
Multivariate Analysis: Rencher/Methods. Wiley Series in Probability and
Statistics. Hoboken, NJ, USA: John Wiley & Sons, Inc.
https://doi.org/10.1002/9781118391686.

StataCorp, L. P. Stata Multivariate Statistics: Reference Manual.
Stata Press Publication.

r   r   g      ?r.      chi2zequal value)r   r   r   r   nullcov_null)r	   r   r   r   tracer
   r    r   rY   r"   r   )r   r&   r[   SS0knfactfact2r   r   r   s               r   test_covrc     s   N 	

34!8$t+A	H	B		!AA"9DAQQ!a%[(Q!a%[1_===DBK'!1u+/22E	RXXaq5kBIIOOB$::;a??EI	
!eqBZZ]]9)F$#) ( r   c                    [         R                  " U 5      n U R                  S   nUS-
  SUS-  -  U-   S-   SU-  -  -
  nX2[         R                  " [         R                  " U 5      5      -  [        U 5      -
  U[         R                  " U5      -  -
  -  nX"S-   -  S-  S-
  n[        R                  R                  X45      n[        UUUSSS9$ )uP  One sample hypothesis test that covariance matrix is spherical

The Null and alternative hypotheses are

.. math::

   H0 &: \Sigma = \sigma I \\
   H1 &: \Sigma \neq \sigma I

where :math:`\sigma_i` is the common variance with unspecified value.

Parameters
----------
cov : array_like
    Covariance matrix of the data, estimated with denominator ``(N - 1)``,
    i.e. `ddof=1`.
nobs : int
    number of observations used in the estimation of the covariance

Returns
-------
res : instance of HolderTuple
    results with ``statistic, pvalue`` and other attributes like ``df``

References
----------
Bartlett, M. S. 1954. “A Note on the Multiplying Factors for Various Χ2
Approximations.” Journal of the Royal Statistical Society. Series B
(Methodological) 16 (2): 296–98.

Rencher, Alvin C., and William F. Christensen. 2012. Methods of
Multivariate Analysis: Rencher/Methods. Wiley Series in Probability and
Statistics. Hoboken, NJ, USA: John Wiley & Sons, Inc.
https://doi.org/10.1002/9781118391686.

StataCorp, L. P. Stata Multivariate Statistics: Reference Manual.
Stata Press Publication.
r   r   r.   rX   rY   	sphericalr   r   r   r   rZ   )
r	   r   r   logr\   r   r   rY   r"   r   )r   r&   r_   r   r   r   s         r   test_cov_sphericalrh   @  s    T **S/C		!AqA1HqL1,Q77IRVVBHHSM**WS\9Aq	MIII	
!eq1	BZZ]]9)F$#'	 r   c                 
   [         R                  " U 5      n U R                  S   n[        U 5      nUS-
  SU-  S-   S-  -
  * [	        U5      -  nX"S-
  -  S-  n[
        R                  R                  XE5      n[        UUUSSS9$ )	a  One sample hypothesis test that covariance matrix is diagonal matrix.

The Null and alternative hypotheses are

.. math::

   H0 &: \Sigma = diag(\sigma_i) \\
   H1 &: \Sigma \neq diag(\sigma_i)

where :math:`\sigma_i` are the variances with unspecified values.

Parameters
----------
cov : array_like
    Covariance matrix of the data, estimated with denominator ``(N - 1)``,
    i.e. `ddof=1`.
nobs : int
    number of observations used in the estimation of the covariance

Returns
-------
res : instance of HolderTuple
    results with ``statistic, pvalue`` and other attributes like ``df``

References
----------
Rencher, Alvin C., and William F. Christensen. 2012. Methods of
Multivariate Analysis: Rencher/Methods. Wiley Series in Probability and
Statistics. Hoboken, NJ, USA: John Wiley & Sons, Inc.
https://doi.org/10.1002/9781118391686.

StataCorp, L. P. Stata Multivariate Statistics: Reference Manual.
Stata Press Publication.
r   r   r.      rX   rY   diagonalrf   )	r	   r   r   r   r   r   rY   r"   r   )r   r&   r_   Rr   r   r   s          r   test_cov_diagonalrm   y  s    F **S/C		!AA(a!eai1_,-
:I	
!eqBZZ]]9)F$#&	 r   c                 6   [        U 5      n[        R                  " U5      nUS   U:X  a  USS nOUS   U:  a  [        S5      e [        R                  " [        R
                  " U5      U5      n/ nU H  nUR                  XSS2S4   U4   5        M!     XT4$ )z$get diagonal blocks from matrix
    Nz)sum of block_len larger than shape of mat)rI   r	   cumsumr1   splitarangeappend)mat	block_lenr_   idx
idx_blocksblocksiis          r   _get_blocksrz     s     	CA
))I
C
2w!|#2h	R1DEE 	"))A,,JFcQW+r/*+ r   c                    [         R                  " U 5      n [        X5      S   nU R                  S   nU Vs/ s H  oUR                  S   PM     nnU[	        U5      :w  a  Sn[        U5      e[	        S U 5       5      nUS-  [	        S U 5       5      -
  n	US-  [	        S U 5       5      -
  n
US-
  SU
-  SU	-  -   S	U	-  -  -
  nX[        U 5      -
  -  nU	S-  n[        R                  R                  X5      n[        UUUS
SS9$ s  snf )a  One sample hypothesis test that covariance is block diagonal.

The Null and alternative hypotheses are

.. math::

   H0 &: \Sigma = diag(\Sigma_i) \\
   H1 &: \Sigma \neq diag(\Sigma_i)

where :math:`\Sigma_i` are covariance blocks with unspecified values.

Parameters
----------
cov : array_like
    Covariance matrix of the data, estimated with denominator ``(N - 1)``,
    i.e. `ddof=1`.
nobs : int
    number of observations used in the estimation of the covariance
block_len : list
    list of length of each square block

Returns
-------
res : instance of HolderTuple
    results with ``statistic, pvalue`` and other attributes like ``df``

References
----------
Rencher, Alvin C., and William F. Christensen. 2012. Methods of
Multivariate Analysis: Rencher/Methods. Wiley Series in Probability and
Statistics. Hoboken, NJ, USA: John Wiley & Sons, Inc.
https://doi.org/10.1002/9781118391686.

StataCorp, L. P. Stata Multivariate Statistics: Reference Manual.
Stata Press Publication.
r   z8sample covariances and blocks do not have matching shapec              3   8   #    U  H  n[        U5      v   M     g 7f)Nr   ).0rO   s     r   	<genexpr>)test_cov_blockdiagonal.<locals>.<genexpr>  s     7Jq

J   r.   c              3   *   #    U  H	  oS -  v   M     g7f)r.   N r~   kis     r   r   r          -HbEH      c              3   *   #    U  H	  oS -  v   M     g7f)r   Nr   r   s     r   r   r     r   r   r   g      @rY   zblock-diagonalrf   )r	   r   rz   r   rK   r1   r   r   rY   r"   r   )r   r&   ru   
cov_blocksr_   rO   k_blocksr7   logdet_blocksa2a3r   r   r   s                 r   test_cov_blockdiagonalr     s   J **S/CS,Q/J		!A$./Jq
JH/CMHo7J77M	
A-H--	-B	
A-H--	-BQVa"f_b99I--I	aBZZ]]9)F$#,	  0s   Dc                    [        [        [        R                  U 5      5      n [	        U 5      n[        U5      nU S   R                  S   n[        S [        X5       5       5      nXSU-
  -  nX2-
  [        U5      -  nU[        S [        X5       5       5      -  n[        S U 5       5      SX2-
  -  -
  nUSU-  U-  SU-  -   S-
  SUS-   -  US-
  -  -  -  nUS-
  U-  US-   -  S-  nSU-
  U-  n	[        R                  R                  X5      n
[        S	 U 5       5      SX2-
  S-  -  -
  nXS-
  US-   -  SUS-
  -  -  -  nUnUS-   [        XS-  -
  5      -  nSU-
  X-  -
  U-  nSU-
  SU-  -   U-  nXS-  :  a  X-  nOX-  nX-  U-  SU-   -  nX4n[        R                  R                  " U/UQ76 n[        UUUU	U
US
UUUSS9$ )a  Multiple sample hypothesis test that covariance matrices are equal.

This is commonly known as Box-M test.

The Null and alternative hypotheses are

.. math::

   H0 &: \Sigma_i = \Sigma_j  \text{ for all i and j} \\
   H1 &: \Sigma_i \neq \Sigma_j \text{ for at least one i and j}

where :math:`\Sigma_i` is the covariance of sample `i`.

Parameters
----------
cov_list : list of array_like
    Covariance matrices of the sample, estimated with denominator
    ``(N - 1)``, i.e. `ddof=1`.
nobs_list : list
    List of the number of observations used in the estimation of the
    covariance for each sample.

Returns
-------
res : instance of HolderTuple
    Results contains test statistic and pvalues for both chisquare and F
    distribution based tests, identified by the name ending "_chi2" and
    "_f".
    Attributes ``statistic, pvalue`` refer to the F-test version.

Notes
-----
approximations to distribution of test statistic is by Box

References
----------
Rencher, Alvin C., and William F. Christensen. 2012. Methods of
Multivariate Analysis: Rencher/Methods. Wiley Series in Probability and
Statistics. Hoboken, NJ, USA: John Wiley & Sons, Inc.
https://doi.org/10.1002/9781118391686.

StataCorp, L. P. Stata Multivariate Statistics: Reference Manual.
Stata Press Publication.
r   c              3   6   #    U  H  u  pUS -
  U-  v   M     g7fr   Nr   r~   r`   rO   s      r   r   "test_cov_oneway.<locals>.<genexpr>.  s     H/GVaa!eq[/Gs   c              3   H   #    U  H  u  pUS -
  [        U5      -  v   M     g7fr   r}   r   s      r   r   r   1  s!     M4L&1!a%71:%4Ls    "c              3   2   #    U  H  nS US -
  -  v   M     g7fr   r   r~   r`   s     r   r   r   4  s     ,)QQ!a%[)s   r   r.   r   rX   c              3   8   #    U  H  nS US -
  S-  -  v   M     g7f)r   r.   Nr   r   s     r   r   r   :  s     /YQ!a%!^Yr   rY   r   )r   r   statistic_basestatistic_chi2pvalue_chi2df_chi2
distr_chi2statistic_fpvalue_fdf_fdistr_f)listmapr	   r   rI   rK   r   zipr   r   rY   r"   absr!   r   )cov_list	nobs_listmr&   r_   
cov_pooledstat0c1r   r   r   c2a1r   b1b2r   tmpr   r   s                       r   test_cov_onewayr     s   \ C

H-.HHAy>D!AHs9/GHHJ!8JX,,E	SMC	4LMMME 
,),	,qDH~	=B1q57QU?Q1A;!a%#8
99B1ukQU#a'G"f%N**--8K	/Y/	/!tx!m2C	CBq5QU
qAE{
++B	B
q&CU
O	#B
b&27
b	 B
b&1r6/R	B	Ezjjgmq3w/8Dwwzz+--H&&+&4#.&"(#. ( "
$ 
$r   )r   T)Ng      ?F)Ng?F)__doc__numpyr	   scipyr    statsmodels.stats.moment_helpersr   statsmodels.stats.baser   statsmodels.tools.validationr   r   r+   r>   rF   rD   rc   rh   rm   rz   r   r   r   r   r   <module>r      sk      5 . 3#(!V)"X;| FJ$)DN&9x6r/d&:zV$r   