
    _MhA                        d dl Z d dlmZ d dlmZmZ d dlmZm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 d	lmZ d d
lmZmZmZ erd dlmZ dgZe G d d                      Z edd          dddddddded         dedef
d            Zded         ddded         dedeee
j                  e
j         ef         f
dZ!dee
j                  de
j         dee
j         e"e"e
j         e"f         fdZ#dee
j                  de
j         de"de
j         de"dee
j         e$e
j         e
j         f         fd Z%	 d$d!e
j         de"d"e
j         ded         dede
j         fd#Z&dS )%    N)Sequence)	dataclassfield)TYPE_CHECKINGLiteral)stats)minimize_scalar)ConfidenceInterval)check_random_state)_var)_transition_to_rngDecimalNumberSeedTypedunnettc                   (   e Zd ZU dZej        ed<   ej        ed<    ed          Ze	d         ed<    ed          Z
ej        ed<    ed          Zeed	<    ed          Zeed
<    ed          Zej        ed<    ed          Zej        ed<    ed          Zej        ed<    ed          Zeed<    ed          Zeed<    edd          Zedz  ed<    edd          Zedz  ed<   d Z	 ddededefdZ	 ddedefdZdS )DunnettResulta  Result object returned by `scipy.stats.dunnett`.

    Attributes
    ----------
    statistic : float ndarray
        The computed statistic of the test for each comparison. The element
        at index ``i`` is the statistic for the comparison between
        groups ``i`` and the control.
    pvalue : float ndarray
        The computed p-value of the test for each comparison. The element
        at index ``i`` is the p-value for the comparison between
        group ``i`` and the control.
    	statisticpvalueF)repr	two-sidedlessgreater_alternative_rho_df_std_mean_samples_mean_control
_n_samples
_n_control_rngN)defaultr   _ci_ci_clc           
      .   | j         |                     d           d| j        dz  dd}t          | j        j                  D ]N}|d| d| j        |         d	| j        |         d	| j         j        |         d	| j         j        |         d	d
z  }O|S )Nffffff?confidence_levelzDunnett's test (d   z.1fzW% Confidence Interval)
Comparison               Statistic  p-value  Lower CI  Upper CI
z	 (Sample z - Control) z>10.3f
)	r$   confidence_intervalr%   ranger   sizer   lowhigh)selfsis      V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/stats/_multicomp.py__str__zDunnettResult.__str__6   s     8$$c$:::PS>P P P 	

 t{'(( 	2 	2A 1a 1 1T^A->G 1[^,1X\!_-1 X]1%01 1 1 2AA
     r'   MbP?r)   tolreturnc                 H    d|z
   fd}t          |d|          }|j        }|j        du s|j        |dz  k    r%t	          j        dd|j        z   z   dd	
           | j        z  t          j        d j	        z  d j
        z  z             z  }t          |          S )aS  Allowance.

        It is the quantity to add/subtract from the observed difference
        between the means of observed groups and the mean of the control
        group. The result gives confidence limits.

        Parameters
        ----------
        confidence_level : float, optional
            Confidence level for the computed confidence interval.
            Default is .95.
        tol : float, optional
            A tolerance for numerical optimization: the allowance will produce
            a confidence within ``10*tol*(1 - confidence_level)`` of the
            specified level, or a warning will be emitted. Tight tolerances
            may be impractical due to noisy evaluation of the objective.
            Default is 1e-3.

        Returns
        -------
        allowance : float
            Allowance around the mean.
           c                     t          j        |           } t          j        j        | j        j                  }t          |z
            z  S )Nrhodfr   alternativerng)nparray_pvalue_dunnettr   r   r   r"   abs)r   sfalphar1   s     r4   pvalue_from_statz2DunnettResult._allowance.<locals>.pvalue_from_state   sS    ++I I$(#1BI  B
 rEz??5((r6   brent)methodr8   F
   zComputation of the confidence interval did not converge to the desired level. The confidence level corresponding with the returned interval is approximately .   )
