bundles / scipy latest / scipy / stats / _resampling / power
function
scipy.stats._resampling:power
Signature
def power ( test , rvs , n_observations , * , significance = 0.01 , vectorized = None , n_resamples = 10000 , batch = None , kwargs = None ) Summary
Simulate the power of a hypothesis test under an alternative hypothesis.
Parameters
test: callableHypothesis test for which the power is to be simulated.
testmust be a callable that accepts a sample (e.g.test(sample)) orlen(rvs)separate samples (e.g.test(samples1, sample2)ifrvscontains two callables andn_observationscontains two values) and returns the p-value of the test. Ifvectorizedis set toTrue,testmust also accept a keyword argumentaxisand be vectorized to perform the test along the providedaxisof the samples. Any callable from scipy.stats with anaxisargument that returns an object with apvalueattribute is also acceptable.rvs: callable or tuple of callablesA callable or sequence of callables that generate(s) random variates under the alternative hypothesis. Each element of
rvsmust accept keyword argumentsize(e.g.rvs(size=(m, n))) and return an N-d array of that shape. Ifrvsis a sequence, the number of callables inrvsmust match the number of elements ofn_observations, i.e.len(rvs) == len(n_observations). Ifrvsis a single callable,n_observationsis treated as a single element.n_observations: tuple of ints or tuple of integer arraysIf a sequence of ints, each is the sizes of a sample to be passed to
test. If a sequence of integer arrays, the power is simulated for each set of corresponding sample sizes. See Examples.significance: float or array_like of floats, default: 0.01The threshold for significance; i.e., the p-value below which the hypothesis test results will be considered as evidence against the null hypothesis. Equivalently, the acceptable rate of Type I error under the null hypothesis. If an array, the power is simulated for each significance threshold.
kwargs: dict, optionalKeyword arguments to be passed to
rvsand/ortestcallables. Introspection is used to determine which keyword arguments may be passed to each callable. The value corresponding with each keyword must be an array. Arrays must be broadcastable with one another and with each array inn_observations. The power is simulated for each set of corresponding sample sizes and arguments. See Examples.vectorized: bool, optionalIf
vectorizedis set toFalse,testwill not be passed keyword argumentaxisand is expected to perform the test only for 1D samples. IfTrue,testwill be passed keyword argumentaxisand is expected to perform the test alongaxiswhen passed N-D sample arrays. IfNone(default),vectorizedwill be setTrueifaxisis a parameter oftest. Use of a vectorized test typically reduces computation time.n_resamples: int, default: 10000Number of samples drawn from each of the callables of
rvs. Equivalently, the number tests performed under the alternative hypothesis to approximate the power.batch: int, optionalThe number of samples to process in each call to
test. Memory usage is proportional to the product ofbatchand the largest sample size. Default isNone, in which casebatchequalsn_resamples.
Returns
res: PowerResultAn object with attributes:
power
power
pvalues
pvalues
Notes
The power is simulated as follows:
Draw many random samples (or sets of samples), each of the size(s) specified by
n_observations, under the alternative specified byrvs.For each sample (or set of samples), compute the p-value according to
test. These p-values are recorded in thepvaluesattribute of the result object.Compute the proportion of p-values that are less than the
significancelevel. This is the power recorded in thepowerattribute of the result object.
Suppose that significance is an array with shape shape1, the elements of kwargs and n_observations are mutually broadcastable to shape shape2, and test returns an array of p-values of shape shape3. Then the result object power attribute will be of shape shape1 + shape2 + shape3, and the pvalues attribute will be of shape shape2 + shape3 + (n_resamples,).
Array API Standard Support
power has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ✅ JAX ⚠️ no JIT ⚠️ no JIT Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
Suppose we wish to simulate the power of the independent sample t-test under the following conditions: - The first sample has 10 observations drawn from a normal distribution with mean 0. - The second sample has 12 observations drawn from a normal distribution with mean 1.0. - The threshold on p-values for significance is 0.05.import numpy as np from scipy import stats rng = np.random.default_rng(2549598345528) test = stats.ttest_ind n_observations = (10, 12) rvs1 = rng.normal rvs2 = lambda size: rng.normal(loc=1, size=size) rvs = (rvs1, rvs2) res = stats.power(test, rvs, n_observations, significance=0.05)✓
res.power
✗import matplotlib.pyplot as plt nobs_x = np.arange(5, 21) nobs_y = nobs_x n_observations = (nobs_x, nobs_y) res = stats.power(test, rvs, n_observations, significance=0.05) ax = plt.subplot()✓
ax.plot(nobs_x, res.power) ax.set_xlabel('Sample Size') ax.set_ylabel('Simulated Power') ax.set_title('Simulated Power of `ttest_ind` with Equal Sample Sizes')✗
plt.show()
✓
n_observations = (10, 12) loc = np.linspace(0, 1, 20) rvs2 = lambda size, loc: rng.normal(loc=loc, size=size) rvs = (rvs1, rvs2) res = stats.power(test, rvs, n_observations, significance=0.05, kwargs={'loc': loc}) ax = plt.subplot()✓
ax.plot(loc, res.power) ax.set_xlabel('Effect Size') ax.set_ylabel('Simulated Power') ax.set_title('Simulated Power of `ttest_ind`, Varying Effect Size')✗
plt.show()
✓
test = stats.jarque_bera n_observations = 10 rvs = rng.normal significance = np.linspace(0.0001, 0.1, 1000) res = stats.power(test, rvs, n_observations, significance=significance) size = res.power✓
ax = plt.subplot()
✓ax.plot(significance, size) ax.plot([0, 0.1], [0, 0.1], '--') ax.set_xlabel('nominal significance level') ax.set_ylabel('estimated test size (Type I error rate)') ax.set_title('Estimated test size vs nominal significance level')✗
ax.set_aspect('equal', 'box')
✓ax.legend(('`ttest_1samp`', 'ideal test'))
✗plt.show()
✓
rvs = rng.laplace significance = np.linspace(0.0001, 0.1, 1000) res = stats.power(test, rvs, n_observations, significance=0.05) print(res.power)✓
Aliases
-
scipy.stats.power