# [SciPy-User] Power

josef.pktd@gmai... josef.pktd@gmai...
Wed Feb 27 20:08:40 CST 2013

```see for example http://www.statmethods.net/stats/power.html

some almost one liners coming soon to statsmodels

------------
import numpy as np
from scipy import stats

def chisquare_power(effect_size, nobs, n_bins, alpha=0.05, ddof=0):
'''power of chisquare goodness of fit test

effect size is sqrt of chisquare statistic divided by nobs

Parameters
----------
effect_size : float
This is the deviation from the Null of the normalized chi_square
statistic .
nobs : int or float
number of observations
n_bins : int (or float)
number of bins, or points in the discrete distribution
alpha : float in (0,1)
significance level of the test, default alpha=0.05

Returns
-------
power : float
power of the test at given significance level at effect size

Notes
-----
This function also works vectorized if all arguments broadcast.

'''
crit = stats.chi2.isf(alpha, n_bins - 1 - ddof)
power = stats.ncx2.sf(crit, n_bins - 1 - ddof, effect_size**2 * nobs)
return power

def chisquare_effectsize(probs0, probs1):
'''effect size for a chisquare goodness-of-fit test

Parameters
----------
probs0 : array_like
probabilities or cell frequencies under the Null hypothesis
probs1 : array_like
probabilities or cell frequencies under the Alternative hypothesis
probs0 and probs1 need to have the same shape.
Both probs0 and probs1 are normalized to add to one.

Returns
-------
effectsize : float
effect size of chisquare test

'''
probs0 = np.asarray(probs0, float)
probs1 = np.asarray(probs1, float)
probs0 = probs0 / probs0.sum(0)
probs1 = probs1 / probs1.sum(0)

return np.sqrt(((probs1 - probs0)**2 / probs0).sum(0))
------------

Josef
Brain: We must prepare for tomorrow night.
Pinky: Why? What are we going to do tomorrow night?
Brain: The same thing we do every night, Pinky - try to take over the world!
```