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 (ie 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
?
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)
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!
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.