Skip to content

Instantly share code, notes, and snippets.

@anaveenan
anaveenan / get_sample_size_analytical.py
Created March 4, 2019 02:42
simulate power using simulation
#Analtyical solution to compute sample size
from statsmodels.stats.power import tt_ind_solve_power
treat_mean=control_mean*(1+delta)
mean_diff=treat_mean-control_mean
cohen_d=mean_diff/np.sqrt((control_sd**2+control_sd**2)/2)
n = tt_ind_solve_power(effect_size=cohen_d, alpha=alpha, power=0.8, ratio=1, alternative='two-sided')
print('Minimum sample size required to reach significance: {:.0f}'.format(round(n)))
@anaveenan
anaveenan / get_sample_size.py
Created March 4, 2019 02:40
Estimate power using simulation
#increment sample size till required power is reached
sample_size=1000
np.random.seed(123)
while True:
control_time_spent, treatment_time_spent=simulate_data(control_mean,control_sd,sample_size,n_sim)
t_stat, p_value = st.ttest_ind(control_time_spent, treatment_time_spent)
power=(p_value<alpha).sum()/n_sim
if power>.80:
print("Minimum sample size required to reach significance {}".format(sample_size))
break
@anaveenan
anaveenan / get_power.py
Created March 4, 2019 02:38
Estimate power using simulation
import numpy as np
import scipy.stats as st
# Initialize delta(minimum lift the product manager expect), control_mean, control_sd
delta=0.05
control_mean=2
control_sd=1
sample_size=1000
alpha=0.05#significance of the experiment
n_sim=1000#Total number of samples to simulate
@anaveenan
anaveenan / sql.export.gbm.R
Created October 29, 2015 03:28 — forked from shanebutler/sql.export.gbm.R
Deploy your GBM models in SQL! This tool enables in-database scoring of GBM models built using R. To use it, you simply call the function with the GBM model, output filename, SQL input data table and the name of the unique key on that table. For example:sql.export.gbm(gbm1, file="model_output.SQL", input.table="source_table", id="id") Please let…
# sql.export.gbm(): save a GBM model as SQL
# v0.11
# Copyright (c) 2013-2014 Shane Butler <shane dot butler at gmail dot com>
#
# sql.export.gbm is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# sql.export.gbm is distributed in the hope that it will be useful, but
@anaveenan
anaveenan / sql.export.randomForest.R
Created October 29, 2015 03:28 — forked from shanebutler/sql.export.randomForest.R
Deploy your RandomForest models in SQL! This tool enables in-database scoring of Random Forest models built using R. To use it, you simply call the function with the Random Forest model, output filename, SQL input data table and the name of the unique key on that table. For example:sql.export.rf(rf.mdl, file="model_output.SQL", input.table="sour…
# sql.export.rf(): save a randomForest model as SQL
# v0.04
# Copyright (c) 2013-2014 Shane Butler <shane dot butler at gmail dot com>
#
# sql.export.rf is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# sql.export.rf is distributed in the hope that it will be useful, but