← Back
Editing: _hypotests.cpython-311.pyc
� d�c�2 � � � d dl mZ d dlmZ d dlmZ d dlZd dlZd dl m Z d dlZd dl mZ ddlmZ dd lmZ dd lmZmZ d dlmZmZmZ d dlmZ dd lmZ ddlmZm Z! d dlm"Z" g d�Z# edd� � Z$d9d�Z%d dd�d�Z&d� Z' G d� d� � Z(d� Z)d� Z*d:d�Z+d;d�Z,d � Z-d!� Z.d"� Z/d<d#�Z0 ed$d%� � Z1d=d&�Z2d'� Z3d(� Z4 ed)d*e5fd+e5fg� � Z6d>d.�Z7 ed/d*e5fd+e5fg� � Z8d?d0�Z9d1� Z:d2� Z;d@d4�Z< G d5� d6� � Z=d7� Z>d8� Z?dS )A� )� namedtuple)�make_dataclass)�combN)�combinations)�shgo� )� distributions)�ConfidenceInterval)�chi2�norm)�gamma�kv�gammaln)�ifft)�_a_ij_Aij_Dij2)�_concordant_pairs�_discordant_pairs)� _stats_py)�epps_singleton_2samp�cramervonmises�somersd� barnard_exact�boschloo_exact�cramervonmises_2samp� tukey_hsd�poisson_means_test�Epps_Singleton_2sampResult�� statistic�pvalue�g�������?g�������?c �� � t j | � � t j |� � t j |� � }}} | j dk r't d� | j � � � � �|j dk r't d� |j � � � � �t | � � t |� � }}|dk s|dk r#t d� ||� � � � �t j | � � � � � st d� � �t j |� � � � � st d� � �||z }|j dk r't d� |j � � � � �t j |d � � � � � rt d � � �d dl m} |t j | |f� � � � dz }t j |d � � |z }t j t j || z � � t j || z � � f� � j } t j t j ||z � � t j ||z � � f� � j } t j | j d�� � }t j | j d�� � }||z |z ||z |z z } t j � | � � }t j � |� � }|dt |� � z k rt- j d� � t j | d �� � t j | d �� � z }|t j |j t j ||� � � � z }t5 ||� � dk rdd|dz z d|dz |dz z z z z }||z }t7 j ||� � }t; ||� � S )a� Compute the Epps-Singleton (ES) test statistic. Test the null hypothesis that two samples have the same underlying probability distribution. Parameters ---------- x, y : array-like The two samples of observations to be tested. Input must not have more than one dimension. Samples can have different lengths. t : array-like, optional The points (t1, ..., tn) where the empirical characteristic function is to be evaluated. It should be positive distinct numbers. The default value (0.4, 0.8) is proposed in [1]_. Input must not have more than one dimension. Returns ------- statistic : float The test statistic. pvalue : float The associated p-value based on the asymptotic chi2-distribution. See Also -------- ks_2samp, anderson_ksamp Notes ----- Testing whether two samples are generated by the same underlying distribution is a classical question in statistics. A widely used test is the Kolmogorov-Smirnov (KS) test which relies on the empirical distribution function. Epps and Singleton introduce a test based on the empirical characteristic function in [1]_. One advantage of the ES test compared to the KS test is that is does not assume a continuous distribution. In [1]_, the authors conclude that the test also has a higher power than the KS test in many examples. They recommend the use of the ES test for discrete samples as well as continuous samples with at least 25 observations each, whereas `anderson_ksamp` is recommended for smaller sample sizes in the continuous case. The p-value is computed from the asymptotic distribution of the test statistic which follows a `chi2` distribution. If the sample size of both `x` and `y` is below 25, the small sample correction proposed in [1]_ is applied to the test statistic. The default values of `t` are determined in [1]_ by considering various distributions and finding good values that lead to a high power of the test in general. Table III in [1]_ gives the optimal values for the distributions tested in that study. The values of `t` are scaled by the semi-interquartile range in the implementation, see [1]_. References ---------- .. [1] T. W. Epps and K. J. Singleton, "An omnibus test for the two-sample problem using the empirical characteristic function", Journal of Statistical Computation and Simulation 26, p. 177--203, 1986. .. [2] S. J. Goerg and J. Kaiser, "Nonparametric testing of distributions - the Epps-Singleton two-sample test using the empirical characteristic function", The Stata Journal 9(3), p. 454--465, 2009. r z#x must be 1d, but x.ndim equals {}.z#y must be 1d, but y.ndim equals {}.� zIx and y should have at least 5 elements, but len(x) = {} and len(y) = {}.z$x must not contain nonfinite values.z$y must not contain nonfinite values.z#t must be 1d, but t.ndim equals {}.r z&t must contain positive elements only.)�iqr� )���r T)�biasz�Estimated covariance matrix does not have full rank. This indicates a bad choice of the input t and the test might not be consistent.��axis� � �?g������ܿg333333$@g333333��)�np�asarray�ndim� ValueError�format�len�isfinite�all� less_equal�any�scipy.statsr$ �hstack�reshape�vstack�cos�sin�T�cov�linalg�pinv�matrix_rank�warnings�warn�mean�dot�maxr �sfr )�x�y�t�nx�ny�nr$ �sigma�ts�gx�gy�cov_x�cov_y�est_cov�est_cov_inv�r�g_diff�w�corr�ps �8/usr/lib/python3/dist-packages/scipy/stats/_hypotests.pyr r s� � �D �j��m�m�R�Z��]�]�B�J�q�M�M�!�q�A��v��z�z��>�E�E�a�f�M�M�N�N�N��v��z�z��>�E�E�a�f�M�M�N�N�N� ��V�V�S��V�V��B� �Q���B��F�F�� 1�17���B���A� A� A� �;�q�>�>����� A��?�@�@�@� �;�q�>�>����� A��?�@�@�@� �R��A� �v��z�z��>�E�E�a�f�M�M�N�N�N� �}�Q������ � � C��A�B�B�B� �������C�� �1�a�&�!�!�"�"�Q�&�E� ��A�w� � �%� '�B� ��B�F�2�a�4�L�L�"�&��A��,�,�/� 0� 0� 2�B� ��B�F�2�a�4�L�L�"�&��A��,�,�/� 0� 0� 2�B��F�2�4�d�#�#�#�E��F�2�4�d�#�#�#�E���t�U�l�a��d�E�\�)�G��)�.�.��)�)�K� � ���k�*�*�A��1�S��V�V�8�|�|�� � 6� 7� 7� 7� �W�R�a� � � �2�7�2�A�#6�#6�#6� 6�F� �"�&���2�6�+�v�6�6� 7� 7�7�A� �B����b����C�!�e�*�$�t�R�$�Z�"�t�*�-D�'E�E�F���1�H�����1� � �A�%�a��+�+�+� � two-sided)�diff�alternativec �H � t | |||||� � | |z ||z z ||z ||z z z }|dk rt j dd� � S | |dz z ||dz z z }| |z ||z z |z t j |� � z }|||z z } ||z } t j � ddg| � � \ }}t j � ddg| � � \ } }t j ||dz � � }t j | |dz � � dd�df }t j � || � � }t j � || � � }||z }||z }||z |z }||z ||z z }t j dd�� � 5 |t j |� � z }ddd� � n# 1 swxY w Y |d k r+t j |� � t j |� � k }n|d k r||k }n||k }t j ||z | � � }t j ||� � S )a� Performs the Poisson means test, AKA the "E-test". This is a test of the null hypothesis that the difference between means of two Poisson distributions is `diff`. The samples are provided as the number of events `k1` and `k2` observed within measurement intervals (e.g. of time, space, number of observations) of sizes `n1` and `n2`. Parameters ---------- k1 : int Number of events observed from distribution 1. n1: float Size of sample from distribution 1. k2 : int Number of events observed from distribution 2. n2 : float Size of sample from distribution 2. diff : float, default=0 The hypothesized difference in means between the distributions underlying the samples. alternative : {'two-sided', 'less', 'greater'}, optional Defines the alternative hypothesis. The following options are available (default is 'two-sided'): * 'two-sided': the difference between distribution means is not equal to `diff` * 'less': the difference between distribution means is less than `diff` * 'greater': the difference between distribution means is greater than `diff` Returns ------- statistic : float The test statistic (see [1]_ equation 3.3). pvalue : float The probability of achieving such an extreme value of the test statistic under the null hypothesis. Notes ----- Let: .. math:: X_1 \sim \mbox{Poisson}(\mathtt{n1}\lambda_1) be a random variable independent of .. math:: X_2 \sim \mbox{Poisson}(\mathtt{n2}\lambda_2) and let ``k1`` and ``k2`` be the observed values of :math:`X_1` and :math:`X_2`, respectively. Then `poisson_means_test` uses the number of observed events ``k1`` and ``k2`` from samples of size ``n1`` and ``n2``, respectively, to test the null hypothesis that .. math:: H_0: \lambda_1 - \lambda_2 = \mathtt{diff} A benefit of the E-test is that it has good power for small sample sizes, which can reduce sampling costs [1]_. It has been evaluated and determined to be more powerful than the comparable C-test, sometimes referred to as the Poisson exact test. References ---------- .. [1] Krishnamoorthy, K., & Thomson, J. (2004). A more powerful test for comparing two Poisson means. Journal of Statistical Planning and Inference, 119(1), 23-35. .. [2] Przyborowski, J., & Wilenski, H. (1940). Homogeneity of results in testing samples from Poisson series: With an application to testing clover seed for dodder. Biometrika, 31(3/4), 313-323. Examples -------- Suppose that a gardener wishes to test the number of dodder (weed) seeds in a sack of clover seeds that they buy from a seed company. It has previously been established that the number of dodder seeds in clover follows the Poisson distribution. A 100 gram sample is drawn from the sack before being shipped to the gardener. The sample is analyzed, and it is found to contain no dodder seeds; that is, `k1` is 0. However, upon arrival, the gardener draws another 100 gram sample from the sack. This time, three dodder seeds are found in the sample; that is, `k2` is 3. The gardener would like to know if the difference is significant and not due to chance. The null hypothesis is that the difference between the two samples is merely due to chance, or that :math:`\lambda_1 - \lambda_2 = \mathtt{diff}` where :math:`\mathtt{diff} = 0`. The alternative hypothesis is that the difference is not due to chance, or :math:`\lambda_1 - \lambda_2 \ne 0`. The gardener selects a significance level of 5% to reject the null hypothesis in favor of the alternative [2]_. >>> import scipy.stats as stats >>> res = stats.poisson_means_test(0, 100, 3, 100) >>> res.statistic, res.pvalue (-1.7320508075688772, 0.08837900929018157) The p-value is .088, indicating a near 9% chance of observing a value of the test statistic under the null hypothesis. This exceeds 5%, so the gardener does not reject the null hypothesis as the difference cannot be regarded as significant at this level. r r r% g�����|�=g�������?N�ignore)�invalid�divider\ �less) �_poisson_means_test_ivr �SignificanceResultr, �sqrtr �poisson�ppf�arange�pmf�errstate�abs�sum)�k1�n1�k2�n2r] r^ � lmbd_hat2�var�t_k1k2� nlmbd_hat1� nlmbd_hat2�x1_lb�x1_ub�x2_lb�x2_ub�x1�x2�prob_x1�prob_x2�lmbd_x1�lmbd_x2� lmbds_diff�var_x1x2�t_x1x2� indicatorr s rZ r r � s� � �V �2�r�2�r�4��=�=�=� �r�'�b�2�g�&����b�2�g�)>�>�I� �A�~�~��+�A�q�1�1�1� ��a��.�2��q��>� )�C� �2�g��R���$�&�"�'�#�,�,� 6�F� �y�4�'�(�J��i��J� !�(�,�,�e�Y�-?��L�L�L�E�5� �(�,�,�e�Y�-?��L�L�L�E�5� ��5�%�!�)� $� $�B� ��5�%�!�)� $� $�Q�Q�Q��W� -�B� �#�'�'��J�7�7�G��#�'�'��J�7�7�G� �2�g�G��2�g�G��7�"�T�)�J���|�g��l�*�H� ��X�h� 7� 7� 7� 0� 0��b�g�h�/�/�/��0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0���� 0� 0� 0� 0� �k�!�!��F�6�N�N�b�f�V�n�n�4� � � �� � ��f�$� � ��f�$� � �V�W�w�&� �2� 3� 3�F��'���7�7�7s �?F#�#F'�*F'c �d � | t | � � k s|t |� � k rt d� � �d}| dk s|dk rt |� � �|dk s|dk rt d� � �|dk rt d� � �h d�}|� � � |vrt d|� d�� � �d S ) Nz`k1` and `k2` must be integers.z1`k1` and `k2` must be greater than or equal to 0.r z%`n1` and `n2` must be greater than 0.z(diff must be greater than or equal to 0.> rc �greaterr\ zAlternative must be one of 'z'.)�int� TypeErrorr/ �lower)rn ro rp rq r] r^ � count_err�alternativess rZ rd rd N s� � � �S��W�W�}�}��c�"�g�g� � ��9�:�:�:�C�I� �A�v�v��a�����#�#�#� �Q�w�w�"��'�'��@�A�A�A��a�x�x��C�D�D�D�3�3�3�L������,�.�.��H��H�H�H�I�I�I� /�.r[ c � � e Zd Zd� Zd� ZdS )�CramerVonMisesResultc �"