_binomtest.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. from math import sqrt
  2. import numpy as np
  3. from scipy._lib._array_api import xp_capabilities
  4. from scipy._lib._util import _validate_int
  5. from scipy.optimize import brentq
  6. from scipy.special import ndtri
  7. from ._discrete_distns import binom
  8. from ._common import ConfidenceInterval
  9. class BinomTestResult:
  10. """
  11. Result of `scipy.stats.binomtest`.
  12. Attributes
  13. ----------
  14. k : int
  15. The number of successes (copied from `binomtest` input).
  16. n : int
  17. The number of trials (copied from `binomtest` input).
  18. alternative : str
  19. Indicates the alternative hypothesis specified in the input
  20. to `binomtest`. It will be one of ``'two-sided'``, ``'greater'``,
  21. or ``'less'``.
  22. statistic: float
  23. The estimate of the proportion of successes.
  24. pvalue : float
  25. The p-value of the hypothesis test.
  26. """
  27. def __init__(self, k, n, alternative, statistic, pvalue):
  28. self.k = k
  29. self.n = n
  30. self.alternative = alternative
  31. self.statistic = statistic
  32. self.pvalue = pvalue
  33. # add alias for backward compatibility
  34. self.proportion_estimate = statistic
  35. def __repr__(self):
  36. s = ("BinomTestResult("
  37. f"k={self.k}, "
  38. f"n={self.n}, "
  39. f"alternative={self.alternative!r}, "
  40. f"statistic={self.statistic}, "
  41. f"pvalue={self.pvalue})")
  42. return s
  43. def proportion_ci(self, confidence_level=0.95, method='exact'):
  44. """
  45. Compute the confidence interval for ``statistic``.
  46. Parameters
  47. ----------
  48. confidence_level : float, optional
  49. Confidence level for the computed confidence interval
  50. of the estimated proportion. Default is 0.95.
  51. method : {'exact', 'wilson', 'wilsoncc'}, optional
  52. Selects the method used to compute the confidence interval
  53. for the estimate of the proportion:
  54. 'exact' :
  55. Use the Clopper-Pearson exact method [1]_.
  56. 'wilson' :
  57. Wilson's method, without continuity correction ([2]_, [3]_).
  58. 'wilsoncc' :
  59. Wilson's method, with continuity correction ([2]_, [3]_).
  60. Default is ``'exact'``.
  61. Returns
  62. -------
  63. ci : ``ConfidenceInterval`` object
  64. The object has attributes ``low`` and ``high`` that hold the
  65. lower and upper bounds of the confidence interval.
  66. References
  67. ----------
  68. .. [1] C. J. Clopper and E. S. Pearson, The use of confidence or
  69. fiducial limits illustrated in the case of the binomial,
  70. Biometrika, Vol. 26, No. 4, pp 404-413 (Dec. 1934).
  71. .. [2] E. B. Wilson, Probable inference, the law of succession, and
  72. statistical inference, J. Amer. Stat. Assoc., 22, pp 209-212
  73. (1927).
  74. .. [3] Robert G. Newcombe, Two-sided confidence intervals for the
  75. single proportion: comparison of seven methods, Statistics
  76. in Medicine, 17, pp 857-872 (1998).
  77. Examples
  78. --------
  79. >>> from scipy.stats import binomtest
  80. >>> result = binomtest(k=7, n=50, p=0.1)
  81. >>> result.statistic
  82. 0.14
  83. >>> result.proportion_ci()
  84. ConfidenceInterval(low=0.05819170033997342, high=0.26739600249700846)
  85. """
  86. if method not in ('exact', 'wilson', 'wilsoncc'):
  87. raise ValueError(f"method ('{method}') must be one of 'exact', "
  88. "'wilson' or 'wilsoncc'.")
  89. if not (0 <= confidence_level <= 1):
  90. raise ValueError(f'confidence_level ({confidence_level}) must be in '
  91. 'the interval [0, 1].')
  92. if method == 'exact':
  93. low, high = _binom_exact_conf_int(self.k, self.n,
  94. confidence_level,
  95. self.alternative)
  96. else:
  97. # method is 'wilson' or 'wilsoncc'
  98. low, high = _binom_wilson_conf_int(self.k, self.n,
  99. confidence_level,
  100. self.alternative,
  101. correction=method == 'wilsoncc')
  102. return ConfidenceInterval(low=low, high=high)
  103. def _findp(func):
  104. try:
  105. p = brentq(func, 0, 1)
  106. except RuntimeError:
  107. raise RuntimeError('numerical solver failed to converge when '
  108. 'computing the confidence limits') from None
  109. except ValueError as exc:
  110. raise ValueError('brentq raised a ValueError; report this to the '
  111. 'SciPy developers') from exc
  112. return p
  113. def _binom_exact_conf_int(k, n, confidence_level, alternative):
  114. """
  115. Compute the estimate and confidence interval for the binomial test.
  116. Returns proportion, prop_low, prop_high
  117. """
  118. if alternative == 'two-sided':
  119. alpha = (1 - confidence_level) / 2
  120. if k == 0:
  121. plow = 0.0
  122. else:
  123. plow = _findp(lambda p: binom.sf(k-1, n, p) - alpha)
  124. if k == n:
  125. phigh = 1.0
  126. else:
  127. phigh = _findp(lambda p: binom.cdf(k, n, p) - alpha)
  128. elif alternative == 'less':
  129. alpha = 1 - confidence_level
  130. plow = 0.0
  131. if k == n:
  132. phigh = 1.0
  133. else:
  134. phigh = _findp(lambda p: binom.cdf(k, n, p) - alpha)
  135. elif alternative == 'greater':
  136. alpha = 1 - confidence_level
  137. if k == 0:
  138. plow = 0.0
  139. else:
  140. plow = _findp(lambda p: binom.sf(k-1, n, p) - alpha)
  141. phigh = 1.0
  142. return plow, phigh
  143. def _binom_wilson_conf_int(k, n, confidence_level, alternative, correction):
  144. # This function assumes that the arguments have already been validated.
  145. # In particular, `alternative` must be one of 'two-sided', 'less' or
  146. # 'greater'.
  147. p = k / n
  148. if alternative == 'two-sided':
  149. z = ndtri(0.5 + 0.5*confidence_level)
  150. else:
  151. z = ndtri(confidence_level)
  152. # For reference, the formulas implemented here are from
  153. # Newcombe (1998) (ref. [3] in the proportion_ci docstring).
  154. denom = 2*(n + z**2)
  155. center = (2*n*p + z**2)/denom
  156. q = 1 - p
  157. if correction:
  158. if alternative == 'less' or k == 0:
  159. lo = 0.0
  160. else:
  161. dlo = (1 + z*sqrt(z**2 - 2 - 1/n + 4*p*(n*q + 1))) / denom
  162. lo = center - dlo
  163. if alternative == 'greater' or k == n:
  164. hi = 1.0
  165. else:
  166. dhi = (1 + z*sqrt(z**2 + 2 - 1/n + 4*p*(n*q - 1))) / denom
  167. hi = center + dhi
  168. else:
  169. delta = z/denom * sqrt(4*n*p*q + z**2)
  170. if alternative == 'less' or k == 0:
  171. lo = 0.0
  172. else:
  173. lo = center - delta
  174. if alternative == 'greater' or k == n:
  175. hi = 1.0
  176. else:
  177. hi = center + delta
  178. return lo, hi
  179. @xp_capabilities(np_only=True)
  180. def binomtest(k, n, p=0.5, alternative='two-sided'):
  181. """
  182. Perform a test that the probability of success is p.
  183. The binomial test [1]_ is a test of the null hypothesis that the
  184. probability of success in a Bernoulli experiment is `p`.
  185. Details of the test can be found in many texts on statistics, such
  186. as section 24.5 of [2]_.
  187. Parameters
  188. ----------
  189. k : int
  190. The number of successes.
  191. n : int
  192. The number of trials.
  193. p : float, optional
  194. The hypothesized probability of success, i.e. the expected
  195. proportion of successes. The value must be in the interval
  196. ``0 <= p <= 1``. The default value is ``p = 0.5``.
  197. alternative : {'two-sided', 'greater', 'less'}, optional
  198. Indicates the alternative hypothesis. The default value is
  199. 'two-sided'.
  200. Returns
  201. -------
  202. result : `~scipy.stats._result_classes.BinomTestResult` instance
  203. The return value is an object with the following attributes:
  204. k : int
  205. The number of successes (copied from `binomtest` input).
  206. n : int
  207. The number of trials (copied from `binomtest` input).
  208. alternative : str
  209. Indicates the alternative hypothesis specified in the input
  210. to `binomtest`. It will be one of ``'two-sided'``, ``'greater'``,
  211. or ``'less'``.
  212. statistic : float
  213. The estimate of the proportion of successes.
  214. pvalue : float
  215. The p-value of the hypothesis test.
  216. The object has the following methods:
  217. proportion_ci(confidence_level=0.95, method='exact') :
  218. Compute the confidence interval for ``statistic``.
  219. Notes
  220. -----
  221. .. versionadded:: 1.7.0
  222. References
  223. ----------
  224. .. [1] Binomial test, https://en.wikipedia.org/wiki/Binomial_test
  225. .. [2] Jerrold H. Zar, Biostatistical Analysis (fifth edition),
  226. Prentice Hall, Upper Saddle River, New Jersey USA (2010)
  227. Examples
  228. --------
  229. >>> from scipy.stats import binomtest
  230. A car manufacturer claims that no more than 10% of their cars are unsafe.
  231. 15 cars are inspected for safety, 3 were found to be unsafe. Test the
  232. manufacturer's claim:
  233. >>> result = binomtest(3, n=15, p=0.1, alternative='greater')
  234. >>> result.pvalue
  235. 0.18406106910639114
  236. The null hypothesis cannot be rejected at the 5% level of significance
  237. because the returned p-value is greater than the critical value of 5%.
  238. The test statistic is equal to the estimated proportion, which is simply
  239. ``3/15``:
  240. >>> result.statistic
  241. 0.2
  242. We can use the `proportion_ci()` method of the result to compute the
  243. confidence interval of the estimate:
  244. >>> result.proportion_ci(confidence_level=0.95)
  245. ConfidenceInterval(low=0.05684686759024681, high=1.0)
  246. """
  247. k = _validate_int(k, 'k', minimum=0)
  248. n = _validate_int(n, 'n', minimum=1)
  249. if k > n:
  250. raise ValueError(f'k ({k}) must not be greater than n ({n}).')
  251. if not (0 <= p <= 1):
  252. raise ValueError(f"p ({p}) must be in range [0,1]")
  253. if alternative not in ('two-sided', 'less', 'greater'):
  254. raise ValueError(f"alternative ('{alternative}') not recognized; \n"
  255. "must be 'two-sided', 'less' or 'greater'")
  256. if alternative == 'less':
  257. pval = binom.cdf(k, n, p)
  258. elif alternative == 'greater':
  259. pval = binom.sf(k-1, n, p)
  260. else:
  261. # alternative is 'two-sided'
  262. d = binom.pmf(k, n, p)
  263. rerr = 1 + 1e-7
  264. if k == p * n:
  265. # special case as shortcut, would also be handled by `else` below
  266. pval = 1.
  267. elif k < p * n:
  268. ix = _binary_search_for_binom_tst(lambda x1: -binom.pmf(x1, n, p),
  269. -d*rerr, np.ceil(p * n), n)
  270. # y is the number of terms between mode and n that are <= d*rerr.
  271. # ix gave us the first term where a(ix) <= d*rerr < a(ix-1)
  272. # if the first equality doesn't hold, y=n-ix. Otherwise, we
  273. # need to include ix as well as the equality holds. Note that
  274. # the equality will hold in very very rare situations due to rerr.
  275. y = n - ix + int(d*rerr == binom.pmf(ix, n, p))
  276. pval = binom.cdf(k, n, p) + binom.sf(n - y, n, p)
  277. else:
  278. ix = _binary_search_for_binom_tst(lambda x1: binom.pmf(x1, n, p),
  279. d*rerr, 0, np.floor(p * n))
  280. # y is the number of terms between 0 and mode that are <= d*rerr.
  281. # we need to add a 1 to account for the 0 index.
  282. # For comparing this with old behavior, see
  283. # tst_binary_srch_for_binom_tst method in test_morestats.
  284. y = ix + 1
  285. pval = binom.cdf(y-1, n, p) + binom.sf(k-1, n, p)
  286. pval = min(1.0, pval)
  287. result = BinomTestResult(k=k, n=n, alternative=alternative,
  288. statistic=k/n, pvalue=pval)
  289. return result
  290. def _binary_search_for_binom_tst(a, d, lo, hi):
  291. """
  292. Conducts an implicit binary search on a function specified by `a`.
  293. Meant to be used on the binomial PMF for the case of two-sided tests
  294. to obtain the value on the other side of the mode where the tail
  295. probability should be computed. The values on either side of
  296. the mode are always in order, meaning binary search is applicable.
  297. Parameters
  298. ----------
  299. a : callable
  300. The function over which to perform binary search. Its values
  301. for inputs lo and hi should be in ascending order.
  302. d : float
  303. The value to search.
  304. lo : int
  305. The lower end of range to search.
  306. hi : int
  307. The higher end of the range to search.
  308. Returns
  309. -------
  310. int
  311. The index, i between lo and hi
  312. such that a(i)<=d<a(i+1)
  313. """
  314. while lo < hi:
  315. mid = lo + (hi-lo)//2
  316. midval = a(mid)
  317. if midval < d:
  318. lo = mid+1
  319. elif midval > d:
  320. hi = mid-1
  321. else:
  322. return mid
  323. if a(lo) <= d:
  324. return lo
  325. else:
  326. return lo-1