I need to calculate the sample size for the data I have. I am given a margin of error5% with a confidence level 95% .

Which formula can I use to get the sample size value if the total population size is X? Sorry, I searched the internet but couldn't find a formula for this purpose.


Any help is appreciated.


2 Answers 2


If you are estimating a binomial proportion near to $p\approx 1/2$ based on a sample of size $n,$ then a 95% CI is of the form $\hat p \pm 1.96\sqrt{\hat p(1-\hat p)/n}.$ for which the margin of error is $$M = 1.96\sqrt{\hat p(1-\hat p)/n} \approx 1.96\sqrt{(1/2)(1/2)/n}\\ = 1.96\sqrt{1/4n} \approx 1/\sqrt{n}.$$

So, if $M = 0.05 = 5\%.$ then $n \approx 1/M^2 = 1/(.05)^2 = 400.$

You do not give many details about the parameter being estimated or the kind of confidence being used. My computations above are based on estimating binomial success probability $p \approx 1/2,$ as in some election polling situations. This is the only elementary application I know about where your information is sufficient.

If this is not the application you have in mind, then you will need to give more information: for example, the approximate value of $p$ for a binomial CI or an estimate of population variance $\sigma^2$ if you are trying to estimate the mean $\mu$ of a normal population. Other applications of confidence intervals may require various additional information.


Here is a simple Python implementation (not requiring any external libraries):

# https://github.com/shawnohare/samplesize/blob/master/samplesize.py

def sampleSize(population_size, margin_error=.05,confidence_level=.95,sigma=1/2):

    alpha = 1 - (confidence_level)
    zdict = {
        .90: 1.645,
        .91: 1.695,
        .99: 2.576,
        .97: 2.17,
        .94: 1.881,
        .93: 1.812,
        .95: 1.96,
        .98: 2.326,
        .96: 2.054,
        .92: 1.751
    if confidence_level in zdict:
        z = zdict[confidence_level]
        from scipy.stats import norm
        z = norm.ppf(1 - (alpha/2))
    N = population_size
    M = margin_error
    numerator = z**2 * sigma**2 * (N / (N-1))
    denom = M**2 + ((z**2 * sigma**2)/(N-1))
    return int(numerator/denom + 0.5)

for population in [100,1000,10000,50000]:
    n = sampleSize(population)
    print("Population %d requires sample size of %d" % (population, n))


Population 100 requires sample size of 80
Population 1000 requires sample size of 278
Population 10000 requires sample size of 370
Population 50000 requires sample size of 381

Validated by checking: https://www.surveymonkey.com/mp/sample-size-calculator/

  • $\begingroup$ Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. $\endgroup$
    – Community Bot
    Dec 15, 2022 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.