L iAddlZddlmZddlmZmZddlmZmZddl Z ddl m Z ddl mZddlmZddlmZdd lmZdd lmZmZmZerddlmZd gZeGd d Zeddddddddddeddedef dZdeddddeddedeee j@e j@eff dZ!dee j@de j@dee j@e"e"e j@e"ffdZ#dee j@de j@de"de j@de"dee j@e$e j@e j@ff d Z% d$d!e j@de"d"e j@deddede j@f d#Z&y)%N)Sequence) dataclassfield) TYPE_CHECKINGLiteral)stats)minimize_scalar)ConfidenceInterval)check_random_state)_var)_transition_to_rng DecimalNumberSeedTypedunnettceZdZUdZej ed<ej ed<edZe ded<edZ ej ed<edZ e ed <edZ eed <edZej ed <edZej ed <edZej ed <edZe ed<edZeed<eddZedzed<eddZedzed<dZ ddededefdZ ddedefdZy) DunnettResultaResult 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. statisticpvalueF)repr two-sidedlessgreater _alternative_rho_df_std _mean_samples _mean_control _n_samples _n_control_rngN)defaultr_ci_ci_clc n|j|jdd|jdzdd}t|jj D]`}|d|d|j |d |j|d |jj|d |jj|d d z }b|S) Nffffff?confidence_levelzDunnett's test (dz.1fzW% Confidence Interval) Comparison Statistic p-value Lower CI Upper CI z (Sample z - Control) z>10.3f ) r$confidence_intervalr%rangersizerlowhigh)selfsis \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/stats/_multicomp.py__str__zDunnettResult.__str__6s 88   $ $c $ : S%&P P t{{''( 2A IaS T^^A->v,F[[^F+XX\\!_V,XX]]1%f-R1 2A 2 r)tolreturncd|z fd}t|d|}|j}|jdus|j|dzk\r+t j dd|jzzdd |j ztjdjz djz zz}t|S) aSAllowance. 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. ctj|}tjj|j j }t|z z S)Nrhodfr alternativerng)nparray_pvalue_dunnettrrrr"abs)rsfalphar1s r4pvalue_from_statz2DunnettResult._allowance..pvalue_from_statesO+I II$((#1B1BIIB rEz?5( (r6brent)methodr7F zComputation of the confidence interval did not converge to the desired level. The confidence level corresponding with the returned interval is approximately .) stacklevel) r xsuccessfunwarningswarnrrAsqrtr r!rD)r1r)r7rGrescritical_value allowancerFs` @r4 _allowancezDunnettResult._allowanceIs4$$ ).wCH ;;% 377c"f#4 MM::?377:K9LAO  #499,RWW doo $// 1 1.  9~r6c|j||jk(r |jSd|cxkrdkstdtd|j|}|j|j z }||z }||z}|j dk(rtjgt|z}n-|j dk(rtj gt|z}||_t|||_|jS)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``. rr:z)Confidence level must be between 0 and 1.r(rr)r/r0) r$r% ValueErrorrWrrrrAinflenr )r1r)rV diff_meansr/r0s r4r,z!DunnettResult.confidence_intervals, HH '74;;'F88O$(q(HI I)HI IOO5EOF ''$*<*<< ")#    )FF8c*o-D   & (FF7)c*o-C& % xxr6)r'gMbP?)r')__name__ __module__ __qualname____doc__rAndarray__annotations__rrrrrintrfloatrrr r!r"rr$r r%rr5rWr,r6r4rrs+ zz JJ`_ 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`. )rhrir?r@)rhrir<) rrrrrrrrr r!r") _iv_dunnett_params_dunnett_statistic_dunnettrCr)rir?r@rhsamples_control_r=r>n_group n_samples n_controlrstd mean_control mean_samplesrs r4rrsR*SHh .=(.*CWi2D(B 92.IsL, B)#F F bs""   r6cBt|}|dvr tdd}d}tj|}|Dcgc]}tj|}}||gz}|D]6}|jdkDr t||j dks-t||||fScc}w)z$Input validation for Dunnett's test.>rrrz4alternative 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 observationr:)r rYrAasarrayndimr.) rhrir?r@ndim_msg n_obs_msgsamplernsamples_controls r4rkrk\s S !C:: B  >HMIjj!G189v 6"9H9)1G9(r=s r4rlrl|sG<&&++<=I}}H I9A7|H X B I  !C BGGC4L3tQw</ 0 0CS! Hi 22=sB0r>rqrrc tj|}tj|Dcgc]}tj|c}}|g|z}tj|g|g} tjt || D cgc]\}} t || |jz!c} }|z } tj| } ||z tjd|z d|z zz } | | z | ||fScc}wcc} }w)zcStatistic of Dunnett's test. Computation based on the original single-step test from [1]. )meanr:) rArrB concatenater~zipr r.rS)rhrir>rqrrrtr{ru all_samples all_meansrs2rszs r4rmrms777#L887CRWWV_CDL)g%K =>I %(i%@B!VTf4(4B CEG HB ''"+C  $) a k0I(JJA s7C| 33D Bs C9$C> r=rcn|jdd}tj|||}|dk(r#t|}d|j || z }nO|dk(r&d|j |t j z }n$d|j 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)reshapermultivariate_trDcdfrArZ atleast_1d)r=r>rr?r@mvtrs r4rCrCs!!"a(I   SRc :Ck! N SWWYYJW??  !SWWYRVVGW<<SWWRVVW;; ==  r6)N)'rQcollections.abcr dataclassesrrtypingrrnumpyrAscipyrscipy.optimizer scipy.stats._commonr scipy.stats._qmcr scipy.stats._stats_pyr scipy._lib._utilr rr numpy.typingnpt__all__rrtuplelistrarkrcrlrdrmrCrer6r4rs$()*2/&HH   YY YxN6