statsmodels.stats.proportion.samplesize_confint_proportion#
- statsmodels.stats.proportion.samplesize_confint_proportion(proportion, half_length, alpha=0.05, method='normal')[source]#
Find sample size to get desired confidence interval length
- Parameters:
- proportion
floator array_likein[0, 1] Proportion or quantile. All values have to be in the closed interval [0, 1].
- half_length
floator array_like,positive Desired half length of the confidence interval. All values have to be positive.
- alpha
floator array_likein(0, 1),optional Significance level, default 0.05. Coverage of the two-sided interval is (approximately)
1 - alpha. All values have to be in the open interval (0, 1).- method{“normal”},
optional Method to use for the confidence interval. Currently only the normal approximation is available.
- proportion
- Returns:
- n
float,ndarray,SeriesorDataFrame Sample size to get the desired half length of the confidence interval. The sample size is not rounded to an integer, and it is 0 if
proportionis 0 or 1. The result is a float if all arguments are scalars, and an ndarray if the arguments are array_like. It is a pandas Series or DataFrame ifproportionorhalf_lengthis one, in which case the result has the index of that argument.
- n
- Raises:
ValueErrorIf a value of
alphais not in (0, 1), a value ofhalf_lengthis not positive, or a value ofproportionis not in [0, 1]. The check also rejects nan. NumPy raises aValueErrorif an argument, for example a string, cannot be converted to float.TypeErrorIf
proportion,half_lengthoralphais None.NotImplementedErrorIf
methodis not “normal”.
Notes
This is mainly to store the formula. For the normal approximation the sample size is
proportion * (1 - proportion) / (half_length / z) ** 2, wherezis the1 - alpha / 2quantile of the standard normal distribution.A possible application is the number of replications in bootstrap samples.
Changed in version 0.15.1: Values of
alpha,half_lengthandproportionthat are out of range or nan raise aValueError. Previously the function returnedinf,nanor a meaningless, possibly negative, sample size. Lists are accepted for the three arguments.