L i>3ddlmZddlZddlmZddlmZddlm Z ddl m Z ddl m Z Gd d Zd Zd Zd ZddZdZy))sqrtN) _validate_int)brentq)ndtri)binom)ConfidenceIntervalc$eZdZdZdZdZddZy)BinomTestResulta( Result of `scipy.stats.binomtest`. Attributes ---------- k : int The number of successes (copied from `binomtest` input). n : int The number of trials (copied from `binomtest` input). alternative : str Indicates the alternative hypothesis specified in the input to `binomtest`. It will be one of ``'two-sided'``, ``'greater'``, or ``'less'``. statistic: float The estimate of the proportion of successes. pvalue : float The p-value of the hypothesis test. cX||_||_||_||_||_||_yN)kn alternative statisticpvalueproportion_estimate)selfrrrrrs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/stats/_binomtest.py__init__zBinomTestResult.__init__s0&" $- c d|jd|jd|jd|jd|jd }|S)NzBinomTestResult(k=z, n=z, alternative=z , statistic=z , pvalue=)rrrrr)rss r__repr__zBinomTestResult.__repr__(s\&&&& ,,/0..)*{{m1 & rcL|dvrtd|dd|cxkrdksntd|d|dk(r0t|j|j||j\}}n4t |j|j||j|d k( \}}t || S) ad Compute the confidence interval for ``statistic``. Parameters ---------- confidence_level : float, optional Confidence level for the computed confidence interval of the estimated proportion. Default is 0.95. method : {'exact', 'wilson', 'wilsoncc'}, optional Selects the method used to compute the confidence interval for the estimate of the proportion: 'exact' : Use the Clopper-Pearson exact method [1]_. 'wilson' : Wilson's method, without continuity correction ([2]_, [3]_). 'wilsoncc' : Wilson's method, with continuity correction ([2]_, [3]_). Default is ``'exact'``. Returns ------- ci : ``ConfidenceInterval`` object The object has attributes ``low`` and ``high`` that hold the lower and upper bounds of the confidence interval. References ---------- .. [1] C. J. Clopper and E. S. Pearson, The use of confidence or fiducial limits illustrated in the case of the binomial, Biometrika, Vol. 26, No. 4, pp 404-413 (Dec. 1934). .. [2] E. B. Wilson, Probable inference, the law of succession, and statistical inference, J. Amer. Stat. Assoc., 22, pp 209-212 (1927). .. [3] Robert G. Newcombe, Two-sided confidence intervals for the single proportion: comparison of seven methods, Statistics in Medicine, 17, pp 857-872 (1998). Examples -------- >>> from scipy.stats import binomtest >>> result = binomtest(k=7, n=50, p=0.1) >>> result.statistic 0.14 >>> result.proportion_ci() ConfidenceInterval(low=0.05819170033997342, high=0.26739600249700846) )exactwilsonwilsonccz method ('z2') must be one of 'exact', 'wilson' or 'wilsoncc'.rrzconfidence_level (z!) must be in the interval [0, 1].rr ) correction)lowhigh) ValueError_binom_exact_conf_intrrr_binom_wilson_conf_intr )rconfidence_levelmethodr"r#s r proportion_cizBinomTestResult.proportion_ci1sb 8 8y1778 8%**12B1CD445 5 W -dffdff.>.2.>.>@IC /tvvtvv/?/3/?/?:@J:NPIC"c55rN)gffffff?r)__name__ __module__ __qualname____doc__rrr)rrr r s&-A6rr c t|dd}|S#t$r tddt$r}td|d}~wwxYw)NrrzHnumerical solver failed to converge when computing the confidence limitsz?brentq raised a ValueError; report this to the SciPy developers)r RuntimeErrorr$)funcpexcs r_findpr4usb6 4A  H H=>CG H 6,-25 66s ? :?c6|dk(r?d|z dz dk(rd}ntfd}k(rd}||fStfd}||fS|d k(r&d|z d}k(rd}||fStfd }||fS|d k(rd|z dk(rd}ntfd }d}fS) zz Compute the estimate and confidence interval for the binomial test. Returns proportion, prop_low, prop_high two-sidedrrc>tjdz |z SNrrsfr2alpharrs rz'_binom_exact_conf_int..EHHQqS!Q$7%$?r?c8tj|z Sr rcdfr=s rr?z'_binom_exact_conf_int..UYYq!Q%7%%?rlessc8tj|z Sr rCr=s rr?z'_binom_exact_conf_int..rErgreaterc>tjdz |z Sr:r;r=s rr?z'_binom_exact_conf_int..r@r)r4)rrr'rplowphighr>s`` @rr%r%s k!%%* 6D?@D 6E" ;?@E ;  $$ 6E ;?@E ;  !$$ 6D?@D ;rc>||z }|dk(rtdd|zz}n t|}d||dzzz}d|z|z|dzz|z }d|z } |r|dk(s|dk(rd} n4d|t|dzdz d|z z d|z|| zdzzzzz|z } || z } |d k(s||k(rd } | | fSd|t|dzdzd|z z d|z|| zdz zzzz|z } || z} | | fS||z td|z|z| z|dzzz}|dk(s|dk(rd} n||z } |d k(s||k(rd } | | fS||z} | | fS) Nr6?r7rrFrr8rHrA)rr)rrr'rr!r2zdenomcenterqlodlohidhideltas rr&r&s AAk! #,,, - " # q1a4xLEc!eadlE !F AA & AFBqadQh1nqsAaC!G}<===FC#B ) #qAvB r6MqadQh1nqsAaC!G}<===FC#B r6M%$qs1uQwA~.. & AFB%B ) #qAvB r6M%B r6Mrc t|dd}tdd|kDrtd|dddcxkrdksntd d |d vrtd |d |dk(rtj|}nI|dk(rtj|dz }n(tj |}d}|zk(rd}n|zkrt fd| |ztjz}|z t||ztj |k(z}tj|tj|z z}net fd||zdtjz}|dz}tj|dz tj|dz z}td|}t|||z |} | S)a Perform a test that the probability of success is p. The binomial test [1]_ is a test of the null hypothesis that the probability of success in a Bernoulli experiment is `p`. Details of the test can be found in many texts on statistics, such as section 24.5 of [2]_. Parameters ---------- k : int The number of successes. n : int The number of trials. p : float, optional The hypothesized probability of success, i.e. the expected proportion of successes. The value must be in the interval ``0 <= p <= 1``. The default value is ``p = 0.5``. alternative : {'two-sided', 'greater', 'less'}, optional Indicates the alternative hypothesis. The default value is 'two-sided'. Returns ------- result : `~scipy.stats._result_classes.BinomTestResult` instance The return value is an object with the following attributes: k : int The number of successes (copied from `binomtest` input). n : int The number of trials (copied from `binomtest` input). alternative : str Indicates the alternative hypothesis specified in the input to `binomtest`. It will be one of ``'two-sided'``, ``'greater'``, or ``'less'``. statistic : float The estimate of the proportion of successes. pvalue : float The p-value of the hypothesis test. The object has the following methods: proportion_ci(confidence_level=0.95, method='exact') : Compute the confidence interval for ``statistic``. Notes ----- .. versionadded:: 1.7.0 References ---------- .. [1] Binomial test, https://en.wikipedia.org/wiki/Binomial_test .. [2] Jerrold H. Zar, Biostatistical Analysis (fifth edition), Prentice Hall, Upper Saddle River, New Jersey USA (2010) Examples -------- >>> from scipy.stats import binomtest A car manufacturer claims that no more than 10% of their cars are unsafe. 15 cars are inspected for safety, 3 were found to be unsafe. Test the manufacturer's claim: >>> result = binomtest(3, n=15, p=0.1, alternative='greater') >>> result.pvalue 0.18406106910639114 The null hypothesis cannot be rejected at the 5% level of significance because the returned p-value is greater than the critical value of 5%. The test statistic is equal to the estimated proportion, which is simply ``3/15``: >>> result.statistic 0.2 We can use the `proportion_ci()` method of the result to compute the confidence interval of the estimate: >>> result.proportion_ci(confidence_level=0.95) ConfidenceInterval(low=0.05684686759024681, high=1.0) rr)minimumrrzk (z) must not be greater than n (z).zp (z) must be in range [0,1])r6rFrHzalternative ('z<') not recognized; must be 'two-sided', 'less' or 'greater'rFrHg?rAc4tj| Sr rpmfx1rr2s rr?zbinomtest..6s%))B1:M9Mrc2tj|Sr r[r]s rr?zbinomtest..@s2q!9Lrr) rr$rrDr<r\_binary_search_for_binom_tstnpceilintfloorminr ) rrr2rpvaldrerrixyresults `` r binomtestrlsj aa(Aaa(A1u3qc!?s"EFF KaK3qc!9:;;::>+7DDE EfyyAq!  !xx!Q" IIaA  A:D QY-.M/0bgrwwq1u~qJBBQtVuyyQ'::;;A99Q1%Q1(==D-.L./fa!a%JB QA99QqS!Q'%((1Q31*==D3~ qA;'(s49F Mrc||kr1|||z dzz}||}||kr|dz}n ||kDr|dz }n|S||kr1|||kr|S|dz S)a  Conducts an implicit binary search on a function specified by `a`. Meant to be used on the binomial PMF for the case of two-sided tests to obtain the value on the other side of the mode where the tail probability should be computed. The values on either side of the mode are always in order, meaning binary search is applicable. Parameters ---------- a : callable The function over which to perform binary search. Its values for inputs lo and hi should be in ascending order. d : float The value to search. lo : int The lower end of range to search. hi : int The higher end of the range to search. Returns ------- int The index, i between lo and hi such that a(i)<=drxsD*!#'h6h6V  B%PCL'r