scipy.stats.binomtest#
- scipy.stats.binomtest(k, n, p=0.5, alternative='two-sided')[source]#
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:
- kint
The number of successes.
- nint
The number of trials.
- pfloat, 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 isp = 0.5
.- alternative{‘two-sided’, ‘greater’, ‘less’}, optional
Indicates the alternative hypothesis. The default value is ‘two-sided’.
- Returns:
- result
BinomTestResult
instance The return value is an object with the following attributes:
- kint
The number of successes (copied from
binomtest
input).- nint
The number of trials (copied from
binomtest
input).- alternativestr
Indicates the alternative hypothesis specified in the input to
binomtest
. It will be one of'two-sided'
,'greater'
, or'less'
.- statisticfloat
The estimate of the proportion of successes.
- pvaluefloat
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
.
- result
Notes
Added in version 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)