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:
proportionfloat or array_like in [0, 1]

Proportion or quantile. All values have to be in the closed interval [0, 1].

half_lengthfloat or array_like, positive

Desired half length of the confidence interval. All values have to be positive.

alphafloat or array_like in (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.

Returns:
nfloat, ndarray, Series or DataFrame

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 proportion is 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 if proportion or half_length is one, in which case the result has the index of that argument.

Raises:
ValueError

If a value of alpha is not in (0, 1), a value of half_length is not positive, or a value of proportion is not in [0, 1]. The check also rejects nan. NumPy raises a ValueError if an argument, for example a string, cannot be converted to float.

TypeError

If proportion, half_length or alpha is None.

NotImplementedError

If method is 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, where z is the 1 - alpha / 2 quantile 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_length and proportion that are out of range or nan raise a ValueError. Previously the function returned inf, nan or a meaningless, possibly negative, sample size. Lists are accepted for the three arguments.