简体   繁体   中英

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- 样本量公式

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM