_resampling.py 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. import warnings
  2. import numpy as np
  3. from itertools import combinations, permutations, product
  4. import inspect
  5. from scipy._lib._util import check_random_state
  6. from scipy.special import ndtr, ndtri, comb, factorial
  7. from scipy._lib._util import rng_integers
  8. from dataclasses import make_dataclass
  9. from ._common import ConfidenceInterval
  10. from ._axis_nan_policy import _broadcast_concatenate, _broadcast_arrays
  11. from ._warnings_errors import DegenerateDataWarning
  12. __all__ = ['bootstrap', 'monte_carlo_test', 'permutation_test']
  13. def _vectorize_statistic(statistic):
  14. """Vectorize an n-sample statistic"""
  15. # This is a little cleaner than np.nditer at the expense of some data
  16. # copying: concatenate samples together, then use np.apply_along_axis
  17. def stat_nd(*data, axis=0):
  18. lengths = [sample.shape[axis] for sample in data]
  19. split_indices = np.cumsum(lengths)[:-1]
  20. z = _broadcast_concatenate(data, axis)
  21. # move working axis to position 0 so that new dimensions in the output
  22. # of `statistic` are _prepended_. ("This axis is removed, and replaced
  23. # with new dimensions...")
  24. z = np.moveaxis(z, axis, 0)
  25. def stat_1d(z):
  26. data = np.split(z, split_indices)
  27. return statistic(*data)
  28. return np.apply_along_axis(stat_1d, 0, z)[()]
  29. return stat_nd
  30. def _jackknife_resample(sample, batch=None):
  31. """Jackknife resample the sample. Only one-sample stats for now."""
  32. n = sample.shape[-1]
  33. batch_nominal = batch or n
  34. for k in range(0, n, batch_nominal):
  35. # col_start:col_end are the observations to remove
  36. batch_actual = min(batch_nominal, n-k)
  37. # jackknife - each row leaves out one observation
  38. j = np.ones((batch_actual, n), dtype=bool)
  39. np.fill_diagonal(j[:, k:k+batch_actual], False)
  40. i = np.arange(n)
  41. i = np.broadcast_to(i, (batch_actual, n))
  42. i = i[j].reshape((batch_actual, n-1))
  43. resamples = sample[..., i]
  44. yield resamples
  45. def _bootstrap_resample(sample, n_resamples=None, random_state=None):
  46. """Bootstrap resample the sample."""
  47. n = sample.shape[-1]
  48. # bootstrap - each row is a random resample of original observations
  49. i = rng_integers(random_state, 0, n, (n_resamples, n))
  50. resamples = sample[..., i]
  51. return resamples
  52. def _percentile_of_score(a, score, axis):
  53. """Vectorized, simplified `scipy.stats.percentileofscore`.
  54. Uses logic of the 'mean' value of percentileofscore's kind parameter.
  55. Unlike `stats.percentileofscore`, the percentile returned is a fraction
  56. in [0, 1].
  57. """
  58. B = a.shape[axis]
  59. return ((a < score).sum(axis=axis) + (a <= score).sum(axis=axis)) / (2 * B)
  60. def _percentile_along_axis(theta_hat_b, alpha):
  61. """`np.percentile` with different percentile for each slice."""
  62. # the difference between _percentile_along_axis and np.percentile is that
  63. # np.percentile gets _all_ the qs for each axis slice, whereas
  64. # _percentile_along_axis gets the q corresponding with each axis slice
  65. shape = theta_hat_b.shape[:-1]
  66. alpha = np.broadcast_to(alpha, shape)
  67. percentiles = np.zeros_like(alpha, dtype=np.float64)
  68. for indices, alpha_i in np.ndenumerate(alpha):
  69. if np.isnan(alpha_i):
  70. # e.g. when bootstrap distribution has only one unique element
  71. msg = (
  72. "The BCa confidence interval cannot be calculated."
  73. " This problem is known to occur when the distribution"
  74. " is degenerate or the statistic is np.min."
  75. )
  76. warnings.warn(DegenerateDataWarning(msg))
  77. percentiles[indices] = np.nan
  78. else:
  79. theta_hat_b_i = theta_hat_b[indices]
  80. percentiles[indices] = np.percentile(theta_hat_b_i, alpha_i)
  81. return percentiles[()] # return scalar instead of 0d array
  82. def _bca_interval(data, statistic, axis, alpha, theta_hat_b, batch):
  83. """Bias-corrected and accelerated interval."""
  84. # closely follows [1] 14.3 and 15.4 (Eq. 15.36)
  85. # calculate z0_hat
  86. theta_hat = np.asarray(statistic(*data, axis=axis))[..., None]
  87. percentile = _percentile_of_score(theta_hat_b, theta_hat, axis=-1)
  88. z0_hat = ndtri(percentile)
  89. # calculate a_hat
  90. theta_hat_ji = [] # j is for sample of data, i is for jackknife resample
  91. for j, sample in enumerate(data):
  92. # _jackknife_resample will add an axis prior to the last axis that
  93. # corresponds with the different jackknife resamples. Do the same for
  94. # each sample of the data to ensure broadcastability. We need to
  95. # create a copy of the list containing the samples anyway, so do this
  96. # in the loop to simplify the code. This is not the bottleneck...
  97. samples = [np.expand_dims(sample, -2) for sample in data]
  98. theta_hat_i = []
  99. for jackknife_sample in _jackknife_resample(sample, batch):
  100. samples[j] = jackknife_sample
  101. broadcasted = _broadcast_arrays(samples, axis=-1)
  102. theta_hat_i.append(statistic(*broadcasted, axis=-1))
  103. theta_hat_ji.append(theta_hat_i)
  104. theta_hat_ji = [np.concatenate(theta_hat_i, axis=-1)
  105. for theta_hat_i in theta_hat_ji]
  106. n_j = [theta_hat_i.shape[-1] for theta_hat_i in theta_hat_ji]
  107. theta_hat_j_dot = [theta_hat_i.mean(axis=-1, keepdims=True)
  108. for theta_hat_i in theta_hat_ji]
  109. U_ji = [(n - 1) * (theta_hat_dot - theta_hat_i)
  110. for theta_hat_dot, theta_hat_i, n
  111. in zip(theta_hat_j_dot, theta_hat_ji, n_j)]
  112. nums = [(U_i**3).sum(axis=-1)/n**3 for U_i, n in zip(U_ji, n_j)]
  113. dens = [(U_i**2).sum(axis=-1)/n**2 for U_i, n in zip(U_ji, n_j)]
  114. a_hat = 1/6 * sum(nums) / sum(dens)**(3/2)
  115. # calculate alpha_1, alpha_2
  116. z_alpha = ndtri(alpha)
  117. z_1alpha = -z_alpha
  118. num1 = z0_hat + z_alpha
  119. alpha_1 = ndtr(z0_hat + num1/(1 - a_hat*num1))
  120. num2 = z0_hat + z_1alpha
  121. alpha_2 = ndtr(z0_hat + num2/(1 - a_hat*num2))
  122. return alpha_1, alpha_2, a_hat # return a_hat for testing
  123. def _bootstrap_iv(data, statistic, vectorized, paired, axis, confidence_level,
  124. n_resamples, batch, method, bootstrap_result, random_state):
  125. """Input validation and standardization for `bootstrap`."""
  126. if vectorized not in {True, False, None}:
  127. raise ValueError("`vectorized` must be `True`, `False`, or `None`.")
  128. if vectorized is None:
  129. vectorized = 'axis' in inspect.signature(statistic).parameters
  130. if not vectorized:
  131. statistic = _vectorize_statistic(statistic)
  132. axis_int = int(axis)
  133. if axis != axis_int:
  134. raise ValueError("`axis` must be an integer.")
  135. n_samples = 0
  136. try:
  137. n_samples = len(data)
  138. except TypeError:
  139. raise ValueError("`data` must be a sequence of samples.")
  140. if n_samples == 0:
  141. raise ValueError("`data` must contain at least one sample.")
  142. data_iv = []
  143. for sample in data:
  144. sample = np.atleast_1d(sample)
  145. if sample.shape[axis_int] <= 1:
  146. raise ValueError("each sample in `data` must contain two or more "
  147. "observations along `axis`.")
  148. sample = np.moveaxis(sample, axis_int, -1)
  149. data_iv.append(sample)
  150. if paired not in {True, False}:
  151. raise ValueError("`paired` must be `True` or `False`.")
  152. if paired:
  153. n = data_iv[0].shape[-1]
  154. for sample in data_iv[1:]:
  155. if sample.shape[-1] != n:
  156. message = ("When `paired is True`, all samples must have the "
  157. "same length along `axis`")
  158. raise ValueError(message)
  159. # to generate the bootstrap distribution for paired-sample statistics,
  160. # resample the indices of the observations
  161. def statistic(i, axis=-1, data=data_iv, unpaired_statistic=statistic):
  162. data = [sample[..., i] for sample in data]
  163. return unpaired_statistic(*data, axis=axis)
  164. data_iv = [np.arange(n)]
  165. confidence_level_float = float(confidence_level)
  166. n_resamples_int = int(n_resamples)
  167. if n_resamples != n_resamples_int or n_resamples_int < 0:
  168. raise ValueError("`n_resamples` must be a non-negative integer.")
  169. if batch is None:
  170. batch_iv = batch
  171. else:
  172. batch_iv = int(batch)
  173. if batch != batch_iv or batch_iv <= 0:
  174. raise ValueError("`batch` must be a positive integer or None.")
  175. methods = {'percentile', 'basic', 'bca'}
  176. method = method.lower()
  177. if method not in methods:
  178. raise ValueError(f"`method` must be in {methods}")
  179. message = "`bootstrap_result` must have attribute `bootstrap_distribution'"
  180. if (bootstrap_result is not None
  181. and not hasattr(bootstrap_result, "bootstrap_distribution")):
  182. raise ValueError(message)
  183. message = ("Either `bootstrap_result.bootstrap_distribution.size` or "
  184. "`n_resamples` must be positive.")
  185. if ((not bootstrap_result or
  186. not bootstrap_result.bootstrap_distribution.size)
  187. and n_resamples_int == 0):
  188. raise ValueError(message)
  189. random_state = check_random_state(random_state)
  190. return (data_iv, statistic, vectorized, paired, axis_int,
  191. confidence_level_float, n_resamples_int, batch_iv,
  192. method, bootstrap_result, random_state)
  193. fields = ['confidence_interval', 'bootstrap_distribution', 'standard_error']
  194. BootstrapResult = make_dataclass("BootstrapResult", fields)
  195. def bootstrap(data, statistic, *, n_resamples=9999, batch=None,
  196. vectorized=None, paired=False, axis=0, confidence_level=0.95,
  197. method='BCa', bootstrap_result=None, random_state=None):
  198. r"""
  199. Compute a two-sided bootstrap confidence interval of a statistic.
  200. When `method` is ``'percentile'``, a bootstrap confidence interval is
  201. computed according to the following procedure.
  202. 1. Resample the data: for each sample in `data` and for each of
  203. `n_resamples`, take a random sample of the original sample
  204. (with replacement) of the same size as the original sample.
  205. 2. Compute the bootstrap distribution of the statistic: for each set of
  206. resamples, compute the test statistic.
  207. 3. Determine the confidence interval: find the interval of the bootstrap
  208. distribution that is
  209. - symmetric about the median and
  210. - contains `confidence_level` of the resampled statistic values.
  211. While the ``'percentile'`` method is the most intuitive, it is rarely
  212. used in practice. Two more common methods are available, ``'basic'``
  213. ('reverse percentile') and ``'BCa'`` ('bias-corrected and accelerated');
  214. they differ in how step 3 is performed.
  215. If the samples in `data` are taken at random from their respective
  216. distributions :math:`n` times, the confidence interval returned by
  217. `bootstrap` will contain the true value of the statistic for those
  218. distributions approximately `confidence_level`:math:`\, \times \, n` times.
  219. Parameters
  220. ----------
  221. data : sequence of array-like
  222. Each element of data is a sample from an underlying distribution.
  223. statistic : callable
  224. Statistic for which the confidence interval is to be calculated.
  225. `statistic` must be a callable that accepts ``len(data)`` samples
  226. as separate arguments and returns the resulting statistic.
  227. If `vectorized` is set ``True``,
  228. `statistic` must also accept a keyword argument `axis` and be
  229. vectorized to compute the statistic along the provided `axis`.
  230. n_resamples : int, default: ``9999``
  231. The number of resamples performed to form the bootstrap distribution
  232. of the statistic.
  233. batch : int, optional
  234. The number of resamples to process in each vectorized call to
  235. `statistic`. Memory usage is O(`batch`*``n``), where ``n`` is the
  236. sample size. Default is ``None``, in which case ``batch = n_resamples``
  237. (or ``batch = max(n_resamples, n)`` for ``method='BCa'``).
  238. vectorized : bool, optional
  239. If `vectorized` is set ``False``, `statistic` will not be passed
  240. keyword argument `axis` and is expected to calculate the statistic
  241. only for 1D samples. If ``True``, `statistic` will be passed keyword
  242. argument `axis` and is expected to calculate the statistic along `axis`
  243. when passed an ND sample array. If ``None`` (default), `vectorized`
  244. will be set ``True`` if ``axis`` is a parameter of `statistic`. Use of
  245. a vectorized statistic typically reduces computation time.
  246. paired : bool, default: ``False``
  247. Whether the statistic treats corresponding elements of the samples
  248. in `data` as paired.
  249. axis : int, default: ``0``
  250. The axis of the samples in `data` along which the `statistic` is
  251. calculated.
  252. confidence_level : float, default: ``0.95``
  253. The confidence level of the confidence interval.
  254. method : {'percentile', 'basic', 'bca'}, default: ``'BCa'``
  255. Whether to return the 'percentile' bootstrap confidence interval
  256. (``'percentile'``), the 'basic' (AKA 'reverse') bootstrap confidence
  257. interval (``'basic'``), or the bias-corrected and accelerated bootstrap
  258. confidence interval (``'BCa'``).
  259. bootstrap_result : BootstrapResult, optional
  260. Provide the result object returned by a previous call to `bootstrap`
  261. to include the previous bootstrap distribution in the new bootstrap
  262. distribution. This can be used, for example, to change
  263. `confidence_level`, change `method`, or see the effect of performing
  264. additional resampling without repeating computations.
  265. random_state : {None, int, `numpy.random.Generator`,
  266. `numpy.random.RandomState`}, optional
  267. Pseudorandom number generator state used to generate resamples.
  268. If `random_state` is ``None`` (or `np.random`), the
  269. `numpy.random.RandomState` singleton is used.
  270. If `random_state` is an int, a new ``RandomState`` instance is used,
  271. seeded with `random_state`.
  272. If `random_state` is already a ``Generator`` or ``RandomState``
  273. instance then that instance is used.
  274. Returns
  275. -------
  276. res : BootstrapResult
  277. An object with attributes:
  278. confidence_interval : ConfidenceInterval
  279. The bootstrap confidence interval as an instance of
  280. `collections.namedtuple` with attributes `low` and `high`.
  281. bootstrap_distribution : ndarray
  282. The bootstrap distribution, that is, the value of `statistic` for
  283. each resample. The last dimension corresponds with the resamples
  284. (e.g. ``res.bootstrap_distribution.shape[-1] == n_resamples``).
  285. standard_error : float or ndarray
  286. The bootstrap standard error, that is, the sample standard
  287. deviation of the bootstrap distribution.
  288. Warns
  289. -----
  290. `~scipy.stats.DegenerateDataWarning`
  291. Generated when ``method='BCa'`` and the bootstrap distribution is
  292. degenerate (e.g. all elements are identical).
  293. Notes
  294. -----
  295. Elements of the confidence interval may be NaN for ``method='BCa'`` if
  296. the bootstrap distribution is degenerate (e.g. all elements are identical).
  297. In this case, consider using another `method` or inspecting `data` for
  298. indications that other analysis may be more appropriate (e.g. all
  299. observations are identical).
  300. References
  301. ----------
  302. .. [1] B. Efron and R. J. Tibshirani, An Introduction to the Bootstrap,
  303. Chapman & Hall/CRC, Boca Raton, FL, USA (1993)
  304. .. [2] Nathaniel E. Helwig, "Bootstrap Confidence Intervals",
  305. http://users.stat.umn.edu/~helwig/notes/bootci-Notes.pdf
  306. .. [3] Bootstrapping (statistics), Wikipedia,
  307. https://en.wikipedia.org/wiki/Bootstrapping_%28statistics%29
  308. Examples
  309. --------
  310. Suppose we have sampled data from an unknown distribution.
  311. >>> import numpy as np
  312. >>> rng = np.random.default_rng()
  313. >>> from scipy.stats import norm
  314. >>> dist = norm(loc=2, scale=4) # our "unknown" distribution
  315. >>> data = dist.rvs(size=100, random_state=rng)
  316. We are interested in the standard deviation of the distribution.
  317. >>> std_true = dist.std() # the true value of the statistic
  318. >>> print(std_true)
  319. 4.0
  320. >>> std_sample = np.std(data) # the sample statistic
  321. >>> print(std_sample)
  322. 3.9460644295563863
  323. The bootstrap is used to approximate the variability we would expect if we
  324. were to repeatedly sample from the unknown distribution and calculate the
  325. statistic of the sample each time. It does this by repeatedly resampling
  326. values *from the original sample* with replacement and calculating the
  327. statistic of each resample. This results in a "bootstrap distribution" of
  328. the statistic.
  329. >>> import matplotlib.pyplot as plt
  330. >>> from scipy.stats import bootstrap
  331. >>> data = (data,) # samples must be in a sequence
  332. >>> res = bootstrap(data, np.std, confidence_level=0.9,
  333. ... random_state=rng)
  334. >>> fig, ax = plt.subplots()
  335. >>> ax.hist(res.bootstrap_distribution, bins=25)
  336. >>> ax.set_title('Bootstrap Distribution')
  337. >>> ax.set_xlabel('statistic value')
  338. >>> ax.set_ylabel('frequency')
  339. >>> plt.show()
  340. The standard error quantifies this variability. It is calculated as the
  341. standard deviation of the bootstrap distribution.
  342. >>> res.standard_error
  343. 0.24427002125829136
  344. >>> res.standard_error == np.std(res.bootstrap_distribution, ddof=1)
  345. True
  346. The bootstrap distribution of the statistic is often approximately normal
  347. with scale equal to the standard error.
  348. >>> x = np.linspace(3, 5)
  349. >>> pdf = norm.pdf(x, loc=std_sample, scale=res.standard_error)
  350. >>> fig, ax = plt.subplots()
  351. >>> ax.hist(res.bootstrap_distribution, bins=25, density=True)
  352. >>> ax.plot(x, pdf)
  353. >>> ax.set_title('Normal Approximation of the Bootstrap Distribution')
  354. >>> ax.set_xlabel('statistic value')
  355. >>> ax.set_ylabel('pdf')
  356. >>> plt.show()
  357. This suggests that we could construct a 90% confidence interval on the
  358. statistic based on quantiles of this normal distribution.
  359. >>> norm.interval(0.9, loc=std_sample, scale=res.standard_error)
  360. (3.5442759991341726, 4.3478528599786)
  361. Due to central limit theorem, this normal approximation is accurate for a
  362. variety of statistics and distributions underlying the samples; however,
  363. the approximation is not reliable in all cases. Because `bootstrap` is
  364. designed to work with arbitrary underlying distributions and statistics,
  365. it uses more advanced techniques to generate an accurate confidence
  366. interval.
  367. >>> print(res.confidence_interval)
  368. ConfidenceInterval(low=3.57655333533867, high=4.382043696342881)
  369. If we sample from the original distribution 1000 times and form a bootstrap
  370. confidence interval for each sample, the confidence interval
  371. contains the true value of the statistic approximately 90% of the time.
  372. >>> n_trials = 1000
  373. >>> ci_contains_true_std = 0
  374. >>> for i in range(n_trials):
  375. ... data = (dist.rvs(size=100, random_state=rng),)
  376. ... ci = bootstrap(data, np.std, confidence_level=0.9, n_resamples=1000,
  377. ... random_state=rng).confidence_interval
  378. ... if ci[0] < std_true < ci[1]:
  379. ... ci_contains_true_std += 1
  380. >>> print(ci_contains_true_std)
  381. 875
  382. Rather than writing a loop, we can also determine the confidence intervals
  383. for all 1000 samples at once.
  384. >>> data = (dist.rvs(size=(n_trials, 100), random_state=rng),)
  385. >>> res = bootstrap(data, np.std, axis=-1, confidence_level=0.9,
  386. ... n_resamples=1000, random_state=rng)
  387. >>> ci_l, ci_u = res.confidence_interval
  388. Here, `ci_l` and `ci_u` contain the confidence interval for each of the
  389. ``n_trials = 1000`` samples.
  390. >>> print(ci_l[995:])
  391. [3.77729695 3.75090233 3.45829131 3.34078217 3.48072829]
  392. >>> print(ci_u[995:])
  393. [4.88316666 4.86924034 4.32032996 4.2822427 4.59360598]
  394. And again, approximately 90% contain the true value, ``std_true = 4``.
  395. >>> print(np.sum((ci_l < std_true) & (std_true < ci_u)))
  396. 900
  397. `bootstrap` can also be used to estimate confidence intervals of
  398. multi-sample statistics, including those calculated by hypothesis
  399. tests. `scipy.stats.mood` perform's Mood's test for equal scale parameters,
  400. and it returns two outputs: a statistic, and a p-value. To get a
  401. confidence interval for the test statistic, we first wrap
  402. `scipy.stats.mood` in a function that accepts two sample arguments,
  403. accepts an `axis` keyword argument, and returns only the statistic.
  404. >>> from scipy.stats import mood
  405. >>> def my_statistic(sample1, sample2, axis):
  406. ... statistic, _ = mood(sample1, sample2, axis=-1)
  407. ... return statistic
  408. Here, we use the 'percentile' method with the default 95% confidence level.
  409. >>> sample1 = norm.rvs(scale=1, size=100, random_state=rng)
  410. >>> sample2 = norm.rvs(scale=2, size=100, random_state=rng)
  411. >>> data = (sample1, sample2)
  412. >>> res = bootstrap(data, my_statistic, method='basic', random_state=rng)
  413. >>> print(mood(sample1, sample2)[0]) # element 0 is the statistic
  414. -5.521109549096542
  415. >>> print(res.confidence_interval)
  416. ConfidenceInterval(low=-7.255994487314675, high=-4.016202624747605)
  417. The bootstrap estimate of the standard error is also available.
  418. >>> print(res.standard_error)
  419. 0.8344963846318795
  420. Paired-sample statistics work, too. For example, consider the Pearson
  421. correlation coefficient.
  422. >>> from scipy.stats import pearsonr
  423. >>> n = 100
  424. >>> x = np.linspace(0, 10, n)
  425. >>> y = x + rng.uniform(size=n)
  426. >>> print(pearsonr(x, y)[0]) # element 0 is the statistic
  427. 0.9962357936065914
  428. We wrap `pearsonr` so that it returns only the statistic.
  429. >>> def my_statistic(x, y):
  430. ... return pearsonr(x, y)[0]
  431. We call `bootstrap` using ``paired=True``.
  432. Also, since ``my_statistic`` isn't vectorized to calculate the statistic
  433. along a given axis, we pass in ``vectorized=False``.
  434. >>> res = bootstrap((x, y), my_statistic, vectorized=False, paired=True,
  435. ... random_state=rng)
  436. >>> print(res.confidence_interval)
  437. ConfidenceInterval(low=0.9950085825848624, high=0.9971212407917498)
  438. The result object can be passed back into `bootstrap` to perform additional
  439. resampling:
  440. >>> len(res.bootstrap_distribution)
  441. 9999
  442. >>> res = bootstrap((x, y), my_statistic, vectorized=False, paired=True,
  443. ... n_resamples=1001, random_state=rng,
  444. ... bootstrap_result=res)
  445. >>> len(res.bootstrap_distribution)
  446. 11000
  447. or to change the confidence interval options:
  448. >>> res2 = bootstrap((x, y), my_statistic, vectorized=False, paired=True,
  449. ... n_resamples=0, random_state=rng, bootstrap_result=res,
  450. ... method='percentile', confidence_level=0.9)
  451. >>> np.testing.assert_equal(res2.bootstrap_distribution,
  452. ... res.bootstrap_distribution)
  453. >>> res.confidence_interval
  454. ConfidenceInterval(low=0.9950035351407804, high=0.9971170323404578)
  455. without repeating computation of the original bootstrap distribution.
  456. """
  457. # Input validation
  458. args = _bootstrap_iv(data, statistic, vectorized, paired, axis,
  459. confidence_level, n_resamples, batch, method,
  460. bootstrap_result, random_state)
  461. data, statistic, vectorized, paired, axis, confidence_level = args[:6]
  462. n_resamples, batch, method, bootstrap_result, random_state = args[6:]
  463. theta_hat_b = ([] if bootstrap_result is None
  464. else [bootstrap_result.bootstrap_distribution])
  465. batch_nominal = batch or n_resamples or 1
  466. for k in range(0, n_resamples, batch_nominal):
  467. batch_actual = min(batch_nominal, n_resamples-k)
  468. # Generate resamples
  469. resampled_data = []
  470. for sample in data:
  471. resample = _bootstrap_resample(sample, n_resamples=batch_actual,
  472. random_state=random_state)
  473. resampled_data.append(resample)
  474. # Compute bootstrap distribution of statistic
  475. theta_hat_b.append(statistic(*resampled_data, axis=-1))
  476. theta_hat_b = np.concatenate(theta_hat_b, axis=-1)
  477. # Calculate percentile interval
  478. alpha = (1 - confidence_level)/2
  479. if method == 'bca':
  480. interval = _bca_interval(data, statistic, axis=-1, alpha=alpha,
  481. theta_hat_b=theta_hat_b, batch=batch)[:2]
  482. percentile_fun = _percentile_along_axis
  483. else:
  484. interval = alpha, 1-alpha
  485. def percentile_fun(a, q):
  486. return np.percentile(a=a, q=q, axis=-1)
  487. # Calculate confidence interval of statistic
  488. ci_l = percentile_fun(theta_hat_b, interval[0]*100)
  489. ci_u = percentile_fun(theta_hat_b, interval[1]*100)
  490. if method == 'basic': # see [3]
  491. theta_hat = statistic(*data, axis=-1)
  492. ci_l, ci_u = 2*theta_hat - ci_u, 2*theta_hat - ci_l
  493. return BootstrapResult(confidence_interval=ConfidenceInterval(ci_l, ci_u),
  494. bootstrap_distribution=theta_hat_b,
  495. standard_error=np.std(theta_hat_b, ddof=1, axis=-1))
  496. def _monte_carlo_test_iv(sample, rvs, statistic, vectorized, n_resamples,
  497. batch, alternative, axis):
  498. """Input validation for `monte_carlo_test`."""
  499. axis_int = int(axis)
  500. if axis != axis_int:
  501. raise ValueError("`axis` must be an integer.")
  502. if vectorized not in {True, False, None}:
  503. raise ValueError("`vectorized` must be `True`, `False`, or `None`.")
  504. if not callable(rvs):
  505. raise TypeError("`rvs` must be callable.")
  506. if not callable(statistic):
  507. raise TypeError("`statistic` must be callable.")
  508. if vectorized is None:
  509. vectorized = 'axis' in inspect.signature(statistic).parameters
  510. if not vectorized:
  511. statistic_vectorized = _vectorize_statistic(statistic)
  512. else:
  513. statistic_vectorized = statistic
  514. sample = np.atleast_1d(sample)
  515. sample = np.moveaxis(sample, axis, -1)
  516. n_resamples_int = int(n_resamples)
  517. if n_resamples != n_resamples_int or n_resamples_int <= 0:
  518. raise ValueError("`n_resamples` must be a positive integer.")
  519. if batch is None:
  520. batch_iv = batch
  521. else:
  522. batch_iv = int(batch)
  523. if batch != batch_iv or batch_iv <= 0:
  524. raise ValueError("`batch` must be a positive integer or None.")
  525. alternatives = {'two-sided', 'greater', 'less'}
  526. alternative = alternative.lower()
  527. if alternative not in alternatives:
  528. raise ValueError(f"`alternative` must be in {alternatives}")
  529. return (sample, rvs, statistic_vectorized, vectorized, n_resamples_int,
  530. batch_iv, alternative, axis_int)
  531. fields = ['statistic', 'pvalue', 'null_distribution']
  532. MonteCarloTestResult = make_dataclass("MonteCarloTestResult", fields)
  533. def monte_carlo_test(sample, rvs, statistic, *, vectorized=None,
  534. n_resamples=9999, batch=None, alternative="two-sided",
  535. axis=0):
  536. r"""
  537. Monte Carlo test that a sample is drawn from a given distribution.
  538. The null hypothesis is that the provided `sample` was drawn at random from
  539. the distribution for which `rvs` generates random variates. The value of
  540. the `statistic` for the given sample is compared against a Monte Carlo null
  541. distribution: the value of the statistic for each of `n_resamples`
  542. samples generated by `rvs`. This gives the p-value, the probability of
  543. observing such an extreme value of the test statistic under the null
  544. hypothesis.
  545. Parameters
  546. ----------
  547. sample : array-like
  548. An array of observations.
  549. rvs : callable
  550. Generates random variates from the distribution against which `sample`
  551. will be tested. `rvs` must be a callable that accepts keyword argument
  552. ``size`` (e.g. ``rvs(size=(m, n))``) and returns an N-d array sample
  553. of that shape.
  554. statistic : callable
  555. Statistic for which the p-value of the hypothesis test is to be
  556. calculated. `statistic` must be a callable that accepts a sample
  557. (e.g. ``statistic(sample)``) and returns the resulting statistic.
  558. If `vectorized` is set ``True``, `statistic` must also accept a keyword
  559. argument `axis` and be vectorized to compute the statistic along the
  560. provided `axis` of the sample array.
  561. vectorized : bool, optional
  562. If `vectorized` is set ``False``, `statistic` will not be passed
  563. keyword argument `axis` and is expected to calculate the statistic
  564. only for 1D samples. If ``True``, `statistic` will be passed keyword
  565. argument `axis` and is expected to calculate the statistic along `axis`
  566. when passed an ND sample array. If ``None`` (default), `vectorized`
  567. will be set ``True`` if ``axis`` is a parameter of `statistic`. Use of
  568. a vectorized statistic typically reduces computation time.
  569. n_resamples : int, default: 9999
  570. Number of random permutations used to approximate the Monte Carlo null
  571. distribution.
  572. batch : int, optional
  573. The number of permutations to process in each call to `statistic`.
  574. Memory usage is O(`batch`*``sample.size[axis]``). Default is
  575. ``None``, in which case `batch` equals `n_resamples`.
  576. alternative : {'two-sided', 'less', 'greater'}
  577. The alternative hypothesis for which the p-value is calculated.
  578. For each alternative, the p-value is defined as follows.
  579. - ``'greater'`` : the percentage of the null distribution that is
  580. greater than or equal to the observed value of the test statistic.
  581. - ``'less'`` : the percentage of the null distribution that is
  582. less than or equal to the observed value of the test statistic.
  583. - ``'two-sided'`` : twice the smaller of the p-values above.
  584. axis : int, default: 0
  585. The axis of `sample` over which to calculate the statistic.
  586. Returns
  587. -------
  588. statistic : float or ndarray
  589. The observed test statistic of the sample.
  590. pvalue : float or ndarray
  591. The p-value for the given alternative.
  592. null_distribution : ndarray
  593. The values of the test statistic generated under the null hypothesis.
  594. References
  595. ----------
  596. .. [1] B. Phipson and G. K. Smyth. "Permutation P-values Should Never Be
  597. Zero: Calculating Exact P-values When Permutations Are Randomly Drawn."
  598. Statistical Applications in Genetics and Molecular Biology 9.1 (2010).
  599. Examples
  600. --------
  601. Suppose we wish to test whether a small sample has been drawn from a normal
  602. distribution. We decide that we will use the skew of the sample as a
  603. test statistic, and we will consider a p-value of 0.05 to be statistically
  604. significant.
  605. >>> import numpy as np
  606. >>> from scipy import stats
  607. >>> def statistic(x, axis):
  608. ... return stats.skew(x, axis)
  609. After collecting our data, we calculate the observed value of the test
  610. statistic.
  611. >>> rng = np.random.default_rng()
  612. >>> x = stats.skewnorm.rvs(a=1, size=50, random_state=rng)
  613. >>> statistic(x, axis=0)
  614. 0.12457412450240658
  615. To determine the probability of observing such an extreme value of the
  616. skewness by chance if the sample were drawn from the normal distribution,
  617. we can perform a Monte Carlo hypothesis test. The test will draw many
  618. samples at random from their normal distribution, calculate the skewness
  619. of each sample, and compare our original skewness against this
  620. distribution to determine an approximate p-value.
  621. >>> from scipy.stats import monte_carlo_test
  622. >>> # because our statistic is vectorized, we pass `vectorized=True`
  623. >>> rvs = lambda size: stats.norm.rvs(size=size, random_state=rng)
  624. >>> res = monte_carlo_test(x, rvs, statistic, vectorized=True)
  625. >>> print(res.statistic)
  626. 0.12457412450240658
  627. >>> print(res.pvalue)
  628. 0.7012
  629. The probability of obtaining a test statistic less than or equal to the
  630. observed value under the null hypothesis is ~70%. This is greater than
  631. our chosen threshold of 5%, so we cannot consider this to be significant
  632. evidence against the null hypothesis.
  633. Note that this p-value essentially matches that of
  634. `scipy.stats.skewtest`, which relies on an asymptotic distribution of a
  635. test statistic based on the sample skewness.
  636. >>> stats.skewtest(x).pvalue
  637. 0.6892046027110614
  638. This asymptotic approximation is not valid for small sample sizes, but
  639. `monte_carlo_test` can be used with samples of any size.
  640. >>> x = stats.skewnorm.rvs(a=1, size=7, random_state=rng)
  641. >>> # stats.skewtest(x) would produce an error due to small sample
  642. >>> res = monte_carlo_test(x, rvs, statistic, vectorized=True)
  643. The Monte Carlo distribution of the test statistic is provided for
  644. further investigation.
  645. >>> import matplotlib.pyplot as plt
  646. >>> fig, ax = plt.subplots()
  647. >>> ax.hist(res.null_distribution, bins=50)
  648. >>> ax.set_title("Monte Carlo distribution of test statistic")
  649. >>> ax.set_xlabel("Value of Statistic")
  650. >>> ax.set_ylabel("Frequency")
  651. >>> plt.show()
  652. """
  653. args = _monte_carlo_test_iv(sample, rvs, statistic, vectorized,
  654. n_resamples, batch, alternative, axis)
  655. (sample, rvs, statistic, vectorized,
  656. n_resamples, batch, alternative, axis) = args
  657. # Some statistics return plain floats; ensure they're at least np.float64
  658. observed = np.asarray(statistic(sample, axis=-1))[()]
  659. n_observations = sample.shape[-1]
  660. batch_nominal = batch or n_resamples
  661. null_distribution = []
  662. for k in range(0, n_resamples, batch_nominal):
  663. batch_actual = min(batch_nominal, n_resamples-k)
  664. resamples = rvs(size=(batch_actual, n_observations))
  665. null_distribution.append(statistic(resamples, axis=-1))
  666. null_distribution = np.concatenate(null_distribution)
  667. null_distribution = null_distribution.reshape([-1] + [1]*observed.ndim)
  668. def less(null_distribution, observed):
  669. cmps = null_distribution <= observed
  670. pvalues = (cmps.sum(axis=0) + 1) / (n_resamples + 1) # see [1]
  671. return pvalues
  672. def greater(null_distribution, observed):
  673. cmps = null_distribution >= observed
  674. pvalues = (cmps.sum(axis=0) + 1) / (n_resamples + 1) # see [1]
  675. return pvalues
  676. def two_sided(null_distribution, observed):
  677. pvalues_less = less(null_distribution, observed)
  678. pvalues_greater = greater(null_distribution, observed)
  679. pvalues = np.minimum(pvalues_less, pvalues_greater) * 2
  680. return pvalues
  681. compare = {"less": less,
  682. "greater": greater,
  683. "two-sided": two_sided}
  684. pvalues = compare[alternative](null_distribution, observed)
  685. pvalues = np.clip(pvalues, 0, 1)
  686. return MonteCarloTestResult(observed, pvalues, null_distribution)
  687. attributes = ('statistic', 'pvalue', 'null_distribution')
  688. PermutationTestResult = make_dataclass('PermutationTestResult', attributes)
  689. def _all_partitions_concatenated(ns):
  690. """
  691. Generate all partitions of indices of groups of given sizes, concatenated
  692. `ns` is an iterable of ints.
  693. """
  694. def all_partitions(z, n):
  695. for c in combinations(z, n):
  696. x0 = set(c)
  697. x1 = z - x0
  698. yield [x0, x1]
  699. def all_partitions_n(z, ns):
  700. if len(ns) == 0:
  701. yield [z]
  702. return
  703. for c in all_partitions(z, ns[0]):
  704. for d in all_partitions_n(c[1], ns[1:]):
  705. yield c[0:1] + d
  706. z = set(range(np.sum(ns)))
  707. for partitioning in all_partitions_n(z, ns[:]):
  708. x = np.concatenate([list(partition)
  709. for partition in partitioning]).astype(int)
  710. yield x
  711. def _batch_generator(iterable, batch):
  712. """A generator that yields batches of elements from an iterable"""
  713. iterator = iter(iterable)
  714. if batch <= 0:
  715. raise ValueError("`batch` must be positive.")
  716. z = [item for i, item in zip(range(batch), iterator)]
  717. while z: # we don't want StopIteration without yielding an empty list
  718. yield z
  719. z = [item for i, item in zip(range(batch), iterator)]
  720. def _pairings_permutations_gen(n_permutations, n_samples, n_obs_sample, batch,
  721. random_state):
  722. # Returns a generator that yields arrays of size
  723. # `(batch, n_samples, n_obs_sample)`.
  724. # Each row is an independent permutation of indices 0 to `n_obs_sample`.
  725. batch = min(batch, n_permutations)
  726. if hasattr(random_state, 'permuted'):
  727. def batched_perm_generator():
  728. indices = np.arange(n_obs_sample)
  729. indices = np.tile(indices, (batch, n_samples, 1))
  730. for k in range(0, n_permutations, batch):
  731. batch_actual = min(batch, n_permutations-k)
  732. # Don't permute in place, otherwise results depend on `batch`
  733. permuted_indices = random_state.permuted(indices, axis=-1)
  734. yield permuted_indices[:batch_actual]
  735. else: # RandomState and early Generators don't have `permuted`
  736. def batched_perm_generator():
  737. for k in range(0, n_permutations, batch):
  738. batch_actual = min(batch, n_permutations-k)
  739. size = (batch_actual, n_samples, n_obs_sample)
  740. x = random_state.random(size=size)
  741. yield np.argsort(x, axis=-1)[:batch_actual]
  742. return batched_perm_generator()
  743. def _calculate_null_both(data, statistic, n_permutations, batch,
  744. random_state=None):
  745. """
  746. Calculate null distribution for independent sample tests.
  747. """
  748. n_samples = len(data)
  749. # compute number of permutations
  750. # (distinct partitions of data into samples of these sizes)
  751. n_obs_i = [sample.shape[-1] for sample in data] # observations per sample
  752. n_obs_ic = np.cumsum(n_obs_i)
  753. n_obs = n_obs_ic[-1] # total number of observations
  754. n_max = np.prod([comb(n_obs_ic[i], n_obs_ic[i-1])
  755. for i in range(n_samples-1, 0, -1)])
  756. # perm_generator is an iterator that produces permutations of indices
  757. # from 0 to n_obs. We'll concatenate the samples, use these indices to
  758. # permute the data, then split the samples apart again.
  759. if n_permutations >= n_max:
  760. exact_test = True
  761. n_permutations = n_max
  762. perm_generator = _all_partitions_concatenated(n_obs_i)
  763. else:
  764. exact_test = False
  765. # Neither RandomState.permutation nor Generator.permutation
  766. # can permute axis-slices independently. If this feature is
  767. # added in the future, batches of the desired size should be
  768. # generated in a single call.
  769. perm_generator = (random_state.permutation(n_obs)
  770. for i in range(n_permutations))
  771. batch = batch or int(n_permutations)
  772. null_distribution = []
  773. # First, concatenate all the samples. In batches, permute samples with
  774. # indices produced by the `perm_generator`, split them into new samples of
  775. # the original sizes, compute the statistic for each batch, and add these
  776. # statistic values to the null distribution.
  777. data = np.concatenate(data, axis=-1)
  778. for indices in _batch_generator(perm_generator, batch=batch):
  779. indices = np.array(indices)
  780. # `indices` is 2D: each row is a permutation of the indices.
  781. # We use it to index `data` along its last axis, which corresponds
  782. # with observations.
  783. # After indexing, the second to last axis of `data_batch` corresponds
  784. # with permutations, and the last axis corresponds with observations.
  785. data_batch = data[..., indices]
  786. # Move the permutation axis to the front: we'll concatenate a list
  787. # of batched statistic values along this zeroth axis to form the
  788. # null distribution.
  789. data_batch = np.moveaxis(data_batch, -2, 0)
  790. data_batch = np.split(data_batch, n_obs_ic[:-1], axis=-1)
  791. null_distribution.append(statistic(*data_batch, axis=-1))
  792. null_distribution = np.concatenate(null_distribution, axis=0)
  793. return null_distribution, n_permutations, exact_test
  794. def _calculate_null_pairings(data, statistic, n_permutations, batch,
  795. random_state=None):
  796. """
  797. Calculate null distribution for association tests.
  798. """
  799. n_samples = len(data)
  800. # compute number of permutations (factorial(n) permutations of each sample)
  801. n_obs_sample = data[0].shape[-1] # observations per sample; same for each
  802. n_max = factorial(n_obs_sample)**n_samples
  803. # `perm_generator` is an iterator that produces a list of permutations of
  804. # indices from 0 to n_obs_sample, one for each sample.
  805. if n_permutations >= n_max:
  806. exact_test = True
  807. n_permutations = n_max
  808. batch = batch or int(n_permutations)
  809. # cartesian product of the sets of all permutations of indices
  810. perm_generator = product(*(permutations(range(n_obs_sample))
  811. for i in range(n_samples)))
  812. batched_perm_generator = _batch_generator(perm_generator, batch=batch)
  813. else:
  814. exact_test = False
  815. batch = batch or int(n_permutations)
  816. # Separate random permutations of indices for each sample.
  817. # Again, it would be nice if RandomState/Generator.permutation
  818. # could permute each axis-slice separately.
  819. args = n_permutations, n_samples, n_obs_sample, batch, random_state
  820. batched_perm_generator = _pairings_permutations_gen(*args)
  821. null_distribution = []
  822. for indices in batched_perm_generator:
  823. indices = np.array(indices)
  824. # `indices` is 3D: the zeroth axis is for permutations, the next is
  825. # for samples, and the last is for observations. Swap the first two
  826. # to make the zeroth axis correspond with samples, as it does for
  827. # `data`.
  828. indices = np.swapaxes(indices, 0, 1)
  829. # When we're done, `data_batch` will be a list of length `n_samples`.
  830. # Each element will be a batch of random permutations of one sample.
  831. # The zeroth axis of each batch will correspond with permutations,
  832. # and the last will correspond with observations. (This makes it
  833. # easy to pass into `statistic`.)
  834. data_batch = [None]*n_samples
  835. for i in range(n_samples):
  836. data_batch[i] = data[i][..., indices[i]]
  837. data_batch[i] = np.moveaxis(data_batch[i], -2, 0)
  838. null_distribution.append(statistic(*data_batch, axis=-1))
  839. null_distribution = np.concatenate(null_distribution, axis=0)
  840. return null_distribution, n_permutations, exact_test
  841. def _calculate_null_samples(data, statistic, n_permutations, batch,
  842. random_state=None):
  843. """
  844. Calculate null distribution for paired-sample tests.
  845. """
  846. n_samples = len(data)
  847. # By convention, the meaning of the "samples" permutations type for
  848. # data with only one sample is to flip the sign of the observations.
  849. # Achieve this by adding a second sample - the negative of the original.
  850. if n_samples == 1:
  851. data = [data[0], -data[0]]
  852. # The "samples" permutation strategy is the same as the "pairings"
  853. # strategy except the roles of samples and observations are flipped.
  854. # So swap these axes, then we'll use the function for the "pairings"
  855. # strategy to do all the work!
  856. data = np.swapaxes(data, 0, -1)
  857. # (Of course, the user's statistic doesn't know what we've done here,
  858. # so we need to pass it what it's expecting.)
  859. def statistic_wrapped(*data, axis):
  860. data = np.swapaxes(data, 0, -1)
  861. if n_samples == 1:
  862. data = data[0:1]
  863. return statistic(*data, axis=axis)
  864. return _calculate_null_pairings(data, statistic_wrapped, n_permutations,
  865. batch, random_state)
  866. def _permutation_test_iv(data, statistic, permutation_type, vectorized,
  867. n_resamples, batch, alternative, axis, random_state):
  868. """Input validation for `permutation_test`."""
  869. axis_int = int(axis)
  870. if axis != axis_int:
  871. raise ValueError("`axis` must be an integer.")
  872. permutation_types = {'samples', 'pairings', 'independent'}
  873. permutation_type = permutation_type.lower()
  874. if permutation_type not in permutation_types:
  875. raise ValueError(f"`permutation_type` must be in {permutation_types}.")
  876. if vectorized not in {True, False, None}:
  877. raise ValueError("`vectorized` must be `True`, `False`, or `None`.")
  878. if vectorized is None:
  879. vectorized = 'axis' in inspect.signature(statistic).parameters
  880. if not vectorized:
  881. statistic = _vectorize_statistic(statistic)
  882. message = "`data` must be a tuple containing at least two samples"
  883. try:
  884. if len(data) < 2 and permutation_type == 'independent':
  885. raise ValueError(message)
  886. except TypeError:
  887. raise TypeError(message)
  888. data = _broadcast_arrays(data, axis)
  889. data_iv = []
  890. for sample in data:
  891. sample = np.atleast_1d(sample)
  892. if sample.shape[axis] <= 1:
  893. raise ValueError("each sample in `data` must contain two or more "
  894. "observations along `axis`.")
  895. sample = np.moveaxis(sample, axis_int, -1)
  896. data_iv.append(sample)
  897. n_resamples_int = (int(n_resamples) if not np.isinf(n_resamples)
  898. else np.inf)
  899. if n_resamples != n_resamples_int or n_resamples_int <= 0:
  900. raise ValueError("`n_resamples` must be a positive integer.")
  901. if batch is None:
  902. batch_iv = batch
  903. else:
  904. batch_iv = int(batch)
  905. if batch != batch_iv or batch_iv <= 0:
  906. raise ValueError("`batch` must be a positive integer or None.")
  907. alternatives = {'two-sided', 'greater', 'less'}
  908. alternative = alternative.lower()
  909. if alternative not in alternatives:
  910. raise ValueError(f"`alternative` must be in {alternatives}")
  911. random_state = check_random_state(random_state)
  912. return (data_iv, statistic, permutation_type, vectorized, n_resamples_int,
  913. batch_iv, alternative, axis_int, random_state)
  914. def permutation_test(data, statistic, *, permutation_type='independent',
  915. vectorized=None, n_resamples=9999, batch=None,
  916. alternative="two-sided", axis=0, random_state=None):
  917. r"""
  918. Performs a permutation test of a given statistic on provided data.
  919. For independent sample statistics, the null hypothesis is that the data are
  920. randomly sampled from the same distribution.
  921. For paired sample statistics, two null hypothesis can be tested:
  922. that the data are paired at random or that the data are assigned to samples
  923. at random.
  924. Parameters
  925. ----------
  926. data : iterable of array-like
  927. Contains the samples, each of which is an array of observations.
  928. Dimensions of sample arrays must be compatible for broadcasting except
  929. along `axis`.
  930. statistic : callable
  931. Statistic for which the p-value of the hypothesis test is to be
  932. calculated. `statistic` must be a callable that accepts samples
  933. as separate arguments (e.g. ``statistic(*data)``) and returns the
  934. resulting statistic.
  935. If `vectorized` is set ``True``, `statistic` must also accept a keyword
  936. argument `axis` and be vectorized to compute the statistic along the
  937. provided `axis` of the sample arrays.
  938. permutation_type : {'independent', 'samples', 'pairings'}, optional
  939. The type of permutations to be performed, in accordance with the
  940. null hypothesis. The first two permutation types are for paired sample
  941. statistics, in which all samples contain the same number of
  942. observations and observations with corresponding indices along `axis`
  943. are considered to be paired; the third is for independent sample
  944. statistics.
  945. - ``'samples'`` : observations are assigned to different samples
  946. but remain paired with the same observations from other samples.
  947. This permutation type is appropriate for paired sample hypothesis
  948. tests such as the Wilcoxon signed-rank test and the paired t-test.
  949. - ``'pairings'`` : observations are paired with different observations,
  950. but they remain within the same sample. This permutation type is
  951. appropriate for association/correlation tests with statistics such
  952. as Spearman's :math:`\rho`, Kendall's :math:`\tau`, and Pearson's
  953. :math:`r`.
  954. - ``'independent'`` (default) : observations are assigned to different
  955. samples. Samples may contain different numbers of observations. This
  956. permutation type is appropriate for independent sample hypothesis
  957. tests such as the Mann-Whitney :math:`U` test and the independent
  958. sample t-test.
  959. Please see the Notes section below for more detailed descriptions
  960. of the permutation types.
  961. vectorized : bool, optional
  962. If `vectorized` is set ``False``, `statistic` will not be passed
  963. keyword argument `axis` and is expected to calculate the statistic
  964. only for 1D samples. If ``True``, `statistic` will be passed keyword
  965. argument `axis` and is expected to calculate the statistic along `axis`
  966. when passed an ND sample array. If ``None`` (default), `vectorized`
  967. will be set ``True`` if ``axis`` is a parameter of `statistic`. Use
  968. of a vectorized statistic typically reduces computation time.
  969. n_resamples : int or np.inf, default: 9999
  970. Number of random permutations (resamples) used to approximate the null
  971. distribution. If greater than or equal to the number of distinct
  972. permutations, the exact null distribution will be computed.
  973. Note that the number of distinct permutations grows very rapidly with
  974. the sizes of samples, so exact tests are feasible only for very small
  975. data sets.
  976. batch : int, optional
  977. The number of permutations to process in each call to `statistic`.
  978. Memory usage is O(`batch`*``n``), where ``n`` is the total size
  979. of all samples, regardless of the value of `vectorized`. Default is
  980. ``None``, in which case ``batch`` is the number of permutations.
  981. alternative : {'two-sided', 'less', 'greater'}, optional
  982. The alternative hypothesis for which the p-value is calculated.
  983. For each alternative, the p-value is defined for exact tests as
  984. follows.
  985. - ``'greater'`` : the percentage of the null distribution that is
  986. greater than or equal to the observed value of the test statistic.
  987. - ``'less'`` : the percentage of the null distribution that is
  988. less than or equal to the observed value of the test statistic.
  989. - ``'two-sided'`` (default) : twice the smaller of the p-values above.
  990. Note that p-values for randomized tests are calculated according to the
  991. conservative (over-estimated) approximation suggested in [2]_ and [3]_
  992. rather than the unbiased estimator suggested in [4]_. That is, when
  993. calculating the proportion of the randomized null distribution that is
  994. as extreme as the observed value of the test statistic, the values in
  995. the numerator and denominator are both increased by one. An
  996. interpretation of this adjustment is that the observed value of the
  997. test statistic is always included as an element of the randomized
  998. null distribution.
  999. The convention used for two-sided p-values is not universal;
  1000. the observed test statistic and null distribution are returned in
  1001. case a different definition is preferred.
  1002. axis : int, default: 0
  1003. The axis of the (broadcasted) samples over which to calculate the
  1004. statistic. If samples have a different number of dimensions,
  1005. singleton dimensions are prepended to samples with fewer dimensions
  1006. before `axis` is considered.
  1007. random_state : {None, int, `numpy.random.Generator`,
  1008. `numpy.random.RandomState`}, optional
  1009. Pseudorandom number generator state used to generate permutations.
  1010. If `random_state` is ``None`` (default), the
  1011. `numpy.random.RandomState` singleton is used.
  1012. If `random_state` is an int, a new ``RandomState`` instance is used,
  1013. seeded with `random_state`.
  1014. If `random_state` is already a ``Generator`` or ``RandomState``
  1015. instance then that instance is used.
  1016. Returns
  1017. -------
  1018. statistic : float or ndarray
  1019. The observed test statistic of the data.
  1020. pvalue : float or ndarray
  1021. The p-value for the given alternative.
  1022. null_distribution : ndarray
  1023. The values of the test statistic generated under the null hypothesis.
  1024. Notes
  1025. -----
  1026. The three types of permutation tests supported by this function are
  1027. described below.
  1028. **Unpaired statistics** (``permutation_type='independent'``):
  1029. The null hypothesis associated with this permutation type is that all
  1030. observations are sampled from the same underlying distribution and that
  1031. they have been assigned to one of the samples at random.
  1032. Suppose ``data`` contains two samples; e.g. ``a, b = data``.
  1033. When ``1 < n_resamples < binom(n, k)``, where
  1034. * ``k`` is the number of observations in ``a``,
  1035. * ``n`` is the total number of observations in ``a`` and ``b``, and
  1036. * ``binom(n, k)`` is the binomial coefficient (``n`` choose ``k``),
  1037. the data are pooled (concatenated), randomly assigned to either the first
  1038. or second sample, and the statistic is calculated. This process is
  1039. performed repeatedly, `permutation` times, generating a distribution of the
  1040. statistic under the null hypothesis. The statistic of the original
  1041. data is compared to this distribution to determine the p-value.
  1042. When ``n_resamples >= binom(n, k)``, an exact test is performed: the data
  1043. are *partitioned* between the samples in each distinct way exactly once,
  1044. and the exact null distribution is formed.
  1045. Note that for a given partitioning of the data between the samples,
  1046. only one ordering/permutation of the data *within* each sample is
  1047. considered. For statistics that do not depend on the order of the data
  1048. within samples, this dramatically reduces computational cost without
  1049. affecting the shape of the null distribution (because the frequency/count
  1050. of each value is affected by the same factor).
  1051. For ``a = [a1, a2, a3, a4]`` and ``b = [b1, b2, b3]``, an example of this
  1052. permutation type is ``x = [b3, a1, a2, b2]`` and ``y = [a4, b1, a3]``.
  1053. Because only one ordering/permutation of the data *within* each sample
  1054. is considered in an exact test, a resampling like ``x = [b3, a1, b2, a2]``
  1055. and ``y = [a4, a3, b1]`` would *not* be considered distinct from the
  1056. example above.
  1057. ``permutation_type='independent'`` does not support one-sample statistics,
  1058. but it can be applied to statistics with more than two samples. In this
  1059. case, if ``n`` is an array of the number of observations within each
  1060. sample, the number of distinct partitions is::
  1061. np.product([binom(sum(n[i:]), sum(n[i+1:])) for i in range(len(n)-1)])
  1062. **Paired statistics, permute pairings** (``permutation_type='pairings'``):
  1063. The null hypothesis associated with this permutation type is that
  1064. observations within each sample are drawn from the same underlying
  1065. distribution and that pairings with elements of other samples are
  1066. assigned at random.
  1067. Suppose ``data`` contains only one sample; e.g. ``a, = data``, and we
  1068. wish to consider all possible pairings of elements of ``a`` with elements
  1069. of a second sample, ``b``. Let ``n`` be the number of observations in
  1070. ``a``, which must also equal the number of observations in ``b``.
  1071. When ``1 < n_resamples < factorial(n)``, the elements of ``a`` are
  1072. randomly permuted. The user-supplied statistic accepts one data argument,
  1073. say ``a_perm``, and calculates the statistic considering ``a_perm`` and
  1074. ``b``. This process is performed repeatedly, `permutation` times,
  1075. generating a distribution of the statistic under the null hypothesis.
  1076. The statistic of the original data is compared to this distribution to
  1077. determine the p-value.
  1078. When ``n_resamples >= factorial(n)``, an exact test is performed:
  1079. ``a`` is permuted in each distinct way exactly once. Therefore, the
  1080. `statistic` is computed for each unique pairing of samples between ``a``
  1081. and ``b`` exactly once.
  1082. For ``a = [a1, a2, a3]`` and ``b = [b1, b2, b3]``, an example of this
  1083. permutation type is ``a_perm = [a3, a1, a2]`` while ``b`` is left
  1084. in its original order.
  1085. ``permutation_type='pairings'`` supports ``data`` containing any number
  1086. of samples, each of which must contain the same number of observations.
  1087. All samples provided in ``data`` are permuted *independently*. Therefore,
  1088. if ``m`` is the number of samples and ``n`` is the number of observations
  1089. within each sample, then the number of permutations in an exact test is::
  1090. factorial(n)**m
  1091. Note that if a two-sample statistic, for example, does not inherently
  1092. depend on the order in which observations are provided - only on the
  1093. *pairings* of observations - then only one of the two samples should be
  1094. provided in ``data``. This dramatically reduces computational cost without
  1095. affecting the shape of the null distribution (because the frequency/count
  1096. of each value is affected by the same factor).
  1097. **Paired statistics, permute samples** (``permutation_type='samples'``):
  1098. The null hypothesis associated with this permutation type is that
  1099. observations within each pair are drawn from the same underlying
  1100. distribution and that the sample to which they are assigned is random.
  1101. Suppose ``data`` contains two samples; e.g. ``a, b = data``.
  1102. Let ``n`` be the number of observations in ``a``, which must also equal
  1103. the number of observations in ``b``.
  1104. When ``1 < n_resamples < 2**n``, the elements of ``a`` are ``b`` are
  1105. randomly swapped between samples (maintaining their pairings) and the
  1106. statistic is calculated. This process is performed repeatedly,
  1107. `permutation` times, generating a distribution of the statistic under the
  1108. null hypothesis. The statistic of the original data is compared to this
  1109. distribution to determine the p-value.
  1110. When ``n_resamples >= 2**n``, an exact test is performed: the observations
  1111. are assigned to the two samples in each distinct way (while maintaining
  1112. pairings) exactly once.
  1113. For ``a = [a1, a2, a3]`` and ``b = [b1, b2, b3]``, an example of this
  1114. permutation type is ``x = [b1, a2, b3]`` and ``y = [a1, b2, a3]``.
  1115. ``permutation_type='samples'`` supports ``data`` containing any number
  1116. of samples, each of which must contain the same number of observations.
  1117. If ``data`` contains more than one sample, paired observations within
  1118. ``data`` are exchanged between samples *independently*. Therefore, if ``m``
  1119. is the number of samples and ``n`` is the number of observations within
  1120. each sample, then the number of permutations in an exact test is::
  1121. factorial(m)**n
  1122. Several paired-sample statistical tests, such as the Wilcoxon signed rank
  1123. test and paired-sample t-test, can be performed considering only the
  1124. *difference* between two paired elements. Accordingly, if ``data`` contains
  1125. only one sample, then the null distribution is formed by independently
  1126. changing the *sign* of each observation.
  1127. .. warning::
  1128. The p-value is calculated by counting the elements of the null
  1129. distribution that are as extreme or more extreme than the observed
  1130. value of the statistic. Due to the use of finite precision arithmetic,
  1131. some statistic functions return numerically distinct values when the
  1132. theoretical values would be exactly equal. In some cases, this could
  1133. lead to a large error in the calculated p-value. `permutation_test`
  1134. guards against this by considering elements in the null distribution
  1135. that are "close" (within a factor of ``1+1e-14``) to the observed
  1136. value of the test statistic as equal to the observed value of the
  1137. test statistic. However, the user is advised to inspect the null
  1138. distribution to assess whether this method of comparison is
  1139. appropriate, and if not, calculate the p-value manually. See example
  1140. below.
  1141. References
  1142. ----------
  1143. .. [1] R. A. Fisher. The Design of Experiments, 6th Ed (1951).
  1144. .. [2] B. Phipson and G. K. Smyth. "Permutation P-values Should Never Be
  1145. Zero: Calculating Exact P-values When Permutations Are Randomly Drawn."
  1146. Statistical Applications in Genetics and Molecular Biology 9.1 (2010).
  1147. .. [3] M. D. Ernst. "Permutation Methods: A Basis for Exact Inference".
  1148. Statistical Science (2004).
  1149. .. [4] B. Efron and R. J. Tibshirani. An Introduction to the Bootstrap
  1150. (1993).
  1151. Examples
  1152. --------
  1153. Suppose we wish to test whether two samples are drawn from the same
  1154. distribution. Assume that the underlying distributions are unknown to us,
  1155. and that before observing the data, we hypothesized that the mean of the
  1156. first sample would be less than that of the second sample. We decide that
  1157. we will use the difference between the sample means as a test statistic,
  1158. and we will consider a p-value of 0.05 to be statistically significant.
  1159. For efficiency, we write the function defining the test statistic in a
  1160. vectorized fashion: the samples ``x`` and ``y`` can be ND arrays, and the
  1161. statistic will be calculated for each axis-slice along `axis`.
  1162. >>> import numpy as np
  1163. >>> def statistic(x, y, axis):
  1164. ... return np.mean(x, axis=axis) - np.mean(y, axis=axis)
  1165. After collecting our data, we calculate the observed value of the test
  1166. statistic.
  1167. >>> from scipy.stats import norm
  1168. >>> rng = np.random.default_rng()
  1169. >>> x = norm.rvs(size=5, random_state=rng)
  1170. >>> y = norm.rvs(size=6, loc = 3, random_state=rng)
  1171. >>> statistic(x, y, 0)
  1172. -3.5411688580987266
  1173. Indeed, the test statistic is negative, suggesting that the true mean of
  1174. the distribution underlying ``x`` is less than that of the distribution
  1175. underlying ``y``. To determine the probability of this occuring by chance
  1176. if the two samples were drawn from the same distribution, we perform
  1177. a permutation test.
  1178. >>> from scipy.stats import permutation_test
  1179. >>> # because our statistic is vectorized, we pass `vectorized=True`
  1180. >>> # `n_resamples=np.inf` indicates that an exact test is to be performed
  1181. >>> res = permutation_test((x, y), statistic, vectorized=True,
  1182. ... n_resamples=np.inf, alternative='less')
  1183. >>> print(res.statistic)
  1184. -3.5411688580987266
  1185. >>> print(res.pvalue)
  1186. 0.004329004329004329
  1187. The probability of obtaining a test statistic less than or equal to the
  1188. observed value under the null hypothesis is 0.4329%. This is less than our
  1189. chosen threshold of 5%, so we consider this to be significant evidence
  1190. against the null hypothesis in favor of the alternative.
  1191. Because the size of the samples above was small, `permutation_test` could
  1192. perform an exact test. For larger samples, we resort to a randomized
  1193. permutation test.
  1194. >>> x = norm.rvs(size=100, random_state=rng)
  1195. >>> y = norm.rvs(size=120, loc=0.3, random_state=rng)
  1196. >>> res = permutation_test((x, y), statistic, n_resamples=100000,
  1197. ... vectorized=True, alternative='less',
  1198. ... random_state=rng)
  1199. >>> print(res.statistic)
  1200. -0.5230459671240913
  1201. >>> print(res.pvalue)
  1202. 0.00016999830001699983
  1203. The approximate probability of obtaining a test statistic less than or
  1204. equal to the observed value under the null hypothesis is 0.0225%. This is
  1205. again less than our chosen threshold of 5%, so again we have significant
  1206. evidence to reject the null hypothesis in favor of the alternative.
  1207. For large samples and number of permutations, the result is comparable to
  1208. that of the corresponding asymptotic test, the independent sample t-test.
  1209. >>> from scipy.stats import ttest_ind
  1210. >>> res_asymptotic = ttest_ind(x, y, alternative='less')
  1211. >>> print(res_asymptotic.pvalue)
  1212. 0.00012688101537979522
  1213. The permutation distribution of the test statistic is provided for
  1214. further investigation.
  1215. >>> import matplotlib.pyplot as plt
  1216. >>> plt.hist(res.null_distribution, bins=50)
  1217. >>> plt.title("Permutation distribution of test statistic")
  1218. >>> plt.xlabel("Value of Statistic")
  1219. >>> plt.ylabel("Frequency")
  1220. >>> plt.show()
  1221. Inspection of the null distribution is essential if the statistic suffers
  1222. from inaccuracy due to limited machine precision. Consider the following
  1223. case:
  1224. >>> from scipy.stats import pearsonr
  1225. >>> x = [1, 2, 4, 3]
  1226. >>> y = [2, 4, 6, 8]
  1227. >>> def statistic(x, y):
  1228. ... return pearsonr(x, y).statistic
  1229. >>> res = permutation_test((x, y), statistic, vectorized=False,
  1230. ... permutation_type='pairings',
  1231. ... alternative='greater')
  1232. >>> r, pvalue, null = res.statistic, res.pvalue, res.null_distribution
  1233. In this case, some elements of the null distribution differ from the
  1234. observed value of the correlation coefficient ``r`` due to numerical noise.
  1235. We manually inspect the elements of the null distribution that are nearly
  1236. the same as the observed value of the test statistic.
  1237. >>> r
  1238. 0.8
  1239. >>> unique = np.unique(null)
  1240. >>> unique
  1241. array([-1. , -0.8, -0.8, -0.6, -0.4, -0.2, -0.2, 0. , 0.2, 0.2, 0.4,
  1242. 0.6, 0.8, 0.8, 1. ]) # may vary
  1243. >>> unique[np.isclose(r, unique)].tolist()
  1244. [0.7999999999999999, 0.8]
  1245. If `permutation_test` were to perform the comparison naively, the
  1246. elements of the null distribution with value ``0.7999999999999999`` would
  1247. not be considered as extreme or more extreme as the observed value of the
  1248. statistic, so the calculated p-value would be too small.
  1249. >>> incorrect_pvalue = np.count_nonzero(null >= r) / len(null)
  1250. >>> incorrect_pvalue
  1251. 0.1111111111111111 # may vary
  1252. Instead, `permutation_test` treats elements of the null distribution that
  1253. are within ``max(1e-14, abs(r)*1e-14)`` of the observed value of the
  1254. statistic ``r`` to be equal to ``r``.
  1255. >>> correct_pvalue = np.count_nonzero(null >= r - 1e-14) / len(null)
  1256. >>> correct_pvalue
  1257. 0.16666666666666666
  1258. >>> res.pvalue == correct_pvalue
  1259. True
  1260. This method of comparison is expected to be accurate in most practical
  1261. situations, but the user is advised to assess this by inspecting the
  1262. elements of the null distribution that are close to the observed value
  1263. of the statistic. Also, consider the use of statistics that can be
  1264. calculated using exact arithmetic (e.g. integer statistics).
  1265. """
  1266. args = _permutation_test_iv(data, statistic, permutation_type, vectorized,
  1267. n_resamples, batch, alternative, axis,
  1268. random_state)
  1269. (data, statistic, permutation_type, vectorized, n_resamples, batch,
  1270. alternative, axis, random_state) = args
  1271. observed = statistic(*data, axis=-1)
  1272. null_calculators = {"pairings": _calculate_null_pairings,
  1273. "samples": _calculate_null_samples,
  1274. "independent": _calculate_null_both}
  1275. null_calculator_args = (data, statistic, n_resamples,
  1276. batch, random_state)
  1277. calculate_null = null_calculators[permutation_type]
  1278. null_distribution, n_resamples, exact_test = (
  1279. calculate_null(*null_calculator_args))
  1280. # See References [2] and [3]
  1281. adjustment = 0 if exact_test else 1
  1282. # relative tolerance for detecting numerically distinct but
  1283. # theoretically equal values in the null distribution
  1284. eps = 1e-14
  1285. gamma = np.maximum(eps, np.abs(eps * observed))
  1286. def less(null_distribution, observed):
  1287. cmps = null_distribution <= observed + gamma
  1288. pvalues = (cmps.sum(axis=0) + adjustment) / (n_resamples + adjustment)
  1289. return pvalues
  1290. def greater(null_distribution, observed):
  1291. cmps = null_distribution >= observed - gamma
  1292. pvalues = (cmps.sum(axis=0) + adjustment) / (n_resamples + adjustment)
  1293. return pvalues
  1294. def two_sided(null_distribution, observed):
  1295. pvalues_less = less(null_distribution, observed)
  1296. pvalues_greater = greater(null_distribution, observed)
  1297. pvalues = np.minimum(pvalues_less, pvalues_greater) * 2
  1298. return pvalues
  1299. compare = {"less": less,
  1300. "greater": greater,
  1301. "two-sided": two_sided}
  1302. pvalues = compare[alternative](null_distribution, observed)
  1303. pvalues = np.clip(pvalues, 0, 1)
  1304. return PermutationTestResult(observed, pvalues, null_distribution)