Is there a python (scipy) function to determine parameters needed to obtain a target power?
Question: In R there is a very useful function that helps with determining parameters
for a two sided t-test in order to obtain a target statistical power.

The function is called `power.prop.test`.

<http://stat.ethz.ch/R-manual/R-patched/library/stats/html/power.prop.test.html>

You can call it using:

    
    
    power.prop.test(p1 = .50, p2 = .75, power = .90)
    

And it will tell you n the sample size needed to obtain this power. This is
extremely useful in deterring sample sizes for tests.

Is there a similar function in the scipy package?


Answer: I've managed to replicate the function using the below formula for n and the
inverse survival function `norm.isf` from scipy.stats

![enter image description here](http://i.stack.imgur.com/8q6Cb.gif)

    
    
    from scipy.stats import norm, zscore
    
    def sample_power_probtest(p1, p2, power=0.8, sig=0.05):
        z = norm.isf([sig/2]) #two-sided t test
        zp = -1 * norm.isf([power]) 
        d = (p1-p2)
        s =2*((p1+p2) /2)*(1-((p1+p2) /2))
        n = s * ((zp + z)**2) / (d**2)
        return int(round(n[0]))
    
    def sample_power_difftest(d, s, power=0.8, sig=0.05):
        z = norm.isf([sig/2])
        zp = -1 * norm.isf([power])
        n = s * ((zp + z)**2) / (d**2)
        return int(round(n[0]))
    
    if __name__ == '__main__':
    
        n = sample_power_probtest(0.1, 0.11, power=0.8, sig=0.05)
        print n  #14752
    
        n = sample_power_difftest(0.1, 0.5, power=0.8, sig=0.05)
        print n  #392
    

