Ravaal
Ravaal

Reputation: 3359

How do I calculate sample size in Python using this equation?

The following equation is something that I am having trouble translating into Python. When I do it it pops out a ridiculously large number. My code is 100000/1+((2.575**2)*.38*(1-.38)/(.005**2)*100000) but it's wrong.

Here is the equation- Sample Size Formula

In this formula, let's assume that the population proportion is .38 (38%). The z score is 2.575. The margin of error is .005 (.5%). The population size is 100,000.

Can somebody help me translate this into Python? The expected result is 38,549.

Upvotes: 1

Views: 4199

Answers (2)

rdas
rdas

Reputation: 21285

100000/1+((2.575**2)*.38*(1-.38)/(.005**2)*100000)

You have a missing parentheses which is making python carry out the wrong calculation. Remember your PEMDAS. 100000/1 has higher priority than +

Anyway, here's the fixed version:

def the_factor(n, z, p_cap, epsilon, N):
    return n / (1 + (((z**2) * (p_cap * (1 - p_cap)))/((epsilon**2) * N)))

print(the_factor(100000, 2.575, 0.38, 0.005, 100000))

Output:

61543.38122167427

Upvotes: 2

OmG
OmG

Reputation: 18838

You have a mistake in the parenthesis of division parts!

 100000 / (1 + ((2.575**2) * .38 * (1-.38) / ((.005**2) * 100000)))

Besides putting all parts of the division inside the parenthesis, you should put * of the second division inside the parenthesis as well.

Upvotes: 1

Related Questions