0

I'm trying to see if there's an easy way to calculate minimum sample size required for a one-sample Z-test to reject the null hypothesis.

I know that we can reject the null hypothesis (i.e. the A/B test is successful) if

1 - scipy.stats.norm.cdf((x-mu)/(s/np.sqrt(n)) < alpha

where x is the sample mean, mu is the population mean, s is the population standard deviation and n the sample size.

Is there a way in python to solve the equation above for n?

3
  • To clarify, are you saying you want to vary n while keeping x fixed? Commented Jan 17, 2022 at 16:56
  • I know all other variables and I need to solve for n Commented Jan 17, 2022 at 16:58
  • 1
    Ok thanks. One further clarification: you refer to a t-test but your equation implies your test statistic is normally distributed which would make this a Z-test not a t-test. I assume the equation is correct and you meant to say Z-test, but can you double check? Commented Jan 17, 2022 at 17:11

1 Answer 1

1

Distributions in scipy.stats have an inverse of the cdf function, which is called ppf. ppf stands for "percentage point function" but this is a misnomer because it actually deals with quantiles, not percentiles. We can use this function and the fact that ppf(cdf(x)) = x to rearrange and solve your equation.

Assuming that alpha < 0.5, there are two cases to cases to consider. If x <= mu then there are no solutions, otherwise we can rearrange the equation to:

np.sqrt(n) > s * scipy.stats.norm.ppf(1 - alpha) / (x - mu)

An aside:

If alpha >= 0.5 then the solutions are strange: either any n will do (if x >= mu) or you get an upper bound on n (if x < mu). But that's what you get for treating your null hypothesis with such contempt!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.