
    M/PhK                         d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 d Zdd	Zd
 ZddZ	 	 ddZ	 d Zd Zd Zd Zd Zd ZdS )zL
Created on Sun Nov  5 14:48:19 2017

Author: Josef Perktold
License: BSD-3
    N)stats)cov2corr)HolderTuple)
array_likec                 L    t           j                            |           d         S )N   )nplinalgslogdet)xs    ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/stats/multivariate.py_logdetr      s    9Q""    Tc                    t          j        |           }|j        \  }}|                    d          }t          j        |dd          }||z
  }||                    t           j                            ||                    z  }	|dz
  |z  ||z
  z  }
|	|
z  }|||z
  f}t          j	        
                    ||d         |d                   }|rt          ||||	d          }|S ||fS )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7LD&66!99D
&5q
)
)
)C)D	d3344	4BQh& D6M2FVI
$-	 BWZZ	2a5"Q%00F !I!' #	% % %
 
&  r   c                    t          | dd          }t          |dd          }|j        \  }}|j        \  }}||k    rd}t          |          |                    d          }	|                    d          }
t	          j        |dd	          }t	          j        |dd	          }||z   }|dz
  |z  |dz
  |z  z   |dz
  z  }|	|
z
  }||z  |z  |z  t          j                            ||          z  }|dz
  |z  ||z
  dz
  z  }||z  }||dz
  |z
  f}t          j	        
                    ||d         |d                   }t          ||||d
          S )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>   @   s   $ 
E4a	(	(	(B	E4a	(	(	(BHME6XNE7&DooGGAJJEGGAJJE6"U+++D6"U+++DU]FQY$&%!)t);;
KL5=D
%-6	!D	(29??<+N+N	NBzV#!(;<FVI
&1*v%	&BWZZ	2a5"Q%00F$ 	" " " "r         ?Fc                     t          j        |           }|j        \  }}|t          j        |          }|                    d          }t          j        |dd          }t          ||||||          }	|	S )a  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.
    Nr   Fr   )
lin_transfalphasimult)r	   r   r   eyer   r   confint_mvmean_fromstats)
r#   rA   rB   rC   r   r&   r'   r   r   cis
             r   confint_mvmeanrG   l   s}    d 	
4A7LD&VF^^
66!99D
&5q
)
)
)C	!$Tj(-f
> 
> 
>BIr   皙?c                 >   t          j        |           } t          j        |          }t          j        |          }t          |           }|du r|                    |           }||                    |j                  j        z                      d          }	|dz
  }
t          j        	                    |dz  |
          }t          j
        |	|
z            |z  }||z
  }||z   }n|                    |           }||                    |j                  j        z                      d          }	|dz
  |z  ||z
  z  |z  }|||z
  f}
t          j        	                    ||
d         |
d                   }t          j
        ||	z  |z            }||z
  }||z   }|||fS )a  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&   rA   rB   rC   cr'   values	quad_formr   	t_critvalci_difflowuppr)   	f_critvals                    r   rE   rE      s   Z :dD
*S//C
j!!AYYFt',,Q//	AXGKK	2..	')b.))I5wwt',,Q//	(f$v6=dVm$GKKr!ube44	'&9,y899wwVr   c                 .   t          j        |           |dz
  z  |z  }t          j        |          }| j        d         }|}|dz
  }|dd|z  dz   d|dz   z  z
  d|dz
  z  dz
  z  z
  z  }t          |          t          ||dz
  z  |z            z
  }|t          j        ||dz
  z  t           j                            ||          z            |z
  z  }||z  }	||dz   z  dz  }
t          j        	                    |	|
          }t          |	||
dd|          S )	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   r[   r"   r   )r   r&   r]   SS0knfactfact2r   r   r   s               r   test_covre     s.   N 	
34!8$t+A	H		B	!AA"9DAQQ!a%[(Q!a%[1_===DBKK'!q1u+/222E	RXa1q5kBIOOB$:$::;;a??EuI	
a!eqBZ]]9b))F$#) (   r   c                    t          j        |           } | j        d         }|dz
  d|dz  z  |z   dz   d|z  z  z
  }||t          j        t          j        |                     z  t          |           z
  |t          j        |          z  z
  z  }||dz   z  dz  dz
  }t          j                            ||          }t          |||dd          S )u  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.   rZ   r[   	sphericalr   r   r   r   r\   )
r	   r   r   logr^   r   r   r[   r"   r   )r   r&   ra   r   r   r   s         r   test_cov_sphericalrj   @  s    T *S//C	!AqA1HqL1,Q77IRVBHSMM***WS\\9Aq		MIII	
a!eq1	BZ]]9b))F$#'	   r   c                 $   t          j        |           } | j        d         }t          |           }|dz
  d|z  dz   dz  z
   t	          |          z  }||dz
  z  dz  }t
          j                            ||          }t          |||dd          S )	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.      rZ   r[   diagonalrh   )	r	   r   r   r   r   r   r[   r"   r   )r   r&   ra   Rr   r   r   s          r   test_cov_diagonalro   y  s    F *S//C	!AA(a!eai1_,-

:I	
a!eqBZ]]9b))F$#&	   r   c                 `   t          |           }t          j        |          }|d         |k    r|dd         }n|d         |k    rt          d          	 t          j        t          j        |          |          }g }|D ])}|                    | |dddf         |f                    *||fS )z$get diagonal blocks from matrix
    Nz)sum of block_len larger than shape of mat)rK   r	   cumsumr1   splitarangeappend)mat	block_lenra   idx
