def _power_analysis(self): n_required = sms.NormalIndPower().solve_power(self.effect_size, power=self.beta, alpha=self.alpha, ratio=1) n_required = ceil(n_required) self.n_required = n_required
def two_proportions_sample_size(p1, p2, alpha=0.05, power=0.8, frac=0.5): ratio = frac / (1. - frac) es = sms.proportion_effectsize(p1, p2) n = np.floor(sms.NormalIndPower().solve_power(es, power=power, alpha=alpha, ratio=ratio)) n1, n2 = n * ratio, n return n1, n2
def post(self): sig_level = 0.05 power = 0.8 body = request.get_json(silent=True) or {} p1 = body['p1'] p2 = body['p2'] p1_and_p2 = sms.proportion_effectsize(p1, p2) sample_size = sms.NormalIndPower().solve_power(p1_and_p2, power=power, alpha=sig_level) return { 'error': False, 'message': 'The required sample size for this campaign is:', 'data': round(sample_size), 'status_code': 200 }, 200
def get_sample_size_for_binomial(p0, p1, power = 0.8, significance = 0.05): """ Calculate the sample size for the binomial distribution Parameters ---------- p0 : [0, 1] current proportion value p1 : [0, 1] expected proportion value power : power of the test, e.g. 0.8, is one minus the probability of a type II error. Power is the probability that the test correctly rejects the Null Hypothesis if the Alternative Hypothesis is true. significance : significance level, e.g. 0.05, is the probability of a type I error, that is wrong rejections if the Null Hypothesis is true. Returns ------- required_n : Number of observations per sample. """ effect_size = sms.proportion_effectsize(p0, p1) required_n = sms.NormalIndPower().solve_power(effect_size, power=power, alpha=significance) return ceil(required_n)
NC_cont = payments_cont / clicks_cont NC_exp = payments_exp / clicks_exp NC_pooled = (payments_cont + payments_exp) / (clicks_cont + clicks_exp) NC_sd_pooled = mt.sqrt(NC_pooled * (1 - NC_pooled) * (1 / clicks_cont + 1 / clicks_exp)) NC_ME = round(get_z_score(1 - alpha / 2) * NC_sd_pooled, 4) NC_diff = round(NC_exp - NC_cont, 4) # print("The change due to the experiment is",NC_diff*100,"%") # print("Confidence Interval: [",NC_diff-NC_ME,",",NC_diff+NC_ME,"]") # print ("The change is statistically significant if the CI doesn't include 0. In that case, it is practically significant if",NC["d_min"],"is not in the CI as well.") # Case91_嘗試以函數算出樣本數_Calculating effect size based on our expected rates effect_size = sms.proportion_effectsize(GC["p"] - 1.0 * GC["d_min"], GC["p"] + 0.0 * GC["d_min"]) required_n = sms.NormalIndPower().solve_power(effect_size, power=0.8, alpha=0.05, ratio=1) required_n = ceil(required_n) print(effect_size, required_n) # Case02-自行開發雙樣本比例的信賴區間函數 def two_proprotions_confint(success_a, size_a, success_b, size_b, significance=0.05): prop_a = success_a / size_a prop_b = success_b / size_b var = prop_a * (1 - prop_a) / size_a + prop_b * (1 - prop_b) / size_b se = np.sqrt(var)
%matplotlib inline # Some plot styling preferences plt.style.use('seaborn-whitegrid') font = {'family' : 'Helvetica', 'weight' : 'bold', 'size' : 14} mpl.rc('font', **font) # calculate effect size by propotion effect_size = sms.proportion_effectsize(0.13, 0.15) # Calculating effect size based on our expected rates required_n = sms.NormalIndPower().solve_power( effect_size, power=0.8, alpha=0.05, ratio=1 ) # Calculating sample size needed required_n = ceil(required_n) # Rounding up to next whole number print(required_n) # get dataframe info df.info() pd.crosstab(df['group'], df['landing_page']) # get sample from both groups control_sample = df[df['group'] == 'control'].sample(n=required_n, random_state=22)