I am new to coding and trying to write a program to calculate sample size, n
, in A/B testing.
I am stuck in the nested loop in main function below. I want to create an array, n_dist_all
, with 3 different rows containing values of n
depending upon different values of beta using nested loop. However, the function just adds the values for three different betas in the same row. I am trying to use numpy.hstack
. I have also tried concatenate
and append
but they are all giving me the same result.
import numpy as np
import math
import scipy.stats as st
def N(alpha, beta, delta):
Z_alpha = st.norm.ppf(1-alpha)
Z_beta = st.norm.ppf(1-alpha)
pA = 0.01
pB = pA + delta
qA = 1.0 - pA
qB = 1.0 - qA
n = (((Z_alpha*math.sqrt((pA+pB)*(qA+qB)/float(2)))+(Z_beta*math.sqrt((pA*qA)*(pB*qB))))/(pA-pB))**float(2)
return int(n)
def float_range(start, stop, step):
i = start
while i < (stop + step):
yield i
i += step
def main():
n_dist_all = np.array([])
beta_1 = 0.2
beta_2 = 0.1
beta_3 = 0.05
beta_group = [beta_1, beta_2, beta_3]
alpha = 0.05
for beta in beta_group:
n_dist = np.array([])
for delta in float_range(0.001,0.03,0.0005):
n_dist = np.append(n_dist, N(alpha, beta, delta))
n_dist_all = np.hstack((n_dist_all, n_dist))
n_dist = []
print n_dist_all
if __name__ == "__main__":
main()