idx_blocksblocksiis          r   _get_blocksr|     s     	CA
)I

C
2w!||#2#h	R1DEEE 	")A,,,,JF , ,c"QQQW+r/*++++:r   c                 H   t          j        |           } t          | |          d         }| j        d         }d |D             }|t	          |          k    rd}t          |          t	          d |D                       }|dz  t	          d |D                       z
  }|dz  t	          d |D                       z
  }	|d	z
  d|	z  d|z  z   d
|z  z  z
  }
|
|t          |           z
  z  }
|dz  }t          j        	                    |
|          }t          |
||dd          S )aK  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   c                 (    g | ]}|j         d          S )r   )r   .0rQ   s     r   
<listcomp>z*test_cov_blockdiagonal.<locals>.<listcomp>  s    ///q
///r   z8sample covariances and blocks do not have matching shapec              3   4   K   | ]}t          |          V  d S )Nr   r   s     r   	<genexpr>z)test_cov_blockdiagonal.<locals>.<genexpr>  s(      77q

777777r   r.   c              3       K   | ]	}|d z  V  
dS )r.   N r   kis     r   r   z)test_cov_blockdiagonal.<locals>.<genexpr>  &      --bBE------r      c              3       K   | ]	}|d z  V  
dS )r   Nr   r   s     r   r   z)test_cov_blockdiagonal.<locals>.<genexpr>  r   r   r   g      @r[   zblock-diagonalrh   )r	   r   r|   r   rM   r1   r   r   r[   r"   r   )r   r&   rw   
cov_blocksra   k_blocksr7   logdet_blocksa2a3r   r   r   s                r   test_cov_blockdiagonalr     sG   J *S//CS),,Q/J	!A//J///HCMMHoo77J77777M	
A--H-----	-B	
A--H-----	-BQVa"f_b99I--I	aBZ]]9b))F$#,	   r   c                 "   t          t          t          j        |                     } t	          |           }t          |          }| d         j        d         }t          d t          ||           D                       }|||z
  z  }||z
  t          |          z  }|t          d t          ||           D                       z  }t          d |D                       d||z
  z  z
  }|d|z  |z  d|z  z   dz
  d|dz   z  |dz
  z  z  z  }|dz
  |z  |dz   z  dz  }d|z
  |z  }	t          j
                            |	|          }
t          d	 |D                       d||z
  dz  z  z
  }||dz
  |dz   z  d|dz
  z  z  z  }|}|dz   t          ||dz  z
            z  }d|z
  ||z  z
  |z  }d|z
  d|z  z   |z  }||dz  k    r||z  }n||z  }||z  |z  d|z   z  }||f}t          j        j        |g|R  }t          ||||	|
|d
|||d          S )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   ,   K   | ]\  }}|d z
  |z  V  dS r   Nr   r   rb   rQ   s      r   r   z"test_cov_oneway.<locals>.<genexpr>.  s.      HHVaa!eq[HHHHHHr   c              3   F   K   | ]\  }}|d z
  t          |          z  V  dS r   r   r   s      r   r   z"test_cov_oneway.<locals>.<genexpr>1  s5      MM&1a!a%71::%MMMMMMr   c              3   &   K   | ]}d |d z
  z  V  dS r   r   r   rb   s     r   r   z"test_cov_oneway.<locals>.<genexpr>4  s*      ,,QQ!a%[,,,,,,r   r   r.   r   rZ   c              3   ,   K   | ]}d |d z
  dz  z  V  dS )r   r.   Nr   r   s     r   r   z"test_cov_oneway.<locals>.<genexpr>:  s.      //Q!a%!^//////r   r[   r   )r   r   statistic_basestatistic_chi2pvalue_chi2df_chi2
distr_chi2statistic_fpvalue_fdf_fdistr_f)listmapr	   r   rK   rM   r   zipr   r   r[   r"   absr!   r   )cov_list	nobs_listmr&   ra   
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HHs9h/G/GHHHHHJ4!8JAX,,,E	SMMC	84L4LMMMMMME 
,,),,,	,	,qD1H~	=B1q57QU?Q1A;!a%#8
99B1ukQU#a'G"f%N*--88K	//Y///	/	/!tax!m2C	CB1q5QU
qAE{
++B	B
q&CRU
OO	#B
b&27
b	 B
b&1r6/R	B	BEzz5j5j2gmq3w/8Dwz+----H&&+&4#.&"(#. ( "
$ 
$ 
$ 
$r   )r   T)Nr?   F)NrH   F)__doc__numpyr	   scipyr    statsmodels.stats.moment_helpersr   statsmodels.stats.baser   statsmodels.tools.validationr   r   r+   r>   rG   rE   re   rj   ro   r|   r   r   r   r   r   <module>r      sC              5 5 5 5 5 5 . . . . . . 3 3 3 3 3 3# # #(! (! (! (!V)" )" )"X; ; ; ;| FJ$)D D D DN&9 9 9x6 6 6r/ / /d  &: : :zV$ V$ V$ V$ V$r   