Skip to content

Instantly share code, notes, and snippets.

View marknagelberg's full-sized avatar

Mark Nagelberg marknagelberg

View GitHub Profile
@marknagelberg
marknagelberg / twosampleprobability1.py
Last active July 22, 2018 17:36
Function to calculate sample size
import math
import numpy as np
from scipy import stats
import pandas as pd
import seaborn as sns
def z_calc(p1, p2, n1, n2):
p_star = (p1*n1 + p2*n2) / (n1 + n2)
return (p2 - p1) / math.sqrt(p_star*(1 - p_star)*((1.0 / n1) + (1.0 / n2)))
@marknagelberg
marknagelberg / twosampleprobability2.py
Created July 22, 2018 17:37
Code to support post on significance levels required for two sample z test of proportions.
def sample_required(p1, p_diff, alpha):
if p_diff <= 0:
raise ValueError("p_diff must be > 0")
n = 1
while True:
z = z_calc(p1, p1+p_diff, n1=n, n2=n)
p = 1 - stats.norm.cdf(z)
if p < alpha:
break
n += 1
@marknagelberg
marknagelberg / twosampleprobability3.py
Created July 22, 2018 17:39
Code to support post on significance levels required for two sample z test of proportions.
#Map how sample size changes as choice of p1 changes, holding all
#else constant.
p1s = [x*.01 for x in range(96)]
data = []
for p1 in p1s:
record = {}
record['Probability Difference'] = p_diff
record['Sample Size to Detect Difference'] = sample_required(p1=p1,
@marknagelberg
marknagelberg / twosampleprobability4.py
Created July 22, 2018 17:40
Code to support post on significance levels required for two sample z test of proportions.
from matplotlib import pyplot
fig, ax = pyplot.subplots(figsize=(9, 9))
sns.set(style='darkgrid')
plot = sns.pointplot(x='Initial Probability',
y='Sample Size to Detect Difference',
hue='Confidence Level', ax = ax,
data=df)
@marknagelberg
marknagelberg / twosampleprobability5.py
Created July 22, 2018 17:41
Code to support post on significance levels required for two sample z test of proportions.
#Observe probability difference from 2% to 10%
p_diffs = [x*.01 for x in range(2,11)]
data = []
for p_diff in p_diffs:
record = {}
record['Probability Difference'] = p_diff * 100
record['Sample Size to Detect Difference'] = sample_required(p1=.5,
p_diff=p_diff,
alpha=.05)
@marknagelberg
marknagelberg / twosampleprobability6.py
Created July 22, 2018 17:42
Code to support post on significance levels required for two sample z test of proportions.
from matplotlib import pyplot
fig, ax = pyplot.subplots(figsize=(10, 10))
sns.set(style='darkgrid')
plot = sns.pointplot(x='Probability Difference',
y='Sample Size to Detect Difference',
hue='Confidence Level', ax = ax,
data=df)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
import os
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or 'secret string'
class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL')
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import config
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
from . import db
class Name(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), nullable=False)