stacklevel)r	   xsuccessfunwarningswarnr   rB   sqrtr    r!   rE   )r1   r)   r8   rH   rescritical_value	allowancerG   s   `      @r4   
_allowancezDunnettResult._allowanceI   s    4 $$	) 	) 	) 	) 	) 	) .wCHHH ;%37c"f#4#4MO:?37:KO O O 	    #49,RWdo$/ 11.
 .
 
	 9~~r6   c                    | j         || j        k    r| j         S d|cxk     rdk     sn t          d          |                     |          }| j        | j        z
  }||z
  }||z   }| j        dk    rt          j        gt          |          z  }n)| j        dk    rt          j         gt          |          z  }|| _        t          ||          | _         | j         S )	a_  Compute the confidence interval for the specified confidence level.

        Parameters
        ----------
        confidence_level : float, optional
            Confidence level for the computed confidence interval.
            Default is .95.

        Returns
        -------
        ci : ``ConfidenceInterval`` object
            The object has attributes ``low`` and ``high`` that hold the
            lower and upper bounds of the confidence intervals for each
            comparison. The high and low values are accessible for each
            comparison at index ``i`` for each group ``i``.

        Nr   r;   z)Confidence level must be between 0 and 1.r(   r   r   )r/   r0   )r$   r%   
ValueErrorrX   r   r   r   rB   inflenr
   )r1   r)   rW   
diff_meansr/   r0   s         r4   r,   z!DunnettResult.confidence_interval   s    , H '74;'F'F8O$((((q((((HIIIOO5EOFF	'$*<<
")#	))F8c*oo-DD&((F7)c*oo-C&%
 
 
 xr6   )r'   r7   )r'   )__name__
__module____qualname____doc__rB   ndarray__annotations__r   r   r   r   r   intr   floatr   r   r    r!   r"   r   r$   r
   r%   r   r5   rX   r,    r6   r4   r   r      s          zJ<AEu<M<M<ML'89MMMu%(((D"*(((u%   C   %U###D%### %5 1 1 1M2:111 %5 1 1 1M2:111"U...J
...e'''J'''U&&&D(&&&%*U4e%D%D%DC	d	"DDD#(5E#B#B#BFMD BBB  ( LP< < -<;H<	< < < <~ 15, , -,	, , , , , ,r6   r   random_stateF)replace_docr   )r@   rA   samplesznpt.ArrayLikecontrolr@   r   rA   r9   c                     t          || ||          \  }}}t          ||          \  }}}}	}
t          ||||	|
          \  }}}}t          |||||          }t	          |||||||||	|
|          S )a  Dunnett's test: multiple comparisons of means against a control group.

    This is an implementation of Dunnett's original, single-step test as
    described in [1]_.

    Parameters
    ----------
    sample1, sample2, ... : 1D array_like
        The sample measurements for each experimental group.
    control : 1D array_like
        The sample measurements for the control group.
    alternative : {'two-sided', 'less', 'greater'}, optional
        Defines the alternative hypothesis.

        The null hypothesis is that the means of the distributions underlying
        the samples and control are equal. The following alternative
        hypotheses are available (default is 'two-sided'):

        * 'two-sided': the means of the distributions underlying the samples
          and control are unequal.
        * 'less': the means of the distributions underlying the samples
          are less than the mean of the distribution underlying the control.
        * 'greater': the means of the distributions underlying the
          samples are greater than the mean of the distribution underlying
          the control.
    rng : `numpy.random.Generator`, optional
        Pseudorandom number generator state. When `rng` is None, a new
        `numpy.random.Generator` is created using entropy from the
        operating system. Types other than `numpy.random.Generator` are
        passed to `numpy.random.default_rng` to instantiate a ``Generator``.

        .. versionchanged:: 1.15.0

            As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
            transition from use of `numpy.random.RandomState` to
            `numpy.random.Generator`, this keyword was changed from `random_state` to
            `rng`. For an interim period, both keywords will continue to work, although
            only one may be specified at a time. After the interim period, function
            calls using the `random_state` keyword will emit warnings. Following a
            deprecation period, the `random_state` keyword will be removed.

    Returns
    -------
    res : `~scipy.stats._result_classes.DunnettResult`
        An object containing attributes:

        statistic : float ndarray
            The computed statistic of the test for each comparison. The element
            at index ``i`` is the statistic for the comparison between
            groups ``i`` and the control.
        pvalue : float ndarray
            The computed p-value of the test for each comparison. The element
            at index ``i`` is the p-value for the comparison between
            group ``i`` and the control.

        And the following method:

        confidence_interval(confidence_level=0.95) :
            Compute the difference in means of the groups
            with the control +- the allowance.

    See Also
    --------
    tukey_hsd : performs pairwise comparison of means.
    :ref:`hypothesis_dunnett` : Extended example

    Notes
    -----
    Like the independent-sample t-test, Dunnett's test [1]_ is used to make
    inferences about the means of distributions from which samples were drawn.
    However, when multiple t-tests are performed at a fixed significance level,
    the "family-wise error rate" - the probability of incorrectly rejecting the
    null hypothesis in at least one test - will exceed the significance level.
    Dunnett's test is designed to perform multiple comparisons while
    controlling the family-wise error rate.

    Dunnett's test compares the means of multiple experimental groups
    against a single control group. Tukey's Honestly Significant Difference Test
    is another multiple-comparison test that controls the family-wise error
    rate, but `tukey_hsd` performs *all* pairwise comparisons between groups.
    When pairwise comparisons between experimental groups are not needed,
    Dunnett's test is preferable due to its higher power.

    The use of this test relies on several assumptions.

    1. The observations are independent within and among groups.
    2. The observations within each group are normally distributed.
    3. The distributions from which the samples are drawn have the same finite
       variance.

    References
    ----------
    .. [1] Dunnett, Charles W. (1955) "A Multiple Comparison Procedure for
           Comparing Several Treatments with a Control." Journal of the American
           Statistical Association, 50:272, 1096-1121,
           :doi:`10.1080/01621459.1955.10501294`
    .. [2] Thomson, M. L., & Short, M. D. (1969). Mucociliary function in
           health, chronic obstructive airway disease, and asbestosis. Journal
           of applied physiology, 26(5), 535-539.
           :doi:`10.1152/jappl.1969.26.5.535`

    Examples
    --------
    We'll use data from [2]_, Table 1. The null hypothesis is that the means of
    the distributions underlying the samples and control are equal.

    First, we test that the means of the distributions underlying the samples
    and control are unequal (``alternative='two-sided'``, the default).

    >>> import numpy as np
    >>> from scipy.stats import dunnett
    >>> samples = [[3.8, 2.7, 4.0, 2.4], [2.8, 3.4, 3.7, 2.2, 2.0]]
    >>> control = [2.9, 3.0, 2.5, 2.6, 3.2]
    >>> res = dunnett(*samples, control=control)
    >>> res.statistic
    array([ 0.90874545, -0.05007117])
    >>> res.pvalue
    array([0.58325114, 0.99819341])

    Now, we test that the means of the distributions underlying the samples are
    greater than the mean of the distribution underlying the control.

    >>> res = dunnett(*samples, control=control, alternative='greater')
    >>> res.statistic
    array([ 0.90874545, -0.05007117])
    >>> res.pvalue
    array([0.30230596, 0.69115597])

    For a more detailed example, see :ref:`hypothesis_dunnett`.
    )ri   rj   r@   rA   )ri   rj   r=   )r   r   r   r   r   r   r   r   r    r!   r"   )_iv_dunnett_params_dunnett_statistic_dunnettrD   r   )rj   r@   rA   ri   samples_control_r>   r?   n_group	n_samples	n_controlr   stdmean_controlmean_samplesr   s                   r4   r   r      s    R *S  Hh
 .=(. . .*CWi 2D(B	92 2.IsL, B)#  F F bs""	 	 	 	r6   c                    t          |          }|dvrt          d          d}d}t          j        |          }d | D             }||gz   }|D ]6}|j        dk    rt          |          |j        dk     rt          |          7|||fS )z$Input validation for Dunnett's test.>   r   r   r   z4alternative must be 'less', 'greater' or 'two-sided'z,Control and samples groups must be 1D arraysz;Control and samples groups must have at least 1 observationc                 6    g | ]}t          j        |          S rf   )rB   asarray.0samples     r4   
<listcomp>z_iv_dunnett.<locals>.<listcomp>n  s"    999v
6""999r6   r;   )r   rZ   rB   ry   ndimr.   )	ri   rj   r@   rA   ndim_msg	n_obs_msgro   samples_controlr|   s	            r4   rl   rl   \  s     S
!
!C:::B
 
 	
 >HMIj!!G99999H )1G9(<O! ( (;??X&&&;??Y'''  Wc!!r6   c                 P   t          j        d | D                       }|                                }|j        }||z   }t	          |           }||z
  dz
  }||z  dz   }dt          j        |dddf         |dddf         z            z  }t          j        |d           |||||fS )zSpecific parameters for Dunnett's test.

    Degree of freedom is the number of observations minus the number of groups
    including the control.
    c                     g | ]	}|j         
S rf   )r.   rz   s     r4   r}   z#_params_dunnett.<locals>.<listcomp>  s    <<<&&+<<<r6   r;   N)rB   rC   sumr.   r\   rT   fill_diagonal)	ri   rj   rr   n_samplers   nn_groupsr?   r>   s	            r4   rm   rm   |  s     <<G<<<==I }}HI9A7||H	
X	B I

!C
BGC4L3tQQQw</00
0CS!Hi22r6   r?   rr   rs   c                 ~   t          j        |          }t          j        d | D                       }|g| z   }t          j        |g|g          }t          j        d t          ||          D                       |z  }	t          j        |	          }
||z
  t          j        d|z  d|z  z             z  }||
z  |
||fS )zcStatistic of Dunnett's test.

    Computation based on the original single-step test from [1].
    c                 6    g | ]}t          j        |          S rf   )rB   meanrz   s     r4   r}   z&_statistic_dunnett.<locals>.<listcomp>  s     CCCRWV__CCCr6   c                 F    g | ]\  }}t          ||           |j        z  S ))r   )r   r.   )r{   r|   r   s      r4   r}   z&_statistic_dunnett.<locals>.<listcomp>  sF     B B B!VT f4(((4 B B Br6   r;   )rB   r   rC   concatenater   ziprT   )ri   rj   r?   rr   rs   ru   rv   all_samples	all_meanss2rt   zs               r4   rn   rn     s     77##L8CC7CCCDDL)g%K=>>I 
 B B%(i%@%@B B B 
C 
CEG
HB
'"++C 
	$)a	k0I(J(JJAs7C|33r6   r>   r   c                    |                     dd          }t          j        | ||          }|dk    r+t          |          }d|                    ||           z
  }nP|dk    r&d|                    |t
          j                   z
  }n$d|                    t
          j        |          z
  }t          j        |          S )zypvalue from the multivariate t-distribution.

    Critical values come from the multivariate student-t distribution.
    r;   )shaper?   seedr   )lower_limitr   )reshaper   multivariate_trE   cdfrB   r[   
atleast_1d)r>   r?   r   r@   rA   mvtr   s          r4   rD   rD     s     !!"a((I

SRc
:
:
:Ck!!	NN	SWWYYJW???			!	!SWWYRVGW<<<SWWRVW;;;=   r6   )N)'rR   collections.abcr   dataclassesr   r   typingr   r   numpyrB   scipyr   scipy.optimizer	   scipy.stats._commonr
   scipy.stats._qmcr   scipy.stats._stats_pyr   scipy._lib._utilr   r   r   numpy.typingnpt__all__r   r   tuplelistrb   rl   rd   rm   re   rn   rD   rf   r6   r4   <module>r      s6    $ $ $ $ $ $ ( ( ( ( ( ( ( ( ) ) ) ) ) ) ) )           * * * * * * 2 2 2 2 2 2 / / / / / / & & & & & & H H H H H H H H H H   
 Y Y Y Y Y Y Y Yx N666 <G	b b bbb 78b 
	b
 b b b 76bJ"o&"" 78" 
	"
 4
RZ12" " " "@3"*3(*
3
2:sCS013 3 3 324"*4(*
48;4z4&)4 2:ubj"*454 4 4 46 ! !	!!)+!78! 
! Z	! ! ! ! ! !r6   