scipy 1.16.2__cp313-cp313t-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1530) hide show
  1. scipy/__config__.py +161 -0
  2. scipy/__init__.py +150 -0
  3. scipy/_cyutility.cp313t-win_arm64.lib +0 -0
  4. scipy/_cyutility.cp313t-win_arm64.pyd +0 -0
  5. scipy/_distributor_init.py +18 -0
  6. scipy/_lib/__init__.py +14 -0
  7. scipy/_lib/_array_api.py +931 -0
  8. scipy/_lib/_array_api_compat_vendor.py +9 -0
  9. scipy/_lib/_array_api_no_0d.py +103 -0
  10. scipy/_lib/_bunch.py +229 -0
  11. scipy/_lib/_ccallback.py +251 -0
  12. scipy/_lib/_ccallback_c.cp313t-win_arm64.lib +0 -0
  13. scipy/_lib/_ccallback_c.cp313t-win_arm64.pyd +0 -0
  14. scipy/_lib/_disjoint_set.py +254 -0
  15. scipy/_lib/_docscrape.py +761 -0
  16. scipy/_lib/_elementwise_iterative_method.py +346 -0
  17. scipy/_lib/_fpumode.cp313t-win_arm64.lib +0 -0
  18. scipy/_lib/_fpumode.cp313t-win_arm64.pyd +0 -0
  19. scipy/_lib/_gcutils.py +105 -0
  20. scipy/_lib/_pep440.py +487 -0
  21. scipy/_lib/_sparse.py +41 -0
  22. scipy/_lib/_test_ccallback.cp313t-win_arm64.lib +0 -0
  23. scipy/_lib/_test_ccallback.cp313t-win_arm64.pyd +0 -0
  24. scipy/_lib/_test_deprecation_call.cp313t-win_arm64.lib +0 -0
  25. scipy/_lib/_test_deprecation_call.cp313t-win_arm64.pyd +0 -0
  26. scipy/_lib/_test_deprecation_def.cp313t-win_arm64.lib +0 -0
  27. scipy/_lib/_test_deprecation_def.cp313t-win_arm64.pyd +0 -0
  28. scipy/_lib/_testutils.py +373 -0
  29. scipy/_lib/_threadsafety.py +58 -0
  30. scipy/_lib/_tmpdirs.py +86 -0
  31. scipy/_lib/_uarray/LICENSE +29 -0
  32. scipy/_lib/_uarray/__init__.py +116 -0
  33. scipy/_lib/_uarray/_backend.py +707 -0
  34. scipy/_lib/_uarray/_uarray.cp313t-win_arm64.lib +0 -0
  35. scipy/_lib/_uarray/_uarray.cp313t-win_arm64.pyd +0 -0
  36. scipy/_lib/_util.py +1283 -0
  37. scipy/_lib/array_api_compat/__init__.py +22 -0
  38. scipy/_lib/array_api_compat/_internal.py +59 -0
  39. scipy/_lib/array_api_compat/common/__init__.py +1 -0
  40. scipy/_lib/array_api_compat/common/_aliases.py +727 -0
  41. scipy/_lib/array_api_compat/common/_fft.py +213 -0
  42. scipy/_lib/array_api_compat/common/_helpers.py +1058 -0
  43. scipy/_lib/array_api_compat/common/_linalg.py +232 -0
  44. scipy/_lib/array_api_compat/common/_typing.py +192 -0
  45. scipy/_lib/array_api_compat/cupy/__init__.py +13 -0
  46. scipy/_lib/array_api_compat/cupy/_aliases.py +156 -0
  47. scipy/_lib/array_api_compat/cupy/_info.py +336 -0
  48. scipy/_lib/array_api_compat/cupy/_typing.py +31 -0
  49. scipy/_lib/array_api_compat/cupy/fft.py +36 -0
  50. scipy/_lib/array_api_compat/cupy/linalg.py +49 -0
  51. scipy/_lib/array_api_compat/dask/__init__.py +0 -0
  52. scipy/_lib/array_api_compat/dask/array/__init__.py +12 -0
  53. scipy/_lib/array_api_compat/dask/array/_aliases.py +376 -0
  54. scipy/_lib/array_api_compat/dask/array/_info.py +416 -0
  55. scipy/_lib/array_api_compat/dask/array/fft.py +21 -0
  56. scipy/_lib/array_api_compat/dask/array/linalg.py +72 -0
  57. scipy/_lib/array_api_compat/numpy/__init__.py +28 -0
  58. scipy/_lib/array_api_compat/numpy/_aliases.py +190 -0
  59. scipy/_lib/array_api_compat/numpy/_info.py +366 -0
  60. scipy/_lib/array_api_compat/numpy/_typing.py +30 -0
  61. scipy/_lib/array_api_compat/numpy/fft.py +35 -0
  62. scipy/_lib/array_api_compat/numpy/linalg.py +143 -0
  63. scipy/_lib/array_api_compat/torch/__init__.py +22 -0
  64. scipy/_lib/array_api_compat/torch/_aliases.py +855 -0
  65. scipy/_lib/array_api_compat/torch/_info.py +369 -0
  66. scipy/_lib/array_api_compat/torch/_typing.py +3 -0
  67. scipy/_lib/array_api_compat/torch/fft.py +85 -0
  68. scipy/_lib/array_api_compat/torch/linalg.py +121 -0
  69. scipy/_lib/array_api_extra/__init__.py +38 -0
  70. scipy/_lib/array_api_extra/_delegation.py +171 -0
  71. scipy/_lib/array_api_extra/_lib/__init__.py +1 -0
  72. scipy/_lib/array_api_extra/_lib/_at.py +463 -0
  73. scipy/_lib/array_api_extra/_lib/_backends.py +46 -0
  74. scipy/_lib/array_api_extra/_lib/_funcs.py +937 -0
  75. scipy/_lib/array_api_extra/_lib/_lazy.py +357 -0
  76. scipy/_lib/array_api_extra/_lib/_testing.py +278 -0
  77. scipy/_lib/array_api_extra/_lib/_utils/__init__.py +1 -0
  78. scipy/_lib/array_api_extra/_lib/_utils/_compat.py +74 -0
  79. scipy/_lib/array_api_extra/_lib/_utils/_compat.pyi +45 -0
  80. scipy/_lib/array_api_extra/_lib/_utils/_helpers.py +559 -0
  81. scipy/_lib/array_api_extra/_lib/_utils/_typing.py +10 -0
  82. scipy/_lib/array_api_extra/_lib/_utils/_typing.pyi +105 -0
  83. scipy/_lib/array_api_extra/testing.py +359 -0
  84. scipy/_lib/cobyqa/__init__.py +20 -0
  85. scipy/_lib/cobyqa/framework.py +1240 -0
  86. scipy/_lib/cobyqa/main.py +1506 -0
  87. scipy/_lib/cobyqa/models.py +1529 -0
  88. scipy/_lib/cobyqa/problem.py +1296 -0
  89. scipy/_lib/cobyqa/settings.py +132 -0
  90. scipy/_lib/cobyqa/subsolvers/__init__.py +14 -0
  91. scipy/_lib/cobyqa/subsolvers/geometry.py +387 -0
  92. scipy/_lib/cobyqa/subsolvers/optim.py +1203 -0
  93. scipy/_lib/cobyqa/utils/__init__.py +18 -0
  94. scipy/_lib/cobyqa/utils/exceptions.py +22 -0
  95. scipy/_lib/cobyqa/utils/math.py +77 -0
  96. scipy/_lib/cobyqa/utils/versions.py +67 -0
  97. scipy/_lib/decorator.py +399 -0
  98. scipy/_lib/deprecation.py +274 -0
  99. scipy/_lib/doccer.py +366 -0
  100. scipy/_lib/messagestream.cp313t-win_arm64.lib +0 -0
  101. scipy/_lib/messagestream.cp313t-win_arm64.pyd +0 -0
  102. scipy/_lib/pyprima/__init__.py +212 -0
  103. scipy/_lib/pyprima/cobyla/__init__.py +0 -0
  104. scipy/_lib/pyprima/cobyla/cobyla.py +559 -0
  105. scipy/_lib/pyprima/cobyla/cobylb.py +714 -0
  106. scipy/_lib/pyprima/cobyla/geometry.py +226 -0
  107. scipy/_lib/pyprima/cobyla/initialize.py +215 -0
  108. scipy/_lib/pyprima/cobyla/trustregion.py +492 -0
  109. scipy/_lib/pyprima/cobyla/update.py +289 -0
  110. scipy/_lib/pyprima/common/__init__.py +0 -0
  111. scipy/_lib/pyprima/common/_bounds.py +34 -0
  112. scipy/_lib/pyprima/common/_linear_constraints.py +46 -0
  113. scipy/_lib/pyprima/common/_nonlinear_constraints.py +54 -0
  114. scipy/_lib/pyprima/common/_project.py +173 -0
  115. scipy/_lib/pyprima/common/checkbreak.py +93 -0
  116. scipy/_lib/pyprima/common/consts.py +47 -0
  117. scipy/_lib/pyprima/common/evaluate.py +99 -0
  118. scipy/_lib/pyprima/common/history.py +38 -0
  119. scipy/_lib/pyprima/common/infos.py +30 -0
  120. scipy/_lib/pyprima/common/linalg.py +435 -0
  121. scipy/_lib/pyprima/common/message.py +290 -0
  122. scipy/_lib/pyprima/common/powalg.py +131 -0
  123. scipy/_lib/pyprima/common/preproc.py +277 -0
  124. scipy/_lib/pyprima/common/present.py +5 -0
  125. scipy/_lib/pyprima/common/ratio.py +54 -0
  126. scipy/_lib/pyprima/common/redrho.py +47 -0
  127. scipy/_lib/pyprima/common/selectx.py +296 -0
  128. scipy/_lib/tests/__init__.py +0 -0
  129. scipy/_lib/tests/test__gcutils.py +110 -0
  130. scipy/_lib/tests/test__pep440.py +67 -0
  131. scipy/_lib/tests/test__testutils.py +32 -0
  132. scipy/_lib/tests/test__threadsafety.py +51 -0
  133. scipy/_lib/tests/test__util.py +641 -0
  134. scipy/_lib/tests/test_array_api.py +322 -0
  135. scipy/_lib/tests/test_bunch.py +169 -0
  136. scipy/_lib/tests/test_ccallback.py +196 -0
  137. scipy/_lib/tests/test_config.py +45 -0
  138. scipy/_lib/tests/test_deprecation.py +10 -0
  139. scipy/_lib/tests/test_doccer.py +143 -0
  140. scipy/_lib/tests/test_import_cycles.py +18 -0
  141. scipy/_lib/tests/test_public_api.py +482 -0
  142. scipy/_lib/tests/test_scipy_version.py +28 -0
  143. scipy/_lib/tests/test_tmpdirs.py +48 -0
  144. scipy/_lib/tests/test_warnings.py +137 -0
  145. scipy/_lib/uarray.py +31 -0
  146. scipy/cluster/__init__.py +31 -0
  147. scipy/cluster/_hierarchy.cp313t-win_arm64.lib +0 -0
  148. scipy/cluster/_hierarchy.cp313t-win_arm64.pyd +0 -0
  149. scipy/cluster/_optimal_leaf_ordering.cp313t-win_arm64.lib +0 -0
  150. scipy/cluster/_optimal_leaf_ordering.cp313t-win_arm64.pyd +0 -0
  151. scipy/cluster/_vq.cp313t-win_arm64.lib +0 -0
  152. scipy/cluster/_vq.cp313t-win_arm64.pyd +0 -0
  153. scipy/cluster/hierarchy.py +4348 -0
  154. scipy/cluster/tests/__init__.py +0 -0
  155. scipy/cluster/tests/hierarchy_test_data.py +145 -0
  156. scipy/cluster/tests/test_disjoint_set.py +202 -0
  157. scipy/cluster/tests/test_hierarchy.py +1238 -0
  158. scipy/cluster/tests/test_vq.py +434 -0
  159. scipy/cluster/vq.py +832 -0
  160. scipy/conftest.py +683 -0
  161. scipy/constants/__init__.py +358 -0
  162. scipy/constants/_codata.py +2266 -0
  163. scipy/constants/_constants.py +369 -0
  164. scipy/constants/codata.py +21 -0
  165. scipy/constants/constants.py +53 -0
  166. scipy/constants/tests/__init__.py +0 -0
  167. scipy/constants/tests/test_codata.py +78 -0
  168. scipy/constants/tests/test_constants.py +83 -0
  169. scipy/datasets/__init__.py +90 -0
  170. scipy/datasets/_download_all.py +71 -0
  171. scipy/datasets/_fetchers.py +225 -0
  172. scipy/datasets/_registry.py +26 -0
  173. scipy/datasets/_utils.py +81 -0
  174. scipy/datasets/tests/__init__.py +0 -0
  175. scipy/datasets/tests/test_data.py +128 -0
  176. scipy/differentiate/__init__.py +27 -0
  177. scipy/differentiate/_differentiate.py +1129 -0
  178. scipy/differentiate/tests/__init__.py +0 -0
  179. scipy/differentiate/tests/test_differentiate.py +694 -0
  180. scipy/fft/__init__.py +114 -0
  181. scipy/fft/_backend.py +196 -0
  182. scipy/fft/_basic.py +1650 -0
  183. scipy/fft/_basic_backend.py +197 -0
  184. scipy/fft/_debug_backends.py +22 -0
  185. scipy/fft/_fftlog.py +223 -0
  186. scipy/fft/_fftlog_backend.py +200 -0
  187. scipy/fft/_helper.py +348 -0
  188. scipy/fft/_pocketfft/LICENSE.md +25 -0
  189. scipy/fft/_pocketfft/__init__.py +9 -0
  190. scipy/fft/_pocketfft/basic.py +251 -0
  191. scipy/fft/_pocketfft/helper.py +249 -0
  192. scipy/fft/_pocketfft/pypocketfft.cp313t-win_arm64.lib +0 -0
  193. scipy/fft/_pocketfft/pypocketfft.cp313t-win_arm64.pyd +0 -0
  194. scipy/fft/_pocketfft/realtransforms.py +109 -0
  195. scipy/fft/_pocketfft/tests/__init__.py +0 -0
  196. scipy/fft/_pocketfft/tests/test_basic.py +1011 -0
  197. scipy/fft/_pocketfft/tests/test_real_transforms.py +505 -0
  198. scipy/fft/_realtransforms.py +706 -0
  199. scipy/fft/_realtransforms_backend.py +63 -0
  200. scipy/fft/tests/__init__.py +0 -0
  201. scipy/fft/tests/mock_backend.py +96 -0
  202. scipy/fft/tests/test_backend.py +98 -0
  203. scipy/fft/tests/test_basic.py +504 -0
  204. scipy/fft/tests/test_fftlog.py +215 -0
  205. scipy/fft/tests/test_helper.py +558 -0
  206. scipy/fft/tests/test_multithreading.py +84 -0
  207. scipy/fft/tests/test_real_transforms.py +247 -0
  208. scipy/fftpack/__init__.py +103 -0
  209. scipy/fftpack/_basic.py +428 -0
  210. scipy/fftpack/_helper.py +115 -0
  211. scipy/fftpack/_pseudo_diffs.py +554 -0
  212. scipy/fftpack/_realtransforms.py +598 -0
  213. scipy/fftpack/basic.py +20 -0
  214. scipy/fftpack/convolve.cp313t-win_arm64.lib +0 -0
  215. scipy/fftpack/convolve.cp313t-win_arm64.pyd +0 -0
  216. scipy/fftpack/helper.py +19 -0
  217. scipy/fftpack/pseudo_diffs.py +22 -0
  218. scipy/fftpack/realtransforms.py +19 -0
  219. scipy/fftpack/tests/__init__.py +0 -0
  220. scipy/fftpack/tests/fftw_double_ref.npz +0 -0
  221. scipy/fftpack/tests/fftw_longdouble_ref.npz +0 -0
  222. scipy/fftpack/tests/fftw_single_ref.npz +0 -0
  223. scipy/fftpack/tests/test.npz +0 -0
  224. scipy/fftpack/tests/test_basic.py +877 -0
  225. scipy/fftpack/tests/test_helper.py +54 -0
  226. scipy/fftpack/tests/test_import.py +33 -0
  227. scipy/fftpack/tests/test_pseudo_diffs.py +388 -0
  228. scipy/fftpack/tests/test_real_transforms.py +836 -0
  229. scipy/integrate/__init__.py +122 -0
  230. scipy/integrate/_bvp.py +1160 -0
  231. scipy/integrate/_cubature.py +729 -0
  232. scipy/integrate/_dop.cp313t-win_arm64.lib +0 -0
  233. scipy/integrate/_dop.cp313t-win_arm64.pyd +0 -0
  234. scipy/integrate/_ivp/__init__.py +8 -0
  235. scipy/integrate/_ivp/base.py +290 -0
  236. scipy/integrate/_ivp/bdf.py +478 -0
  237. scipy/integrate/_ivp/common.py +451 -0
  238. scipy/integrate/_ivp/dop853_coefficients.py +193 -0
  239. scipy/integrate/_ivp/ivp.py +755 -0
  240. scipy/integrate/_ivp/lsoda.py +224 -0
  241. scipy/integrate/_ivp/radau.py +572 -0
  242. scipy/integrate/_ivp/rk.py +601 -0
  243. scipy/integrate/_ivp/tests/__init__.py +0 -0
  244. scipy/integrate/_ivp/tests/test_ivp.py +1287 -0
  245. scipy/integrate/_ivp/tests/test_rk.py +37 -0
  246. scipy/integrate/_lebedev.py +5450 -0
  247. scipy/integrate/_lsoda.cp313t-win_arm64.lib +0 -0
  248. scipy/integrate/_lsoda.cp313t-win_arm64.pyd +0 -0
  249. scipy/integrate/_ode.py +1395 -0
  250. scipy/integrate/_odepack.cp313t-win_arm64.lib +0 -0
  251. scipy/integrate/_odepack.cp313t-win_arm64.pyd +0 -0
  252. scipy/integrate/_odepack_py.py +273 -0
  253. scipy/integrate/_quad_vec.py +674 -0
  254. scipy/integrate/_quadpack.cp313t-win_arm64.lib +0 -0
  255. scipy/integrate/_quadpack.cp313t-win_arm64.pyd +0 -0
  256. scipy/integrate/_quadpack_py.py +1283 -0
  257. scipy/integrate/_quadrature.py +1336 -0
  258. scipy/integrate/_rules/__init__.py +12 -0
  259. scipy/integrate/_rules/_base.py +518 -0
  260. scipy/integrate/_rules/_gauss_kronrod.py +202 -0
  261. scipy/integrate/_rules/_gauss_legendre.py +62 -0
  262. scipy/integrate/_rules/_genz_malik.py +210 -0
  263. scipy/integrate/_tanhsinh.py +1385 -0
  264. scipy/integrate/_test_multivariate.cp313t-win_arm64.lib +0 -0
  265. scipy/integrate/_test_multivariate.cp313t-win_arm64.pyd +0 -0
  266. scipy/integrate/_test_odeint_banded.cp313t-win_arm64.lib +0 -0
  267. scipy/integrate/_test_odeint_banded.cp313t-win_arm64.pyd +0 -0
  268. scipy/integrate/_vode.cp313t-win_arm64.lib +0 -0
  269. scipy/integrate/_vode.cp313t-win_arm64.pyd +0 -0
  270. scipy/integrate/dop.py +15 -0
  271. scipy/integrate/lsoda.py +15 -0
  272. scipy/integrate/odepack.py +17 -0
  273. scipy/integrate/quadpack.py +23 -0
  274. scipy/integrate/tests/__init__.py +0 -0
  275. scipy/integrate/tests/test__quad_vec.py +211 -0
  276. scipy/integrate/tests/test_banded_ode_solvers.py +305 -0
  277. scipy/integrate/tests/test_bvp.py +714 -0
  278. scipy/integrate/tests/test_cubature.py +1375 -0
  279. scipy/integrate/tests/test_integrate.py +840 -0
  280. scipy/integrate/tests/test_odeint_jac.py +74 -0
  281. scipy/integrate/tests/test_quadpack.py +680 -0
  282. scipy/integrate/tests/test_quadrature.py +730 -0
  283. scipy/integrate/tests/test_tanhsinh.py +1171 -0
  284. scipy/integrate/vode.py +15 -0
  285. scipy/interpolate/__init__.py +228 -0
  286. scipy/interpolate/_bary_rational.py +715 -0
  287. scipy/interpolate/_bsplines.py +2469 -0
  288. scipy/interpolate/_cubic.py +973 -0
  289. scipy/interpolate/_dfitpack.cp313t-win_arm64.lib +0 -0
  290. scipy/interpolate/_dfitpack.cp313t-win_arm64.pyd +0 -0
  291. scipy/interpolate/_dierckx.cp313t-win_arm64.lib +0 -0
  292. scipy/interpolate/_dierckx.cp313t-win_arm64.pyd +0 -0
  293. scipy/interpolate/_fitpack.cp313t-win_arm64.lib +0 -0
  294. scipy/interpolate/_fitpack.cp313t-win_arm64.pyd +0 -0
  295. scipy/interpolate/_fitpack2.py +2397 -0
  296. scipy/interpolate/_fitpack_impl.py +811 -0
  297. scipy/interpolate/_fitpack_py.py +898 -0
  298. scipy/interpolate/_fitpack_repro.py +996 -0
  299. scipy/interpolate/_interpnd.cp313t-win_arm64.lib +0 -0
  300. scipy/interpolate/_interpnd.cp313t-win_arm64.pyd +0 -0
  301. scipy/interpolate/_interpolate.py +2266 -0
  302. scipy/interpolate/_ndbspline.py +415 -0
  303. scipy/interpolate/_ndgriddata.py +329 -0
  304. scipy/interpolate/_pade.py +67 -0
  305. scipy/interpolate/_polyint.py +1025 -0
  306. scipy/interpolate/_ppoly.cp313t-win_arm64.lib +0 -0
  307. scipy/interpolate/_ppoly.cp313t-win_arm64.pyd +0 -0
  308. scipy/interpolate/_rbf.py +290 -0
  309. scipy/interpolate/_rbfinterp.py +550 -0
  310. scipy/interpolate/_rbfinterp_pythran.cp313t-win_arm64.lib +0 -0
  311. scipy/interpolate/_rbfinterp_pythran.cp313t-win_arm64.pyd +0 -0
  312. scipy/interpolate/_rgi.py +764 -0
  313. scipy/interpolate/_rgi_cython.cp313t-win_arm64.lib +0 -0
  314. scipy/interpolate/_rgi_cython.cp313t-win_arm64.pyd +0 -0
  315. scipy/interpolate/dfitpack.py +24 -0
  316. scipy/interpolate/fitpack.py +31 -0
  317. scipy/interpolate/fitpack2.py +29 -0
  318. scipy/interpolate/interpnd.py +24 -0
  319. scipy/interpolate/interpolate.py +30 -0
  320. scipy/interpolate/ndgriddata.py +23 -0
  321. scipy/interpolate/polyint.py +24 -0
  322. scipy/interpolate/rbf.py +18 -0
  323. scipy/interpolate/tests/__init__.py +0 -0
  324. scipy/interpolate/tests/data/bug-1310.npz +0 -0
  325. scipy/interpolate/tests/data/estimate_gradients_hang.npy +0 -0
  326. scipy/interpolate/tests/data/gcvspl.npz +0 -0
  327. scipy/interpolate/tests/test_bary_rational.py +368 -0
  328. scipy/interpolate/tests/test_bsplines.py +3754 -0
  329. scipy/interpolate/tests/test_fitpack.py +519 -0
  330. scipy/interpolate/tests/test_fitpack2.py +1431 -0
  331. scipy/interpolate/tests/test_gil.py +64 -0
  332. scipy/interpolate/tests/test_interpnd.py +452 -0
  333. scipy/interpolate/tests/test_interpolate.py +2630 -0
  334. scipy/interpolate/tests/test_ndgriddata.py +308 -0
  335. scipy/interpolate/tests/test_pade.py +107 -0
  336. scipy/interpolate/tests/test_polyint.py +972 -0
  337. scipy/interpolate/tests/test_rbf.py +246 -0
  338. scipy/interpolate/tests/test_rbfinterp.py +534 -0
  339. scipy/interpolate/tests/test_rgi.py +1151 -0
  340. scipy/io/__init__.py +116 -0
  341. scipy/io/_fast_matrix_market/__init__.py +600 -0
  342. scipy/io/_fast_matrix_market/_fmm_core.cp313t-win_arm64.lib +0 -0
  343. scipy/io/_fast_matrix_market/_fmm_core.cp313t-win_arm64.pyd +0 -0
  344. scipy/io/_fortran.py +354 -0
  345. scipy/io/_harwell_boeing/__init__.py +7 -0
  346. scipy/io/_harwell_boeing/_fortran_format_parser.py +316 -0
  347. scipy/io/_harwell_boeing/hb.py +571 -0
  348. scipy/io/_harwell_boeing/tests/__init__.py +0 -0
  349. scipy/io/_harwell_boeing/tests/test_fortran_format.py +74 -0
  350. scipy/io/_harwell_boeing/tests/test_hb.py +70 -0
  351. scipy/io/_idl.py +917 -0
  352. scipy/io/_mmio.py +968 -0
  353. scipy/io/_netcdf.py +1104 -0
  354. scipy/io/_test_fortran.cp313t-win_arm64.lib +0 -0
  355. scipy/io/_test_fortran.cp313t-win_arm64.pyd +0 -0
  356. scipy/io/arff/__init__.py +28 -0
  357. scipy/io/arff/_arffread.py +873 -0
  358. scipy/io/arff/arffread.py +19 -0
  359. scipy/io/arff/tests/__init__.py +0 -0
  360. scipy/io/arff/tests/data/iris.arff +225 -0
  361. scipy/io/arff/tests/data/missing.arff +8 -0
  362. scipy/io/arff/tests/data/nodata.arff +11 -0
  363. scipy/io/arff/tests/data/quoted_nominal.arff +13 -0
  364. scipy/io/arff/tests/data/quoted_nominal_spaces.arff +13 -0
  365. scipy/io/arff/tests/data/test1.arff +10 -0
  366. scipy/io/arff/tests/data/test10.arff +8 -0
  367. scipy/io/arff/tests/data/test11.arff +11 -0
  368. scipy/io/arff/tests/data/test2.arff +15 -0
  369. scipy/io/arff/tests/data/test3.arff +6 -0
  370. scipy/io/arff/tests/data/test4.arff +11 -0
  371. scipy/io/arff/tests/data/test5.arff +26 -0
  372. scipy/io/arff/tests/data/test6.arff +12 -0
  373. scipy/io/arff/tests/data/test7.arff +15 -0
  374. scipy/io/arff/tests/data/test8.arff +12 -0
  375. scipy/io/arff/tests/data/test9.arff +14 -0
  376. scipy/io/arff/tests/test_arffread.py +421 -0
  377. scipy/io/harwell_boeing.py +17 -0
  378. scipy/io/idl.py +17 -0
  379. scipy/io/matlab/__init__.py +66 -0
  380. scipy/io/matlab/_byteordercodes.py +75 -0
  381. scipy/io/matlab/_mio.py +375 -0
  382. scipy/io/matlab/_mio4.py +632 -0
  383. scipy/io/matlab/_mio5.py +901 -0
  384. scipy/io/matlab/_mio5_params.py +281 -0
  385. scipy/io/matlab/_mio5_utils.cp313t-win_arm64.lib +0 -0
  386. scipy/io/matlab/_mio5_utils.cp313t-win_arm64.pyd +0 -0
  387. scipy/io/matlab/_mio_utils.cp313t-win_arm64.lib +0 -0
  388. scipy/io/matlab/_mio_utils.cp313t-win_arm64.pyd +0 -0
  389. scipy/io/matlab/_miobase.py +435 -0
  390. scipy/io/matlab/_streams.cp313t-win_arm64.lib +0 -0
  391. scipy/io/matlab/_streams.cp313t-win_arm64.pyd +0 -0
  392. scipy/io/matlab/byteordercodes.py +17 -0
  393. scipy/io/matlab/mio.py +16 -0
  394. scipy/io/matlab/mio4.py +17 -0
  395. scipy/io/matlab/mio5.py +19 -0
  396. scipy/io/matlab/mio5_params.py +18 -0
  397. scipy/io/matlab/mio5_utils.py +17 -0
  398. scipy/io/matlab/mio_utils.py +17 -0
  399. scipy/io/matlab/miobase.py +16 -0
  400. scipy/io/matlab/streams.py +16 -0
  401. scipy/io/matlab/tests/__init__.py +0 -0
  402. scipy/io/matlab/tests/data/bad_miuint32.mat +0 -0
  403. scipy/io/matlab/tests/data/bad_miutf8_array_name.mat +0 -0
  404. scipy/io/matlab/tests/data/big_endian.mat +0 -0
  405. scipy/io/matlab/tests/data/broken_utf8.mat +0 -0
  406. scipy/io/matlab/tests/data/corrupted_zlib_checksum.mat +0 -0
  407. scipy/io/matlab/tests/data/corrupted_zlib_data.mat +0 -0
  408. scipy/io/matlab/tests/data/debigged_m4.mat +0 -0
  409. scipy/io/matlab/tests/data/japanese_utf8.txt +5 -0
  410. scipy/io/matlab/tests/data/little_endian.mat +0 -0
  411. scipy/io/matlab/tests/data/logical_sparse.mat +0 -0
  412. scipy/io/matlab/tests/data/malformed1.mat +0 -0
  413. scipy/io/matlab/tests/data/miuint32_for_miint32.mat +0 -0
  414. scipy/io/matlab/tests/data/miutf8_array_name.mat +0 -0
  415. scipy/io/matlab/tests/data/nasty_duplicate_fieldnames.mat +0 -0
  416. scipy/io/matlab/tests/data/one_by_zero_char.mat +0 -0
  417. scipy/io/matlab/tests/data/parabola.mat +0 -0
  418. scipy/io/matlab/tests/data/single_empty_string.mat +0 -0
  419. scipy/io/matlab/tests/data/some_functions.mat +0 -0
  420. scipy/io/matlab/tests/data/sqr.mat +0 -0
  421. scipy/io/matlab/tests/data/test3dmatrix_6.1_SOL2.mat +0 -0
  422. scipy/io/matlab/tests/data/test3dmatrix_6.5.1_GLNX86.mat +0 -0
  423. scipy/io/matlab/tests/data/test3dmatrix_7.1_GLNX86.mat +0 -0
  424. scipy/io/matlab/tests/data/test3dmatrix_7.4_GLNX86.mat +0 -0
  425. scipy/io/matlab/tests/data/test_empty_struct.mat +0 -0
  426. scipy/io/matlab/tests/data/test_mat4_le_floats.mat +0 -0
  427. scipy/io/matlab/tests/data/test_skip_variable.mat +0 -0
  428. scipy/io/matlab/tests/data/testbool_8_WIN64.mat +0 -0
  429. scipy/io/matlab/tests/data/testcell_6.1_SOL2.mat +0 -0
  430. scipy/io/matlab/tests/data/testcell_6.5.1_GLNX86.mat +0 -0
  431. scipy/io/matlab/tests/data/testcell_7.1_GLNX86.mat +0 -0
  432. scipy/io/matlab/tests/data/testcell_7.4_GLNX86.mat +0 -0
  433. scipy/io/matlab/tests/data/testcellnest_6.1_SOL2.mat +0 -0
  434. scipy/io/matlab/tests/data/testcellnest_6.5.1_GLNX86.mat +0 -0
  435. scipy/io/matlab/tests/data/testcellnest_7.1_GLNX86.mat +0 -0
  436. scipy/io/matlab/tests/data/testcellnest_7.4_GLNX86.mat +0 -0
  437. scipy/io/matlab/tests/data/testcomplex_4.2c_SOL2.mat +0 -0
  438. scipy/io/matlab/tests/data/testcomplex_6.1_SOL2.mat +0 -0
  439. scipy/io/matlab/tests/data/testcomplex_6.5.1_GLNX86.mat +0 -0
  440. scipy/io/matlab/tests/data/testcomplex_7.1_GLNX86.mat +0 -0
  441. scipy/io/matlab/tests/data/testcomplex_7.4_GLNX86.mat +0 -0
  442. scipy/io/matlab/tests/data/testdouble_4.2c_SOL2.mat +0 -0
  443. scipy/io/matlab/tests/data/testdouble_6.1_SOL2.mat +0 -0
  444. scipy/io/matlab/tests/data/testdouble_6.5.1_GLNX86.mat +0 -0
  445. scipy/io/matlab/tests/data/testdouble_7.1_GLNX86.mat +0 -0
  446. scipy/io/matlab/tests/data/testdouble_7.4_GLNX86.mat +0 -0
  447. scipy/io/matlab/tests/data/testemptycell_5.3_SOL2.mat +0 -0
  448. scipy/io/matlab/tests/data/testemptycell_6.5.1_GLNX86.mat +0 -0
  449. scipy/io/matlab/tests/data/testemptycell_7.1_GLNX86.mat +0 -0
  450. scipy/io/matlab/tests/data/testemptycell_7.4_GLNX86.mat +0 -0
  451. scipy/io/matlab/tests/data/testfunc_7.4_GLNX86.mat +0 -0
  452. scipy/io/matlab/tests/data/testhdf5_7.4_GLNX86.mat +0 -0
  453. scipy/io/matlab/tests/data/testmatrix_4.2c_SOL2.mat +0 -0
  454. scipy/io/matlab/tests/data/testmatrix_6.1_SOL2.mat +0 -0
  455. scipy/io/matlab/tests/data/testmatrix_6.5.1_GLNX86.mat +0 -0
  456. scipy/io/matlab/tests/data/testmatrix_7.1_GLNX86.mat +0 -0
  457. scipy/io/matlab/tests/data/testmatrix_7.4_GLNX86.mat +0 -0
  458. scipy/io/matlab/tests/data/testminus_4.2c_SOL2.mat +0 -0
  459. scipy/io/matlab/tests/data/testminus_6.1_SOL2.mat +0 -0
  460. scipy/io/matlab/tests/data/testminus_6.5.1_GLNX86.mat +0 -0
  461. scipy/io/matlab/tests/data/testminus_7.1_GLNX86.mat +0 -0
  462. scipy/io/matlab/tests/data/testminus_7.4_GLNX86.mat +0 -0
  463. scipy/io/matlab/tests/data/testmulti_4.2c_SOL2.mat +0 -0
  464. scipy/io/matlab/tests/data/testmulti_7.1_GLNX86.mat +0 -0
  465. scipy/io/matlab/tests/data/testmulti_7.4_GLNX86.mat +0 -0
  466. scipy/io/matlab/tests/data/testobject_6.1_SOL2.mat +0 -0
  467. scipy/io/matlab/tests/data/testobject_6.5.1_GLNX86.mat +0 -0
  468. scipy/io/matlab/tests/data/testobject_7.1_GLNX86.mat +0 -0
  469. scipy/io/matlab/tests/data/testobject_7.4_GLNX86.mat +0 -0
  470. scipy/io/matlab/tests/data/testonechar_4.2c_SOL2.mat +0 -0
  471. scipy/io/matlab/tests/data/testonechar_6.1_SOL2.mat +0 -0
  472. scipy/io/matlab/tests/data/testonechar_6.5.1_GLNX86.mat +0 -0
  473. scipy/io/matlab/tests/data/testonechar_7.1_GLNX86.mat +0 -0
  474. scipy/io/matlab/tests/data/testonechar_7.4_GLNX86.mat +0 -0
  475. scipy/io/matlab/tests/data/testscalarcell_7.4_GLNX86.mat +0 -0
  476. scipy/io/matlab/tests/data/testsimplecell.mat +0 -0
  477. scipy/io/matlab/tests/data/testsparse_4.2c_SOL2.mat +0 -0
  478. scipy/io/matlab/tests/data/testsparse_6.1_SOL2.mat +0 -0
  479. scipy/io/matlab/tests/data/testsparse_6.5.1_GLNX86.mat +0 -0
  480. scipy/io/matlab/tests/data/testsparse_7.1_GLNX86.mat +0 -0
  481. scipy/io/matlab/tests/data/testsparse_7.4_GLNX86.mat +0 -0
  482. scipy/io/matlab/tests/data/testsparsecomplex_4.2c_SOL2.mat +0 -0
  483. scipy/io/matlab/tests/data/testsparsecomplex_6.1_SOL2.mat +0 -0
  484. scipy/io/matlab/tests/data/testsparsecomplex_6.5.1_GLNX86.mat +0 -0
  485. scipy/io/matlab/tests/data/testsparsecomplex_7.1_GLNX86.mat +0 -0
  486. scipy/io/matlab/tests/data/testsparsecomplex_7.4_GLNX86.mat +0 -0
  487. scipy/io/matlab/tests/data/testsparsefloat_7.4_GLNX86.mat +0 -0
  488. scipy/io/matlab/tests/data/teststring_4.2c_SOL2.mat +0 -0
  489. scipy/io/matlab/tests/data/teststring_6.1_SOL2.mat +0 -0
  490. scipy/io/matlab/tests/data/teststring_6.5.1_GLNX86.mat +0 -0
  491. scipy/io/matlab/tests/data/teststring_7.1_GLNX86.mat +0 -0
  492. scipy/io/matlab/tests/data/teststring_7.4_GLNX86.mat +0 -0
  493. scipy/io/matlab/tests/data/teststringarray_4.2c_SOL2.mat +0 -0
  494. scipy/io/matlab/tests/data/teststringarray_6.1_SOL2.mat +0 -0
  495. scipy/io/matlab/tests/data/teststringarray_6.5.1_GLNX86.mat +0 -0
  496. scipy/io/matlab/tests/data/teststringarray_7.1_GLNX86.mat +0 -0
  497. scipy/io/matlab/tests/data/teststringarray_7.4_GLNX86.mat +0 -0
  498. scipy/io/matlab/tests/data/teststruct_6.1_SOL2.mat +0 -0
  499. scipy/io/matlab/tests/data/teststruct_6.5.1_GLNX86.mat +0 -0
  500. scipy/io/matlab/tests/data/teststruct_7.1_GLNX86.mat +0 -0
  501. scipy/io/matlab/tests/data/teststruct_7.4_GLNX86.mat +0 -0
  502. scipy/io/matlab/tests/data/teststructarr_6.1_SOL2.mat +0 -0
  503. scipy/io/matlab/tests/data/teststructarr_6.5.1_GLNX86.mat +0 -0
  504. scipy/io/matlab/tests/data/teststructarr_7.1_GLNX86.mat +0 -0
  505. scipy/io/matlab/tests/data/teststructarr_7.4_GLNX86.mat +0 -0
  506. scipy/io/matlab/tests/data/teststructnest_6.1_SOL2.mat +0 -0
  507. scipy/io/matlab/tests/data/teststructnest_6.5.1_GLNX86.mat +0 -0
  508. scipy/io/matlab/tests/data/teststructnest_7.1_GLNX86.mat +0 -0
  509. scipy/io/matlab/tests/data/teststructnest_7.4_GLNX86.mat +0 -0
  510. scipy/io/matlab/tests/data/testunicode_7.1_GLNX86.mat +0 -0
  511. scipy/io/matlab/tests/data/testunicode_7.4_GLNX86.mat +0 -0
  512. scipy/io/matlab/tests/data/testvec_4_GLNX86.mat +0 -0
  513. scipy/io/matlab/tests/test_byteordercodes.py +29 -0
  514. scipy/io/matlab/tests/test_mio.py +1399 -0
  515. scipy/io/matlab/tests/test_mio5_utils.py +179 -0
  516. scipy/io/matlab/tests/test_mio_funcs.py +51 -0
  517. scipy/io/matlab/tests/test_mio_utils.py +45 -0
  518. scipy/io/matlab/tests/test_miobase.py +32 -0
  519. scipy/io/matlab/tests/test_pathological.py +33 -0
  520. scipy/io/matlab/tests/test_streams.py +241 -0
  521. scipy/io/mmio.py +17 -0
  522. scipy/io/netcdf.py +17 -0
  523. scipy/io/tests/__init__.py +0 -0
  524. scipy/io/tests/data/Transparent Busy.ani +0 -0
  525. scipy/io/tests/data/array_float32_1d.sav +0 -0
  526. scipy/io/tests/data/array_float32_2d.sav +0 -0
  527. scipy/io/tests/data/array_float32_3d.sav +0 -0
  528. scipy/io/tests/data/array_float32_4d.sav +0 -0
  529. scipy/io/tests/data/array_float32_5d.sav +0 -0
  530. scipy/io/tests/data/array_float32_6d.sav +0 -0
  531. scipy/io/tests/data/array_float32_7d.sav +0 -0
  532. scipy/io/tests/data/array_float32_8d.sav +0 -0
  533. scipy/io/tests/data/array_float32_pointer_1d.sav +0 -0
  534. scipy/io/tests/data/array_float32_pointer_2d.sav +0 -0
  535. scipy/io/tests/data/array_float32_pointer_3d.sav +0 -0
  536. scipy/io/tests/data/array_float32_pointer_4d.sav +0 -0
  537. scipy/io/tests/data/array_float32_pointer_5d.sav +0 -0
  538. scipy/io/tests/data/array_float32_pointer_6d.sav +0 -0
  539. scipy/io/tests/data/array_float32_pointer_7d.sav +0 -0
  540. scipy/io/tests/data/array_float32_pointer_8d.sav +0 -0
  541. scipy/io/tests/data/example_1.nc +0 -0
  542. scipy/io/tests/data/example_2.nc +0 -0
  543. scipy/io/tests/data/example_3_maskedvals.nc +0 -0
  544. scipy/io/tests/data/fortran-3x3d-2i.dat +0 -0
  545. scipy/io/tests/data/fortran-mixed.dat +0 -0
  546. scipy/io/tests/data/fortran-sf8-11x1x10.dat +0 -0
  547. scipy/io/tests/data/fortran-sf8-15x10x22.dat +0 -0
  548. scipy/io/tests/data/fortran-sf8-1x1x1.dat +0 -0
  549. scipy/io/tests/data/fortran-sf8-1x1x5.dat +0 -0
  550. scipy/io/tests/data/fortran-sf8-1x1x7.dat +0 -0
  551. scipy/io/tests/data/fortran-sf8-1x3x5.dat +0 -0
  552. scipy/io/tests/data/fortran-si4-11x1x10.dat +0 -0
  553. scipy/io/tests/data/fortran-si4-15x10x22.dat +0 -0
  554. scipy/io/tests/data/fortran-si4-1x1x1.dat +0 -0
  555. scipy/io/tests/data/fortran-si4-1x1x5.dat +0 -0
  556. scipy/io/tests/data/fortran-si4-1x1x7.dat +0 -0
  557. scipy/io/tests/data/fortran-si4-1x3x5.dat +0 -0
  558. scipy/io/tests/data/invalid_pointer.sav +0 -0
  559. scipy/io/tests/data/null_pointer.sav +0 -0
  560. scipy/io/tests/data/scalar_byte.sav +0 -0
  561. scipy/io/tests/data/scalar_byte_descr.sav +0 -0
  562. scipy/io/tests/data/scalar_complex32.sav +0 -0
  563. scipy/io/tests/data/scalar_complex64.sav +0 -0
  564. scipy/io/tests/data/scalar_float32.sav +0 -0
  565. scipy/io/tests/data/scalar_float64.sav +0 -0
  566. scipy/io/tests/data/scalar_heap_pointer.sav +0 -0
  567. scipy/io/tests/data/scalar_int16.sav +0 -0
  568. scipy/io/tests/data/scalar_int32.sav +0 -0
  569. scipy/io/tests/data/scalar_int64.sav +0 -0
  570. scipy/io/tests/data/scalar_string.sav +0 -0
  571. scipy/io/tests/data/scalar_uint16.sav +0 -0
  572. scipy/io/tests/data/scalar_uint32.sav +0 -0
  573. scipy/io/tests/data/scalar_uint64.sav +0 -0
  574. scipy/io/tests/data/struct_arrays.sav +0 -0
  575. scipy/io/tests/data/struct_arrays_byte_idl80.sav +0 -0
  576. scipy/io/tests/data/struct_arrays_replicated.sav +0 -0
  577. scipy/io/tests/data/struct_arrays_replicated_3d.sav +0 -0
  578. scipy/io/tests/data/struct_inherit.sav +0 -0
  579. scipy/io/tests/data/struct_pointer_arrays.sav +0 -0
  580. scipy/io/tests/data/struct_pointer_arrays_replicated.sav +0 -0
  581. scipy/io/tests/data/struct_pointer_arrays_replicated_3d.sav +0 -0
  582. scipy/io/tests/data/struct_pointers.sav +0 -0
  583. scipy/io/tests/data/struct_pointers_replicated.sav +0 -0
  584. scipy/io/tests/data/struct_pointers_replicated_3d.sav +0 -0
  585. scipy/io/tests/data/struct_scalars.sav +0 -0
  586. scipy/io/tests/data/struct_scalars_replicated.sav +0 -0
  587. scipy/io/tests/data/struct_scalars_replicated_3d.sav +0 -0
  588. scipy/io/tests/data/test-1234Hz-le-1ch-10S-20bit-extra.wav +0 -0
  589. scipy/io/tests/data/test-44100Hz-2ch-32bit-float-be.wav +0 -0
  590. scipy/io/tests/data/test-44100Hz-2ch-32bit-float-le.wav +0 -0
  591. scipy/io/tests/data/test-44100Hz-be-1ch-4bytes.wav +0 -0
  592. scipy/io/tests/data/test-44100Hz-le-1ch-4bytes-early-eof-no-data.wav +0 -0
  593. scipy/io/tests/data/test-44100Hz-le-1ch-4bytes-early-eof.wav +0 -0
  594. scipy/io/tests/data/test-44100Hz-le-1ch-4bytes-incomplete-chunk.wav +0 -0
  595. scipy/io/tests/data/test-44100Hz-le-1ch-4bytes-rf64.wav +0 -0
  596. scipy/io/tests/data/test-44100Hz-le-1ch-4bytes.wav +0 -0
  597. scipy/io/tests/data/test-48000Hz-2ch-64bit-float-le-wavex.wav +0 -0
  598. scipy/io/tests/data/test-8000Hz-be-3ch-5S-24bit.wav +0 -0
  599. scipy/io/tests/data/test-8000Hz-le-1ch-1byte-ulaw.wav +0 -0
  600. scipy/io/tests/data/test-8000Hz-le-2ch-1byteu.wav +0 -0
  601. scipy/io/tests/data/test-8000Hz-le-3ch-5S-24bit-inconsistent.wav +0 -0
  602. scipy/io/tests/data/test-8000Hz-le-3ch-5S-24bit-rf64.wav +0 -0
  603. scipy/io/tests/data/test-8000Hz-le-3ch-5S-24bit.wav +0 -0
  604. scipy/io/tests/data/test-8000Hz-le-3ch-5S-36bit.wav +0 -0
  605. scipy/io/tests/data/test-8000Hz-le-3ch-5S-45bit.wav +0 -0
  606. scipy/io/tests/data/test-8000Hz-le-3ch-5S-53bit.wav +0 -0
  607. scipy/io/tests/data/test-8000Hz-le-3ch-5S-64bit.wav +0 -0
  608. scipy/io/tests/data/test-8000Hz-le-4ch-9S-12bit.wav +0 -0
  609. scipy/io/tests/data/test-8000Hz-le-5ch-9S-5bit.wav +0 -0
  610. scipy/io/tests/data/various_compressed.sav +0 -0
  611. scipy/io/tests/test_fortran.py +264 -0
  612. scipy/io/tests/test_idl.py +483 -0
  613. scipy/io/tests/test_mmio.py +831 -0
  614. scipy/io/tests/test_netcdf.py +550 -0
  615. scipy/io/tests/test_paths.py +93 -0
  616. scipy/io/tests/test_wavfile.py +501 -0
  617. scipy/io/wavfile.py +938 -0
  618. scipy/linalg/__init__.pxd +1 -0
  619. scipy/linalg/__init__.py +236 -0
  620. scipy/linalg/_basic.py +2146 -0
  621. scipy/linalg/_blas_subroutines.h +164 -0
  622. scipy/linalg/_cythonized_array_utils.cp313t-win_arm64.lib +0 -0
  623. scipy/linalg/_cythonized_array_utils.cp313t-win_arm64.pyd +0 -0
  624. scipy/linalg/_cythonized_array_utils.pxd +40 -0
  625. scipy/linalg/_cythonized_array_utils.pyi +16 -0
  626. scipy/linalg/_decomp.py +1645 -0
  627. scipy/linalg/_decomp_cholesky.py +413 -0
  628. scipy/linalg/_decomp_cossin.py +236 -0
  629. scipy/linalg/_decomp_interpolative.cp313t-win_arm64.lib +0 -0
  630. scipy/linalg/_decomp_interpolative.cp313t-win_arm64.pyd +0 -0
  631. scipy/linalg/_decomp_ldl.py +356 -0
  632. scipy/linalg/_decomp_lu.py +401 -0
  633. scipy/linalg/_decomp_lu_cython.cp313t-win_arm64.lib +0 -0
  634. scipy/linalg/_decomp_lu_cython.cp313t-win_arm64.pyd +0 -0
  635. scipy/linalg/_decomp_lu_cython.pyi +6 -0
  636. scipy/linalg/_decomp_polar.py +113 -0
  637. scipy/linalg/_decomp_qr.py +494 -0
  638. scipy/linalg/_decomp_qz.py +452 -0
  639. scipy/linalg/_decomp_schur.py +336 -0
  640. scipy/linalg/_decomp_svd.py +545 -0
  641. scipy/linalg/_decomp_update.cp313t-win_arm64.lib +0 -0
  642. scipy/linalg/_decomp_update.cp313t-win_arm64.pyd +0 -0
  643. scipy/linalg/_expm_frechet.py +417 -0
  644. scipy/linalg/_fblas.cp313t-win_arm64.lib +0 -0
  645. scipy/linalg/_fblas.cp313t-win_arm64.pyd +0 -0
  646. scipy/linalg/_flapack.cp313t-win_arm64.lib +0 -0
  647. scipy/linalg/_flapack.cp313t-win_arm64.pyd +0 -0
  648. scipy/linalg/_lapack_subroutines.h +1521 -0
  649. scipy/linalg/_linalg_pythran.cp313t-win_arm64.lib +0 -0
  650. scipy/linalg/_linalg_pythran.cp313t-win_arm64.pyd +0 -0
  651. scipy/linalg/_matfuncs.py +1050 -0
  652. scipy/linalg/_matfuncs_expm.cp313t-win_arm64.lib +0 -0
  653. scipy/linalg/_matfuncs_expm.cp313t-win_arm64.pyd +0 -0
  654. scipy/linalg/_matfuncs_expm.pyi +6 -0
  655. scipy/linalg/_matfuncs_inv_ssq.py +886 -0
  656. scipy/linalg/_matfuncs_schur_sqrtm.cp313t-win_arm64.lib +0 -0
  657. scipy/linalg/_matfuncs_schur_sqrtm.cp313t-win_arm64.pyd +0 -0
  658. scipy/linalg/_matfuncs_sqrtm.py +107 -0
  659. scipy/linalg/_matfuncs_sqrtm_triu.cp313t-win_arm64.lib +0 -0
  660. scipy/linalg/_matfuncs_sqrtm_triu.cp313t-win_arm64.pyd +0 -0
  661. scipy/linalg/_misc.py +191 -0
  662. scipy/linalg/_procrustes.py +113 -0
  663. scipy/linalg/_sketches.py +189 -0
  664. scipy/linalg/_solve_toeplitz.cp313t-win_arm64.lib +0 -0
  665. scipy/linalg/_solve_toeplitz.cp313t-win_arm64.pyd +0 -0
  666. scipy/linalg/_solvers.py +862 -0
  667. scipy/linalg/_special_matrices.py +1322 -0
  668. scipy/linalg/_testutils.py +65 -0
  669. scipy/linalg/basic.py +23 -0
  670. scipy/linalg/blas.py +495 -0
  671. scipy/linalg/cython_blas.cp313t-win_arm64.lib +0 -0
  672. scipy/linalg/cython_blas.cp313t-win_arm64.pyd +0 -0
  673. scipy/linalg/cython_blas.pxd +169 -0
  674. scipy/linalg/cython_blas.pyx +1432 -0
  675. scipy/linalg/cython_lapack.cp313t-win_arm64.lib +0 -0
  676. scipy/linalg/cython_lapack.cp313t-win_arm64.pyd +0 -0
  677. scipy/linalg/cython_lapack.pxd +1528 -0
  678. scipy/linalg/cython_lapack.pyx +12045 -0
  679. scipy/linalg/decomp.py +23 -0
  680. scipy/linalg/decomp_cholesky.py +21 -0
  681. scipy/linalg/decomp_lu.py +21 -0
  682. scipy/linalg/decomp_qr.py +20 -0
  683. scipy/linalg/decomp_schur.py +21 -0
  684. scipy/linalg/decomp_svd.py +21 -0
  685. scipy/linalg/interpolative.py +989 -0
  686. scipy/linalg/lapack.py +1081 -0
  687. scipy/linalg/matfuncs.py +23 -0
  688. scipy/linalg/misc.py +21 -0
  689. scipy/linalg/special_matrices.py +22 -0
  690. scipy/linalg/tests/__init__.py +0 -0
  691. scipy/linalg/tests/_cython_examples/extending.pyx +23 -0
  692. scipy/linalg/tests/_cython_examples/meson.build +34 -0
  693. scipy/linalg/tests/data/carex_15_data.npz +0 -0
  694. scipy/linalg/tests/data/carex_18_data.npz +0 -0
  695. scipy/linalg/tests/data/carex_19_data.npz +0 -0
  696. scipy/linalg/tests/data/carex_20_data.npz +0 -0
  697. scipy/linalg/tests/data/carex_6_data.npz +0 -0
  698. scipy/linalg/tests/data/gendare_20170120_data.npz +0 -0
  699. scipy/linalg/tests/test_basic.py +2074 -0
  700. scipy/linalg/tests/test_batch.py +588 -0
  701. scipy/linalg/tests/test_blas.py +1127 -0
  702. scipy/linalg/tests/test_cython_blas.py +118 -0
  703. scipy/linalg/tests/test_cython_lapack.py +22 -0
  704. scipy/linalg/tests/test_cythonized_array_utils.py +130 -0
  705. scipy/linalg/tests/test_decomp.py +3189 -0
  706. scipy/linalg/tests/test_decomp_cholesky.py +268 -0
  707. scipy/linalg/tests/test_decomp_cossin.py +314 -0
  708. scipy/linalg/tests/test_decomp_ldl.py +137 -0
  709. scipy/linalg/tests/test_decomp_lu.py +308 -0
  710. scipy/linalg/tests/test_decomp_polar.py +110 -0
  711. scipy/linalg/tests/test_decomp_update.py +1701 -0
  712. scipy/linalg/tests/test_extending.py +46 -0
  713. scipy/linalg/tests/test_fblas.py +607 -0
  714. scipy/linalg/tests/test_interpolative.py +232 -0
  715. scipy/linalg/tests/test_lapack.py +3620 -0
  716. scipy/linalg/tests/test_matfuncs.py +1125 -0
  717. scipy/linalg/tests/test_matmul_toeplitz.py +136 -0
  718. scipy/linalg/tests/test_procrustes.py +214 -0
  719. scipy/linalg/tests/test_sketches.py +118 -0
  720. scipy/linalg/tests/test_solve_toeplitz.py +150 -0
  721. scipy/linalg/tests/test_solvers.py +844 -0
  722. scipy/linalg/tests/test_special_matrices.py +636 -0
  723. scipy/misc/__init__.py +6 -0
  724. scipy/misc/common.py +6 -0
  725. scipy/misc/doccer.py +6 -0
  726. scipy/ndimage/__init__.py +174 -0
  727. scipy/ndimage/_ctest.cp313t-win_arm64.lib +0 -0
  728. scipy/ndimage/_ctest.cp313t-win_arm64.pyd +0 -0
  729. scipy/ndimage/_cytest.cp313t-win_arm64.lib +0 -0
  730. scipy/ndimage/_cytest.cp313t-win_arm64.pyd +0 -0
  731. scipy/ndimage/_delegators.py +303 -0
  732. scipy/ndimage/_filters.py +2422 -0
  733. scipy/ndimage/_fourier.py +306 -0
  734. scipy/ndimage/_interpolation.py +1033 -0
  735. scipy/ndimage/_measurements.py +1689 -0
  736. scipy/ndimage/_morphology.py +2634 -0
  737. scipy/ndimage/_nd_image.cp313t-win_arm64.lib +0 -0
  738. scipy/ndimage/_nd_image.cp313t-win_arm64.pyd +0 -0
  739. scipy/ndimage/_ndimage_api.py +16 -0
  740. scipy/ndimage/_ni_docstrings.py +214 -0
  741. scipy/ndimage/_ni_label.cp313t-win_arm64.lib +0 -0
  742. scipy/ndimage/_ni_label.cp313t-win_arm64.pyd +0 -0
  743. scipy/ndimage/_ni_support.py +139 -0
  744. scipy/ndimage/_rank_filter_1d.cp313t-win_arm64.lib +0 -0
  745. scipy/ndimage/_rank_filter_1d.cp313t-win_arm64.pyd +0 -0
  746. scipy/ndimage/_support_alternative_backends.py +84 -0
  747. scipy/ndimage/filters.py +27 -0
  748. scipy/ndimage/fourier.py +21 -0
  749. scipy/ndimage/interpolation.py +22 -0
  750. scipy/ndimage/measurements.py +24 -0
  751. scipy/ndimage/morphology.py +27 -0
  752. scipy/ndimage/tests/__init__.py +12 -0
  753. scipy/ndimage/tests/data/label_inputs.txt +21 -0
  754. scipy/ndimage/tests/data/label_results.txt +294 -0
  755. scipy/ndimage/tests/data/label_strels.txt +42 -0
  756. scipy/ndimage/tests/dots.png +0 -0
  757. scipy/ndimage/tests/test_c_api.py +102 -0
  758. scipy/ndimage/tests/test_datatypes.py +67 -0
  759. scipy/ndimage/tests/test_filters.py +3083 -0
  760. scipy/ndimage/tests/test_fourier.py +187 -0
  761. scipy/ndimage/tests/test_interpolation.py +1491 -0
  762. scipy/ndimage/tests/test_measurements.py +1592 -0
  763. scipy/ndimage/tests/test_morphology.py +2950 -0
  764. scipy/ndimage/tests/test_ni_support.py +78 -0
  765. scipy/ndimage/tests/test_splines.py +70 -0
  766. scipy/odr/__init__.py +131 -0
  767. scipy/odr/__odrpack.cp313t-win_arm64.lib +0 -0
  768. scipy/odr/__odrpack.cp313t-win_arm64.pyd +0 -0
  769. scipy/odr/_add_newdocs.py +34 -0
  770. scipy/odr/_models.py +315 -0
  771. scipy/odr/_odrpack.py +1154 -0
  772. scipy/odr/models.py +20 -0
  773. scipy/odr/odrpack.py +21 -0
  774. scipy/odr/tests/__init__.py +0 -0
  775. scipy/odr/tests/test_odr.py +607 -0
  776. scipy/optimize/__init__.pxd +1 -0
  777. scipy/optimize/__init__.py +460 -0
  778. scipy/optimize/_basinhopping.py +741 -0
  779. scipy/optimize/_bglu_dense.cp313t-win_arm64.lib +0 -0
  780. scipy/optimize/_bglu_dense.cp313t-win_arm64.pyd +0 -0
  781. scipy/optimize/_bracket.py +706 -0
  782. scipy/optimize/_chandrupatla.py +551 -0
  783. scipy/optimize/_cobyla_py.py +297 -0
  784. scipy/optimize/_cobyqa_py.py +72 -0
  785. scipy/optimize/_constraints.py +598 -0
  786. scipy/optimize/_dcsrch.py +728 -0
  787. scipy/optimize/_differentiable_functions.py +835 -0
  788. scipy/optimize/_differentialevolution.py +1970 -0
  789. scipy/optimize/_direct.cp313t-win_arm64.lib +0 -0
  790. scipy/optimize/_direct.cp313t-win_arm64.pyd +0 -0
  791. scipy/optimize/_direct_py.py +280 -0
  792. scipy/optimize/_dual_annealing.py +732 -0
  793. scipy/optimize/_elementwise.py +798 -0
  794. scipy/optimize/_group_columns.cp313t-win_arm64.lib +0 -0
  795. scipy/optimize/_group_columns.cp313t-win_arm64.pyd +0 -0
  796. scipy/optimize/_hessian_update_strategy.py +479 -0
  797. scipy/optimize/_highspy/__init__.py +0 -0
  798. scipy/optimize/_highspy/_core.cp313t-win_arm64.lib +0 -0
  799. scipy/optimize/_highspy/_core.cp313t-win_arm64.pyd +0 -0
  800. scipy/optimize/_highspy/_highs_options.cp313t-win_arm64.lib +0 -0
  801. scipy/optimize/_highspy/_highs_options.cp313t-win_arm64.pyd +0 -0
  802. scipy/optimize/_highspy/_highs_wrapper.py +338 -0
  803. scipy/optimize/_isotonic.py +157 -0
  804. scipy/optimize/_lbfgsb.cp313t-win_arm64.lib +0 -0
  805. scipy/optimize/_lbfgsb.cp313t-win_arm64.pyd +0 -0
  806. scipy/optimize/_lbfgsb_py.py +634 -0
  807. scipy/optimize/_linesearch.py +896 -0
  808. scipy/optimize/_linprog.py +733 -0
  809. scipy/optimize/_linprog_doc.py +1434 -0
  810. scipy/optimize/_linprog_highs.py +422 -0
  811. scipy/optimize/_linprog_ip.py +1141 -0
  812. scipy/optimize/_linprog_rs.py +572 -0
  813. scipy/optimize/_linprog_simplex.py +663 -0
  814. scipy/optimize/_linprog_util.py +1521 -0
  815. scipy/optimize/_lsap.cp313t-win_arm64.lib +0 -0
  816. scipy/optimize/_lsap.cp313t-win_arm64.pyd +0 -0
  817. scipy/optimize/_lsq/__init__.py +5 -0
  818. scipy/optimize/_lsq/bvls.py +183 -0
  819. scipy/optimize/_lsq/common.py +731 -0
  820. scipy/optimize/_lsq/dogbox.py +345 -0
  821. scipy/optimize/_lsq/givens_elimination.cp313t-win_arm64.lib +0 -0
  822. scipy/optimize/_lsq/givens_elimination.cp313t-win_arm64.pyd +0 -0
  823. scipy/optimize/_lsq/least_squares.py +1044 -0
  824. scipy/optimize/_lsq/lsq_linear.py +361 -0
  825. scipy/optimize/_lsq/trf.py +587 -0
  826. scipy/optimize/_lsq/trf_linear.py +249 -0
  827. scipy/optimize/_milp.py +394 -0
  828. scipy/optimize/_minimize.py +1199 -0
  829. scipy/optimize/_minpack.cp313t-win_arm64.lib +0 -0
  830. scipy/optimize/_minpack.cp313t-win_arm64.pyd +0 -0
  831. scipy/optimize/_minpack_py.py +1178 -0
  832. scipy/optimize/_moduleTNC.cp313t-win_arm64.lib +0 -0
  833. scipy/optimize/_moduleTNC.cp313t-win_arm64.pyd +0 -0
  834. scipy/optimize/_nnls.py +96 -0
  835. scipy/optimize/_nonlin.py +1634 -0
  836. scipy/optimize/_numdiff.py +963 -0
  837. scipy/optimize/_optimize.py +4169 -0
  838. scipy/optimize/_pava_pybind.cp313t-win_arm64.lib +0 -0
  839. scipy/optimize/_pava_pybind.cp313t-win_arm64.pyd +0 -0
  840. scipy/optimize/_qap.py +760 -0
  841. scipy/optimize/_remove_redundancy.py +522 -0
  842. scipy/optimize/_root.py +732 -0
  843. scipy/optimize/_root_scalar.py +538 -0
  844. scipy/optimize/_shgo.py +1606 -0
  845. scipy/optimize/_shgo_lib/__init__.py +0 -0
  846. scipy/optimize/_shgo_lib/_complex.py +1225 -0
  847. scipy/optimize/_shgo_lib/_vertex.py +460 -0
  848. scipy/optimize/_slsqp_py.py +603 -0
  849. scipy/optimize/_slsqplib.cp313t-win_arm64.lib +0 -0
  850. scipy/optimize/_slsqplib.cp313t-win_arm64.pyd +0 -0
  851. scipy/optimize/_spectral.py +260 -0
  852. scipy/optimize/_tnc.py +438 -0
  853. scipy/optimize/_trlib/__init__.py +12 -0
  854. scipy/optimize/_trlib/_trlib.cp313t-win_arm64.lib +0 -0
  855. scipy/optimize/_trlib/_trlib.cp313t-win_arm64.pyd +0 -0
  856. scipy/optimize/_trustregion.py +318 -0
  857. scipy/optimize/_trustregion_constr/__init__.py +6 -0
  858. scipy/optimize/_trustregion_constr/canonical_constraint.py +390 -0
  859. scipy/optimize/_trustregion_constr/equality_constrained_sqp.py +231 -0
  860. scipy/optimize/_trustregion_constr/minimize_trustregion_constr.py +584 -0
  861. scipy/optimize/_trustregion_constr/projections.py +411 -0
  862. scipy/optimize/_trustregion_constr/qp_subproblem.py +637 -0
  863. scipy/optimize/_trustregion_constr/report.py +49 -0
  864. scipy/optimize/_trustregion_constr/tests/__init__.py +0 -0
  865. scipy/optimize/_trustregion_constr/tests/test_canonical_constraint.py +296 -0
  866. scipy/optimize/_trustregion_constr/tests/test_nested_minimize.py +39 -0
  867. scipy/optimize/_trustregion_constr/tests/test_projections.py +214 -0
  868. scipy/optimize/_trustregion_constr/tests/test_qp_subproblem.py +645 -0
  869. scipy/optimize/_trustregion_constr/tests/test_report.py +34 -0
  870. scipy/optimize/_trustregion_constr/tr_interior_point.py +361 -0
  871. scipy/optimize/_trustregion_dogleg.py +122 -0
  872. scipy/optimize/_trustregion_exact.py +437 -0
  873. scipy/optimize/_trustregion_krylov.py +65 -0
  874. scipy/optimize/_trustregion_ncg.py +126 -0
  875. scipy/optimize/_tstutils.py +972 -0
  876. scipy/optimize/_zeros.cp313t-win_arm64.lib +0 -0
  877. scipy/optimize/_zeros.cp313t-win_arm64.pyd +0 -0
  878. scipy/optimize/_zeros_py.py +1475 -0
  879. scipy/optimize/cobyla.py +19 -0
  880. scipy/optimize/cython_optimize/__init__.py +133 -0
  881. scipy/optimize/cython_optimize/_zeros.cp313t-win_arm64.lib +0 -0
  882. scipy/optimize/cython_optimize/_zeros.cp313t-win_arm64.pyd +0 -0
  883. scipy/optimize/cython_optimize/_zeros.pxd +33 -0
  884. scipy/optimize/cython_optimize/c_zeros.pxd +26 -0
  885. scipy/optimize/cython_optimize.pxd +11 -0
  886. scipy/optimize/elementwise.py +38 -0
  887. scipy/optimize/lbfgsb.py +23 -0
  888. scipy/optimize/linesearch.py +18 -0
  889. scipy/optimize/minpack.py +27 -0
  890. scipy/optimize/minpack2.py +17 -0
  891. scipy/optimize/moduleTNC.py +19 -0
  892. scipy/optimize/nonlin.py +29 -0
  893. scipy/optimize/optimize.py +40 -0
  894. scipy/optimize/slsqp.py +22 -0
  895. scipy/optimize/tests/__init__.py +0 -0
  896. scipy/optimize/tests/_cython_examples/extending.pyx +43 -0
  897. scipy/optimize/tests/_cython_examples/meson.build +32 -0
  898. scipy/optimize/tests/test__basinhopping.py +535 -0
  899. scipy/optimize/tests/test__differential_evolution.py +1703 -0
  900. scipy/optimize/tests/test__dual_annealing.py +416 -0
  901. scipy/optimize/tests/test__linprog_clean_inputs.py +312 -0
  902. scipy/optimize/tests/test__numdiff.py +885 -0
  903. scipy/optimize/tests/test__remove_redundancy.py +228 -0
  904. scipy/optimize/tests/test__root.py +124 -0
  905. scipy/optimize/tests/test__shgo.py +1164 -0
  906. scipy/optimize/tests/test__spectral.py +226 -0
  907. scipy/optimize/tests/test_bracket.py +896 -0
  908. scipy/optimize/tests/test_chandrupatla.py +982 -0
  909. scipy/optimize/tests/test_cobyla.py +195 -0
  910. scipy/optimize/tests/test_cobyqa.py +252 -0
  911. scipy/optimize/tests/test_constraint_conversion.py +286 -0
  912. scipy/optimize/tests/test_constraints.py +255 -0
  913. scipy/optimize/tests/test_cython_optimize.py +92 -0
  914. scipy/optimize/tests/test_differentiable_functions.py +1025 -0
  915. scipy/optimize/tests/test_direct.py +321 -0
  916. scipy/optimize/tests/test_extending.py +28 -0
  917. scipy/optimize/tests/test_hessian_update_strategy.py +300 -0
  918. scipy/optimize/tests/test_isotonic_regression.py +167 -0
  919. scipy/optimize/tests/test_lbfgsb_hessinv.py +65 -0
  920. scipy/optimize/tests/test_lbfgsb_setulb.py +122 -0
  921. scipy/optimize/tests/test_least_squares.py +986 -0
  922. scipy/optimize/tests/test_linear_assignment.py +116 -0
  923. scipy/optimize/tests/test_linesearch.py +328 -0
  924. scipy/optimize/tests/test_linprog.py +2577 -0
  925. scipy/optimize/tests/test_lsq_common.py +297 -0
  926. scipy/optimize/tests/test_lsq_linear.py +287 -0
  927. scipy/optimize/tests/test_milp.py +459 -0
  928. scipy/optimize/tests/test_minimize_constrained.py +845 -0
  929. scipy/optimize/tests/test_minpack.py +1194 -0
  930. scipy/optimize/tests/test_nnls.py +469 -0
  931. scipy/optimize/tests/test_nonlin.py +572 -0
  932. scipy/optimize/tests/test_optimize.py +3344 -0
  933. scipy/optimize/tests/test_quadratic_assignment.py +455 -0
  934. scipy/optimize/tests/test_regression.py +40 -0
  935. scipy/optimize/tests/test_slsqp.py +645 -0
  936. scipy/optimize/tests/test_tnc.py +345 -0
  937. scipy/optimize/tests/test_trustregion.py +110 -0
  938. scipy/optimize/tests/test_trustregion_exact.py +351 -0
  939. scipy/optimize/tests/test_trustregion_krylov.py +170 -0
  940. scipy/optimize/tests/test_zeros.py +998 -0
  941. scipy/optimize/tnc.py +22 -0
  942. scipy/optimize/zeros.py +26 -0
  943. scipy/signal/__init__.py +316 -0
  944. scipy/signal/_arraytools.py +264 -0
  945. scipy/signal/_czt.py +575 -0
  946. scipy/signal/_delegators.py +568 -0
  947. scipy/signal/_filter_design.py +5893 -0
  948. scipy/signal/_fir_filter_design.py +1458 -0
  949. scipy/signal/_lti_conversion.py +534 -0
  950. scipy/signal/_ltisys.py +3546 -0
  951. scipy/signal/_max_len_seq.py +139 -0
  952. scipy/signal/_max_len_seq_inner.cp313t-win_arm64.lib +0 -0
  953. scipy/signal/_max_len_seq_inner.cp313t-win_arm64.pyd +0 -0
  954. scipy/signal/_peak_finding.py +1310 -0
  955. scipy/signal/_peak_finding_utils.cp313t-win_arm64.lib +0 -0
  956. scipy/signal/_peak_finding_utils.cp313t-win_arm64.pyd +0 -0
  957. scipy/signal/_polyutils.py +172 -0
  958. scipy/signal/_savitzky_golay.py +357 -0
  959. scipy/signal/_short_time_fft.py +2228 -0
  960. scipy/signal/_signal_api.py +30 -0
  961. scipy/signal/_signaltools.py +5309 -0
  962. scipy/signal/_sigtools.cp313t-win_arm64.lib +0 -0
  963. scipy/signal/_sigtools.cp313t-win_arm64.pyd +0 -0
  964. scipy/signal/_sosfilt.cp313t-win_arm64.lib +0 -0
  965. scipy/signal/_sosfilt.cp313t-win_arm64.pyd +0 -0
  966. scipy/signal/_spectral_py.py +2471 -0
  967. scipy/signal/_spline.cp313t-win_arm64.lib +0 -0
  968. scipy/signal/_spline.cp313t-win_arm64.pyd +0 -0
  969. scipy/signal/_spline.pyi +34 -0
  970. scipy/signal/_spline_filters.py +848 -0
  971. scipy/signal/_support_alternative_backends.py +73 -0
  972. scipy/signal/_upfirdn.py +219 -0
  973. scipy/signal/_upfirdn_apply.cp313t-win_arm64.lib +0 -0
  974. scipy/signal/_upfirdn_apply.cp313t-win_arm64.pyd +0 -0
  975. scipy/signal/_waveforms.py +687 -0
  976. scipy/signal/_wavelets.py +29 -0
  977. scipy/signal/bsplines.py +21 -0
  978. scipy/signal/filter_design.py +28 -0
  979. scipy/signal/fir_filter_design.py +21 -0
  980. scipy/signal/lti_conversion.py +20 -0
  981. scipy/signal/ltisys.py +25 -0
  982. scipy/signal/signaltools.py +27 -0
  983. scipy/signal/spectral.py +21 -0
  984. scipy/signal/spline.py +18 -0
  985. scipy/signal/tests/__init__.py +0 -0
  986. scipy/signal/tests/_scipy_spectral_test_shim.py +311 -0
  987. scipy/signal/tests/mpsig.py +122 -0
  988. scipy/signal/tests/test_array_tools.py +111 -0
  989. scipy/signal/tests/test_bsplines.py +365 -0
  990. scipy/signal/tests/test_cont2discrete.py +424 -0
  991. scipy/signal/tests/test_czt.py +221 -0
  992. scipy/signal/tests/test_dltisys.py +599 -0
  993. scipy/signal/tests/test_filter_design.py +4744 -0
  994. scipy/signal/tests/test_fir_filter_design.py +851 -0
  995. scipy/signal/tests/test_ltisys.py +1225 -0
  996. scipy/signal/tests/test_max_len_seq.py +71 -0
  997. scipy/signal/tests/test_peak_finding.py +915 -0
  998. scipy/signal/tests/test_result_type.py +51 -0
  999. scipy/signal/tests/test_savitzky_golay.py +363 -0
  1000. scipy/signal/tests/test_short_time_fft.py +1107 -0
  1001. scipy/signal/tests/test_signaltools.py +4735 -0
  1002. scipy/signal/tests/test_spectral.py +2141 -0
  1003. scipy/signal/tests/test_splines.py +427 -0
  1004. scipy/signal/tests/test_upfirdn.py +322 -0
  1005. scipy/signal/tests/test_waveforms.py +400 -0
  1006. scipy/signal/tests/test_wavelets.py +59 -0
  1007. scipy/signal/tests/test_windows.py +987 -0
  1008. scipy/signal/waveforms.py +20 -0
  1009. scipy/signal/wavelets.py +17 -0
  1010. scipy/signal/windows/__init__.py +52 -0
  1011. scipy/signal/windows/_windows.py +2513 -0
  1012. scipy/signal/windows/windows.py +23 -0
  1013. scipy/sparse/__init__.py +350 -0
  1014. scipy/sparse/_base.py +1613 -0
  1015. scipy/sparse/_bsr.py +880 -0
  1016. scipy/sparse/_compressed.py +1328 -0
  1017. scipy/sparse/_construct.py +1454 -0
  1018. scipy/sparse/_coo.py +1581 -0
  1019. scipy/sparse/_csc.py +367 -0
  1020. scipy/sparse/_csparsetools.cp313t-win_arm64.lib +0 -0
  1021. scipy/sparse/_csparsetools.cp313t-win_arm64.pyd +0 -0
  1022. scipy/sparse/_csr.py +558 -0
  1023. scipy/sparse/_data.py +569 -0
  1024. scipy/sparse/_dia.py +677 -0
  1025. scipy/sparse/_dok.py +669 -0
  1026. scipy/sparse/_extract.py +178 -0
  1027. scipy/sparse/_index.py +444 -0
  1028. scipy/sparse/_lil.py +632 -0
  1029. scipy/sparse/_matrix.py +169 -0
  1030. scipy/sparse/_matrix_io.py +167 -0
  1031. scipy/sparse/_sparsetools.cp313t-win_arm64.lib +0 -0
  1032. scipy/sparse/_sparsetools.cp313t-win_arm64.pyd +0 -0
  1033. scipy/sparse/_spfuncs.py +76 -0
  1034. scipy/sparse/_sputils.py +632 -0
  1035. scipy/sparse/base.py +24 -0
  1036. scipy/sparse/bsr.py +22 -0
  1037. scipy/sparse/compressed.py +20 -0
  1038. scipy/sparse/construct.py +38 -0
  1039. scipy/sparse/coo.py +23 -0
  1040. scipy/sparse/csc.py +22 -0
  1041. scipy/sparse/csgraph/__init__.py +210 -0
  1042. scipy/sparse/csgraph/_flow.cp313t-win_arm64.lib +0 -0
  1043. scipy/sparse/csgraph/_flow.cp313t-win_arm64.pyd +0 -0
  1044. scipy/sparse/csgraph/_laplacian.py +563 -0
  1045. scipy/sparse/csgraph/_matching.cp313t-win_arm64.lib +0 -0
  1046. scipy/sparse/csgraph/_matching.cp313t-win_arm64.pyd +0 -0
  1047. scipy/sparse/csgraph/_min_spanning_tree.cp313t-win_arm64.lib +0 -0
  1048. scipy/sparse/csgraph/_min_spanning_tree.cp313t-win_arm64.pyd +0 -0
  1049. scipy/sparse/csgraph/_reordering.cp313t-win_arm64.lib +0 -0
  1050. scipy/sparse/csgraph/_reordering.cp313t-win_arm64.pyd +0 -0
  1051. scipy/sparse/csgraph/_shortest_path.cp313t-win_arm64.lib +0 -0
  1052. scipy/sparse/csgraph/_shortest_path.cp313t-win_arm64.pyd +0 -0
  1053. scipy/sparse/csgraph/_tools.cp313t-win_arm64.lib +0 -0
  1054. scipy/sparse/csgraph/_tools.cp313t-win_arm64.pyd +0 -0
  1055. scipy/sparse/csgraph/_traversal.cp313t-win_arm64.lib +0 -0
  1056. scipy/sparse/csgraph/_traversal.cp313t-win_arm64.pyd +0 -0
  1057. scipy/sparse/csgraph/_validation.py +66 -0
  1058. scipy/sparse/csgraph/tests/__init__.py +0 -0
  1059. scipy/sparse/csgraph/tests/test_connected_components.py +119 -0
  1060. scipy/sparse/csgraph/tests/test_conversions.py +61 -0
  1061. scipy/sparse/csgraph/tests/test_flow.py +209 -0
  1062. scipy/sparse/csgraph/tests/test_graph_laplacian.py +368 -0
  1063. scipy/sparse/csgraph/tests/test_matching.py +307 -0
  1064. scipy/sparse/csgraph/tests/test_pydata_sparse.py +197 -0
  1065. scipy/sparse/csgraph/tests/test_reordering.py +70 -0
  1066. scipy/sparse/csgraph/tests/test_shortest_path.py +540 -0
  1067. scipy/sparse/csgraph/tests/test_spanning_tree.py +66 -0
  1068. scipy/sparse/csgraph/tests/test_traversal.py +148 -0
  1069. scipy/sparse/csr.py +22 -0
  1070. scipy/sparse/data.py +18 -0
  1071. scipy/sparse/dia.py +22 -0
  1072. scipy/sparse/dok.py +22 -0
  1073. scipy/sparse/extract.py +23 -0
  1074. scipy/sparse/lil.py +22 -0
  1075. scipy/sparse/linalg/__init__.py +148 -0
  1076. scipy/sparse/linalg/_dsolve/__init__.py +71 -0
  1077. scipy/sparse/linalg/_dsolve/_add_newdocs.py +147 -0
  1078. scipy/sparse/linalg/_dsolve/_superlu.cp313t-win_arm64.lib +0 -0
  1079. scipy/sparse/linalg/_dsolve/_superlu.cp313t-win_arm64.pyd +0 -0
  1080. scipy/sparse/linalg/_dsolve/linsolve.py +882 -0
  1081. scipy/sparse/linalg/_dsolve/tests/__init__.py +0 -0
  1082. scipy/sparse/linalg/_dsolve/tests/test_linsolve.py +928 -0
  1083. scipy/sparse/linalg/_eigen/__init__.py +22 -0
  1084. scipy/sparse/linalg/_eigen/_svds.py +540 -0
  1085. scipy/sparse/linalg/_eigen/_svds_doc.py +382 -0
  1086. scipy/sparse/linalg/_eigen/arpack/COPYING +45 -0
  1087. scipy/sparse/linalg/_eigen/arpack/__init__.py +20 -0
  1088. scipy/sparse/linalg/_eigen/arpack/_arpack.cp313t-win_arm64.lib +0 -0
  1089. scipy/sparse/linalg/_eigen/arpack/_arpack.cp313t-win_arm64.pyd +0 -0
  1090. scipy/sparse/linalg/_eigen/arpack/arpack.py +1706 -0
  1091. scipy/sparse/linalg/_eigen/arpack/tests/__init__.py +0 -0
  1092. scipy/sparse/linalg/_eigen/arpack/tests/test_arpack.py +717 -0
  1093. scipy/sparse/linalg/_eigen/lobpcg/__init__.py +16 -0
  1094. scipy/sparse/linalg/_eigen/lobpcg/lobpcg.py +1110 -0
  1095. scipy/sparse/linalg/_eigen/lobpcg/tests/__init__.py +0 -0
  1096. scipy/sparse/linalg/_eigen/lobpcg/tests/test_lobpcg.py +725 -0
  1097. scipy/sparse/linalg/_eigen/tests/__init__.py +0 -0
  1098. scipy/sparse/linalg/_eigen/tests/test_svds.py +886 -0
  1099. scipy/sparse/linalg/_expm_multiply.py +816 -0
  1100. scipy/sparse/linalg/_interface.py +920 -0
  1101. scipy/sparse/linalg/_isolve/__init__.py +20 -0
  1102. scipy/sparse/linalg/_isolve/_gcrotmk.py +503 -0
  1103. scipy/sparse/linalg/_isolve/iterative.py +1051 -0
  1104. scipy/sparse/linalg/_isolve/lgmres.py +230 -0
  1105. scipy/sparse/linalg/_isolve/lsmr.py +486 -0
  1106. scipy/sparse/linalg/_isolve/lsqr.py +589 -0
  1107. scipy/sparse/linalg/_isolve/minres.py +372 -0
  1108. scipy/sparse/linalg/_isolve/tests/__init__.py +0 -0
  1109. scipy/sparse/linalg/_isolve/tests/test_gcrotmk.py +183 -0
  1110. scipy/sparse/linalg/_isolve/tests/test_iterative.py +809 -0
  1111. scipy/sparse/linalg/_isolve/tests/test_lgmres.py +225 -0
  1112. scipy/sparse/linalg/_isolve/tests/test_lsmr.py +185 -0
  1113. scipy/sparse/linalg/_isolve/tests/test_lsqr.py +120 -0
  1114. scipy/sparse/linalg/_isolve/tests/test_minres.py +97 -0
  1115. scipy/sparse/linalg/_isolve/tests/test_utils.py +9 -0
  1116. scipy/sparse/linalg/_isolve/tfqmr.py +179 -0
  1117. scipy/sparse/linalg/_isolve/utils.py +121 -0
  1118. scipy/sparse/linalg/_matfuncs.py +940 -0
  1119. scipy/sparse/linalg/_norm.py +195 -0
  1120. scipy/sparse/linalg/_onenormest.py +467 -0
  1121. scipy/sparse/linalg/_propack/_cpropack.cp313t-win_arm64.lib +0 -0
  1122. scipy/sparse/linalg/_propack/_cpropack.cp313t-win_arm64.pyd +0 -0
  1123. scipy/sparse/linalg/_propack/_dpropack.cp313t-win_arm64.lib +0 -0
  1124. scipy/sparse/linalg/_propack/_dpropack.cp313t-win_arm64.pyd +0 -0
  1125. scipy/sparse/linalg/_propack/_spropack.cp313t-win_arm64.lib +0 -0
  1126. scipy/sparse/linalg/_propack/_spropack.cp313t-win_arm64.pyd +0 -0
  1127. scipy/sparse/linalg/_propack/_zpropack.cp313t-win_arm64.lib +0 -0
  1128. scipy/sparse/linalg/_propack/_zpropack.cp313t-win_arm64.pyd +0 -0
  1129. scipy/sparse/linalg/_special_sparse_arrays.py +949 -0
  1130. scipy/sparse/linalg/_svdp.py +309 -0
  1131. scipy/sparse/linalg/dsolve.py +22 -0
  1132. scipy/sparse/linalg/eigen.py +21 -0
  1133. scipy/sparse/linalg/interface.py +20 -0
  1134. scipy/sparse/linalg/isolve.py +22 -0
  1135. scipy/sparse/linalg/matfuncs.py +18 -0
  1136. scipy/sparse/linalg/tests/__init__.py +0 -0
  1137. scipy/sparse/linalg/tests/propack_test_data.npz +0 -0
  1138. scipy/sparse/linalg/tests/test_expm_multiply.py +367 -0
  1139. scipy/sparse/linalg/tests/test_interface.py +561 -0
  1140. scipy/sparse/linalg/tests/test_matfuncs.py +592 -0
  1141. scipy/sparse/linalg/tests/test_norm.py +154 -0
  1142. scipy/sparse/linalg/tests/test_onenormest.py +252 -0
  1143. scipy/sparse/linalg/tests/test_propack.py +165 -0
  1144. scipy/sparse/linalg/tests/test_pydata_sparse.py +272 -0
  1145. scipy/sparse/linalg/tests/test_special_sparse_arrays.py +337 -0
  1146. scipy/sparse/sparsetools.py +17 -0
  1147. scipy/sparse/spfuncs.py +17 -0
  1148. scipy/sparse/sputils.py +17 -0
  1149. scipy/sparse/tests/__init__.py +0 -0
  1150. scipy/sparse/tests/data/csc_py2.npz +0 -0
  1151. scipy/sparse/tests/data/csc_py3.npz +0 -0
  1152. scipy/sparse/tests/test_arithmetic1d.py +341 -0
  1153. scipy/sparse/tests/test_array_api.py +561 -0
  1154. scipy/sparse/tests/test_base.py +5870 -0
  1155. scipy/sparse/tests/test_common1d.py +447 -0
  1156. scipy/sparse/tests/test_construct.py +872 -0
  1157. scipy/sparse/tests/test_coo.py +1119 -0
  1158. scipy/sparse/tests/test_csc.py +98 -0
  1159. scipy/sparse/tests/test_csr.py +214 -0
  1160. scipy/sparse/tests/test_dok.py +209 -0
  1161. scipy/sparse/tests/test_extract.py +51 -0
  1162. scipy/sparse/tests/test_indexing1d.py +603 -0
  1163. scipy/sparse/tests/test_matrix_io.py +109 -0
  1164. scipy/sparse/tests/test_minmax1d.py +128 -0
  1165. scipy/sparse/tests/test_sparsetools.py +344 -0
  1166. scipy/sparse/tests/test_spfuncs.py +97 -0
  1167. scipy/sparse/tests/test_sputils.py +424 -0
  1168. scipy/spatial/__init__.py +129 -0
  1169. scipy/spatial/_ckdtree.cp313t-win_arm64.lib +0 -0
  1170. scipy/spatial/_ckdtree.cp313t-win_arm64.pyd +0 -0
  1171. scipy/spatial/_distance_pybind.cp313t-win_arm64.lib +0 -0
  1172. scipy/spatial/_distance_pybind.cp313t-win_arm64.pyd +0 -0
  1173. scipy/spatial/_distance_wrap.cp313t-win_arm64.lib +0 -0
  1174. scipy/spatial/_distance_wrap.cp313t-win_arm64.pyd +0 -0
  1175. scipy/spatial/_geometric_slerp.py +238 -0
  1176. scipy/spatial/_hausdorff.cp313t-win_arm64.lib +0 -0
  1177. scipy/spatial/_hausdorff.cp313t-win_arm64.pyd +0 -0
  1178. scipy/spatial/_kdtree.py +920 -0
  1179. scipy/spatial/_plotutils.py +274 -0
  1180. scipy/spatial/_procrustes.py +132 -0
  1181. scipy/spatial/_qhull.cp313t-win_arm64.lib +0 -0
  1182. scipy/spatial/_qhull.cp313t-win_arm64.pyd +0 -0
  1183. scipy/spatial/_qhull.pyi +213 -0
  1184. scipy/spatial/_spherical_voronoi.py +341 -0
  1185. scipy/spatial/_voronoi.cp313t-win_arm64.lib +0 -0
  1186. scipy/spatial/_voronoi.cp313t-win_arm64.pyd +0 -0
  1187. scipy/spatial/_voronoi.pyi +4 -0
  1188. scipy/spatial/ckdtree.py +18 -0
  1189. scipy/spatial/distance.py +3147 -0
  1190. scipy/spatial/distance.pyi +210 -0
  1191. scipy/spatial/kdtree.py +25 -0
  1192. scipy/spatial/qhull.py +25 -0
  1193. scipy/spatial/qhull_src/COPYING_QHULL.txt +39 -0
  1194. scipy/spatial/tests/__init__.py +0 -0
  1195. scipy/spatial/tests/data/cdist-X1.txt +10 -0
  1196. scipy/spatial/tests/data/cdist-X2.txt +20 -0
  1197. scipy/spatial/tests/data/degenerate_pointset.npz +0 -0
  1198. scipy/spatial/tests/data/iris.txt +150 -0
  1199. scipy/spatial/tests/data/pdist-boolean-inp.txt +20 -0
  1200. scipy/spatial/tests/data/pdist-chebyshev-ml-iris.txt +1 -0
  1201. scipy/spatial/tests/data/pdist-chebyshev-ml.txt +1 -0
  1202. scipy/spatial/tests/data/pdist-cityblock-ml-iris.txt +1 -0
  1203. scipy/spatial/tests/data/pdist-cityblock-ml.txt +1 -0
  1204. scipy/spatial/tests/data/pdist-correlation-ml-iris.txt +1 -0
  1205. scipy/spatial/tests/data/pdist-correlation-ml.txt +1 -0
  1206. scipy/spatial/tests/data/pdist-cosine-ml-iris.txt +1 -0
  1207. scipy/spatial/tests/data/pdist-cosine-ml.txt +1 -0
  1208. scipy/spatial/tests/data/pdist-double-inp.txt +20 -0
  1209. scipy/spatial/tests/data/pdist-euclidean-ml-iris.txt +1 -0
  1210. scipy/spatial/tests/data/pdist-euclidean-ml.txt +1 -0
  1211. scipy/spatial/tests/data/pdist-hamming-ml.txt +1 -0
  1212. scipy/spatial/tests/data/pdist-jaccard-ml.txt +1 -0
  1213. scipy/spatial/tests/data/pdist-jensenshannon-ml-iris.txt +1 -0
  1214. scipy/spatial/tests/data/pdist-jensenshannon-ml.txt +1 -0
  1215. scipy/spatial/tests/data/pdist-minkowski-3.2-ml-iris.txt +1 -0
  1216. scipy/spatial/tests/data/pdist-minkowski-3.2-ml.txt +1 -0
  1217. scipy/spatial/tests/data/pdist-minkowski-5.8-ml-iris.txt +1 -0
  1218. scipy/spatial/tests/data/pdist-seuclidean-ml-iris.txt +1 -0
  1219. scipy/spatial/tests/data/pdist-seuclidean-ml.txt +1 -0
  1220. scipy/spatial/tests/data/pdist-spearman-ml.txt +1 -0
  1221. scipy/spatial/tests/data/random-bool-data.txt +100 -0
  1222. scipy/spatial/tests/data/random-double-data.txt +100 -0
  1223. scipy/spatial/tests/data/random-int-data.txt +100 -0
  1224. scipy/spatial/tests/data/random-uint-data.txt +100 -0
  1225. scipy/spatial/tests/data/selfdual-4d-polytope.txt +27 -0
  1226. scipy/spatial/tests/test__plotutils.py +91 -0
  1227. scipy/spatial/tests/test__procrustes.py +116 -0
  1228. scipy/spatial/tests/test_distance.py +2389 -0
  1229. scipy/spatial/tests/test_hausdorff.py +199 -0
  1230. scipy/spatial/tests/test_kdtree.py +1536 -0
  1231. scipy/spatial/tests/test_qhull.py +1313 -0
  1232. scipy/spatial/tests/test_slerp.py +417 -0
  1233. scipy/spatial/tests/test_spherical_voronoi.py +358 -0
  1234. scipy/spatial/transform/__init__.py +31 -0
  1235. scipy/spatial/transform/_rigid_transform.cp313t-win_arm64.lib +0 -0
  1236. scipy/spatial/transform/_rigid_transform.cp313t-win_arm64.pyd +0 -0
  1237. scipy/spatial/transform/_rotation.cp313t-win_arm64.lib +0 -0
  1238. scipy/spatial/transform/_rotation.cp313t-win_arm64.pyd +0 -0
  1239. scipy/spatial/transform/_rotation_groups.py +140 -0
  1240. scipy/spatial/transform/_rotation_spline.py +460 -0
  1241. scipy/spatial/transform/rotation.py +21 -0
  1242. scipy/spatial/transform/tests/__init__.py +0 -0
  1243. scipy/spatial/transform/tests/test_rigid_transform.py +1221 -0
  1244. scipy/spatial/transform/tests/test_rotation.py +2569 -0
  1245. scipy/spatial/transform/tests/test_rotation_groups.py +169 -0
  1246. scipy/spatial/transform/tests/test_rotation_spline.py +183 -0
  1247. scipy/special/__init__.pxd +1 -0
  1248. scipy/special/__init__.py +841 -0
  1249. scipy/special/_add_newdocs.py +9961 -0
  1250. scipy/special/_basic.py +3576 -0
  1251. scipy/special/_comb.cp313t-win_arm64.lib +0 -0
  1252. scipy/special/_comb.cp313t-win_arm64.pyd +0 -0
  1253. scipy/special/_ellip_harm.py +214 -0
  1254. scipy/special/_ellip_harm_2.cp313t-win_arm64.lib +0 -0
  1255. scipy/special/_ellip_harm_2.cp313t-win_arm64.pyd +0 -0
  1256. scipy/special/_gufuncs.cp313t-win_arm64.lib +0 -0
  1257. scipy/special/_gufuncs.cp313t-win_arm64.pyd +0 -0
  1258. scipy/special/_input_validation.py +17 -0
  1259. scipy/special/_lambertw.py +149 -0
  1260. scipy/special/_logsumexp.py +426 -0
  1261. scipy/special/_mptestutils.py +453 -0
  1262. scipy/special/_multiufuncs.py +610 -0
  1263. scipy/special/_orthogonal.py +2592 -0
  1264. scipy/special/_orthogonal.pyi +330 -0
  1265. scipy/special/_precompute/__init__.py +0 -0
  1266. scipy/special/_precompute/cosine_cdf.py +17 -0
  1267. scipy/special/_precompute/expn_asy.py +54 -0
  1268. scipy/special/_precompute/gammainc_asy.py +116 -0
  1269. scipy/special/_precompute/gammainc_data.py +124 -0
  1270. scipy/special/_precompute/hyp2f1_data.py +484 -0
  1271. scipy/special/_precompute/lambertw.py +68 -0
  1272. scipy/special/_precompute/loggamma.py +43 -0
  1273. scipy/special/_precompute/struve_convergence.py +131 -0
  1274. scipy/special/_precompute/utils.py +38 -0
  1275. scipy/special/_precompute/wright_bessel.py +342 -0
  1276. scipy/special/_precompute/wright_bessel_data.py +152 -0
  1277. scipy/special/_precompute/wrightomega.py +41 -0
  1278. scipy/special/_precompute/zetac.py +27 -0
  1279. scipy/special/_sf_error.py +15 -0
  1280. scipy/special/_specfun.cp313t-win_arm64.lib +0 -0
  1281. scipy/special/_specfun.cp313t-win_arm64.pyd +0 -0
  1282. scipy/special/_special_ufuncs.cp313t-win_arm64.lib +0 -0
  1283. scipy/special/_special_ufuncs.cp313t-win_arm64.pyd +0 -0
  1284. scipy/special/_spfun_stats.py +106 -0
  1285. scipy/special/_spherical_bessel.py +397 -0
  1286. scipy/special/_support_alternative_backends.py +295 -0
  1287. scipy/special/_test_internal.cp313t-win_arm64.lib +0 -0
  1288. scipy/special/_test_internal.cp313t-win_arm64.pyd +0 -0
  1289. scipy/special/_test_internal.pyi +9 -0
  1290. scipy/special/_testutils.py +321 -0
  1291. scipy/special/_ufuncs.cp313t-win_arm64.lib +0 -0
  1292. scipy/special/_ufuncs.cp313t-win_arm64.pyd +0 -0
  1293. scipy/special/_ufuncs.pyi +522 -0
  1294. scipy/special/_ufuncs.pyx +13173 -0
  1295. scipy/special/_ufuncs_cxx.cp313t-win_arm64.lib +0 -0
  1296. scipy/special/_ufuncs_cxx.cp313t-win_arm64.pyd +0 -0
  1297. scipy/special/_ufuncs_cxx.pxd +142 -0
  1298. scipy/special/_ufuncs_cxx.pyx +427 -0
  1299. scipy/special/_ufuncs_cxx_defs.h +147 -0
  1300. scipy/special/_ufuncs_defs.h +57 -0
  1301. scipy/special/add_newdocs.py +15 -0
  1302. scipy/special/basic.py +87 -0
  1303. scipy/special/cython_special.cp313t-win_arm64.lib +0 -0
  1304. scipy/special/cython_special.cp313t-win_arm64.pyd +0 -0
  1305. scipy/special/cython_special.pxd +259 -0
  1306. scipy/special/cython_special.pyi +3 -0
  1307. scipy/special/orthogonal.py +45 -0
  1308. scipy/special/sf_error.py +20 -0
  1309. scipy/special/specfun.py +24 -0
  1310. scipy/special/spfun_stats.py +17 -0
  1311. scipy/special/tests/__init__.py +0 -0
  1312. scipy/special/tests/_cython_examples/extending.pyx +12 -0
  1313. scipy/special/tests/_cython_examples/meson.build +34 -0
  1314. scipy/special/tests/data/__init__.py +0 -0
  1315. scipy/special/tests/data/boost.npz +0 -0
  1316. scipy/special/tests/data/gsl.npz +0 -0
  1317. scipy/special/tests/data/local.npz +0 -0
  1318. scipy/special/tests/test_basic.py +4815 -0
  1319. scipy/special/tests/test_bdtr.py +112 -0
  1320. scipy/special/tests/test_boost_ufuncs.py +64 -0
  1321. scipy/special/tests/test_boxcox.py +125 -0
  1322. scipy/special/tests/test_cdflib.py +712 -0
  1323. scipy/special/tests/test_cdft_asymptotic.py +49 -0
  1324. scipy/special/tests/test_cephes_intp_cast.py +29 -0
  1325. scipy/special/tests/test_cosine_distr.py +83 -0
  1326. scipy/special/tests/test_cython_special.py +363 -0
  1327. scipy/special/tests/test_data.py +719 -0
  1328. scipy/special/tests/test_dd.py +42 -0
  1329. scipy/special/tests/test_digamma.py +45 -0
  1330. scipy/special/tests/test_ellip_harm.py +278 -0
  1331. scipy/special/tests/test_erfinv.py +89 -0
  1332. scipy/special/tests/test_exponential_integrals.py +118 -0
  1333. scipy/special/tests/test_extending.py +28 -0
  1334. scipy/special/tests/test_faddeeva.py +85 -0
  1335. scipy/special/tests/test_gamma.py +12 -0
  1336. scipy/special/tests/test_gammainc.py +152 -0
  1337. scipy/special/tests/test_hyp2f1.py +2566 -0
  1338. scipy/special/tests/test_hypergeometric.py +234 -0
  1339. scipy/special/tests/test_iv_ratio.py +249 -0
  1340. scipy/special/tests/test_kolmogorov.py +491 -0
  1341. scipy/special/tests/test_lambertw.py +109 -0
  1342. scipy/special/tests/test_legendre.py +1518 -0
  1343. scipy/special/tests/test_log1mexp.py +85 -0
  1344. scipy/special/tests/test_loggamma.py +70 -0
  1345. scipy/special/tests/test_logit.py +162 -0
  1346. scipy/special/tests/test_logsumexp.py +469 -0
  1347. scipy/special/tests/test_mpmath.py +2293 -0
  1348. scipy/special/tests/test_nan_inputs.py +65 -0
  1349. scipy/special/tests/test_ndtr.py +77 -0
  1350. scipy/special/tests/test_ndtri_exp.py +94 -0
  1351. scipy/special/tests/test_orthogonal.py +821 -0
  1352. scipy/special/tests/test_orthogonal_eval.py +275 -0
  1353. scipy/special/tests/test_owens_t.py +53 -0
  1354. scipy/special/tests/test_pcf.py +24 -0
  1355. scipy/special/tests/test_pdtr.py +48 -0
  1356. scipy/special/tests/test_powm1.py +65 -0
  1357. scipy/special/tests/test_precompute_expn_asy.py +24 -0
  1358. scipy/special/tests/test_precompute_gammainc.py +108 -0
  1359. scipy/special/tests/test_precompute_utils.py +36 -0
  1360. scipy/special/tests/test_round.py +18 -0
  1361. scipy/special/tests/test_sf_error.py +146 -0
  1362. scipy/special/tests/test_sici.py +36 -0
  1363. scipy/special/tests/test_specfun.py +48 -0
  1364. scipy/special/tests/test_spence.py +32 -0
  1365. scipy/special/tests/test_spfun_stats.py +61 -0
  1366. scipy/special/tests/test_sph_harm.py +85 -0
  1367. scipy/special/tests/test_spherical_bessel.py +400 -0
  1368. scipy/special/tests/test_support_alternative_backends.py +248 -0
  1369. scipy/special/tests/test_trig.py +72 -0
  1370. scipy/special/tests/test_ufunc_signatures.py +46 -0
  1371. scipy/special/tests/test_wright_bessel.py +205 -0
  1372. scipy/special/tests/test_wrightomega.py +117 -0
  1373. scipy/special/tests/test_zeta.py +301 -0
  1374. scipy/stats/__init__.py +670 -0
  1375. scipy/stats/_ansari_swilk_statistics.cp313t-win_arm64.lib +0 -0
  1376. scipy/stats/_ansari_swilk_statistics.cp313t-win_arm64.pyd +0 -0
  1377. scipy/stats/_axis_nan_policy.py +692 -0
  1378. scipy/stats/_biasedurn.cp313t-win_arm64.lib +0 -0
  1379. scipy/stats/_biasedurn.cp313t-win_arm64.pyd +0 -0
  1380. scipy/stats/_biasedurn.pxd +27 -0
  1381. scipy/stats/_binned_statistic.py +795 -0
  1382. scipy/stats/_binomtest.py +375 -0
  1383. scipy/stats/_bws_test.py +177 -0
  1384. scipy/stats/_censored_data.py +459 -0
  1385. scipy/stats/_common.py +5 -0
  1386. scipy/stats/_constants.py +42 -0
  1387. scipy/stats/_continued_fraction.py +387 -0
  1388. scipy/stats/_continuous_distns.py +12486 -0
  1389. scipy/stats/_correlation.py +210 -0
  1390. scipy/stats/_covariance.py +636 -0
  1391. scipy/stats/_crosstab.py +204 -0
  1392. scipy/stats/_discrete_distns.py +2098 -0
  1393. scipy/stats/_distn_infrastructure.py +4201 -0
  1394. scipy/stats/_distr_params.py +299 -0
  1395. scipy/stats/_distribution_infrastructure.py +5750 -0
  1396. scipy/stats/_entropy.py +428 -0
  1397. scipy/stats/_finite_differences.py +145 -0
  1398. scipy/stats/_fit.py +1351 -0
  1399. scipy/stats/_hypotests.py +2060 -0
  1400. scipy/stats/_kde.py +732 -0
  1401. scipy/stats/_ksstats.py +600 -0
  1402. scipy/stats/_levy_stable/__init__.py +1231 -0
  1403. scipy/stats/_levy_stable/levyst.cp313t-win_arm64.lib +0 -0
  1404. scipy/stats/_levy_stable/levyst.cp313t-win_arm64.pyd +0 -0
  1405. scipy/stats/_mannwhitneyu.py +492 -0
  1406. scipy/stats/_mgc.py +550 -0
  1407. scipy/stats/_morestats.py +4626 -0
  1408. scipy/stats/_mstats_basic.py +3658 -0
  1409. scipy/stats/_mstats_extras.py +521 -0
  1410. scipy/stats/_multicomp.py +449 -0
  1411. scipy/stats/_multivariate.py +7281 -0
  1412. scipy/stats/_new_distributions.py +452 -0
  1413. scipy/stats/_odds_ratio.py +466 -0
  1414. scipy/stats/_page_trend_test.py +486 -0
  1415. scipy/stats/_probability_distribution.py +1964 -0
  1416. scipy/stats/_qmc.py +2956 -0
  1417. scipy/stats/_qmc_cy.cp313t-win_arm64.lib +0 -0
  1418. scipy/stats/_qmc_cy.cp313t-win_arm64.pyd +0 -0
  1419. scipy/stats/_qmc_cy.pyi +54 -0
  1420. scipy/stats/_qmvnt.py +454 -0
  1421. scipy/stats/_qmvnt_cy.cp313t-win_arm64.lib +0 -0
  1422. scipy/stats/_qmvnt_cy.cp313t-win_arm64.pyd +0 -0
  1423. scipy/stats/_quantile.py +335 -0
  1424. scipy/stats/_rcont/__init__.py +4 -0
  1425. scipy/stats/_rcont/rcont.cp313t-win_arm64.lib +0 -0
  1426. scipy/stats/_rcont/rcont.cp313t-win_arm64.pyd +0 -0
  1427. scipy/stats/_relative_risk.py +263 -0
  1428. scipy/stats/_resampling.py +2352 -0
  1429. scipy/stats/_result_classes.py +40 -0
  1430. scipy/stats/_sampling.py +1314 -0
  1431. scipy/stats/_sensitivity_analysis.py +713 -0
  1432. scipy/stats/_sobol.cp313t-win_arm64.lib +0 -0
  1433. scipy/stats/_sobol.cp313t-win_arm64.pyd +0 -0
  1434. scipy/stats/_sobol.pyi +54 -0
  1435. scipy/stats/_sobol_direction_numbers.npz +0 -0
  1436. scipy/stats/_stats.cp313t-win_arm64.lib +0 -0
  1437. scipy/stats/_stats.cp313t-win_arm64.pyd +0 -0
  1438. scipy/stats/_stats.pxd +10 -0
  1439. scipy/stats/_stats_mstats_common.py +322 -0
  1440. scipy/stats/_stats_py.py +11089 -0
  1441. scipy/stats/_stats_pythran.cp313t-win_arm64.lib +0 -0
  1442. scipy/stats/_stats_pythran.cp313t-win_arm64.pyd +0 -0
  1443. scipy/stats/_survival.py +683 -0
  1444. scipy/stats/_tukeylambda_stats.py +199 -0
  1445. scipy/stats/_unuran/__init__.py +0 -0
  1446. scipy/stats/_unuran/unuran_wrapper.cp313t-win_arm64.lib +0 -0
  1447. scipy/stats/_unuran/unuran_wrapper.cp313t-win_arm64.pyd +0 -0
  1448. scipy/stats/_unuran/unuran_wrapper.pyi +179 -0
  1449. scipy/stats/_variation.py +126 -0
  1450. scipy/stats/_warnings_errors.py +38 -0
  1451. scipy/stats/_wilcoxon.py +265 -0
  1452. scipy/stats/biasedurn.py +16 -0
  1453. scipy/stats/contingency.py +521 -0
  1454. scipy/stats/distributions.py +24 -0
  1455. scipy/stats/kde.py +18 -0
  1456. scipy/stats/morestats.py +27 -0
  1457. scipy/stats/mstats.py +140 -0
  1458. scipy/stats/mstats_basic.py +42 -0
  1459. scipy/stats/mstats_extras.py +25 -0
  1460. scipy/stats/mvn.py +17 -0
  1461. scipy/stats/qmc.py +236 -0
  1462. scipy/stats/sampling.py +73 -0
  1463. scipy/stats/stats.py +41 -0
  1464. scipy/stats/tests/__init__.py +0 -0
  1465. scipy/stats/tests/common_tests.py +356 -0
  1466. scipy/stats/tests/data/_mvt.py +171 -0
  1467. scipy/stats/tests/data/fisher_exact_results_from_r.py +607 -0
  1468. scipy/stats/tests/data/jf_skew_t_gamlss_pdf_data.npy +0 -0
  1469. scipy/stats/tests/data/levy_stable/stable-Z1-cdf-sample-data.npy +0 -0
  1470. scipy/stats/tests/data/levy_stable/stable-Z1-pdf-sample-data.npy +0 -0
  1471. scipy/stats/tests/data/levy_stable/stable-loc-scale-sample-data.npy +0 -0
  1472. scipy/stats/tests/data/nist_anova/AtmWtAg.dat +108 -0
  1473. scipy/stats/tests/data/nist_anova/SiRstv.dat +85 -0
  1474. scipy/stats/tests/data/nist_anova/SmLs01.dat +249 -0
  1475. scipy/stats/tests/data/nist_anova/SmLs02.dat +1869 -0
  1476. scipy/stats/tests/data/nist_anova/SmLs03.dat +18069 -0
  1477. scipy/stats/tests/data/nist_anova/SmLs04.dat +249 -0
  1478. scipy/stats/tests/data/nist_anova/SmLs05.dat +1869 -0
  1479. scipy/stats/tests/data/nist_anova/SmLs06.dat +18069 -0
  1480. scipy/stats/tests/data/nist_anova/SmLs07.dat +249 -0
  1481. scipy/stats/tests/data/nist_anova/SmLs08.dat +1869 -0
  1482. scipy/stats/tests/data/nist_anova/SmLs09.dat +18069 -0
  1483. scipy/stats/tests/data/nist_linregress/Norris.dat +97 -0
  1484. scipy/stats/tests/data/rel_breitwigner_pdf_sample_data_ROOT.npy +0 -0
  1485. scipy/stats/tests/data/studentized_range_mpmath_ref.json +1499 -0
  1486. scipy/stats/tests/test_axis_nan_policy.py +1388 -0
  1487. scipy/stats/tests/test_binned_statistic.py +568 -0
  1488. scipy/stats/tests/test_censored_data.py +152 -0
  1489. scipy/stats/tests/test_contingency.py +294 -0
  1490. scipy/stats/tests/test_continued_fraction.py +173 -0
  1491. scipy/stats/tests/test_continuous.py +2198 -0
  1492. scipy/stats/tests/test_continuous_basic.py +1053 -0
  1493. scipy/stats/tests/test_continuous_fit_censored.py +683 -0
  1494. scipy/stats/tests/test_correlation.py +80 -0
  1495. scipy/stats/tests/test_crosstab.py +115 -0
  1496. scipy/stats/tests/test_discrete_basic.py +580 -0
  1497. scipy/stats/tests/test_discrete_distns.py +700 -0
  1498. scipy/stats/tests/test_distributions.py +10413 -0
  1499. scipy/stats/tests/test_entropy.py +322 -0
  1500. scipy/stats/tests/test_fast_gen_inversion.py +435 -0
  1501. scipy/stats/tests/test_fit.py +1090 -0
  1502. scipy/stats/tests/test_hypotests.py +1991 -0
  1503. scipy/stats/tests/test_kdeoth.py +676 -0
  1504. scipy/stats/tests/test_marray.py +289 -0
  1505. scipy/stats/tests/test_mgc.py +217 -0
  1506. scipy/stats/tests/test_morestats.py +3259 -0
  1507. scipy/stats/tests/test_mstats_basic.py +2071 -0
  1508. scipy/stats/tests/test_mstats_extras.py +172 -0
  1509. scipy/stats/tests/test_multicomp.py +405 -0
  1510. scipy/stats/tests/test_multivariate.py +4381 -0
  1511. scipy/stats/tests/test_odds_ratio.py +148 -0
  1512. scipy/stats/tests/test_qmc.py +1492 -0
  1513. scipy/stats/tests/test_quantile.py +199 -0
  1514. scipy/stats/tests/test_rank.py +345 -0
  1515. scipy/stats/tests/test_relative_risk.py +95 -0
  1516. scipy/stats/tests/test_resampling.py +2000 -0
  1517. scipy/stats/tests/test_sampling.py +1450 -0
  1518. scipy/stats/tests/test_sensitivity_analysis.py +310 -0
  1519. scipy/stats/tests/test_stats.py +9707 -0
  1520. scipy/stats/tests/test_survival.py +466 -0
  1521. scipy/stats/tests/test_tukeylambda_stats.py +85 -0
  1522. scipy/stats/tests/test_variation.py +216 -0
  1523. scipy/version.py +12 -0
  1524. scipy-1.16.2.dist-info/DELVEWHEEL +2 -0
  1525. scipy-1.16.2.dist-info/LICENSE.txt +912 -0
  1526. scipy-1.16.2.dist-info/METADATA +1061 -0
  1527. scipy-1.16.2.dist-info/RECORD +1530 -0
  1528. scipy-1.16.2.dist-info/WHEEL +4 -0
  1529. scipy.libs/msvcp140-5f1c5dd31916990d94181e07bc3afb32.dll +0 -0
  1530. scipy.libs/scipy_openblas-f3ac85b1f412f7e86514c923dc4058d1.dll +0 -0
@@ -0,0 +1,4626 @@
1
+ import math
2
+ import warnings
3
+ import threading
4
+ from collections import namedtuple
5
+
6
+ import numpy as np
7
+ from numpy import (isscalar, r_, log, around, unique, asarray, zeros,
8
+ arange, sort, amin, amax, sqrt, array,
9
+ pi, exp, ravel, count_nonzero)
10
+
11
+ from scipy import optimize, special, interpolate, stats
12
+ from scipy._lib._bunch import _make_tuple_bunch
13
+ from scipy._lib._util import _rename_parameter, _contains_nan, _get_nan
14
+
15
+ from scipy._lib._array_api import (
16
+ array_namespace,
17
+ xp_size,
18
+ xp_vector_norm,
19
+ xp_promote,
20
+ )
21
+
22
+ from ._ansari_swilk_statistics import gscale, swilk
23
+ from . import _stats_py, _wilcoxon
24
+ from ._fit import FitResult
25
+ from ._stats_py import (find_repeats, _get_pvalue, SignificanceResult, # noqa:F401
26
+ _SimpleNormal, _SimpleChi2, _length_nonmasked)
27
+ from .contingency import chi2_contingency
28
+ from . import distributions
29
+ from ._distn_infrastructure import rv_generic
30
+ from ._axis_nan_policy import _axis_nan_policy_factory, _broadcast_arrays
31
+
32
+
33
+ __all__ = ['mvsdist',
34
+ 'bayes_mvs', 'kstat', 'kstatvar', 'probplot', 'ppcc_max', 'ppcc_plot',
35
+ 'boxcox_llf', 'boxcox', 'boxcox_normmax', 'boxcox_normplot',
36
+ 'shapiro', 'anderson', 'ansari', 'bartlett', 'levene',
37
+ 'fligner', 'mood', 'wilcoxon', 'median_test',
38
+ 'circmean', 'circvar', 'circstd', 'anderson_ksamp',
39
+ 'yeojohnson_llf', 'yeojohnson', 'yeojohnson_normmax',
40
+ 'yeojohnson_normplot', 'directional_stats',
41
+ 'false_discovery_control'
42
+ ]
43
+
44
+
45
+ Mean = namedtuple('Mean', ('statistic', 'minmax'))
46
+ Variance = namedtuple('Variance', ('statistic', 'minmax'))
47
+ Std_dev = namedtuple('Std_dev', ('statistic', 'minmax'))
48
+
49
+
50
+ def bayes_mvs(data, alpha=0.90):
51
+ r"""
52
+ Bayesian confidence intervals for the mean, var, and std.
53
+
54
+ Parameters
55
+ ----------
56
+ data : array_like
57
+ Input data, if multi-dimensional it is flattened to 1-D by `bayes_mvs`.
58
+ Requires 2 or more data points.
59
+ alpha : float, optional
60
+ Probability that the returned confidence interval contains
61
+ the true parameter.
62
+
63
+ Returns
64
+ -------
65
+ mean_cntr, var_cntr, std_cntr : tuple
66
+ The three results are for the mean, variance and standard deviation,
67
+ respectively. Each result is a tuple of the form::
68
+
69
+ (center, (lower, upper))
70
+
71
+ with ``center`` the mean of the conditional pdf of the value given the
72
+ data, and ``(lower, upper)`` a confidence interval, centered on the
73
+ median, containing the estimate to a probability ``alpha``.
74
+
75
+ See Also
76
+ --------
77
+ mvsdist
78
+
79
+ Notes
80
+ -----
81
+ Each tuple of mean, variance, and standard deviation estimates represent
82
+ the (center, (lower, upper)) with center the mean of the conditional pdf
83
+ of the value given the data and (lower, upper) is a confidence interval
84
+ centered on the median, containing the estimate to a probability
85
+ ``alpha``.
86
+
87
+ Converts data to 1-D and assumes all data has the same mean and variance.
88
+ Uses Jeffrey's prior for variance and std.
89
+
90
+ Equivalent to ``tuple((x.mean(), x.interval(alpha)) for x in mvsdist(dat))``
91
+
92
+ References
93
+ ----------
94
+ T.E. Oliphant, "A Bayesian perspective on estimating mean, variance, and
95
+ standard-deviation from data", https://scholarsarchive.byu.edu/facpub/278,
96
+ 2006.
97
+
98
+ Examples
99
+ --------
100
+ First a basic example to demonstrate the outputs:
101
+
102
+ >>> from scipy import stats
103
+ >>> data = [6, 9, 12, 7, 8, 8, 13]
104
+ >>> mean, var, std = stats.bayes_mvs(data)
105
+ >>> mean
106
+ Mean(statistic=9.0, minmax=(7.103650222612533, 10.896349777387467))
107
+ >>> var
108
+ Variance(statistic=10.0, minmax=(3.176724206, 24.45910382))
109
+ >>> std
110
+ Std_dev(statistic=2.9724954732045084,
111
+ minmax=(1.7823367265645143, 4.945614605014631))
112
+
113
+ Now we generate some normally distributed random data, and get estimates of
114
+ mean and standard deviation with 95% confidence intervals for those
115
+ estimates:
116
+
117
+ >>> n_samples = 100000
118
+ >>> data = stats.norm.rvs(size=n_samples)
119
+ >>> res_mean, res_var, res_std = stats.bayes_mvs(data, alpha=0.95)
120
+
121
+ >>> import matplotlib.pyplot as plt
122
+ >>> fig = plt.figure()
123
+ >>> ax = fig.add_subplot(111)
124
+ >>> ax.hist(data, bins=100, density=True, label='Histogram of data')
125
+ >>> ax.vlines(res_mean.statistic, 0, 0.5, colors='r', label='Estimated mean')
126
+ >>> ax.axvspan(res_mean.minmax[0],res_mean.minmax[1], facecolor='r',
127
+ ... alpha=0.2, label=r'Estimated mean (95% limits)')
128
+ >>> ax.vlines(res_std.statistic, 0, 0.5, colors='g', label='Estimated scale')
129
+ >>> ax.axvspan(res_std.minmax[0],res_std.minmax[1], facecolor='g', alpha=0.2,
130
+ ... label=r'Estimated scale (95% limits)')
131
+
132
+ >>> ax.legend(fontsize=10)
133
+ >>> ax.set_xlim([-4, 4])
134
+ >>> ax.set_ylim([0, 0.5])
135
+ >>> plt.show()
136
+
137
+ """
138
+ m, v, s = mvsdist(data)
139
+ if alpha >= 1 or alpha <= 0:
140
+ raise ValueError(f"0 < alpha < 1 is required, but {alpha=} was given.")
141
+
142
+ m_res = Mean(m.mean(), m.interval(alpha))
143
+ v_res = Variance(v.mean(), v.interval(alpha))
144
+ s_res = Std_dev(s.mean(), s.interval(alpha))
145
+
146
+ return m_res, v_res, s_res
147
+
148
+
149
+ def mvsdist(data):
150
+ """
151
+ 'Frozen' distributions for mean, variance, and standard deviation of data.
152
+
153
+ Parameters
154
+ ----------
155
+ data : array_like
156
+ Input array. Converted to 1-D using ravel.
157
+ Requires 2 or more data-points.
158
+
159
+ Returns
160
+ -------
161
+ mdist : "frozen" distribution object
162
+ Distribution object representing the mean of the data.
163
+ vdist : "frozen" distribution object
164
+ Distribution object representing the variance of the data.
165
+ sdist : "frozen" distribution object
166
+ Distribution object representing the standard deviation of the data.
167
+
168
+ See Also
169
+ --------
170
+ bayes_mvs
171
+
172
+ Notes
173
+ -----
174
+ The return values from ``bayes_mvs(data)`` is equivalent to
175
+ ``tuple((x.mean(), x.interval(0.90)) for x in mvsdist(data))``.
176
+
177
+ In other words, calling ``<dist>.mean()`` and ``<dist>.interval(0.90)``
178
+ on the three distribution objects returned from this function will give
179
+ the same results that are returned from `bayes_mvs`.
180
+
181
+ References
182
+ ----------
183
+ T.E. Oliphant, "A Bayesian perspective on estimating mean, variance, and
184
+ standard-deviation from data", https://scholarsarchive.byu.edu/facpub/278,
185
+ 2006.
186
+
187
+ Examples
188
+ --------
189
+ >>> from scipy import stats
190
+ >>> data = [6, 9, 12, 7, 8, 8, 13]
191
+ >>> mean, var, std = stats.mvsdist(data)
192
+
193
+ We now have frozen distribution objects "mean", "var" and "std" that we can
194
+ examine:
195
+
196
+ >>> mean.mean()
197
+ 9.0
198
+ >>> mean.interval(0.95)
199
+ (6.6120585482655692, 11.387941451734431)
200
+ >>> mean.std()
201
+ 1.1952286093343936
202
+
203
+ """
204
+ x = ravel(data)
205
+ n = len(x)
206
+ if n < 2:
207
+ raise ValueError("Need at least 2 data-points.")
208
+ xbar = x.mean()
209
+ C = x.var()
210
+ if n > 1000: # gaussian approximations for large n
211
+ mdist = distributions.norm(loc=xbar, scale=math.sqrt(C / n))
212
+ sdist = distributions.norm(loc=math.sqrt(C), scale=math.sqrt(C / (2. * n)))
213
+ vdist = distributions.norm(loc=C, scale=math.sqrt(2.0 / n) * C)
214
+ else:
215
+ nm1 = n - 1
216
+ fac = n * C / 2.
217
+ val = nm1 / 2.
218
+ mdist = distributions.t(nm1, loc=xbar, scale=math.sqrt(C / nm1))
219
+ sdist = distributions.gengamma(val, -2, scale=math.sqrt(fac))
220
+ vdist = distributions.invgamma(val, scale=fac)
221
+ return mdist, vdist, sdist
222
+
223
+
224
+ @_axis_nan_policy_factory(
225
+ lambda x: x, result_to_tuple=lambda x, _: (x,), n_outputs=1, default_axis=None
226
+ )
227
+ def kstat(data, n=2, *, axis=None):
228
+ r"""
229
+ Return the `n` th k-statistic ( ``1<=n<=4`` so far).
230
+
231
+ The `n` th k-statistic ``k_n`` is the unique symmetric unbiased estimator of the
232
+ `n` th cumulant :math:`\kappa_n` [1]_ [2]_.
233
+
234
+ Parameters
235
+ ----------
236
+ data : array_like
237
+ Input array.
238
+ n : int, {1, 2, 3, 4}, optional
239
+ Default is equal to 2.
240
+ axis : int or None, default: None
241
+ If an int, the axis of the input along which to compute the statistic.
242
+ The statistic of each axis-slice (e.g. row) of the input will appear
243
+ in a corresponding element of the output. If ``None``, the input will
244
+ be raveled before computing the statistic.
245
+
246
+ Returns
247
+ -------
248
+ kstat : float
249
+ The `n` th k-statistic.
250
+
251
+ See Also
252
+ --------
253
+ kstatvar : Returns an unbiased estimator of the variance of the k-statistic
254
+ moment : Returns the n-th central moment about the mean for a sample.
255
+
256
+ Notes
257
+ -----
258
+ For a sample size :math:`n`, the first few k-statistics are given by
259
+
260
+ .. math::
261
+
262
+ k_1 &= \frac{S_1}{n}, \\
263
+ k_2 &= \frac{nS_2 - S_1^2}{n(n-1)}, \\
264
+ k_3 &= \frac{2S_1^3 - 3nS_1S_2 + n^2S_3}{n(n-1)(n-2)}, \\
265
+ k_4 &= \frac{-6S_1^4 + 12nS_1^2S_2 - 3n(n-1)S_2^2 - 4n(n+1)S_1S_3
266
+ + n^2(n+1)S_4}{n (n-1)(n-2)(n-3)},
267
+
268
+ where
269
+
270
+ .. math::
271
+
272
+ S_r \equiv \sum_{i=1}^n X_i^r,
273
+
274
+ and :math:`X_i` is the :math:`i` th data point.
275
+
276
+ References
277
+ ----------
278
+ .. [1] http://mathworld.wolfram.com/k-Statistic.html
279
+
280
+ .. [2] http://mathworld.wolfram.com/Cumulant.html
281
+
282
+ Examples
283
+ --------
284
+ >>> from scipy import stats
285
+ >>> from numpy.random import default_rng
286
+ >>> rng = default_rng()
287
+
288
+ As sample size increases, `n`-th moment and `n`-th k-statistic converge to the
289
+ same number (although they aren't identical). In the case of the normal
290
+ distribution, they converge to zero.
291
+
292
+ >>> for i in range(2,8):
293
+ ... x = rng.normal(size=10**i)
294
+ ... m, k = stats.moment(x, 3), stats.kstat(x, 3)
295
+ ... print(f"{i=}: {m=:.3g}, {k=:.3g}, {(m-k)=:.3g}")
296
+ i=2: m=-0.631, k=-0.651, (m-k)=0.0194 # random
297
+ i=3: m=0.0282, k=0.0283, (m-k)=-8.49e-05
298
+ i=4: m=-0.0454, k=-0.0454, (m-k)=1.36e-05
299
+ i=6: m=7.53e-05, k=7.53e-05, (m-k)=-2.26e-09
300
+ i=7: m=0.00166, k=0.00166, (m-k)=-4.99e-09
301
+ i=8: m=-2.88e-06 k=-2.88e-06, (m-k)=8.63e-13
302
+ """
303
+ xp = array_namespace(data)
304
+ data = xp.asarray(data)
305
+ if n > 4 or n < 1:
306
+ raise ValueError("k-statistics only supported for 1<=n<=4")
307
+ n = int(n)
308
+ if axis is None:
309
+ data = xp.reshape(data, (-1,))
310
+ axis = 0
311
+
312
+ N = _length_nonmasked(data, axis, xp=xp)
313
+
314
+ S = [None] + [xp.sum(data**k, axis=axis) for k in range(1, n + 1)]
315
+ if n == 1:
316
+ return S[1] * 1.0/N
317
+ elif n == 2:
318
+ return (N*S[2] - S[1]**2.0) / (N*(N - 1.0))
319
+ elif n == 3:
320
+ return (2*S[1]**3 - 3*N*S[1]*S[2] + N*N*S[3]) / (N*(N - 1.0)*(N - 2.0))
321
+ elif n == 4:
322
+ return ((-6*S[1]**4 + 12*N*S[1]**2 * S[2] - 3*N*(N-1.0)*S[2]**2 -
323
+ 4*N*(N+1)*S[1]*S[3] + N*N*(N+1)*S[4]) /
324
+ (N*(N-1.0)*(N-2.0)*(N-3.0)))
325
+ else:
326
+ raise ValueError("Should not be here.")
327
+
328
+
329
+ @_axis_nan_policy_factory(
330
+ lambda x: x, result_to_tuple=lambda x, _: (x,), n_outputs=1, default_axis=None
331
+ )
332
+ def kstatvar(data, n=2, *, axis=None):
333
+ r"""Return an unbiased estimator of the variance of the k-statistic.
334
+
335
+ See `kstat` and [1]_ for more details about the k-statistic.
336
+
337
+ Parameters
338
+ ----------
339
+ data : array_like
340
+ Input array.
341
+ n : int, {1, 2}, optional
342
+ Default is equal to 2.
343
+ axis : int or None, default: None
344
+ If an int, the axis of the input along which to compute the statistic.
345
+ The statistic of each axis-slice (e.g. row) of the input will appear
346
+ in a corresponding element of the output. If ``None``, the input will
347
+ be raveled before computing the statistic.
348
+
349
+ Returns
350
+ -------
351
+ kstatvar : float
352
+ The `n` th k-statistic variance.
353
+
354
+ See Also
355
+ --------
356
+ kstat : Returns the n-th k-statistic.
357
+ moment : Returns the n-th central moment about the mean for a sample.
358
+
359
+ Notes
360
+ -----
361
+ Unbiased estimators of the variances of the first two k-statistics are given by
362
+
363
+ .. math::
364
+
365
+ \mathrm{var}(k_1) &= \frac{k_2}{n}, \\
366
+ \mathrm{var}(k_2) &= \frac{2k_2^2n + (n-1)k_4}{n(n - 1)}.
367
+
368
+ References
369
+ ----------
370
+ .. [1] http://mathworld.wolfram.com/k-Statistic.html
371
+
372
+ """ # noqa: E501
373
+ xp = array_namespace(data)
374
+ data = xp.asarray(data)
375
+ if axis is None:
376
+ data = xp.reshape(data, (-1,))
377
+ axis = 0
378
+ N = _length_nonmasked(data, axis, xp=xp)
379
+
380
+ if n == 1:
381
+ return kstat(data, n=2, axis=axis, _no_deco=True) * 1.0/N
382
+ elif n == 2:
383
+ k2 = kstat(data, n=2, axis=axis, _no_deco=True)
384
+ k4 = kstat(data, n=4, axis=axis, _no_deco=True)
385
+ return (2*N*k2**2 + (N-1)*k4) / (N*(N+1))
386
+ else:
387
+ raise ValueError("Only n=1 or n=2 supported.")
388
+
389
+
390
+ def _calc_uniform_order_statistic_medians(n):
391
+ """Approximations of uniform order statistic medians.
392
+
393
+ Parameters
394
+ ----------
395
+ n : int
396
+ Sample size.
397
+
398
+ Returns
399
+ -------
400
+ v : 1d float array
401
+ Approximations of the order statistic medians.
402
+
403
+ References
404
+ ----------
405
+ .. [1] James J. Filliben, "The Probability Plot Correlation Coefficient
406
+ Test for Normality", Technometrics, Vol. 17, pp. 111-117, 1975.
407
+
408
+ Examples
409
+ --------
410
+ Order statistics of the uniform distribution on the unit interval
411
+ are marginally distributed according to beta distributions.
412
+ The expectations of these order statistic are evenly spaced across
413
+ the interval, but the distributions are skewed in a way that
414
+ pushes the medians slightly towards the endpoints of the unit interval:
415
+
416
+ >>> import numpy as np
417
+ >>> n = 4
418
+ >>> k = np.arange(1, n+1)
419
+ >>> from scipy.stats import beta
420
+ >>> a = k
421
+ >>> b = n-k+1
422
+ >>> beta.mean(a, b)
423
+ array([0.2, 0.4, 0.6, 0.8])
424
+ >>> beta.median(a, b)
425
+ array([0.15910358, 0.38572757, 0.61427243, 0.84089642])
426
+
427
+ The Filliben approximation uses the exact medians of the smallest
428
+ and greatest order statistics, and the remaining medians are approximated
429
+ by points spread evenly across a sub-interval of the unit interval:
430
+
431
+ >>> from scipy.stats._morestats import _calc_uniform_order_statistic_medians
432
+ >>> _calc_uniform_order_statistic_medians(n)
433
+ array([0.15910358, 0.38545246, 0.61454754, 0.84089642])
434
+
435
+ This plot shows the skewed distributions of the order statistics
436
+ of a sample of size four from a uniform distribution on the unit interval:
437
+
438
+ >>> import matplotlib.pyplot as plt
439
+ >>> x = np.linspace(0.0, 1.0, num=50, endpoint=True)
440
+ >>> pdfs = [beta.pdf(x, a[i], b[i]) for i in range(n)]
441
+ >>> plt.figure()
442
+ >>> plt.plot(x, pdfs[0], x, pdfs[1], x, pdfs[2], x, pdfs[3])
443
+
444
+ """
445
+ v = np.empty(n, dtype=np.float64)
446
+ v[-1] = 0.5**(1.0 / n)
447
+ v[0] = 1 - v[-1]
448
+ i = np.arange(2, n)
449
+ v[1:-1] = (i - 0.3175) / (n + 0.365)
450
+ return v
451
+
452
+
453
+ def _parse_dist_kw(dist, enforce_subclass=True):
454
+ """Parse `dist` keyword.
455
+
456
+ Parameters
457
+ ----------
458
+ dist : str or stats.distributions instance.
459
+ Several functions take `dist` as a keyword, hence this utility
460
+ function.
461
+ enforce_subclass : bool, optional
462
+ If True (default), `dist` needs to be a
463
+ `_distn_infrastructure.rv_generic` instance.
464
+ It can sometimes be useful to set this keyword to False, if a function
465
+ wants to accept objects that just look somewhat like such an instance
466
+ (for example, they have a ``ppf`` method).
467
+
468
+ """
469
+ if isinstance(dist, rv_generic):
470
+ pass
471
+ elif isinstance(dist, str):
472
+ try:
473
+ dist = getattr(distributions, dist)
474
+ except AttributeError as e:
475
+ raise ValueError(f"{dist} is not a valid distribution name") from e
476
+ elif enforce_subclass:
477
+ msg = ("`dist` should be a stats.distributions instance or a string "
478
+ "with the name of such a distribution.")
479
+ raise ValueError(msg)
480
+
481
+ return dist
482
+
483
+
484
+ def _add_axis_labels_title(plot, xlabel, ylabel, title):
485
+ """Helper function to add axes labels and a title to stats plots."""
486
+ try:
487
+ if hasattr(plot, 'set_title'):
488
+ # Matplotlib Axes instance or something that looks like it
489
+ plot.set_title(title)
490
+ plot.set_xlabel(xlabel)
491
+ plot.set_ylabel(ylabel)
492
+ else:
493
+ # matplotlib.pyplot module
494
+ plot.title(title)
495
+ plot.xlabel(xlabel)
496
+ plot.ylabel(ylabel)
497
+ except Exception:
498
+ # Not an MPL object or something that looks (enough) like it.
499
+ # Don't crash on adding labels or title
500
+ pass
501
+
502
+
503
+ def probplot(x, sparams=(), dist='norm', fit=True, plot=None, rvalue=False):
504
+ """
505
+ Calculate quantiles for a probability plot, and optionally show the plot.
506
+
507
+ Generates a probability plot of sample data against the quantiles of a
508
+ specified theoretical distribution (the normal distribution by default).
509
+ `probplot` optionally calculates a best-fit line for the data and plots the
510
+ results using Matplotlib or a given plot function.
511
+
512
+ Parameters
513
+ ----------
514
+ x : array_like
515
+ Sample/response data from which `probplot` creates the plot.
516
+ sparams : tuple, optional
517
+ Distribution-specific shape parameters (shape parameters plus location
518
+ and scale).
519
+ dist : str or stats.distributions instance, optional
520
+ Distribution or distribution function name. The default is 'norm' for a
521
+ normal probability plot. Objects that look enough like a
522
+ stats.distributions instance (i.e. they have a ``ppf`` method) are also
523
+ accepted.
524
+ fit : bool, optional
525
+ Fit a least-squares regression (best-fit) line to the sample data if
526
+ True (default).
527
+ plot : object, optional
528
+ If given, plots the quantiles.
529
+ If given and `fit` is True, also plots the least squares fit.
530
+ `plot` is an object that has to have methods "plot" and "text".
531
+ The `matplotlib.pyplot` module or a Matplotlib Axes object can be used,
532
+ or a custom object with the same methods.
533
+ Default is None, which means that no plot is created.
534
+ rvalue : bool, optional
535
+ If `plot` is provided and `fit` is True, setting `rvalue` to True
536
+ includes the coefficient of determination on the plot.
537
+ Default is False.
538
+
539
+ Returns
540
+ -------
541
+ (osm, osr) : tuple of ndarrays
542
+ Tuple of theoretical quantiles (osm, or order statistic medians) and
543
+ ordered responses (osr). `osr` is simply sorted input `x`.
544
+ For details on how `osm` is calculated see the Notes section.
545
+ (slope, intercept, r) : tuple of floats, optional
546
+ Tuple containing the result of the least-squares fit, if that is
547
+ performed by `probplot`. `r` is the square root of the coefficient of
548
+ determination. If ``fit=False`` and ``plot=None``, this tuple is not
549
+ returned.
550
+
551
+ Notes
552
+ -----
553
+ Even if `plot` is given, the figure is not shown or saved by `probplot`;
554
+ ``plt.show()`` or ``plt.savefig('figname.png')`` should be used after
555
+ calling `probplot`.
556
+
557
+ `probplot` generates a probability plot, which should not be confused with
558
+ a Q-Q or a P-P plot. Statsmodels has more extensive functionality of this
559
+ type, see ``statsmodels.api.ProbPlot``.
560
+
561
+ The formula used for the theoretical quantiles (horizontal axis of the
562
+ probability plot) is Filliben's estimate::
563
+
564
+ quantiles = dist.ppf(val), for
565
+
566
+ 0.5**(1/n), for i = n
567
+ val = (i - 0.3175) / (n + 0.365), for i = 2, ..., n-1
568
+ 1 - 0.5**(1/n), for i = 1
569
+
570
+ where ``i`` indicates the i-th ordered value and ``n`` is the total number
571
+ of values.
572
+
573
+ Examples
574
+ --------
575
+ >>> import numpy as np
576
+ >>> from scipy import stats
577
+ >>> import matplotlib.pyplot as plt
578
+ >>> nsample = 100
579
+ >>> rng = np.random.default_rng()
580
+
581
+ A t distribution with small degrees of freedom:
582
+
583
+ >>> ax1 = plt.subplot(221)
584
+ >>> x = stats.t.rvs(3, size=nsample, random_state=rng)
585
+ >>> res = stats.probplot(x, plot=plt)
586
+
587
+ A t distribution with larger degrees of freedom:
588
+
589
+ >>> ax2 = plt.subplot(222)
590
+ >>> x = stats.t.rvs(25, size=nsample, random_state=rng)
591
+ >>> res = stats.probplot(x, plot=plt)
592
+
593
+ A mixture of two normal distributions with broadcasting:
594
+
595
+ >>> ax3 = plt.subplot(223)
596
+ >>> x = stats.norm.rvs(loc=[0,5], scale=[1,1.5],
597
+ ... size=(nsample//2,2), random_state=rng).ravel()
598
+ >>> res = stats.probplot(x, plot=plt)
599
+
600
+ A standard normal distribution:
601
+
602
+ >>> ax4 = plt.subplot(224)
603
+ >>> x = stats.norm.rvs(loc=0, scale=1, size=nsample, random_state=rng)
604
+ >>> res = stats.probplot(x, plot=plt)
605
+
606
+ Produce a new figure with a loggamma distribution, using the ``dist`` and
607
+ ``sparams`` keywords:
608
+
609
+ >>> fig = plt.figure()
610
+ >>> ax = fig.add_subplot(111)
611
+ >>> x = stats.loggamma.rvs(c=2.5, size=500, random_state=rng)
612
+ >>> res = stats.probplot(x, dist=stats.loggamma, sparams=(2.5,), plot=ax)
613
+ >>> ax.set_title("Probplot for loggamma dist with shape parameter 2.5")
614
+
615
+ Show the results with Matplotlib:
616
+
617
+ >>> plt.show()
618
+
619
+ """
620
+ x = np.asarray(x)
621
+ if x.size == 0:
622
+ if fit:
623
+ return (x, x), (np.nan, np.nan, 0.0)
624
+ else:
625
+ return x, x
626
+
627
+ osm_uniform = _calc_uniform_order_statistic_medians(len(x))
628
+ dist = _parse_dist_kw(dist, enforce_subclass=False)
629
+ if sparams is None:
630
+ sparams = ()
631
+ if isscalar(sparams):
632
+ sparams = (sparams,)
633
+ if not isinstance(sparams, tuple):
634
+ sparams = tuple(sparams)
635
+
636
+ osm = dist.ppf(osm_uniform, *sparams)
637
+ osr = sort(x)
638
+ if fit:
639
+ # perform a linear least squares fit.
640
+ slope, intercept, r, prob, _ = _stats_py.linregress(osm, osr)
641
+
642
+ if plot is not None:
643
+ plot.plot(osm, osr, 'bo')
644
+ if fit:
645
+ plot.plot(osm, slope*osm + intercept, 'r-')
646
+ _add_axis_labels_title(plot, xlabel='Theoretical quantiles',
647
+ ylabel='Ordered Values',
648
+ title='Probability Plot')
649
+
650
+ # Add R^2 value to the plot as text
651
+ if fit and rvalue:
652
+ xmin = amin(osm)
653
+ xmax = amax(osm)
654
+ ymin = amin(x)
655
+ ymax = amax(x)
656
+ posx = xmin + 0.70 * (xmax - xmin)
657
+ posy = ymin + 0.01 * (ymax - ymin)
658
+ plot.text(posx, posy, f"$R^2={r ** 2:1.4f}$")
659
+
660
+ if fit:
661
+ return (osm, osr), (slope, intercept, r)
662
+ else:
663
+ return osm, osr
664
+
665
+
666
+ def ppcc_max(x, brack=(0.0, 1.0), dist='tukeylambda'):
667
+ """Calculate the shape parameter that maximizes the PPCC.
668
+
669
+ The probability plot correlation coefficient (PPCC) plot can be used
670
+ to determine the optimal shape parameter for a one-parameter family
671
+ of distributions. ``ppcc_max`` returns the shape parameter that would
672
+ maximize the probability plot correlation coefficient for the given
673
+ data to a one-parameter family of distributions.
674
+
675
+ Parameters
676
+ ----------
677
+ x : array_like
678
+ Input array.
679
+ brack : tuple, optional
680
+ Triple (a,b,c) where (a<b<c). If bracket consists of two numbers (a, c)
681
+ then they are assumed to be a starting interval for a downhill bracket
682
+ search (see `scipy.optimize.brent`).
683
+ dist : str or stats.distributions instance, optional
684
+ Distribution or distribution function name. Objects that look enough
685
+ like a stats.distributions instance (i.e. they have a ``ppf`` method)
686
+ are also accepted. The default is ``'tukeylambda'``.
687
+
688
+ Returns
689
+ -------
690
+ shape_value : float
691
+ The shape parameter at which the probability plot correlation
692
+ coefficient reaches its max value.
693
+
694
+ See Also
695
+ --------
696
+ ppcc_plot, probplot, boxcox
697
+
698
+ Notes
699
+ -----
700
+ The brack keyword serves as a starting point which is useful in corner
701
+ cases. One can use a plot to obtain a rough visual estimate of the location
702
+ for the maximum to start the search near it.
703
+
704
+ References
705
+ ----------
706
+ .. [1] J.J. Filliben, "The Probability Plot Correlation Coefficient Test
707
+ for Normality", Technometrics, Vol. 17, pp. 111-117, 1975.
708
+ .. [2] Engineering Statistics Handbook, NIST/SEMATEC,
709
+ https://www.itl.nist.gov/div898/handbook/eda/section3/ppccplot.htm
710
+
711
+ Examples
712
+ --------
713
+ First we generate some random data from a Weibull distribution
714
+ with shape parameter 2.5:
715
+
716
+ >>> import numpy as np
717
+ >>> from scipy import stats
718
+ >>> import matplotlib.pyplot as plt
719
+ >>> rng = np.random.default_rng()
720
+ >>> c = 2.5
721
+ >>> x = stats.weibull_min.rvs(c, scale=4, size=2000, random_state=rng)
722
+
723
+ Generate the PPCC plot for this data with the Weibull distribution.
724
+
725
+ >>> fig, ax = plt.subplots(figsize=(8, 6))
726
+ >>> res = stats.ppcc_plot(x, c/2, 2*c, dist='weibull_min', plot=ax)
727
+
728
+ We calculate the value where the shape should reach its maximum and a
729
+ red line is drawn there. The line should coincide with the highest
730
+ point in the PPCC graph.
731
+
732
+ >>> cmax = stats.ppcc_max(x, brack=(c/2, 2*c), dist='weibull_min')
733
+ >>> ax.axvline(cmax, color='r')
734
+ >>> plt.show()
735
+
736
+ """
737
+ dist = _parse_dist_kw(dist)
738
+ osm_uniform = _calc_uniform_order_statistic_medians(len(x))
739
+ osr = sort(x)
740
+
741
+ # this function computes the x-axis values of the probability plot
742
+ # and computes a linear regression (including the correlation)
743
+ # and returns 1-r so that a minimization function maximizes the
744
+ # correlation
745
+ def tempfunc(shape, mi, yvals, func):
746
+ xvals = func(mi, shape)
747
+ r, prob = _stats_py.pearsonr(xvals, yvals)
748
+ return 1 - r
749
+
750
+ return optimize.brent(tempfunc, brack=brack,
751
+ args=(osm_uniform, osr, dist.ppf))
752
+
753
+
754
+ def ppcc_plot(x, a, b, dist='tukeylambda', plot=None, N=80):
755
+ """Calculate and optionally plot probability plot correlation coefficient.
756
+
757
+ The probability plot correlation coefficient (PPCC) plot can be used to
758
+ determine the optimal shape parameter for a one-parameter family of
759
+ distributions. It cannot be used for distributions without shape
760
+ parameters
761
+ (like the normal distribution) or with multiple shape parameters.
762
+
763
+ By default a Tukey-Lambda distribution (`stats.tukeylambda`) is used. A
764
+ Tukey-Lambda PPCC plot interpolates from long-tailed to short-tailed
765
+ distributions via an approximately normal one, and is therefore
766
+ particularly useful in practice.
767
+
768
+ Parameters
769
+ ----------
770
+ x : array_like
771
+ Input array.
772
+ a, b : scalar
773
+ Lower and upper bounds of the shape parameter to use.
774
+ dist : str or stats.distributions instance, optional
775
+ Distribution or distribution function name. Objects that look enough
776
+ like a stats.distributions instance (i.e. they have a ``ppf`` method)
777
+ are also accepted. The default is ``'tukeylambda'``.
778
+ plot : object, optional
779
+ If given, plots PPCC against the shape parameter.
780
+ `plot` is an object that has to have methods "plot" and "text".
781
+ The `matplotlib.pyplot` module or a Matplotlib Axes object can be used,
782
+ or a custom object with the same methods.
783
+ Default is None, which means that no plot is created.
784
+ N : int, optional
785
+ Number of points on the horizontal axis (equally distributed from
786
+ `a` to `b`).
787
+
788
+ Returns
789
+ -------
790
+ svals : ndarray
791
+ The shape values for which `ppcc` was calculated.
792
+ ppcc : ndarray
793
+ The calculated probability plot correlation coefficient values.
794
+
795
+ See Also
796
+ --------
797
+ ppcc_max, probplot, boxcox_normplot, tukeylambda
798
+
799
+ References
800
+ ----------
801
+ J.J. Filliben, "The Probability Plot Correlation Coefficient Test for
802
+ Normality", Technometrics, Vol. 17, pp. 111-117, 1975.
803
+
804
+ Examples
805
+ --------
806
+ First we generate some random data from a Weibull distribution
807
+ with shape parameter 2.5, and plot the histogram of the data:
808
+
809
+ >>> import numpy as np
810
+ >>> from scipy import stats
811
+ >>> import matplotlib.pyplot as plt
812
+ >>> rng = np.random.default_rng()
813
+ >>> c = 2.5
814
+ >>> x = stats.weibull_min.rvs(c, scale=4, size=2000, random_state=rng)
815
+
816
+ Take a look at the histogram of the data.
817
+
818
+ >>> fig1, ax = plt.subplots(figsize=(9, 4))
819
+ >>> ax.hist(x, bins=50)
820
+ >>> ax.set_title('Histogram of x')
821
+ >>> plt.show()
822
+
823
+ Now we explore this data with a PPCC plot as well as the related
824
+ probability plot and Box-Cox normplot. A red line is drawn where we
825
+ expect the PPCC value to be maximal (at the shape parameter ``c``
826
+ used above):
827
+
828
+ >>> fig2 = plt.figure(figsize=(12, 4))
829
+ >>> ax1 = fig2.add_subplot(1, 3, 1)
830
+ >>> ax2 = fig2.add_subplot(1, 3, 2)
831
+ >>> ax3 = fig2.add_subplot(1, 3, 3)
832
+ >>> res = stats.probplot(x, plot=ax1)
833
+ >>> res = stats.boxcox_normplot(x, -4, 4, plot=ax2)
834
+ >>> res = stats.ppcc_plot(x, c/2, 2*c, dist='weibull_min', plot=ax3)
835
+ >>> ax3.axvline(c, color='r')
836
+ >>> plt.show()
837
+
838
+ """
839
+ if b <= a:
840
+ raise ValueError("`b` has to be larger than `a`.")
841
+
842
+ svals = np.linspace(a, b, num=N)
843
+ ppcc = np.empty_like(svals)
844
+ for k, sval in enumerate(svals):
845
+ _, r2 = probplot(x, sval, dist=dist, fit=True)
846
+ ppcc[k] = r2[-1]
847
+
848
+ if plot is not None:
849
+ plot.plot(svals, ppcc, 'x')
850
+ _add_axis_labels_title(plot, xlabel='Shape Values',
851
+ ylabel='Prob Plot Corr. Coef.',
852
+ title=f'({dist}) PPCC Plot')
853
+
854
+ return svals, ppcc
855
+
856
+
857
+ def _log_mean(logx, axis):
858
+ # compute log of mean of x from log(x)
859
+ return (
860
+ special.logsumexp(logx, axis=axis, keepdims=True)
861
+ - math.log(logx.shape[axis])
862
+ )
863
+
864
+
865
+ def _log_var(logx, xp, axis):
866
+ # compute log of variance of x from log(x)
867
+ logmean = xp.broadcast_to(_log_mean(logx, axis=axis), logx.shape)
868
+ ones = xp.ones_like(logx)
869
+ logxmu, _ = special.logsumexp(xp.stack((logx, logmean), axis=0), axis=0,
870
+ b=xp.stack((ones, -ones), axis=0), return_sign=True)
871
+ return special.logsumexp(2 * logxmu, axis=axis) - math.log(logx.shape[axis])
872
+
873
+
874
+ def boxcox_llf(lmb, data, *, axis=0, keepdims=False, nan_policy='propagate'):
875
+ r"""The boxcox log-likelihood function.
876
+
877
+ Parameters
878
+ ----------
879
+ lmb : scalar
880
+ Parameter for Box-Cox transformation. See `boxcox` for details.
881
+ data : array_like
882
+ Data to calculate Box-Cox log-likelihood for. If `data` is
883
+ multi-dimensional, the log-likelihood is calculated along the first
884
+ axis.
885
+ axis : int, default: 0
886
+ If an int, the axis of the input along which to compute the statistic.
887
+ The statistic of each axis-slice (e.g. row) of the input will appear in a
888
+ corresponding element of the output.
889
+ If ``None``, the input will be raveled before computing the statistic.
890
+ nan_policy : {'propagate', 'omit', 'raise'
891
+ Defines how to handle input NaNs.
892
+
893
+ - ``propagate``: if a NaN is present in the axis slice (e.g. row) along
894
+ which the statistic is computed, the corresponding entry of the output
895
+ will be NaN.
896
+ - ``omit``: NaNs will be omitted when performing the calculation.
897
+ If insufficient data remains in the axis slice along which the
898
+ statistic is computed, the corresponding entry of the output will be
899
+ NaN.
900
+ - ``raise``: if a NaN is present, a ``ValueError`` will be raised.
901
+ keepdims : bool, default: False
902
+ If this is set to True, the axes which are reduced are left
903
+ in the result as dimensions with size one. With this option,
904
+ the result will broadcast correctly against the input array.
905
+
906
+ Returns
907
+ -------
908
+ llf : float or ndarray
909
+ Box-Cox log-likelihood of `data` given `lmb`. A float for 1-D `data`,
910
+ an array otherwise.
911
+
912
+ See Also
913
+ --------
914
+ boxcox, probplot, boxcox_normplot, boxcox_normmax
915
+
916
+ Notes
917
+ -----
918
+ The Box-Cox log-likelihood function :math:`l` is defined here as
919
+
920
+ .. math::
921
+
922
+ l = (\lambda - 1) \sum_i^N \log(x_i) -
923
+ \frac{N}{2} \log\left(\sum_i^N (y_i - \bar{y})^2 / N\right),
924
+
925
+ where :math:`N` is the number of data points ``data`` and :math:`y` is the Box-Cox
926
+ transformed input data.
927
+ This corresponds to the *profile log-likelihood* of the original data :math:`x`
928
+ with some constant terms dropped.
929
+
930
+ Examples
931
+ --------
932
+ >>> import numpy as np
933
+ >>> from scipy import stats
934
+ >>> import matplotlib.pyplot as plt
935
+ >>> from mpl_toolkits.axes_grid1.inset_locator import inset_axes
936
+
937
+ Generate some random variates and calculate Box-Cox log-likelihood values
938
+ for them for a range of ``lmbda`` values:
939
+
940
+ >>> rng = np.random.default_rng()
941
+ >>> x = stats.loggamma.rvs(5, loc=10, size=1000, random_state=rng)
942
+ >>> lmbdas = np.linspace(-2, 10)
943
+ >>> llf = np.zeros(lmbdas.shape, dtype=float)
944
+ >>> for ii, lmbda in enumerate(lmbdas):
945
+ ... llf[ii] = stats.boxcox_llf(lmbda, x)
946
+
947
+ Also find the optimal lmbda value with `boxcox`:
948
+
949
+ >>> x_most_normal, lmbda_optimal = stats.boxcox(x)
950
+
951
+ Plot the log-likelihood as function of lmbda. Add the optimal lmbda as a
952
+ horizontal line to check that that's really the optimum:
953
+
954
+ >>> fig = plt.figure()
955
+ >>> ax = fig.add_subplot(111)
956
+ >>> ax.plot(lmbdas, llf, 'b.-')
957
+ >>> ax.axhline(stats.boxcox_llf(lmbda_optimal, x), color='r')
958
+ >>> ax.set_xlabel('lmbda parameter')
959
+ >>> ax.set_ylabel('Box-Cox log-likelihood')
960
+
961
+ Now add some probability plots to show that where the log-likelihood is
962
+ maximized the data transformed with `boxcox` looks closest to normal:
963
+
964
+ >>> locs = [3, 10, 4] # 'lower left', 'center', 'lower right'
965
+ >>> for lmbda, loc in zip([-1, lmbda_optimal, 9], locs):
966
+ ... xt = stats.boxcox(x, lmbda=lmbda)
967
+ ... (osm, osr), (slope, intercept, r_sq) = stats.probplot(xt)
968
+ ... ax_inset = inset_axes(ax, width="20%", height="20%", loc=loc)
969
+ ... ax_inset.plot(osm, osr, 'c.', osm, slope*osm + intercept, 'k-')
970
+ ... ax_inset.set_xticklabels([])
971
+ ... ax_inset.set_yticklabels([])
972
+ ... ax_inset.set_title(r'$\lambda=%1.2f$' % lmbda)
973
+
974
+ >>> plt.show()
975
+
976
+ """
977
+ # _axis_nan_policy decorator does not currently support these for non-NumPy arrays
978
+ kwargs = {}
979
+ if keepdims is not False:
980
+ kwargs[keepdims] = keepdims
981
+ if nan_policy != 'propagate':
982
+ kwargs[nan_policy] = nan_policy
983
+ return _boxcox_llf(data, lmb=lmb, axis=axis, **kwargs)
984
+
985
+
986
+ @_axis_nan_policy_factory(lambda x: x, n_outputs=1, default_axis=0,
987
+ result_to_tuple=lambda x, _: (x,))
988
+ def _boxcox_llf(data, axis=0, *, lmb):
989
+ xp = array_namespace(data)
990
+ lmb, data = xp_promote(lmb, data, force_floating=True, xp=xp)
991
+ N = data.shape[axis]
992
+ if N == 0:
993
+ return _get_nan(data, xp=xp)
994
+
995
+ logdata = xp.log(data)
996
+
997
+ # Compute the variance of the transformed data.
998
+ if lmb == 0:
999
+ logvar = xp.log(xp.var(logdata, axis=axis))
1000
+ else:
1001
+ # Transform without the constant offset 1/lmb. The offset does
1002
+ # not affect the variance, and the subtraction of the offset can
1003
+ # lead to loss of precision.
1004
+ # Division by lmb can be factored out to enhance numerical stability.
1005
+ logx = lmb * logdata
1006
+ logvar = _log_var(logx, xp, axis) - 2 * math.log(abs(lmb))
1007
+
1008
+ res = (lmb - 1) * xp.sum(logdata, axis=axis) - N/2 * logvar
1009
+ res = xp.astype(res, data.dtype, copy=False) # compensate for NumPy <2.0
1010
+ res = res[()] if res.ndim == 0 else res
1011
+ return res
1012
+
1013
+
1014
+ def _boxcox_conf_interval(x, lmax, alpha):
1015
+ # Need to find the lambda for which
1016
+ # f(x,lmbda) >= f(x,lmax) - 0.5*chi^2_alpha;1
1017
+ fac = 0.5 * distributions.chi2.ppf(1 - alpha, 1)
1018
+ target = boxcox_llf(lmax, x) - fac
1019
+
1020
+ def rootfunc(lmbda, data, target):
1021
+ return boxcox_llf(lmbda, data) - target
1022
+
1023
+ # Find positive endpoint of interval in which answer is to be found
1024
+ newlm = lmax + 0.5
1025
+ N = 0
1026
+ while (rootfunc(newlm, x, target) > 0.0) and (N < 500):
1027
+ newlm += 0.1
1028
+ N += 1
1029
+
1030
+ if N == 500:
1031
+ raise RuntimeError("Could not find endpoint.")
1032
+
1033
+ lmplus = optimize.brentq(rootfunc, lmax, newlm, args=(x, target))
1034
+
1035
+ # Now find negative interval in the same way
1036
+ newlm = lmax - 0.5
1037
+ N = 0
1038
+ while (rootfunc(newlm, x, target) > 0.0) and (N < 500):
1039
+ newlm -= 0.1
1040
+ N += 1
1041
+
1042
+ if N == 500:
1043
+ raise RuntimeError("Could not find endpoint.")
1044
+
1045
+ lmminus = optimize.brentq(rootfunc, newlm, lmax, args=(x, target))
1046
+ return lmminus, lmplus
1047
+
1048
+
1049
+ def boxcox(x, lmbda=None, alpha=None, optimizer=None):
1050
+ r"""Return a dataset transformed by a Box-Cox power transformation.
1051
+
1052
+ Parameters
1053
+ ----------
1054
+ x : ndarray
1055
+ Input array to be transformed.
1056
+
1057
+ If `lmbda` is not None, this is an alias of
1058
+ `scipy.special.boxcox`.
1059
+ Returns nan if ``x < 0``; returns -inf if ``x == 0 and lmbda < 0``.
1060
+
1061
+ If `lmbda` is None, array must be positive, 1-dimensional, and
1062
+ non-constant.
1063
+
1064
+ lmbda : scalar, optional
1065
+ If `lmbda` is None (default), find the value of `lmbda` that maximizes
1066
+ the log-likelihood function and return it as the second output
1067
+ argument.
1068
+
1069
+ If `lmbda` is not None, do the transformation for that value.
1070
+
1071
+ alpha : float, optional
1072
+ If `lmbda` is None and `alpha` is not None (default), return the
1073
+ ``100 * (1-alpha)%`` confidence interval for `lmbda` as the third
1074
+ output argument. Must be between 0.0 and 1.0.
1075
+
1076
+ If `lmbda` is not None, `alpha` is ignored.
1077
+ optimizer : callable, optional
1078
+ If `lmbda` is None, `optimizer` is the scalar optimizer used to find
1079
+ the value of `lmbda` that minimizes the negative log-likelihood
1080
+ function. `optimizer` is a callable that accepts one argument:
1081
+
1082
+ fun : callable
1083
+ The objective function, which evaluates the negative
1084
+ log-likelihood function at a provided value of `lmbda`
1085
+
1086
+ and returns an object, such as an instance of
1087
+ `scipy.optimize.OptimizeResult`, which holds the optimal value of
1088
+ `lmbda` in an attribute `x`.
1089
+
1090
+ See the example in `boxcox_normmax` or the documentation of
1091
+ `scipy.optimize.minimize_scalar` for more information.
1092
+
1093
+ If `lmbda` is not None, `optimizer` is ignored.
1094
+
1095
+ Returns
1096
+ -------
1097
+ boxcox : ndarray
1098
+ Box-Cox power transformed array.
1099
+ maxlog : float, optional
1100
+ If the `lmbda` parameter is None, the second returned argument is
1101
+ the `lmbda` that maximizes the log-likelihood function.
1102
+ (min_ci, max_ci) : tuple of float, optional
1103
+ If `lmbda` parameter is None and `alpha` is not None, this returned
1104
+ tuple of floats represents the minimum and maximum confidence limits
1105
+ given `alpha`.
1106
+
1107
+ See Also
1108
+ --------
1109
+ probplot, boxcox_normplot, boxcox_normmax, boxcox_llf
1110
+
1111
+ Notes
1112
+ -----
1113
+ The Box-Cox transform is given by:
1114
+
1115
+ .. math::
1116
+
1117
+ y =
1118
+ \begin{cases}
1119
+ \frac{x^\lambda - 1}{\lambda}, &\text{for } \lambda \neq 0
1120
+ \log(x), &\text{for } \lambda = 0
1121
+ \end{cases}
1122
+
1123
+ `boxcox` requires the input data to be positive. Sometimes a Box-Cox
1124
+ transformation provides a shift parameter to achieve this; `boxcox` does
1125
+ not. Such a shift parameter is equivalent to adding a positive constant to
1126
+ `x` before calling `boxcox`.
1127
+
1128
+ The confidence limits returned when `alpha` is provided give the interval
1129
+ where:
1130
+
1131
+ .. math::
1132
+
1133
+ l(\hat{\lambda}) - l(\lambda) < \frac{1}{2}\chi^2(1 - \alpha, 1),
1134
+
1135
+ with :math:`l` the log-likelihood function and :math:`\chi^2` the chi-squared
1136
+ function.
1137
+
1138
+ References
1139
+ ----------
1140
+ G.E.P. Box and D.R. Cox, "An Analysis of Transformations", Journal of the
1141
+ Royal Statistical Society B, 26, 211-252 (1964).
1142
+
1143
+ Examples
1144
+ --------
1145
+ >>> from scipy import stats
1146
+ >>> import matplotlib.pyplot as plt
1147
+
1148
+ We generate some random variates from a non-normal distribution and make a
1149
+ probability plot for it, to show it is non-normal in the tails:
1150
+
1151
+ >>> fig = plt.figure()
1152
+ >>> ax1 = fig.add_subplot(211)
1153
+ >>> x = stats.loggamma.rvs(5, size=500) + 5
1154
+ >>> prob = stats.probplot(x, dist=stats.norm, plot=ax1)
1155
+ >>> ax1.set_xlabel('')
1156
+ >>> ax1.set_title('Probplot against normal distribution')
1157
+
1158
+ We now use `boxcox` to transform the data so it's closest to normal:
1159
+
1160
+ >>> ax2 = fig.add_subplot(212)
1161
+ >>> xt, _ = stats.boxcox(x)
1162
+ >>> prob = stats.probplot(xt, dist=stats.norm, plot=ax2)
1163
+ >>> ax2.set_title('Probplot after Box-Cox transformation')
1164
+
1165
+ >>> plt.show()
1166
+
1167
+ """
1168
+ x = np.asarray(x)
1169
+
1170
+ if lmbda is not None: # single transformation
1171
+ return special.boxcox(x, lmbda)
1172
+
1173
+ if x.ndim != 1:
1174
+ raise ValueError("Data must be 1-dimensional.")
1175
+
1176
+ if x.size == 0:
1177
+ return x
1178
+
1179
+ if np.all(x == x[0]):
1180
+ raise ValueError("Data must not be constant.")
1181
+
1182
+ if np.any(x <= 0):
1183
+ raise ValueError("Data must be positive.")
1184
+
1185
+ # If lmbda=None, find the lmbda that maximizes the log-likelihood function.
1186
+ lmax = boxcox_normmax(x, method='mle', optimizer=optimizer)
1187
+ y = boxcox(x, lmax)
1188
+
1189
+ if alpha is None:
1190
+ return y, lmax
1191
+ else:
1192
+ # Find confidence interval
1193
+ interval = _boxcox_conf_interval(x, lmax, alpha)
1194
+ return y, lmax, interval
1195
+
1196
+
1197
+ def _boxcox_inv_lmbda(x, y):
1198
+ # compute lmbda given x and y for Box-Cox transformation
1199
+ num = special.lambertw(-(x ** (-1 / y)) * np.log(x) / y, k=-1)
1200
+ return np.real(-num / np.log(x) - 1 / y)
1201
+
1202
+
1203
+ class _BigFloat:
1204
+ def __repr__(self):
1205
+ return "BIG_FLOAT"
1206
+
1207
+
1208
+ _BigFloat_singleton = _BigFloat()
1209
+
1210
+
1211
+ def boxcox_normmax(
1212
+ x, brack=None, method='pearsonr', optimizer=None, *, ymax=_BigFloat_singleton
1213
+ ):
1214
+ """Compute optimal Box-Cox transform parameter for input data.
1215
+
1216
+ Parameters
1217
+ ----------
1218
+ x : array_like
1219
+ Input array. All entries must be positive, finite, real numbers.
1220
+ brack : 2-tuple, optional, default (-2.0, 2.0)
1221
+ The starting interval for a downhill bracket search for the default
1222
+ `optimize.brent` solver. Note that this is in most cases not
1223
+ critical; the final result is allowed to be outside this bracket.
1224
+ If `optimizer` is passed, `brack` must be None.
1225
+ method : str, optional
1226
+ The method to determine the optimal transform parameter (`boxcox`
1227
+ ``lmbda`` parameter). Options are:
1228
+
1229
+ 'pearsonr' (default)
1230
+ Maximizes the Pearson correlation coefficient between
1231
+ ``y = boxcox(x)`` and the expected values for ``y`` if `x` would be
1232
+ normally-distributed.
1233
+
1234
+ 'mle'
1235
+ Maximizes the log-likelihood `boxcox_llf`. This is the method used
1236
+ in `boxcox`.
1237
+
1238
+ 'all'
1239
+ Use all optimization methods available, and return all results.
1240
+ Useful to compare different methods.
1241
+ optimizer : callable, optional
1242
+ `optimizer` is a callable that accepts one argument:
1243
+
1244
+ fun : callable
1245
+ The objective function to be minimized. `fun` accepts one argument,
1246
+ the Box-Cox transform parameter `lmbda`, and returns the value of
1247
+ the function (e.g., the negative log-likelihood) at the provided
1248
+ argument. The job of `optimizer` is to find the value of `lmbda`
1249
+ that *minimizes* `fun`.
1250
+
1251
+ and returns an object, such as an instance of
1252
+ `scipy.optimize.OptimizeResult`, which holds the optimal value of
1253
+ `lmbda` in an attribute `x`.
1254
+
1255
+ See the example below or the documentation of
1256
+ `scipy.optimize.minimize_scalar` for more information.
1257
+ ymax : float, optional
1258
+ The unconstrained optimal transform parameter may cause Box-Cox
1259
+ transformed data to have extreme magnitude or even overflow.
1260
+ This parameter constrains MLE optimization such that the magnitude
1261
+ of the transformed `x` does not exceed `ymax`. The default is
1262
+ the maximum value of the input dtype. If set to infinity,
1263
+ `boxcox_normmax` returns the unconstrained optimal lambda.
1264
+ Ignored when ``method='pearsonr'``.
1265
+
1266
+ Returns
1267
+ -------
1268
+ maxlog : float or ndarray
1269
+ The optimal transform parameter found. An array instead of a scalar
1270
+ for ``method='all'``.
1271
+
1272
+ See Also
1273
+ --------
1274
+ boxcox, boxcox_llf, boxcox_normplot, scipy.optimize.minimize_scalar
1275
+
1276
+ Examples
1277
+ --------
1278
+ >>> import numpy as np
1279
+ >>> from scipy import stats
1280
+ >>> import matplotlib.pyplot as plt
1281
+
1282
+ We can generate some data and determine the optimal ``lmbda`` in various
1283
+ ways:
1284
+
1285
+ >>> rng = np.random.default_rng()
1286
+ >>> x = stats.loggamma.rvs(5, size=30, random_state=rng) + 5
1287
+ >>> y, lmax_mle = stats.boxcox(x)
1288
+ >>> lmax_pearsonr = stats.boxcox_normmax(x)
1289
+
1290
+ >>> lmax_mle
1291
+ 2.217563431465757
1292
+ >>> lmax_pearsonr
1293
+ 2.238318660200961
1294
+ >>> stats.boxcox_normmax(x, method='all')
1295
+ array([2.23831866, 2.21756343])
1296
+
1297
+ >>> fig = plt.figure()
1298
+ >>> ax = fig.add_subplot(111)
1299
+ >>> prob = stats.boxcox_normplot(x, -10, 10, plot=ax)
1300
+ >>> ax.axvline(lmax_mle, color='r')
1301
+ >>> ax.axvline(lmax_pearsonr, color='g', ls='--')
1302
+
1303
+ >>> plt.show()
1304
+
1305
+ Alternatively, we can define our own `optimizer` function. Suppose we
1306
+ are only interested in values of `lmbda` on the interval [6, 7], we
1307
+ want to use `scipy.optimize.minimize_scalar` with ``method='bounded'``,
1308
+ and we want to use tighter tolerances when optimizing the log-likelihood
1309
+ function. To do this, we define a function that accepts positional argument
1310
+ `fun` and uses `scipy.optimize.minimize_scalar` to minimize `fun` subject
1311
+ to the provided bounds and tolerances:
1312
+
1313
+ >>> from scipy import optimize
1314
+ >>> options = {'xatol': 1e-12} # absolute tolerance on `x`
1315
+ >>> def optimizer(fun):
1316
+ ... return optimize.minimize_scalar(fun, bounds=(6, 7),
1317
+ ... method="bounded", options=options)
1318
+ >>> stats.boxcox_normmax(x, optimizer=optimizer)
1319
+ 6.000000000
1320
+ """
1321
+ x = np.asarray(x)
1322
+
1323
+ if not np.all(np.isfinite(x) & (x >= 0)):
1324
+ message = ("The `x` argument of `boxcox_normmax` must contain "
1325
+ "only positive, finite, real numbers.")
1326
+ raise ValueError(message)
1327
+
1328
+ end_msg = "exceed specified `ymax`."
1329
+ if ymax is _BigFloat_singleton:
1330
+ dtype = x.dtype if np.issubdtype(x.dtype, np.floating) else np.float64
1331
+ # 10000 is a safety factor because `special.boxcox` overflows prematurely.
1332
+ ymax = np.finfo(dtype).max / 10000
1333
+ end_msg = f"overflow in {dtype}."
1334
+ elif ymax <= 0:
1335
+ raise ValueError("`ymax` must be strictly positive")
1336
+
1337
+ # If optimizer is not given, define default 'brent' optimizer.
1338
+ if optimizer is None:
1339
+
1340
+ # Set default value for `brack`.
1341
+ if brack is None:
1342
+ brack = (-2.0, 2.0)
1343
+
1344
+ def _optimizer(func, args):
1345
+ return optimize.brent(func, args=args, brack=brack)
1346
+
1347
+ # Otherwise check optimizer.
1348
+ else:
1349
+ if not callable(optimizer):
1350
+ raise ValueError("`optimizer` must be a callable")
1351
+
1352
+ if brack is not None:
1353
+ raise ValueError("`brack` must be None if `optimizer` is given")
1354
+
1355
+ # `optimizer` is expected to return a `OptimizeResult` object, we here
1356
+ # get the solution to the optimization problem.
1357
+ def _optimizer(func, args):
1358
+ def func_wrapped(x):
1359
+ return func(x, *args)
1360
+ return getattr(optimizer(func_wrapped), 'x', None)
1361
+
1362
+ def _pearsonr(x):
1363
+ osm_uniform = _calc_uniform_order_statistic_medians(len(x))
1364
+ xvals = distributions.norm.ppf(osm_uniform)
1365
+
1366
+ def _eval_pearsonr(lmbda, xvals, samps):
1367
+ # This function computes the x-axis values of the probability plot
1368
+ # and computes a linear regression (including the correlation) and
1369
+ # returns ``1 - r`` so that a minimization function maximizes the
1370
+ # correlation.
1371
+ y = boxcox(samps, lmbda)
1372
+ yvals = np.sort(y)
1373
+ r, prob = _stats_py.pearsonr(xvals, yvals)
1374
+ return 1 - r
1375
+
1376
+ return _optimizer(_eval_pearsonr, args=(xvals, x))
1377
+
1378
+ def _mle(x):
1379
+ def _eval_mle(lmb, data):
1380
+ # function to minimize
1381
+ return -boxcox_llf(lmb, data)
1382
+
1383
+ return _optimizer(_eval_mle, args=(x,))
1384
+
1385
+ def _all(x):
1386
+ maxlog = np.empty(2, dtype=float)
1387
+ maxlog[0] = _pearsonr(x)
1388
+ maxlog[1] = _mle(x)
1389
+ return maxlog
1390
+
1391
+ methods = {'pearsonr': _pearsonr,
1392
+ 'mle': _mle,
1393
+ 'all': _all}
1394
+ if method not in methods.keys():
1395
+ raise ValueError(f"Method {method} not recognized.")
1396
+
1397
+ optimfunc = methods[method]
1398
+
1399
+ res = optimfunc(x)
1400
+
1401
+ if res is None:
1402
+ message = ("The `optimizer` argument of `boxcox_normmax` must return "
1403
+ "an object containing the optimal `lmbda` in attribute `x`.")
1404
+ raise ValueError(message)
1405
+ elif not np.isinf(ymax): # adjust the final lambda
1406
+ # x > 1, boxcox(x) > 0; x < 1, boxcox(x) < 0
1407
+ xmax, xmin = np.max(x), np.min(x)
1408
+ if xmin >= 1:
1409
+ x_treme = xmax
1410
+ elif xmax <= 1:
1411
+ x_treme = xmin
1412
+ else: # xmin < 1 < xmax
1413
+ indicator = special.boxcox(xmax, res) > abs(special.boxcox(xmin, res))
1414
+ if isinstance(res, np.ndarray):
1415
+ indicator = indicator[1] # select corresponds with 'mle'
1416
+ x_treme = xmax if indicator else xmin
1417
+
1418
+ mask = abs(special.boxcox(x_treme, res)) > ymax
1419
+ if np.any(mask):
1420
+ message = (
1421
+ f"The optimal lambda is {res}, but the returned lambda is the "
1422
+ f"constrained optimum to ensure that the maximum or the minimum "
1423
+ f"of the transformed data does not " + end_msg
1424
+ )
1425
+ warnings.warn(message, stacklevel=2)
1426
+
1427
+ # Return the constrained lambda to ensure the transformation
1428
+ # does not cause overflow or exceed specified `ymax`
1429
+ constrained_res = _boxcox_inv_lmbda(x_treme, ymax * np.sign(x_treme - 1))
1430
+
1431
+ if isinstance(res, np.ndarray):
1432
+ res[mask] = constrained_res
1433
+ else:
1434
+ res = constrained_res
1435
+ return res
1436
+
1437
+
1438
+ def _normplot(method, x, la, lb, plot=None, N=80):
1439
+ """Compute parameters for a Box-Cox or Yeo-Johnson normality plot,
1440
+ optionally show it.
1441
+
1442
+ See `boxcox_normplot` or `yeojohnson_normplot` for details.
1443
+ """
1444
+
1445
+ if method == 'boxcox':
1446
+ title = 'Box-Cox Normality Plot'
1447
+ transform_func = boxcox
1448
+ else:
1449
+ title = 'Yeo-Johnson Normality Plot'
1450
+ transform_func = yeojohnson
1451
+
1452
+ x = np.asarray(x)
1453
+ if x.size == 0:
1454
+ return x
1455
+
1456
+ if lb <= la:
1457
+ raise ValueError("`lb` has to be larger than `la`.")
1458
+
1459
+ if method == 'boxcox' and np.any(x <= 0):
1460
+ raise ValueError("Data must be positive.")
1461
+
1462
+ lmbdas = np.linspace(la, lb, num=N)
1463
+ ppcc = lmbdas * 0.0
1464
+ for i, val in enumerate(lmbdas):
1465
+ # Determine for each lmbda the square root of correlation coefficient
1466
+ # of transformed x
1467
+ z = transform_func(x, lmbda=val)
1468
+ _, (_, _, r) = probplot(z, dist='norm', fit=True)
1469
+ ppcc[i] = r
1470
+
1471
+ if plot is not None:
1472
+ plot.plot(lmbdas, ppcc, 'x')
1473
+ _add_axis_labels_title(plot, xlabel='$\\lambda$',
1474
+ ylabel='Prob Plot Corr. Coef.',
1475
+ title=title)
1476
+
1477
+ return lmbdas, ppcc
1478
+
1479
+
1480
+ def boxcox_normplot(x, la, lb, plot=None, N=80):
1481
+ """Compute parameters for a Box-Cox normality plot, optionally show it.
1482
+
1483
+ A Box-Cox normality plot shows graphically what the best transformation
1484
+ parameter is to use in `boxcox` to obtain a distribution that is close
1485
+ to normal.
1486
+
1487
+ Parameters
1488
+ ----------
1489
+ x : array_like
1490
+ Input array.
1491
+ la, lb : scalar
1492
+ The lower and upper bounds for the ``lmbda`` values to pass to `boxcox`
1493
+ for Box-Cox transformations. These are also the limits of the
1494
+ horizontal axis of the plot if that is generated.
1495
+ plot : object, optional
1496
+ If given, plots the quantiles and least squares fit.
1497
+ `plot` is an object that has to have methods "plot" and "text".
1498
+ The `matplotlib.pyplot` module or a Matplotlib Axes object can be used,
1499
+ or a custom object with the same methods.
1500
+ Default is None, which means that no plot is created.
1501
+ N : int, optional
1502
+ Number of points on the horizontal axis (equally distributed from
1503
+ `la` to `lb`).
1504
+
1505
+ Returns
1506
+ -------
1507
+ lmbdas : ndarray
1508
+ The ``lmbda`` values for which a Box-Cox transform was done.
1509
+ ppcc : ndarray
1510
+ Probability Plot Correlation Coefficient, as obtained from `probplot`
1511
+ when fitting the Box-Cox transformed input `x` against a normal
1512
+ distribution.
1513
+
1514
+ See Also
1515
+ --------
1516
+ probplot, boxcox, boxcox_normmax, boxcox_llf, ppcc_max
1517
+
1518
+ Notes
1519
+ -----
1520
+ Even if `plot` is given, the figure is not shown or saved by
1521
+ `boxcox_normplot`; ``plt.show()`` or ``plt.savefig('figname.png')``
1522
+ should be used after calling `probplot`.
1523
+
1524
+ Examples
1525
+ --------
1526
+ >>> from scipy import stats
1527
+ >>> import matplotlib.pyplot as plt
1528
+
1529
+ Generate some non-normally distributed data, and create a Box-Cox plot:
1530
+
1531
+ >>> x = stats.loggamma.rvs(5, size=500) + 5
1532
+ >>> fig = plt.figure()
1533
+ >>> ax = fig.add_subplot(111)
1534
+ >>> prob = stats.boxcox_normplot(x, -20, 20, plot=ax)
1535
+
1536
+ Determine and plot the optimal ``lmbda`` to transform ``x`` and plot it in
1537
+ the same plot:
1538
+
1539
+ >>> _, maxlog = stats.boxcox(x)
1540
+ >>> ax.axvline(maxlog, color='r')
1541
+
1542
+ >>> plt.show()
1543
+
1544
+ """
1545
+ return _normplot('boxcox', x, la, lb, plot, N)
1546
+
1547
+
1548
+ def yeojohnson(x, lmbda=None):
1549
+ r"""Return a dataset transformed by a Yeo-Johnson power transformation.
1550
+
1551
+ Parameters
1552
+ ----------
1553
+ x : ndarray
1554
+ Input array. Should be 1-dimensional.
1555
+ lmbda : float, optional
1556
+ If ``lmbda`` is ``None``, find the lambda that maximizes the
1557
+ log-likelihood function and return it as the second output argument.
1558
+ Otherwise the transformation is done for the given value.
1559
+
1560
+ Returns
1561
+ -------
1562
+ yeojohnson: ndarray
1563
+ Yeo-Johnson power transformed array.
1564
+ maxlog : float, optional
1565
+ If the `lmbda` parameter is None, the second returned argument is
1566
+ the lambda that maximizes the log-likelihood function.
1567
+
1568
+ See Also
1569
+ --------
1570
+ probplot, yeojohnson_normplot, yeojohnson_normmax, yeojohnson_llf, boxcox
1571
+
1572
+ Notes
1573
+ -----
1574
+ The Yeo-Johnson transform is given by:
1575
+
1576
+ .. math::
1577
+
1578
+ y =
1579
+ \begin{cases}
1580
+ \frac{(x + 1)^\lambda - 1}{\lambda},
1581
+ &\text{for } x \geq 0, \lambda \neq 0
1582
+ \\
1583
+ \log(x + 1),
1584
+ &\text{for } x \geq 0, \lambda = 0
1585
+ \\
1586
+ -\frac{(-x + 1)^{2 - \lambda} - 1}{2 - \lambda},
1587
+ &\text{for } x < 0, \lambda \neq 2
1588
+ \\
1589
+ -\log(-x + 1),
1590
+ &\text{for } x < 0, \lambda = 2
1591
+ \end{cases}
1592
+
1593
+ Unlike `boxcox`, `yeojohnson` does not require the input data to be
1594
+ positive.
1595
+
1596
+ .. versionadded:: 1.2.0
1597
+
1598
+
1599
+ References
1600
+ ----------
1601
+ I. Yeo and R.A. Johnson, "A New Family of Power Transformations to
1602
+ Improve Normality or Symmetry", Biometrika 87.4 (2000):
1603
+
1604
+
1605
+ Examples
1606
+ --------
1607
+ >>> from scipy import stats
1608
+ >>> import matplotlib.pyplot as plt
1609
+
1610
+ We generate some random variates from a non-normal distribution and make a
1611
+ probability plot for it, to show it is non-normal in the tails:
1612
+
1613
+ >>> fig = plt.figure()
1614
+ >>> ax1 = fig.add_subplot(211)
1615
+ >>> x = stats.loggamma.rvs(5, size=500) + 5
1616
+ >>> prob = stats.probplot(x, dist=stats.norm, plot=ax1)
1617
+ >>> ax1.set_xlabel('')
1618
+ >>> ax1.set_title('Probplot against normal distribution')
1619
+
1620
+ We now use `yeojohnson` to transform the data so it's closest to normal:
1621
+
1622
+ >>> ax2 = fig.add_subplot(212)
1623
+ >>> xt, lmbda = stats.yeojohnson(x)
1624
+ >>> prob = stats.probplot(xt, dist=stats.norm, plot=ax2)
1625
+ >>> ax2.set_title('Probplot after Yeo-Johnson transformation')
1626
+
1627
+ >>> plt.show()
1628
+
1629
+ """
1630
+ x = np.asarray(x)
1631
+ if x.size == 0:
1632
+ return x
1633
+
1634
+ if np.issubdtype(x.dtype, np.complexfloating):
1635
+ raise ValueError('Yeo-Johnson transformation is not defined for '
1636
+ 'complex numbers.')
1637
+
1638
+ if np.issubdtype(x.dtype, np.integer):
1639
+ x = x.astype(np.float64, copy=False)
1640
+
1641
+ if lmbda is not None:
1642
+ return _yeojohnson_transform(x, lmbda)
1643
+
1644
+ # if lmbda=None, find the lmbda that maximizes the log-likelihood function.
1645
+ lmax = yeojohnson_normmax(x)
1646
+ y = _yeojohnson_transform(x, lmax)
1647
+
1648
+ return y, lmax
1649
+
1650
+
1651
+ def _yeojohnson_transform(x, lmbda):
1652
+ """Returns `x` transformed by the Yeo-Johnson power transform with given
1653
+ parameter `lmbda`.
1654
+ """
1655
+ dtype = x.dtype if np.issubdtype(x.dtype, np.floating) else np.float64
1656
+ out = np.zeros_like(x, dtype=dtype)
1657
+ pos = x >= 0 # binary mask
1658
+
1659
+ # when x >= 0
1660
+ if abs(lmbda) < np.spacing(1.):
1661
+ out[pos] = np.log1p(x[pos])
1662
+ else: # lmbda != 0
1663
+ # more stable version of: ((x + 1) ** lmbda - 1) / lmbda
1664
+ out[pos] = np.expm1(lmbda * np.log1p(x[pos])) / lmbda
1665
+
1666
+ # when x < 0
1667
+ if abs(lmbda - 2) > np.spacing(1.):
1668
+ out[~pos] = -np.expm1((2 - lmbda) * np.log1p(-x[~pos])) / (2 - lmbda)
1669
+ else: # lmbda == 2
1670
+ out[~pos] = -np.log1p(-x[~pos])
1671
+
1672
+ return out
1673
+
1674
+
1675
+ def yeojohnson_llf(lmb, data):
1676
+ r"""The yeojohnson log-likelihood function.
1677
+
1678
+ Parameters
1679
+ ----------
1680
+ lmb : scalar
1681
+ Parameter for Yeo-Johnson transformation. See `yeojohnson` for
1682
+ details.
1683
+ data : array_like
1684
+ Data to calculate Yeo-Johnson log-likelihood for. If `data` is
1685
+ multi-dimensional, the log-likelihood is calculated along the first
1686
+ axis.
1687
+
1688
+ Returns
1689
+ -------
1690
+ llf : float
1691
+ Yeo-Johnson log-likelihood of `data` given `lmb`.
1692
+
1693
+ See Also
1694
+ --------
1695
+ yeojohnson, probplot, yeojohnson_normplot, yeojohnson_normmax
1696
+
1697
+ Notes
1698
+ -----
1699
+ The Yeo-Johnson log-likelihood function :math:`l` is defined here as
1700
+
1701
+ .. math::
1702
+
1703
+ l = -\frac{N}{2} \log(\hat{\sigma}^2) + (\lambda - 1)
1704
+ \sum_i^N \text{sign}(x_i) \log(|x_i| + 1)
1705
+
1706
+ where :math:`N` is the number of data points :math:`x`=``data`` and
1707
+ :math:`\hat{\sigma}^2` is the estimated variance of the Yeo-Johnson transformed
1708
+ input data :math:`x`.
1709
+ This corresponds to the *profile log-likelihood* of the original data :math:`x`
1710
+ with some constant terms dropped.
1711
+
1712
+ .. versionadded:: 1.2.0
1713
+
1714
+ Examples
1715
+ --------
1716
+ >>> import numpy as np
1717
+ >>> from scipy import stats
1718
+ >>> import matplotlib.pyplot as plt
1719
+ >>> from mpl_toolkits.axes_grid1.inset_locator import inset_axes
1720
+
1721
+ Generate some random variates and calculate Yeo-Johnson log-likelihood
1722
+ values for them for a range of ``lmbda`` values:
1723
+
1724
+ >>> x = stats.loggamma.rvs(5, loc=10, size=1000)
1725
+ >>> lmbdas = np.linspace(-2, 10)
1726
+ >>> llf = np.zeros(lmbdas.shape, dtype=float)
1727
+ >>> for ii, lmbda in enumerate(lmbdas):
1728
+ ... llf[ii] = stats.yeojohnson_llf(lmbda, x)
1729
+
1730
+ Also find the optimal lmbda value with `yeojohnson`:
1731
+
1732
+ >>> x_most_normal, lmbda_optimal = stats.yeojohnson(x)
1733
+
1734
+ Plot the log-likelihood as function of lmbda. Add the optimal lmbda as a
1735
+ horizontal line to check that that's really the optimum:
1736
+
1737
+ >>> fig = plt.figure()
1738
+ >>> ax = fig.add_subplot(111)
1739
+ >>> ax.plot(lmbdas, llf, 'b.-')
1740
+ >>> ax.axhline(stats.yeojohnson_llf(lmbda_optimal, x), color='r')
1741
+ >>> ax.set_xlabel('lmbda parameter')
1742
+ >>> ax.set_ylabel('Yeo-Johnson log-likelihood')
1743
+
1744
+ Now add some probability plots to show that where the log-likelihood is
1745
+ maximized the data transformed with `yeojohnson` looks closest to normal:
1746
+
1747
+ >>> locs = [3, 10, 4] # 'lower left', 'center', 'lower right'
1748
+ >>> for lmbda, loc in zip([-1, lmbda_optimal, 9], locs):
1749
+ ... xt = stats.yeojohnson(x, lmbda=lmbda)
1750
+ ... (osm, osr), (slope, intercept, r_sq) = stats.probplot(xt)
1751
+ ... ax_inset = inset_axes(ax, width="20%", height="20%", loc=loc)
1752
+ ... ax_inset.plot(osm, osr, 'c.', osm, slope*osm + intercept, 'k-')
1753
+ ... ax_inset.set_xticklabels([])
1754
+ ... ax_inset.set_yticklabels([])
1755
+ ... ax_inset.set_title(r'$\lambda=%1.2f$' % lmbda)
1756
+
1757
+ >>> plt.show()
1758
+
1759
+ """
1760
+ data = np.asarray(data)
1761
+ n_samples = data.shape[0]
1762
+
1763
+ if n_samples == 0:
1764
+ return np.nan
1765
+
1766
+ trans = _yeojohnson_transform(data, lmb)
1767
+ trans_var = trans.var(axis=0)
1768
+ loglike = np.empty_like(trans_var)
1769
+
1770
+ # Avoid RuntimeWarning raised by np.log when the variance is too low
1771
+ tiny_variance = trans_var < np.finfo(trans_var.dtype).tiny
1772
+ loglike[tiny_variance] = np.inf
1773
+
1774
+ loglike[~tiny_variance] = (
1775
+ -n_samples / 2 * np.log(trans_var[~tiny_variance]))
1776
+ loglike[~tiny_variance] += (
1777
+ (lmb - 1) * (np.sign(data) * np.log1p(np.abs(data))).sum(axis=0))
1778
+ return loglike
1779
+
1780
+
1781
+ def yeojohnson_normmax(x, brack=None):
1782
+ """Compute optimal Yeo-Johnson transform parameter.
1783
+
1784
+ Compute optimal Yeo-Johnson transform parameter for input data, using
1785
+ maximum likelihood estimation.
1786
+
1787
+ Parameters
1788
+ ----------
1789
+ x : array_like
1790
+ Input array.
1791
+ brack : 2-tuple, optional
1792
+ The starting interval for a downhill bracket search with
1793
+ `optimize.brent`. Note that this is in most cases not critical; the
1794
+ final result is allowed to be outside this bracket. If None,
1795
+ `optimize.fminbound` is used with bounds that avoid overflow.
1796
+
1797
+ Returns
1798
+ -------
1799
+ maxlog : float
1800
+ The optimal transform parameter found.
1801
+
1802
+ See Also
1803
+ --------
1804
+ yeojohnson, yeojohnson_llf, yeojohnson_normplot
1805
+
1806
+ Notes
1807
+ -----
1808
+ .. versionadded:: 1.2.0
1809
+
1810
+ Examples
1811
+ --------
1812
+ >>> import numpy as np
1813
+ >>> from scipy import stats
1814
+ >>> import matplotlib.pyplot as plt
1815
+
1816
+ Generate some data and determine optimal ``lmbda``
1817
+
1818
+ >>> rng = np.random.default_rng()
1819
+ >>> x = stats.loggamma.rvs(5, size=30, random_state=rng) + 5
1820
+ >>> lmax = stats.yeojohnson_normmax(x)
1821
+
1822
+ >>> fig = plt.figure()
1823
+ >>> ax = fig.add_subplot(111)
1824
+ >>> prob = stats.yeojohnson_normplot(x, -10, 10, plot=ax)
1825
+ >>> ax.axvline(lmax, color='r')
1826
+
1827
+ >>> plt.show()
1828
+
1829
+ """
1830
+ def _neg_llf(lmbda, data):
1831
+ llf = yeojohnson_llf(lmbda, data)
1832
+ # reject likelihoods that are inf which are likely due to small
1833
+ # variance in the transformed space
1834
+ llf[np.isinf(llf)] = -np.inf
1835
+ return -llf
1836
+
1837
+ with np.errstate(invalid='ignore'):
1838
+ if not np.all(np.isfinite(x)):
1839
+ raise ValueError('Yeo-Johnson input must be finite.')
1840
+ if np.all(x == 0):
1841
+ return 1.0
1842
+ if brack is not None:
1843
+ return optimize.brent(_neg_llf, brack=brack, args=(x,))
1844
+ x = np.asarray(x)
1845
+ dtype = x.dtype if np.issubdtype(x.dtype, np.floating) else np.float64
1846
+ # Allow values up to 20 times the maximum observed value to be safely
1847
+ # transformed without over- or underflow.
1848
+ log1p_max_x = np.log1p(20 * np.max(np.abs(x)))
1849
+ # Use half of floating point's exponent range to allow safe computation
1850
+ # of the variance of the transformed data.
1851
+ log_eps = np.log(np.finfo(dtype).eps)
1852
+ log_tiny_float = (np.log(np.finfo(dtype).tiny) - log_eps) / 2
1853
+ log_max_float = (np.log(np.finfo(dtype).max) + log_eps) / 2
1854
+ # Compute the bounds by approximating the inverse of the Yeo-Johnson
1855
+ # transform on the smallest and largest floating point exponents, given
1856
+ # the largest data we expect to observe. See [1] for further details.
1857
+ # [1] https://github.com/scipy/scipy/pull/18852#issuecomment-1630286174
1858
+ lb = log_tiny_float / log1p_max_x
1859
+ ub = log_max_float / log1p_max_x
1860
+ # Convert the bounds if all or some of the data is negative.
1861
+ if np.all(x < 0):
1862
+ lb, ub = 2 - ub, 2 - lb
1863
+ elif np.any(x < 0):
1864
+ lb, ub = max(2 - ub, lb), min(2 - lb, ub)
1865
+ # Match `optimize.brent`'s tolerance.
1866
+ tol_brent = 1.48e-08
1867
+ return optimize.fminbound(_neg_llf, lb, ub, args=(x,), xtol=tol_brent)
1868
+
1869
+
1870
+ def yeojohnson_normplot(x, la, lb, plot=None, N=80):
1871
+ """Compute parameters for a Yeo-Johnson normality plot, optionally show it.
1872
+
1873
+ A Yeo-Johnson normality plot shows graphically what the best
1874
+ transformation parameter is to use in `yeojohnson` to obtain a
1875
+ distribution that is close to normal.
1876
+
1877
+ Parameters
1878
+ ----------
1879
+ x : array_like
1880
+ Input array.
1881
+ la, lb : scalar
1882
+ The lower and upper bounds for the ``lmbda`` values to pass to
1883
+ `yeojohnson` for Yeo-Johnson transformations. These are also the
1884
+ limits of the horizontal axis of the plot if that is generated.
1885
+ plot : object, optional
1886
+ If given, plots the quantiles and least squares fit.
1887
+ `plot` is an object that has to have methods "plot" and "text".
1888
+ The `matplotlib.pyplot` module or a Matplotlib Axes object can be used,
1889
+ or a custom object with the same methods.
1890
+ Default is None, which means that no plot is created.
1891
+ N : int, optional
1892
+ Number of points on the horizontal axis (equally distributed from
1893
+ `la` to `lb`).
1894
+
1895
+ Returns
1896
+ -------
1897
+ lmbdas : ndarray
1898
+ The ``lmbda`` values for which a Yeo-Johnson transform was done.
1899
+ ppcc : ndarray
1900
+ Probability Plot Correlation Coefficient, as obtained from `probplot`
1901
+ when fitting the Box-Cox transformed input `x` against a normal
1902
+ distribution.
1903
+
1904
+ See Also
1905
+ --------
1906
+ probplot, yeojohnson, yeojohnson_normmax, yeojohnson_llf, ppcc_max
1907
+
1908
+ Notes
1909
+ -----
1910
+ Even if `plot` is given, the figure is not shown or saved by
1911
+ `boxcox_normplot`; ``plt.show()`` or ``plt.savefig('figname.png')``
1912
+ should be used after calling `probplot`.
1913
+
1914
+ .. versionadded:: 1.2.0
1915
+
1916
+ Examples
1917
+ --------
1918
+ >>> from scipy import stats
1919
+ >>> import matplotlib.pyplot as plt
1920
+
1921
+ Generate some non-normally distributed data, and create a Yeo-Johnson plot:
1922
+
1923
+ >>> x = stats.loggamma.rvs(5, size=500) + 5
1924
+ >>> fig = plt.figure()
1925
+ >>> ax = fig.add_subplot(111)
1926
+ >>> prob = stats.yeojohnson_normplot(x, -20, 20, plot=ax)
1927
+
1928
+ Determine and plot the optimal ``lmbda`` to transform ``x`` and plot it in
1929
+ the same plot:
1930
+
1931
+ >>> _, maxlog = stats.yeojohnson(x)
1932
+ >>> ax.axvline(maxlog, color='r')
1933
+
1934
+ >>> plt.show()
1935
+
1936
+ """
1937
+ return _normplot('yeojohnson', x, la, lb, plot, N)
1938
+
1939
+
1940
+ ShapiroResult = namedtuple('ShapiroResult', ('statistic', 'pvalue'))
1941
+
1942
+
1943
+ @_axis_nan_policy_factory(ShapiroResult, n_samples=1, too_small=2, default_axis=None)
1944
+ def shapiro(x):
1945
+ r"""Perform the Shapiro-Wilk test for normality.
1946
+
1947
+ The Shapiro-Wilk test tests the null hypothesis that the
1948
+ data was drawn from a normal distribution.
1949
+
1950
+ Parameters
1951
+ ----------
1952
+ x : array_like
1953
+ Array of sample data. Must contain at least three observations.
1954
+
1955
+ Returns
1956
+ -------
1957
+ statistic : float
1958
+ The test statistic.
1959
+ p-value : float
1960
+ The p-value for the hypothesis test.
1961
+
1962
+ See Also
1963
+ --------
1964
+ anderson : The Anderson-Darling test for normality
1965
+ kstest : The Kolmogorov-Smirnov test for goodness of fit.
1966
+ :ref:`hypothesis_shapiro` : Extended example
1967
+
1968
+ Notes
1969
+ -----
1970
+ The algorithm used is described in [4]_ but censoring parameters as
1971
+ described are not implemented. For N > 5000 the W test statistic is
1972
+ accurate, but the p-value may not be.
1973
+
1974
+ References
1975
+ ----------
1976
+ .. [1] https://www.itl.nist.gov/div898/handbook/prc/section2/prc213.htm
1977
+ :doi:`10.18434/M32189`
1978
+ .. [2] Shapiro, S. S. & Wilk, M.B, "An analysis of variance test for
1979
+ normality (complete samples)", Biometrika, 1965, Vol. 52,
1980
+ pp. 591-611, :doi:`10.2307/2333709`
1981
+ .. [3] Razali, N. M. & Wah, Y. B., "Power comparisons of Shapiro-Wilk,
1982
+ Kolmogorov-Smirnov, Lilliefors and Anderson-Darling tests", Journal
1983
+ of Statistical Modeling and Analytics, 2011, Vol. 2, pp. 21-33.
1984
+ .. [4] Royston P., "Remark AS R94: A Remark on Algorithm AS 181: The
1985
+ W-test for Normality", 1995, Applied Statistics, Vol. 44,
1986
+ :doi:`10.2307/2986146`
1987
+
1988
+ Examples
1989
+ --------
1990
+
1991
+ >>> import numpy as np
1992
+ >>> from scipy import stats
1993
+ >>> rng = np.random.default_rng()
1994
+ >>> x = stats.norm.rvs(loc=5, scale=3, size=100, random_state=rng)
1995
+ >>> shapiro_test = stats.shapiro(x)
1996
+ >>> shapiro_test
1997
+ ShapiroResult(statistic=0.9813305735588074, pvalue=0.16855233907699585)
1998
+ >>> shapiro_test.statistic
1999
+ 0.9813305735588074
2000
+ >>> shapiro_test.pvalue
2001
+ 0.16855233907699585
2002
+
2003
+ For a more detailed example, see :ref:`hypothesis_shapiro`.
2004
+ """
2005
+ x = np.ravel(x).astype(np.float64)
2006
+
2007
+ N = len(x)
2008
+ if N < 3:
2009
+ raise ValueError("Data must be at least length 3.")
2010
+
2011
+ a = zeros(N//2, dtype=np.float64)
2012
+ init = 0
2013
+
2014
+ y = sort(x)
2015
+ y -= x[N//2] # subtract the median (or a nearby value); see gh-15777
2016
+
2017
+ w, pw, ifault = swilk(y, a, init)
2018
+ if ifault not in [0, 2]:
2019
+ warnings.warn("scipy.stats.shapiro: Input data has range zero. The"
2020
+ " results may not be accurate.", stacklevel=2)
2021
+ if N > 5000:
2022
+ warnings.warn("scipy.stats.shapiro: For N > 5000, computed p-value "
2023
+ f"may not be accurate. Current N is {N}.",
2024
+ stacklevel=2)
2025
+
2026
+ # `w` and `pw` are always Python floats, which are double precision.
2027
+ # We want to ensure that they are NumPy floats, so until dtypes are
2028
+ # respected, we can explicitly convert each to float64 (faster than
2029
+ # `np.array([w, pw])`).
2030
+ return ShapiroResult(np.float64(w), np.float64(pw))
2031
+
2032
+
2033
+ # Values from Stephens, M A, "EDF Statistics for Goodness of Fit and
2034
+ # Some Comparisons", Journal of the American Statistical
2035
+ # Association, Vol. 69, Issue 347, Sept. 1974, pp 730-737
2036
+ _Avals_norm = array([0.576, 0.656, 0.787, 0.918, 1.092])
2037
+ _Avals_expon = array([0.922, 1.078, 1.341, 1.606, 1.957])
2038
+ # From Stephens, M A, "Goodness of Fit for the Extreme Value Distribution",
2039
+ # Biometrika, Vol. 64, Issue 3, Dec. 1977, pp 583-588.
2040
+ _Avals_gumbel = array([0.474, 0.637, 0.757, 0.877, 1.038])
2041
+ # From Stephens, M A, "Tests of Fit for the Logistic Distribution Based
2042
+ # on the Empirical Distribution Function.", Biometrika,
2043
+ # Vol. 66, Issue 3, Dec. 1979, pp 591-595.
2044
+ _Avals_logistic = array([0.426, 0.563, 0.660, 0.769, 0.906, 1.010])
2045
+ # From Richard A. Lockhart and Michael A. Stephens "Estimation and Tests of
2046
+ # Fit for the Three-Parameter Weibull Distribution"
2047
+ # Journal of the Royal Statistical Society.Series B(Methodological)
2048
+ # Vol. 56, No. 3 (1994), pp. 491-500, table 1. Keys are c*100
2049
+ _Avals_weibull = [[0.292, 0.395, 0.467, 0.522, 0.617, 0.711, 0.836, 0.931],
2050
+ [0.295, 0.399, 0.471, 0.527, 0.623, 0.719, 0.845, 0.941],
2051
+ [0.298, 0.403, 0.476, 0.534, 0.631, 0.728, 0.856, 0.954],
2052
+ [0.301, 0.408, 0.483, 0.541, 0.640, 0.738, 0.869, 0.969],
2053
+ [0.305, 0.414, 0.490, 0.549, 0.650, 0.751, 0.885, 0.986],
2054
+ [0.309, 0.421, 0.498, 0.559, 0.662, 0.765, 0.902, 1.007],
2055
+ [0.314, 0.429, 0.508, 0.570, 0.676, 0.782, 0.923, 1.030],
2056
+ [0.320, 0.438, 0.519, 0.583, 0.692, 0.802, 0.947, 1.057],
2057
+ [0.327, 0.448, 0.532, 0.598, 0.711, 0.824, 0.974, 1.089],
2058
+ [0.334, 0.469, 0.547, 0.615, 0.732, 0.850, 1.006, 1.125],
2059
+ [0.342, 0.472, 0.563, 0.636, 0.757, 0.879, 1.043, 1.167]]
2060
+ _Avals_weibull = np.array(_Avals_weibull)
2061
+ _cvals_weibull = np.linspace(0, 0.5, 11)
2062
+ _get_As_weibull = interpolate.interp1d(_cvals_weibull, _Avals_weibull.T,
2063
+ kind='linear', bounds_error=False,
2064
+ fill_value=_Avals_weibull[-1])
2065
+
2066
+
2067
+ def _weibull_fit_check(params, x):
2068
+ # Refine the fit returned by `weibull_min.fit` to ensure that the first
2069
+ # order necessary conditions are satisfied. If not, raise an error.
2070
+ # Here, use `m` for the shape parameter to be consistent with [7]
2071
+ # and avoid confusion with `c` as defined in [7].
2072
+ n = len(x)
2073
+ m, u, s = params
2074
+
2075
+ def dnllf_dm(m, u):
2076
+ # Partial w.r.t. shape w/ optimal scale. See [7] Equation 5.
2077
+ xu = x-u
2078
+ return (1/m - (xu**m*np.log(xu)).sum()/(xu**m).sum()
2079
+ + np.log(xu).sum()/n)
2080
+
2081
+ def dnllf_du(m, u):
2082
+ # Partial w.r.t. loc w/ optimal scale. See [7] Equation 6.
2083
+ xu = x-u
2084
+ return (m-1)/m*(xu**-1).sum() - n*(xu**(m-1)).sum()/(xu**m).sum()
2085
+
2086
+ def get_scale(m, u):
2087
+ # Partial w.r.t. scale solved in terms of shape and location.
2088
+ # See [7] Equation 7.
2089
+ return ((x-u)**m/n).sum()**(1/m)
2090
+
2091
+ def dnllf(params):
2092
+ # Partial derivatives of the NLLF w.r.t. parameters, i.e.
2093
+ # first order necessary conditions for MLE fit.
2094
+ return [dnllf_dm(*params), dnllf_du(*params)]
2095
+
2096
+ suggestion = ("Maximum likelihood estimation is known to be challenging "
2097
+ "for the three-parameter Weibull distribution. Consider "
2098
+ "performing a custom goodness-of-fit test using "
2099
+ "`scipy.stats.monte_carlo_test`.")
2100
+
2101
+ if np.allclose(u, np.min(x)) or m < 1:
2102
+ # The critical values provided by [7] don't seem to control the
2103
+ # Type I error rate in this case. Error out.
2104
+ message = ("Maximum likelihood estimation has converged to "
2105
+ "a solution in which the location is equal to the minimum "
2106
+ "of the data, the shape parameter is less than 2, or both. "
2107
+ "The table of critical values in [7] does not "
2108
+ "include this case. " + suggestion)
2109
+ raise ValueError(message)
2110
+
2111
+ try:
2112
+ # Refine the MLE / verify that first-order necessary conditions are
2113
+ # satisfied. If so, the critical values provided in [7] seem reliable.
2114
+ with np.errstate(over='raise', invalid='raise'):
2115
+ res = optimize.root(dnllf, params[:-1])
2116
+
2117
+ message = ("Solution of MLE first-order conditions failed: "
2118
+ f"{res.message}. `anderson` cannot continue. " + suggestion)
2119
+ if not res.success:
2120
+ raise ValueError(message)
2121
+
2122
+ except (FloatingPointError, ValueError) as e:
2123
+ message = ("An error occurred while fitting the Weibull distribution "
2124
+ "to the data, so `anderson` cannot continue. " + suggestion)
2125
+ raise ValueError(message) from e
2126
+
2127
+ m, u = res.x
2128
+ s = get_scale(m, u)
2129
+ return m, u, s
2130
+
2131
+
2132
+ AndersonResult = _make_tuple_bunch('AndersonResult',
2133
+ ['statistic', 'critical_values',
2134
+ 'significance_level'], ['fit_result'])
2135
+
2136
+
2137
+ def anderson(x, dist='norm'):
2138
+ """Anderson-Darling test for data coming from a particular distribution.
2139
+
2140
+ The Anderson-Darling test tests the null hypothesis that a sample is
2141
+ drawn from a population that follows a particular distribution.
2142
+ For the Anderson-Darling test, the critical values depend on
2143
+ which distribution is being tested against. This function works
2144
+ for normal, exponential, logistic, weibull_min, or Gumbel (Extreme Value
2145
+ Type I) distributions.
2146
+
2147
+ Parameters
2148
+ ----------
2149
+ x : array_like
2150
+ Array of sample data.
2151
+ dist : {'norm', 'expon', 'logistic', 'gumbel', 'gumbel_l', 'gumbel_r', 'extreme1', 'weibull_min'}, optional
2152
+ The type of distribution to test against. The default is 'norm'.
2153
+ The names 'extreme1', 'gumbel_l' and 'gumbel' are synonyms for the
2154
+ same distribution.
2155
+
2156
+ Returns
2157
+ -------
2158
+ result : AndersonResult
2159
+ An object with the following attributes:
2160
+
2161
+ statistic : float
2162
+ The Anderson-Darling test statistic.
2163
+ critical_values : list
2164
+ The critical values for this distribution.
2165
+ significance_level : list
2166
+ The significance levels for the corresponding critical values
2167
+ in percents. The function returns critical values for a
2168
+ differing set of significance levels depending on the
2169
+ distribution that is being tested against.
2170
+ fit_result : `~scipy.stats._result_classes.FitResult`
2171
+ An object containing the results of fitting the distribution to
2172
+ the data.
2173
+
2174
+ See Also
2175
+ --------
2176
+ kstest : The Kolmogorov-Smirnov test for goodness-of-fit.
2177
+
2178
+ Notes
2179
+ -----
2180
+ Critical values provided are for the following significance levels:
2181
+
2182
+ normal/exponential
2183
+ 15%, 10%, 5%, 2.5%, 1%
2184
+ logistic
2185
+ 25%, 10%, 5%, 2.5%, 1%, 0.5%
2186
+ gumbel_l / gumbel_r
2187
+ 25%, 10%, 5%, 2.5%, 1%
2188
+ weibull_min
2189
+ 50%, 25%, 15%, 10%, 5%, 2.5%, 1%, 0.5%
2190
+
2191
+ If the returned statistic is larger than these critical values then
2192
+ for the corresponding significance level, the null hypothesis that
2193
+ the data come from the chosen distribution can be rejected.
2194
+ The returned statistic is referred to as 'A2' in the references.
2195
+
2196
+ For `weibull_min`, maximum likelihood estimation is known to be
2197
+ challenging. If the test returns successfully, then the first order
2198
+ conditions for a maximum likelihood estimate have been verified and
2199
+ the critical values correspond relatively well to the significance levels,
2200
+ provided that the sample is sufficiently large (>10 observations [7]).
2201
+ However, for some data - especially data with no left tail - `anderson`
2202
+ is likely to result in an error message. In this case, consider
2203
+ performing a custom goodness of fit test using
2204
+ `scipy.stats.monte_carlo_test`.
2205
+
2206
+ References
2207
+ ----------
2208
+ .. [1] https://www.itl.nist.gov/div898/handbook/prc/section2/prc213.htm
2209
+ .. [2] Stephens, M. A. (1974). EDF Statistics for Goodness of Fit and
2210
+ Some Comparisons, Journal of the American Statistical Association,
2211
+ Vol. 69, pp. 730-737.
2212
+ .. [3] Stephens, M. A. (1976). Asymptotic Results for Goodness-of-Fit
2213
+ Statistics with Unknown Parameters, Annals of Statistics, Vol. 4,
2214
+ pp. 357-369.
2215
+ .. [4] Stephens, M. A. (1977). Goodness of Fit for the Extreme Value
2216
+ Distribution, Biometrika, Vol. 64, pp. 583-588.
2217
+ .. [5] Stephens, M. A. (1977). Goodness of Fit with Special Reference
2218
+ to Tests for Exponentiality , Technical Report No. 262,
2219
+ Department of Statistics, Stanford University, Stanford, CA.
2220
+ .. [6] Stephens, M. A. (1979). Tests of Fit for the Logistic Distribution
2221
+ Based on the Empirical Distribution Function, Biometrika, Vol. 66,
2222
+ pp. 591-595.
2223
+ .. [7] Richard A. Lockhart and Michael A. Stephens "Estimation and Tests of
2224
+ Fit for the Three-Parameter Weibull Distribution"
2225
+ Journal of the Royal Statistical Society.Series B(Methodological)
2226
+ Vol. 56, No. 3 (1994), pp. 491-500, Table 0.
2227
+
2228
+ Examples
2229
+ --------
2230
+ Test the null hypothesis that a random sample was drawn from a normal
2231
+ distribution (with unspecified mean and standard deviation).
2232
+
2233
+ >>> import numpy as np
2234
+ >>> from scipy.stats import anderson
2235
+ >>> rng = np.random.default_rng()
2236
+ >>> data = rng.random(size=35)
2237
+ >>> res = anderson(data)
2238
+ >>> res.statistic
2239
+ 0.8398018749744764
2240
+ >>> res.critical_values
2241
+ array([0.527, 0.6 , 0.719, 0.839, 0.998])
2242
+ >>> res.significance_level
2243
+ array([15. , 10. , 5. , 2.5, 1. ])
2244
+
2245
+ The value of the statistic (barely) exceeds the critical value associated
2246
+ with a significance level of 2.5%, so the null hypothesis may be rejected
2247
+ at a significance level of 2.5%, but not at a significance level of 1%.
2248
+
2249
+ """ # numpy/numpydoc#87 # noqa: E501
2250
+ dist = dist.lower()
2251
+ if dist in {'extreme1', 'gumbel'}:
2252
+ dist = 'gumbel_l'
2253
+ dists = {'norm', 'expon', 'gumbel_l',
2254
+ 'gumbel_r', 'logistic', 'weibull_min'}
2255
+
2256
+ if dist not in dists:
2257
+ raise ValueError(f"Invalid distribution; dist must be in {dists}.")
2258
+ y = sort(x)
2259
+ xbar = np.mean(x, axis=0)
2260
+ N = len(y)
2261
+ if dist == 'norm':
2262
+ s = np.std(x, ddof=1, axis=0)
2263
+ w = (y - xbar) / s
2264
+ fit_params = xbar, s
2265
+ logcdf = distributions.norm.logcdf(w)
2266
+ logsf = distributions.norm.logsf(w)
2267
+ sig = array([15, 10, 5, 2.5, 1])
2268
+ critical = around(_Avals_norm / (1.0 + 4.0/N - 25.0/N/N), 3)
2269
+ elif dist == 'expon':
2270
+ w = y / xbar
2271
+ fit_params = 0, xbar
2272
+ logcdf = distributions.expon.logcdf(w)
2273
+ logsf = distributions.expon.logsf(w)
2274
+ sig = array([15, 10, 5, 2.5, 1])
2275
+ critical = around(_Avals_expon / (1.0 + 0.6/N), 3)
2276
+ elif dist == 'logistic':
2277
+ def rootfunc(ab, xj, N):
2278
+ a, b = ab
2279
+ tmp = (xj - a) / b
2280
+ tmp2 = exp(tmp)
2281
+ val = [np.sum(1.0/(1+tmp2), axis=0) - 0.5*N,
2282
+ np.sum(tmp*(1.0-tmp2)/(1+tmp2), axis=0) + N]
2283
+ return array(val)
2284
+
2285
+ sol0 = array([xbar, np.std(x, ddof=1, axis=0)])
2286
+ sol = optimize.fsolve(rootfunc, sol0, args=(x, N), xtol=1e-5)
2287
+ w = (y - sol[0]) / sol[1]
2288
+ fit_params = sol
2289
+ logcdf = distributions.logistic.logcdf(w)
2290
+ logsf = distributions.logistic.logsf(w)
2291
+ sig = array([25, 10, 5, 2.5, 1, 0.5])
2292
+ critical = around(_Avals_logistic / (1.0 + 0.25/N), 3)
2293
+ elif dist == 'gumbel_r':
2294
+ xbar, s = distributions.gumbel_r.fit(x)
2295
+ w = (y - xbar) / s
2296
+ fit_params = xbar, s
2297
+ logcdf = distributions.gumbel_r.logcdf(w)
2298
+ logsf = distributions.gumbel_r.logsf(w)
2299
+ sig = array([25, 10, 5, 2.5, 1])
2300
+ critical = around(_Avals_gumbel / (1.0 + 0.2/sqrt(N)), 3)
2301
+ elif dist == 'gumbel_l':
2302
+ xbar, s = distributions.gumbel_l.fit(x)
2303
+ w = (y - xbar) / s
2304
+ fit_params = xbar, s
2305
+ logcdf = distributions.gumbel_l.logcdf(w)
2306
+ logsf = distributions.gumbel_l.logsf(w)
2307
+ sig = array([25, 10, 5, 2.5, 1])
2308
+ critical = around(_Avals_gumbel / (1.0 + 0.2/sqrt(N)), 3)
2309
+ elif dist == 'weibull_min':
2310
+ message = ("Critical values of the test statistic are given for the "
2311
+ "asymptotic distribution. These may not be accurate for "
2312
+ "samples with fewer than 10 observations. Consider using "
2313
+ "`scipy.stats.monte_carlo_test`.")
2314
+ if N < 10:
2315
+ warnings.warn(message, stacklevel=2)
2316
+ # [7] writes our 'c' as 'm', and they write `c = 1/m`. Use their names.
2317
+ m, loc, scale = distributions.weibull_min.fit(y)
2318
+ m, loc, scale = _weibull_fit_check((m, loc, scale), y)
2319
+ fit_params = m, loc, scale
2320
+ logcdf = stats.weibull_min(*fit_params).logcdf(y)
2321
+ logsf = stats.weibull_min(*fit_params).logsf(y)
2322
+ c = 1 / m # m and c are as used in [7]
2323
+ sig = array([0.5, 0.75, 0.85, 0.9, 0.95, 0.975, 0.99, 0.995])
2324
+ critical = _get_As_weibull(c)
2325
+ # Goodness-of-fit tests should only be used to provide evidence
2326
+ # _against_ the null hypothesis. Be conservative and round up.
2327
+ critical = np.round(critical + 0.0005, decimals=3)
2328
+
2329
+ i = arange(1, N + 1)
2330
+ A2 = -N - np.sum((2*i - 1.0) / N * (logcdf + logsf[::-1]), axis=0)
2331
+
2332
+ # FitResult initializer expects an optimize result, so let's work with it
2333
+ message = '`anderson` successfully fit the distribution to the data.'
2334
+ res = optimize.OptimizeResult(success=True, message=message)
2335
+ res.x = np.array(fit_params)
2336
+ fit_result = FitResult(getattr(distributions, dist), y,
2337
+ discrete=False, res=res)
2338
+
2339
+ return AndersonResult(A2, critical, sig, fit_result=fit_result)
2340
+
2341
+
2342
+ def _anderson_ksamp_midrank(samples, Z, Zstar, k, n, N):
2343
+ """Compute A2akN equation 7 of Scholz and Stephens.
2344
+
2345
+ Parameters
2346
+ ----------
2347
+ samples : sequence of 1-D array_like
2348
+ Array of sample arrays.
2349
+ Z : array_like
2350
+ Sorted array of all observations.
2351
+ Zstar : array_like
2352
+ Sorted array of unique observations.
2353
+ k : int
2354
+ Number of samples.
2355
+ n : array_like
2356
+ Number of observations in each sample.
2357
+ N : int
2358
+ Total number of observations.
2359
+
2360
+ Returns
2361
+ -------
2362
+ A2aKN : float
2363
+ The A2aKN statistics of Scholz and Stephens 1987.
2364
+
2365
+ """
2366
+ A2akN = 0.
2367
+ Z_ssorted_left = Z.searchsorted(Zstar, 'left')
2368
+ if N == Zstar.size:
2369
+ lj = 1.
2370
+ else:
2371
+ lj = Z.searchsorted(Zstar, 'right') - Z_ssorted_left
2372
+ Bj = Z_ssorted_left + lj / 2.
2373
+ for i in arange(0, k):
2374
+ s = np.sort(samples[i])
2375
+ s_ssorted_right = s.searchsorted(Zstar, side='right')
2376
+ Mij = s_ssorted_right.astype(float)
2377
+ fij = s_ssorted_right - s.searchsorted(Zstar, 'left')
2378
+ Mij -= fij / 2.
2379
+ inner = lj / float(N) * (N*Mij - Bj*n[i])**2 / (Bj*(N - Bj) - N*lj/4.)
2380
+ A2akN += inner.sum() / n[i]
2381
+ A2akN *= (N - 1.) / N
2382
+ return A2akN
2383
+
2384
+
2385
+ def _anderson_ksamp_right(samples, Z, Zstar, k, n, N):
2386
+ """Compute A2akN equation 6 of Scholz & Stephens.
2387
+
2388
+ Parameters
2389
+ ----------
2390
+ samples : sequence of 1-D array_like
2391
+ Array of sample arrays.
2392
+ Z : array_like
2393
+ Sorted array of all observations.
2394
+ Zstar : array_like
2395
+ Sorted array of unique observations.
2396
+ k : int
2397
+ Number of samples.
2398
+ n : array_like
2399
+ Number of observations in each sample.
2400
+ N : int
2401
+ Total number of observations.
2402
+
2403
+ Returns
2404
+ -------
2405
+ A2KN : float
2406
+ The A2KN statistics of Scholz and Stephens 1987.
2407
+
2408
+ """
2409
+ A2kN = 0.
2410
+ lj = Z.searchsorted(Zstar[:-1], 'right') - Z.searchsorted(Zstar[:-1],
2411
+ 'left')
2412
+ Bj = lj.cumsum()
2413
+ for i in arange(0, k):
2414
+ s = np.sort(samples[i])
2415
+ Mij = s.searchsorted(Zstar[:-1], side='right')
2416
+ inner = lj / float(N) * (N * Mij - Bj * n[i])**2 / (Bj * (N - Bj))
2417
+ A2kN += inner.sum() / n[i]
2418
+ return A2kN
2419
+
2420
+
2421
+ Anderson_ksampResult = _make_tuple_bunch(
2422
+ 'Anderson_ksampResult',
2423
+ ['statistic', 'critical_values', 'pvalue'], []
2424
+ )
2425
+
2426
+
2427
+ def anderson_ksamp(samples, midrank=True, *, method=None):
2428
+ """The Anderson-Darling test for k-samples.
2429
+
2430
+ The k-sample Anderson-Darling test is a modification of the
2431
+ one-sample Anderson-Darling test. It tests the null hypothesis
2432
+ that k-samples are drawn from the same population without having
2433
+ to specify the distribution function of that population. The
2434
+ critical values depend on the number of samples.
2435
+
2436
+ Parameters
2437
+ ----------
2438
+ samples : sequence of 1-D array_like
2439
+ Array of sample data in arrays.
2440
+ midrank : bool, optional
2441
+ Type of Anderson-Darling test which is computed. Default
2442
+ (True) is the midrank test applicable to continuous and
2443
+ discrete populations. If False, the right side empirical
2444
+ distribution is used.
2445
+ method : PermutationMethod, optional
2446
+ Defines the method used to compute the p-value. If `method` is an
2447
+ instance of `PermutationMethod`, the p-value is computed using
2448
+ `scipy.stats.permutation_test` with the provided configuration options
2449
+ and other appropriate settings. Otherwise, the p-value is interpolated
2450
+ from tabulated values.
2451
+
2452
+ Returns
2453
+ -------
2454
+ res : Anderson_ksampResult
2455
+ An object containing attributes:
2456
+
2457
+ statistic : float
2458
+ Normalized k-sample Anderson-Darling test statistic.
2459
+ critical_values : array
2460
+ The critical values for significance levels 25%, 10%, 5%, 2.5%, 1%,
2461
+ 0.5%, 0.1%.
2462
+ pvalue : float
2463
+ The approximate p-value of the test. If `method` is not
2464
+ provided, the value is floored / capped at 0.1% / 25%.
2465
+
2466
+ Raises
2467
+ ------
2468
+ ValueError
2469
+ If fewer than 2 samples are provided, a sample is empty, or no
2470
+ distinct observations are in the samples.
2471
+
2472
+ See Also
2473
+ --------
2474
+ ks_2samp : 2 sample Kolmogorov-Smirnov test
2475
+ anderson : 1 sample Anderson-Darling test
2476
+
2477
+ Notes
2478
+ -----
2479
+ [1]_ defines three versions of the k-sample Anderson-Darling test:
2480
+ one for continuous distributions and two for discrete
2481
+ distributions, in which ties between samples may occur. The
2482
+ default of this routine is to compute the version based on the
2483
+ midrank empirical distribution function. This test is applicable
2484
+ to continuous and discrete data. If midrank is set to False, the
2485
+ right side empirical distribution is used for a test for discrete
2486
+ data. According to [1]_, the two discrete test statistics differ
2487
+ only slightly if a few collisions due to round-off errors occur in
2488
+ the test not adjusted for ties between samples.
2489
+
2490
+ The critical values corresponding to the significance levels from 0.01
2491
+ to 0.25 are taken from [1]_. p-values are floored / capped
2492
+ at 0.1% / 25%. Since the range of critical values might be extended in
2493
+ future releases, it is recommended not to test ``p == 0.25``, but rather
2494
+ ``p >= 0.25`` (analogously for the lower bound).
2495
+
2496
+ .. versionadded:: 0.14.0
2497
+
2498
+ References
2499
+ ----------
2500
+ .. [1] Scholz, F. W and Stephens, M. A. (1987), K-Sample
2501
+ Anderson-Darling Tests, Journal of the American Statistical
2502
+ Association, Vol. 82, pp. 918-924.
2503
+
2504
+ Examples
2505
+ --------
2506
+ >>> import numpy as np
2507
+ >>> from scipy import stats
2508
+ >>> rng = np.random.default_rng()
2509
+ >>> res = stats.anderson_ksamp([rng.normal(size=50),
2510
+ ... rng.normal(loc=0.5, size=30)])
2511
+ >>> res.statistic, res.pvalue
2512
+ (1.974403288713695, 0.04991293614572478)
2513
+ >>> res.critical_values
2514
+ array([0.325, 1.226, 1.961, 2.718, 3.752, 4.592, 6.546])
2515
+
2516
+ The null hypothesis that the two random samples come from the same
2517
+ distribution can be rejected at the 5% level because the returned
2518
+ test value is greater than the critical value for 5% (1.961) but
2519
+ not at the 2.5% level. The interpolation gives an approximate
2520
+ p-value of 4.99%.
2521
+
2522
+ >>> samples = [rng.normal(size=50), rng.normal(size=30),
2523
+ ... rng.normal(size=20)]
2524
+ >>> res = stats.anderson_ksamp(samples)
2525
+ >>> res.statistic, res.pvalue
2526
+ (-0.29103725200789504, 0.25)
2527
+ >>> res.critical_values
2528
+ array([ 0.44925884, 1.3052767 , 1.9434184 , 2.57696569, 3.41634856,
2529
+ 4.07210043, 5.56419101])
2530
+
2531
+ The null hypothesis cannot be rejected for three samples from an
2532
+ identical distribution. The reported p-value (25%) has been capped and
2533
+ may not be very accurate (since it corresponds to the value 0.449
2534
+ whereas the statistic is -0.291).
2535
+
2536
+ In such cases where the p-value is capped or when sample sizes are
2537
+ small, a permutation test may be more accurate.
2538
+
2539
+ >>> method = stats.PermutationMethod(n_resamples=9999, random_state=rng)
2540
+ >>> res = stats.anderson_ksamp(samples, method=method)
2541
+ >>> res.pvalue
2542
+ 0.5254
2543
+
2544
+ """
2545
+ k = len(samples)
2546
+ if (k < 2):
2547
+ raise ValueError("anderson_ksamp needs at least two samples")
2548
+
2549
+ samples = list(map(np.asarray, samples))
2550
+ Z = np.sort(np.hstack(samples))
2551
+ N = Z.size
2552
+ Zstar = np.unique(Z)
2553
+ if Zstar.size < 2:
2554
+ raise ValueError("anderson_ksamp needs more than one distinct "
2555
+ "observation")
2556
+
2557
+ n = np.array([sample.size for sample in samples])
2558
+ if np.any(n == 0):
2559
+ raise ValueError("anderson_ksamp encountered sample without "
2560
+ "observations")
2561
+
2562
+ if midrank:
2563
+ A2kN_fun = _anderson_ksamp_midrank
2564
+ else:
2565
+ A2kN_fun = _anderson_ksamp_right
2566
+ A2kN = A2kN_fun(samples, Z, Zstar, k, n, N)
2567
+
2568
+ def statistic(*samples):
2569
+ return A2kN_fun(samples, Z, Zstar, k, n, N)
2570
+
2571
+ if method is not None:
2572
+ res = stats.permutation_test(samples, statistic, **method._asdict(),
2573
+ alternative='greater')
2574
+
2575
+ H = (1. / n).sum()
2576
+ hs_cs = (1. / arange(N - 1, 1, -1)).cumsum()
2577
+ h = hs_cs[-1] + 1
2578
+ g = (hs_cs / arange(2, N)).sum()
2579
+
2580
+ a = (4*g - 6) * (k - 1) + (10 - 6*g)*H
2581
+ b = (2*g - 4)*k**2 + 8*h*k + (2*g - 14*h - 4)*H - 8*h + 4*g - 6
2582
+ c = (6*h + 2*g - 2)*k**2 + (4*h - 4*g + 6)*k + (2*h - 6)*H + 4*h
2583
+ d = (2*h + 6)*k**2 - 4*h*k
2584
+ sigmasq = (a*N**3 + b*N**2 + c*N + d) / ((N - 1.) * (N - 2.) * (N - 3.))
2585
+ m = k - 1
2586
+ A2 = (A2kN - m) / math.sqrt(sigmasq)
2587
+
2588
+ # The b_i values are the interpolation coefficients from Table 2
2589
+ # of Scholz and Stephens 1987
2590
+ b0 = np.array([0.675, 1.281, 1.645, 1.96, 2.326, 2.573, 3.085])
2591
+ b1 = np.array([-0.245, 0.25, 0.678, 1.149, 1.822, 2.364, 3.615])
2592
+ b2 = np.array([-0.105, -0.305, -0.362, -0.391, -0.396, -0.345, -0.154])
2593
+ critical = b0 + b1 / math.sqrt(m) + b2 / m
2594
+
2595
+ sig = np.array([0.25, 0.1, 0.05, 0.025, 0.01, 0.005, 0.001])
2596
+
2597
+ if A2 < critical.min() and method is None:
2598
+ p = sig.max()
2599
+ msg = (f"p-value capped: true value larger than {p}. Consider "
2600
+ "specifying `method` "
2601
+ "(e.g. `method=stats.PermutationMethod()`.)")
2602
+ warnings.warn(msg, stacklevel=2)
2603
+ elif A2 > critical.max() and method is None:
2604
+ p = sig.min()
2605
+ msg = (f"p-value floored: true value smaller than {p}. Consider "
2606
+ "specifying `method` "
2607
+ "(e.g. `method=stats.PermutationMethod()`.)")
2608
+ warnings.warn(msg, stacklevel=2)
2609
+ elif method is None:
2610
+ # interpolation of probit of significance level
2611
+ pf = np.polyfit(critical, log(sig), 2)
2612
+ p = math.exp(np.polyval(pf, A2))
2613
+ else:
2614
+ p = res.pvalue if method is not None else p
2615
+
2616
+ # create result object with alias for backward compatibility
2617
+ res = Anderson_ksampResult(A2, critical, p)
2618
+ res.significance_level = p
2619
+ return res
2620
+
2621
+
2622
+ AnsariResult = namedtuple('AnsariResult', ('statistic', 'pvalue'))
2623
+
2624
+
2625
+ class _ABW:
2626
+ """Distribution of Ansari-Bradley W-statistic under the null hypothesis."""
2627
+ # TODO: calculate exact distribution considering ties
2628
+ # We could avoid summing over more than half the frequencies,
2629
+ # but initially it doesn't seem worth the extra complexity
2630
+
2631
+ def __init__(self):
2632
+ """Minimal initializer."""
2633
+ self.m = None
2634
+ self.n = None
2635
+ self.astart = None
2636
+ self.total = None
2637
+ self.freqs = None
2638
+
2639
+ def _recalc(self, n, m):
2640
+ """When necessary, recalculate exact distribution."""
2641
+ if n != self.n or m != self.m:
2642
+ self.n, self.m = n, m
2643
+ # distribution is NOT symmetric when m + n is odd
2644
+ # n is len(x), m is len(y), and ratio of scales is defined x/y
2645
+ astart, a1, _ = gscale(n, m)
2646
+ self.astart = astart # minimum value of statistic
2647
+ # Exact distribution of test statistic under null hypothesis
2648
+ # expressed as frequencies/counts/integers to maintain precision.
2649
+ # Stored as floats to avoid overflow of sums.
2650
+ self.freqs = a1.astype(np.float64)
2651
+ self.total = self.freqs.sum() # could calculate from m and n
2652
+ # probability mass is self.freqs / self.total;
2653
+
2654
+ def pmf(self, k, n, m):
2655
+ """Probability mass function."""
2656
+ self._recalc(n, m)
2657
+ # The convention here is that PMF at k = 12.5 is the same as at k = 12,
2658
+ # -> use `floor` in case of ties.
2659
+ ind = np.floor(k - self.astart).astype(int)
2660
+ return self.freqs[ind] / self.total
2661
+
2662
+ def cdf(self, k, n, m):
2663
+ """Cumulative distribution function."""
2664
+ self._recalc(n, m)
2665
+ # Null distribution derived without considering ties is
2666
+ # approximate. Round down to avoid Type I error.
2667
+ ind = np.ceil(k - self.astart).astype(int)
2668
+ return self.freqs[:ind+1].sum() / self.total
2669
+
2670
+ def sf(self, k, n, m):
2671
+ """Survival function."""
2672
+ self._recalc(n, m)
2673
+ # Null distribution derived without considering ties is
2674
+ # approximate. Round down to avoid Type I error.
2675
+ ind = np.floor(k - self.astart).astype(int)
2676
+ return self.freqs[ind:].sum() / self.total
2677
+
2678
+
2679
+ # Maintain state for faster repeat calls to ansari w/ method='exact'
2680
+ # _ABW() is calculated once per thread and stored as an attribute on
2681
+ # this thread-local variable inside ansari().
2682
+ _abw_state = threading.local()
2683
+
2684
+
2685
+ @_axis_nan_policy_factory(AnsariResult, n_samples=2)
2686
+ def ansari(x, y, alternative='two-sided'):
2687
+ """Perform the Ansari-Bradley test for equal scale parameters.
2688
+
2689
+ The Ansari-Bradley test ([1]_, [2]_) is a non-parametric test
2690
+ for the equality of the scale parameter of the distributions
2691
+ from which two samples were drawn. The null hypothesis states that
2692
+ the ratio of the scale of the distribution underlying `x` to the scale
2693
+ of the distribution underlying `y` is 1.
2694
+
2695
+ Parameters
2696
+ ----------
2697
+ x, y : array_like
2698
+ Arrays of sample data.
2699
+ alternative : {'two-sided', 'less', 'greater'}, optional
2700
+ Defines the alternative hypothesis. Default is 'two-sided'.
2701
+ The following options are available:
2702
+
2703
+ * 'two-sided': the ratio of scales is not equal to 1.
2704
+ * 'less': the ratio of scales is less than 1.
2705
+ * 'greater': the ratio of scales is greater than 1.
2706
+
2707
+ .. versionadded:: 1.7.0
2708
+
2709
+ Returns
2710
+ -------
2711
+ statistic : float
2712
+ The Ansari-Bradley test statistic.
2713
+ pvalue : float
2714
+ The p-value of the hypothesis test.
2715
+
2716
+ See Also
2717
+ --------
2718
+ fligner : A non-parametric test for the equality of k variances
2719
+ mood : A non-parametric test for the equality of two scale parameters
2720
+
2721
+ Notes
2722
+ -----
2723
+ The p-value given is exact when the sample sizes are both less than
2724
+ 55 and there are no ties, otherwise a normal approximation for the
2725
+ p-value is used.
2726
+
2727
+ References
2728
+ ----------
2729
+ .. [1] Ansari, A. R. and Bradley, R. A. (1960) Rank-sum tests for
2730
+ dispersions, Annals of Mathematical Statistics, 31, 1174-1189.
2731
+ .. [2] Sprent, Peter and N.C. Smeeton. Applied nonparametric
2732
+ statistical methods. 3rd ed. Chapman and Hall/CRC. 2001.
2733
+ Section 5.8.2.
2734
+ .. [3] Nathaniel E. Helwig "Nonparametric Dispersion and Equality
2735
+ Tests" at http://users.stat.umn.edu/~helwig/notes/npde-Notes.pdf
2736
+
2737
+ Examples
2738
+ --------
2739
+ >>> import numpy as np
2740
+ >>> from scipy.stats import ansari
2741
+ >>> rng = np.random.default_rng()
2742
+
2743
+ For these examples, we'll create three random data sets. The first
2744
+ two, with sizes 35 and 25, are drawn from a normal distribution with
2745
+ mean 0 and standard deviation 2. The third data set has size 25 and
2746
+ is drawn from a normal distribution with standard deviation 1.25.
2747
+
2748
+ >>> x1 = rng.normal(loc=0, scale=2, size=35)
2749
+ >>> x2 = rng.normal(loc=0, scale=2, size=25)
2750
+ >>> x3 = rng.normal(loc=0, scale=1.25, size=25)
2751
+
2752
+ First we apply `ansari` to `x1` and `x2`. These samples are drawn
2753
+ from the same distribution, so we expect the Ansari-Bradley test
2754
+ should not lead us to conclude that the scales of the distributions
2755
+ are different.
2756
+
2757
+ >>> ansari(x1, x2)
2758
+ AnsariResult(statistic=541.0, pvalue=0.9762532927399098)
2759
+
2760
+ With a p-value close to 1, we cannot conclude that there is a
2761
+ significant difference in the scales (as expected).
2762
+
2763
+ Now apply the test to `x1` and `x3`:
2764
+
2765
+ >>> ansari(x1, x3)
2766
+ AnsariResult(statistic=425.0, pvalue=0.0003087020407974518)
2767
+
2768
+ The probability of observing such an extreme value of the statistic
2769
+ under the null hypothesis of equal scales is only 0.03087%. We take this
2770
+ as evidence against the null hypothesis in favor of the alternative:
2771
+ the scales of the distributions from which the samples were drawn
2772
+ are not equal.
2773
+
2774
+ We can use the `alternative` parameter to perform a one-tailed test.
2775
+ In the above example, the scale of `x1` is greater than `x3` and so
2776
+ the ratio of scales of `x1` and `x3` is greater than 1. This means
2777
+ that the p-value when ``alternative='greater'`` should be near 0 and
2778
+ hence we should be able to reject the null hypothesis:
2779
+
2780
+ >>> ansari(x1, x3, alternative='greater')
2781
+ AnsariResult(statistic=425.0, pvalue=0.0001543510203987259)
2782
+
2783
+ As we can see, the p-value is indeed quite low. Use of
2784
+ ``alternative='less'`` should thus yield a large p-value:
2785
+
2786
+ >>> ansari(x1, x3, alternative='less')
2787
+ AnsariResult(statistic=425.0, pvalue=0.9998643258449039)
2788
+
2789
+ """
2790
+ if alternative not in {'two-sided', 'greater', 'less'}:
2791
+ raise ValueError("'alternative' must be 'two-sided',"
2792
+ " 'greater', or 'less'.")
2793
+
2794
+ if not hasattr(_abw_state, 'a'):
2795
+ _abw_state.a = _ABW()
2796
+
2797
+ x, y = asarray(x), asarray(y)
2798
+ n = len(x)
2799
+ m = len(y)
2800
+ if m < 1:
2801
+ raise ValueError("Not enough other observations.")
2802
+ if n < 1:
2803
+ raise ValueError("Not enough test observations.")
2804
+
2805
+ N = m + n
2806
+ xy = r_[x, y] # combine
2807
+ rank = _stats_py.rankdata(xy)
2808
+ symrank = amin(array((rank, N - rank + 1)), 0)
2809
+ AB = np.sum(symrank[:n], axis=0)
2810
+ uxy = unique(xy)
2811
+ repeats = (len(uxy) != len(xy))
2812
+ exact = ((m < 55) and (n < 55) and not repeats)
2813
+ if repeats and (m < 55 or n < 55):
2814
+ warnings.warn("Ties preclude use of exact statistic.", stacklevel=2)
2815
+ if exact:
2816
+ if alternative == 'two-sided':
2817
+ pval = 2.0 * np.minimum(_abw_state.a.cdf(AB, n, m),
2818
+ _abw_state.a.sf(AB, n, m))
2819
+ elif alternative == 'greater':
2820
+ # AB statistic is _smaller_ when ratio of scales is larger,
2821
+ # so this is the opposite of the usual calculation
2822
+ pval = _abw_state.a.cdf(AB, n, m)
2823
+ else:
2824
+ pval = _abw_state.a.sf(AB, n, m)
2825
+ return AnsariResult(AB, min(1.0, pval))
2826
+
2827
+ # otherwise compute normal approximation
2828
+ if N % 2: # N odd
2829
+ mnAB = n * (N+1.0)**2 / 4.0 / N
2830
+ varAB = n * m * (N+1.0) * (3+N**2) / (48.0 * N**2)
2831
+ else:
2832
+ mnAB = n * (N+2.0) / 4.0
2833
+ varAB = m * n * (N+2) * (N-2.0) / 48 / (N-1.0)
2834
+ if repeats: # adjust variance estimates
2835
+ # compute np.sum(tj * rj**2,axis=0)
2836
+ fac = np.sum(symrank**2, axis=0)
2837
+ if N % 2: # N odd
2838
+ varAB = m * n * (16*N*fac - (N+1)**4) / (16.0 * N**2 * (N-1))
2839
+ else: # N even
2840
+ varAB = m * n * (16*fac - N*(N+2)**2) / (16.0 * N * (N-1))
2841
+
2842
+ # Small values of AB indicate larger dispersion for the x sample.
2843
+ # Large values of AB indicate larger dispersion for the y sample.
2844
+ # This is opposite to the way we define the ratio of scales. see [1]_.
2845
+ z = (mnAB - AB) / sqrt(varAB)
2846
+ pvalue = _get_pvalue(z, _SimpleNormal(), alternative, xp=np)
2847
+ return AnsariResult(AB[()], pvalue[()])
2848
+
2849
+
2850
+ BartlettResult = namedtuple('BartlettResult', ('statistic', 'pvalue'))
2851
+
2852
+
2853
+ @_axis_nan_policy_factory(BartlettResult, n_samples=None)
2854
+ def bartlett(*samples, axis=0):
2855
+ r"""Perform Bartlett's test for equal variances.
2856
+
2857
+ Bartlett's test tests the null hypothesis that all input samples
2858
+ are from populations with equal variances. For samples
2859
+ from significantly non-normal populations, Levene's test
2860
+ `levene` is more robust.
2861
+
2862
+ Parameters
2863
+ ----------
2864
+ sample1, sample2, ... : array_like
2865
+ arrays of sample data. Only 1d arrays are accepted, they may have
2866
+ different lengths.
2867
+
2868
+ Returns
2869
+ -------
2870
+ statistic : float
2871
+ The test statistic.
2872
+ pvalue : float
2873
+ The p-value of the test.
2874
+
2875
+ See Also
2876
+ --------
2877
+ fligner : A non-parametric test for the equality of k variances
2878
+ levene : A robust parametric test for equality of k variances
2879
+ :ref:`hypothesis_bartlett` : Extended example
2880
+
2881
+ Notes
2882
+ -----
2883
+ Conover et al. (1981) examine many of the existing parametric and
2884
+ nonparametric tests by extensive simulations and they conclude that the
2885
+ tests proposed by Fligner and Killeen (1976) and Levene (1960) appear to be
2886
+ superior in terms of robustness of departures from normality and power
2887
+ ([3]_).
2888
+
2889
+ References
2890
+ ----------
2891
+ .. [1] https://www.itl.nist.gov/div898/handbook/eda/section3/eda357.htm
2892
+ .. [2] Snedecor, George W. and Cochran, William G. (1989), Statistical
2893
+ Methods, Eighth Edition, Iowa State University Press.
2894
+ .. [3] Park, C. and Lindsay, B. G. (1999). Robust Scale Estimation and
2895
+ Hypothesis Testing based on Quadratic Inference Function. Technical
2896
+ Report #99-03, Center for Likelihood Studies, Pennsylvania State
2897
+ University.
2898
+ .. [4] Bartlett, M. S. (1937). Properties of Sufficiency and Statistical
2899
+ Tests. Proceedings of the Royal Society of London. Series A,
2900
+ Mathematical and Physical Sciences, Vol. 160, No.901, pp. 268-282.
2901
+
2902
+ Examples
2903
+ --------
2904
+
2905
+ Test whether the lists `a`, `b` and `c` come from populations
2906
+ with equal variances.
2907
+
2908
+ >>> import numpy as np
2909
+ >>> from scipy import stats
2910
+ >>> a = [8.88, 9.12, 9.04, 8.98, 9.00, 9.08, 9.01, 8.85, 9.06, 8.99]
2911
+ >>> b = [8.88, 8.95, 9.29, 9.44, 9.15, 9.58, 8.36, 9.18, 8.67, 9.05]
2912
+ >>> c = [8.95, 9.12, 8.95, 8.85, 9.03, 8.84, 9.07, 8.98, 8.86, 8.98]
2913
+ >>> stat, p = stats.bartlett(a, b, c)
2914
+ >>> p
2915
+ 1.1254782518834628e-05
2916
+
2917
+ The very small p-value suggests that the populations do not have equal
2918
+ variances.
2919
+
2920
+ This is not surprising, given that the sample variance of `b` is much
2921
+ larger than that of `a` and `c`:
2922
+
2923
+ >>> [np.var(x, ddof=1) for x in [a, b, c]]
2924
+ [0.007054444444444413, 0.13073888888888888, 0.008890000000000002]
2925
+
2926
+ For a more detailed example, see :ref:`hypothesis_bartlett`.
2927
+ """
2928
+ xp = array_namespace(*samples)
2929
+
2930
+ k = len(samples)
2931
+ if k < 2:
2932
+ raise ValueError("Must enter at least two input sample vectors.")
2933
+
2934
+ samples = _broadcast_arrays(samples, axis=axis, xp=xp)
2935
+ samples = [xp.moveaxis(sample, axis, -1) for sample in samples]
2936
+
2937
+ Ni = [xp.asarray(sample.shape[-1], dtype=sample.dtype) for sample in samples]
2938
+ Ni = [xp.broadcast_to(N, samples[0].shape[:-1]) for N in Ni]
2939
+ ssq = [xp.var(sample, correction=1, axis=-1) for sample in samples]
2940
+ Ni = [arr[xp.newaxis, ...] for arr in Ni]
2941
+ ssq = [arr[xp.newaxis, ...] for arr in ssq]
2942
+ Ni = xp.concat(Ni, axis=0)
2943
+ ssq = xp.concat(ssq, axis=0)
2944
+ dtype = Ni.dtype
2945
+ Ntot = xp.sum(Ni, axis=0)
2946
+ spsq = xp.sum((Ni - 1)*ssq, axis=0, dtype=dtype) / (Ntot - k)
2947
+ numer = ((Ntot - k) * xp.log(spsq)
2948
+ - xp.sum((Ni - 1)*xp.log(ssq), axis=0, dtype=dtype))
2949
+ denom = (1 + 1/(3*(k - 1))
2950
+ * ((xp.sum(1/(Ni - 1), axis=0)) - 1/(Ntot - k)))
2951
+ T = numer / denom
2952
+
2953
+ chi2 = _SimpleChi2(xp.asarray(k-1))
2954
+ pvalue = _get_pvalue(T, chi2, alternative='greater', symmetric=False, xp=xp)
2955
+
2956
+ T = xp.clip(T, min=0., max=xp.inf)
2957
+ T = T[()] if T.ndim == 0 else T
2958
+ pvalue = pvalue[()] if pvalue.ndim == 0 else pvalue
2959
+
2960
+ return BartlettResult(T, pvalue)
2961
+
2962
+
2963
+ LeveneResult = namedtuple('LeveneResult', ('statistic', 'pvalue'))
2964
+
2965
+
2966
+ @_axis_nan_policy_factory(LeveneResult, n_samples=None)
2967
+ def levene(*samples, center='median', proportiontocut=0.05):
2968
+ r"""Perform Levene test for equal variances.
2969
+
2970
+ The Levene test tests the null hypothesis that all input samples
2971
+ are from populations with equal variances. Levene's test is an
2972
+ alternative to Bartlett's test `bartlett` in the case where
2973
+ there are significant deviations from normality.
2974
+
2975
+ Parameters
2976
+ ----------
2977
+ sample1, sample2, ... : array_like
2978
+ The sample data, possibly with different lengths. Only one-dimensional
2979
+ samples are accepted.
2980
+ center : {'mean', 'median', 'trimmed'}, optional
2981
+ Which function of the data to use in the test. The default
2982
+ is 'median'.
2983
+ proportiontocut : float, optional
2984
+ When `center` is 'trimmed', this gives the proportion of data points
2985
+ to cut from each end. (See `scipy.stats.trim_mean`.)
2986
+ Default is 0.05.
2987
+
2988
+ Returns
2989
+ -------
2990
+ statistic : float
2991
+ The test statistic.
2992
+ pvalue : float
2993
+ The p-value for the test.
2994
+
2995
+ See Also
2996
+ --------
2997
+ fligner : A non-parametric test for the equality of k variances
2998
+ bartlett : A parametric test for equality of k variances in normal samples
2999
+ :ref:`hypothesis_levene` : Extended example
3000
+
3001
+ Notes
3002
+ -----
3003
+ Three variations of Levene's test are possible. The possibilities
3004
+ and their recommended usages are:
3005
+
3006
+ * 'median' : Recommended for skewed (non-normal) distributions>
3007
+ * 'mean' : Recommended for symmetric, moderate-tailed distributions.
3008
+ * 'trimmed' : Recommended for heavy-tailed distributions.
3009
+
3010
+ The test version using the mean was proposed in the original article
3011
+ of Levene ([2]_) while the median and trimmed mean have been studied by
3012
+ Brown and Forsythe ([3]_), sometimes also referred to as Brown-Forsythe
3013
+ test.
3014
+
3015
+ References
3016
+ ----------
3017
+ .. [1] https://www.itl.nist.gov/div898/handbook/eda/section3/eda35a.htm
3018
+ .. [2] Levene, H. (1960). In Contributions to Probability and Statistics:
3019
+ Essays in Honor of Harold Hotelling, I. Olkin et al. eds.,
3020
+ Stanford University Press, pp. 278-292.
3021
+ .. [3] Brown, M. B. and Forsythe, A. B. (1974), Journal of the American
3022
+ Statistical Association, 69, 364-367
3023
+
3024
+ Examples
3025
+ --------
3026
+
3027
+ Test whether the lists `a`, `b` and `c` come from populations
3028
+ with equal variances.
3029
+
3030
+ >>> import numpy as np
3031
+ >>> from scipy import stats
3032
+ >>> a = [8.88, 9.12, 9.04, 8.98, 9.00, 9.08, 9.01, 8.85, 9.06, 8.99]
3033
+ >>> b = [8.88, 8.95, 9.29, 9.44, 9.15, 9.58, 8.36, 9.18, 8.67, 9.05]
3034
+ >>> c = [8.95, 9.12, 8.95, 8.85, 9.03, 8.84, 9.07, 8.98, 8.86, 8.98]
3035
+ >>> stat, p = stats.levene(a, b, c)
3036
+ >>> p
3037
+ 0.002431505967249681
3038
+
3039
+ The small p-value suggests that the populations do not have equal
3040
+ variances.
3041
+
3042
+ This is not surprising, given that the sample variance of `b` is much
3043
+ larger than that of `a` and `c`:
3044
+
3045
+ >>> [np.var(x, ddof=1) for x in [a, b, c]]
3046
+ [0.007054444444444413, 0.13073888888888888, 0.008890000000000002]
3047
+
3048
+ For a more detailed example, see :ref:`hypothesis_levene`.
3049
+ """
3050
+ if center not in ['mean', 'median', 'trimmed']:
3051
+ raise ValueError("center must be 'mean', 'median' or 'trimmed'.")
3052
+
3053
+ k = len(samples)
3054
+ if k < 2:
3055
+ raise ValueError("Must enter at least two input sample vectors.")
3056
+
3057
+ Ni = np.empty(k)
3058
+ Yci = np.empty(k, 'd')
3059
+
3060
+ if center == 'median':
3061
+
3062
+ def func(x):
3063
+ return np.median(x, axis=0)
3064
+
3065
+ elif center == 'mean':
3066
+
3067
+ def func(x):
3068
+ return np.mean(x, axis=0)
3069
+
3070
+ else: # center == 'trimmed'
3071
+ samples = tuple(_stats_py.trimboth(np.sort(sample), proportiontocut)
3072
+ for sample in samples)
3073
+
3074
+ def func(x):
3075
+ return np.mean(x, axis=0)
3076
+
3077
+ for j in range(k):
3078
+ Ni[j] = len(samples[j])
3079
+ Yci[j] = func(samples[j])
3080
+ Ntot = np.sum(Ni, axis=0)
3081
+
3082
+ # compute Zij's
3083
+ Zij = [None] * k
3084
+ for i in range(k):
3085
+ Zij[i] = abs(asarray(samples[i]) - Yci[i])
3086
+
3087
+ # compute Zbari
3088
+ Zbari = np.empty(k, 'd')
3089
+ Zbar = 0.0
3090
+ for i in range(k):
3091
+ Zbari[i] = np.mean(Zij[i], axis=0)
3092
+ Zbar += Zbari[i] * Ni[i]
3093
+
3094
+ Zbar /= Ntot
3095
+ numer = (Ntot - k) * np.sum(Ni * (Zbari - Zbar)**2, axis=0)
3096
+
3097
+ # compute denom_variance
3098
+ dvar = 0.0
3099
+ for i in range(k):
3100
+ dvar += np.sum((Zij[i] - Zbari[i])**2, axis=0)
3101
+
3102
+ denom = (k - 1.0) * dvar
3103
+
3104
+ W = numer / denom
3105
+ pval = distributions.f.sf(W, k-1, Ntot-k) # 1 - cdf
3106
+ return LeveneResult(W, pval)
3107
+
3108
+
3109
+ def _apply_func(x, g, func):
3110
+ # g is list of indices into x
3111
+ # separating x into different groups
3112
+ # func should be applied over the groups
3113
+ g = unique(r_[0, g, len(x)])
3114
+ output = [func(x[g[k]:g[k+1]]) for k in range(len(g) - 1)]
3115
+
3116
+ return asarray(output)
3117
+
3118
+
3119
+ FlignerResult = namedtuple('FlignerResult', ('statistic', 'pvalue'))
3120
+
3121
+
3122
+ @_axis_nan_policy_factory(FlignerResult, n_samples=None)
3123
+ def fligner(*samples, center='median', proportiontocut=0.05):
3124
+ r"""Perform Fligner-Killeen test for equality of variance.
3125
+
3126
+ Fligner's test tests the null hypothesis that all input samples
3127
+ are from populations with equal variances. Fligner-Killeen's test is
3128
+ distribution free when populations are identical [2]_.
3129
+
3130
+ Parameters
3131
+ ----------
3132
+ sample1, sample2, ... : array_like
3133
+ Arrays of sample data. Need not be the same length.
3134
+ center : {'mean', 'median', 'trimmed'}, optional
3135
+ Keyword argument controlling which function of the data is used in
3136
+ computing the test statistic. The default is 'median'.
3137
+ proportiontocut : float, optional
3138
+ When `center` is 'trimmed', this gives the proportion of data points
3139
+ to cut from each end. (See `scipy.stats.trim_mean`.)
3140
+ Default is 0.05.
3141
+
3142
+ Returns
3143
+ -------
3144
+ statistic : float
3145
+ The test statistic.
3146
+ pvalue : float
3147
+ The p-value for the hypothesis test.
3148
+
3149
+ See Also
3150
+ --------
3151
+ bartlett : A parametric test for equality of k variances in normal samples
3152
+ levene : A robust parametric test for equality of k variances
3153
+ :ref:`hypothesis_fligner` : Extended example
3154
+
3155
+ Notes
3156
+ -----
3157
+ As with Levene's test there are three variants of Fligner's test that
3158
+ differ by the measure of central tendency used in the test. See `levene`
3159
+ for more information.
3160
+
3161
+ Conover et al. (1981) examine many of the existing parametric and
3162
+ nonparametric tests by extensive simulations and they conclude that the
3163
+ tests proposed by Fligner and Killeen (1976) and Levene (1960) appear to be
3164
+ superior in terms of robustness of departures from normality and power
3165
+ [3]_.
3166
+
3167
+ References
3168
+ ----------
3169
+ .. [1] Park, C. and Lindsay, B. G. (1999). Robust Scale Estimation and
3170
+ Hypothesis Testing based on Quadratic Inference Function. Technical
3171
+ Report #99-03, Center for Likelihood Studies, Pennsylvania State
3172
+ University.
3173
+ https://cecas.clemson.edu/~cspark/cv/paper/qif/draftqif2.pdf
3174
+ .. [2] Fligner, M.A. and Killeen, T.J. (1976). Distribution-free two-sample
3175
+ tests for scale. Journal of the American Statistical Association.
3176
+ 71(353), 210-213.
3177
+ .. [3] Park, C. and Lindsay, B. G. (1999). Robust Scale Estimation and
3178
+ Hypothesis Testing based on Quadratic Inference Function. Technical
3179
+ Report #99-03, Center for Likelihood Studies, Pennsylvania State
3180
+ University.
3181
+ .. [4] Conover, W. J., Johnson, M. E. and Johnson M. M. (1981). A
3182
+ comparative study of tests for homogeneity of variances, with
3183
+ applications to the outer continental shelf bidding data.
3184
+ Technometrics, 23(4), 351-361.
3185
+
3186
+ Examples
3187
+ --------
3188
+
3189
+ >>> import numpy as np
3190
+ >>> from scipy import stats
3191
+
3192
+ Test whether the lists `a`, `b` and `c` come from populations
3193
+ with equal variances.
3194
+
3195
+ >>> a = [8.88, 9.12, 9.04, 8.98, 9.00, 9.08, 9.01, 8.85, 9.06, 8.99]
3196
+ >>> b = [8.88, 8.95, 9.29, 9.44, 9.15, 9.58, 8.36, 9.18, 8.67, 9.05]
3197
+ >>> c = [8.95, 9.12, 8.95, 8.85, 9.03, 8.84, 9.07, 8.98, 8.86, 8.98]
3198
+ >>> stat, p = stats.fligner(a, b, c)
3199
+ >>> p
3200
+ 0.00450826080004775
3201
+
3202
+ The small p-value suggests that the populations do not have equal
3203
+ variances.
3204
+
3205
+ This is not surprising, given that the sample variance of `b` is much
3206
+ larger than that of `a` and `c`:
3207
+
3208
+ >>> [np.var(x, ddof=1) for x in [a, b, c]]
3209
+ [0.007054444444444413, 0.13073888888888888, 0.008890000000000002]
3210
+
3211
+ For a more detailed example, see :ref:`hypothesis_fligner`.
3212
+ """
3213
+ if center not in ['mean', 'median', 'trimmed']:
3214
+ raise ValueError("center must be 'mean', 'median' or 'trimmed'.")
3215
+
3216
+ k = len(samples)
3217
+ if k < 2:
3218
+ raise ValueError("Must enter at least two input sample vectors.")
3219
+
3220
+ # Handle empty input
3221
+ for sample in samples:
3222
+ if sample.size == 0:
3223
+ NaN = _get_nan(*samples)
3224
+ return FlignerResult(NaN, NaN)
3225
+
3226
+ if center == 'median':
3227
+
3228
+ def func(x):
3229
+ return np.median(x, axis=0)
3230
+
3231
+ elif center == 'mean':
3232
+
3233
+ def func(x):
3234
+ return np.mean(x, axis=0)
3235
+
3236
+ else: # center == 'trimmed'
3237
+ samples = tuple(_stats_py.trimboth(sample, proportiontocut)
3238
+ for sample in samples)
3239
+
3240
+ def func(x):
3241
+ return np.mean(x, axis=0)
3242
+
3243
+ Ni = asarray([len(samples[j]) for j in range(k)])
3244
+ Yci = asarray([func(samples[j]) for j in range(k)])
3245
+ Ntot = np.sum(Ni, axis=0)
3246
+ # compute Zij's
3247
+ Zij = [abs(asarray(samples[i]) - Yci[i]) for i in range(k)]
3248
+ allZij = []
3249
+ g = [0]
3250
+ for i in range(k):
3251
+ allZij.extend(list(Zij[i]))
3252
+ g.append(len(allZij))
3253
+
3254
+ ranks = _stats_py.rankdata(allZij)
3255
+ sample = distributions.norm.ppf(ranks / (2*(Ntot + 1.0)) + 0.5)
3256
+
3257
+ # compute Aibar
3258
+ Aibar = _apply_func(sample, g, np.sum) / Ni
3259
+ anbar = np.mean(sample, axis=0)
3260
+ varsq = np.var(sample, axis=0, ddof=1)
3261
+ statistic = np.sum(Ni * (asarray(Aibar) - anbar)**2.0, axis=0) / varsq
3262
+ chi2 = _SimpleChi2(k-1)
3263
+ pval = _get_pvalue(statistic, chi2, alternative='greater', symmetric=False, xp=np)
3264
+ return FlignerResult(statistic, pval)
3265
+
3266
+
3267
+ @_axis_nan_policy_factory(lambda x1: (x1,), n_samples=4, n_outputs=1)
3268
+ def _mood_inner_lc(xy, x, diffs, sorted_xy, n, m, N) -> float:
3269
+ # Obtain the unique values and their frequencies from the pooled samples.
3270
+ # "a_j, + b_j, = t_j, for j = 1, ... k" where `k` is the number of unique
3271
+ # classes, and "[t]he number of values associated with the x's and y's in
3272
+ # the jth class will be denoted by a_j, and b_j respectively."
3273
+ # (Mielke, 312)
3274
+ # Reuse previously computed sorted array and `diff` arrays to obtain the
3275
+ # unique values and counts. Prepend `diffs` with a non-zero to indicate
3276
+ # that the first element should be marked as not matching what preceded it.
3277
+ diffs_prep = np.concatenate(([1], diffs))
3278
+ # Unique elements are where the was a difference between elements in the
3279
+ # sorted array
3280
+ uniques = sorted_xy[diffs_prep != 0]
3281
+ # The count of each element is the bin size for each set of consecutive
3282
+ # differences where the difference is zero. Replace nonzero differences
3283
+ # with 1 and then use the cumulative sum to count the indices.
3284
+ t = np.bincount(np.cumsum(np.asarray(diffs_prep != 0, dtype=int)))[1:]
3285
+ k = len(uniques)
3286
+ js = np.arange(1, k + 1, dtype=int)
3287
+ # the `b` array mentioned in the paper is not used, outside of the
3288
+ # calculation of `t`, so we do not need to calculate it separately. Here
3289
+ # we calculate `a`. In plain language, `a[j]` is the number of values in
3290
+ # `x` that equal `uniques[j]`.
3291
+ sorted_xyx = np.sort(np.concatenate((xy, x)))
3292
+ diffs = np.diff(sorted_xyx)
3293
+ diffs_prep = np.concatenate(([1], diffs))
3294
+ diff_is_zero = np.asarray(diffs_prep != 0, dtype=int)
3295
+ xyx_counts = np.bincount(np.cumsum(diff_is_zero))[1:]
3296
+ a = xyx_counts - t
3297
+ # "Define .. a_0 = b_0 = t_0 = S_0 = 0" (Mielke 312) so we shift `a`
3298
+ # and `t` arrays over 1 to allow a first element of 0 to accommodate this
3299
+ # indexing.
3300
+ t = np.concatenate(([0], t))
3301
+ a = np.concatenate(([0], a))
3302
+ # S is built from `t`, so it does not need a preceding zero added on.
3303
+ S = np.cumsum(t)
3304
+ # define a copy of `S` with a prepending zero for later use to avoid
3305
+ # the need for indexing.
3306
+ S_i_m1 = np.concatenate(([0], S[:-1]))
3307
+
3308
+ # Psi, as defined by the 6th unnumbered equation on page 313 (Mielke).
3309
+ # Note that in the paper there is an error where the denominator `2` is
3310
+ # squared when it should be the entire equation.
3311
+ def psi(indicator):
3312
+ return (indicator - (N + 1)/2)**2
3313
+
3314
+ # define summation range for use in calculation of phi, as seen in sum
3315
+ # in the unnumbered equation on the bottom of page 312 (Mielke).
3316
+ s_lower = S[js - 1] + 1
3317
+ s_upper = S[js] + 1
3318
+ phi_J = [np.arange(s_lower[idx], s_upper[idx]) for idx in range(k)]
3319
+
3320
+ # for every range in the above array, determine the sum of psi(I) for
3321
+ # every element in the range. Divide all the sums by `t`. Following the
3322
+ # last unnumbered equation on page 312.
3323
+ phis = [np.sum(psi(I_j)) for I_j in phi_J] / t[js]
3324
+
3325
+ # `T` is equal to a[j] * phi[j], per the first unnumbered equation on
3326
+ # page 312. `phis` is already in the order based on `js`, so we index
3327
+ # into `a` with `js` as well.
3328
+ T = sum(phis * a[js])
3329
+
3330
+ # The approximate statistic
3331
+ E_0_T = n * (N * N - 1) / 12
3332
+
3333
+ varM = (m * n * (N + 1.0) * (N ** 2 - 4) / 180 -
3334
+ m * n / (180 * N * (N - 1)) * np.sum(
3335
+ t * (t**2 - 1) * (t**2 - 4 + (15 * (N - S - S_i_m1) ** 2))
3336
+ ))
3337
+
3338
+ return ((T - E_0_T) / np.sqrt(varM),)
3339
+
3340
+
3341
+ def _mood_too_small(samples, kwargs, axis=-1):
3342
+ x, y = samples
3343
+ n = x.shape[axis]
3344
+ m = y.shape[axis]
3345
+ N = m + n
3346
+ return N < 3
3347
+
3348
+
3349
+ @_axis_nan_policy_factory(SignificanceResult, n_samples=2, too_small=_mood_too_small)
3350
+ def mood(x, y, axis=0, alternative="two-sided"):
3351
+ """Perform Mood's test for equal scale parameters.
3352
+
3353
+ Mood's two-sample test for scale parameters is a non-parametric
3354
+ test for the null hypothesis that two samples are drawn from the
3355
+ same distribution with the same scale parameter.
3356
+
3357
+ Parameters
3358
+ ----------
3359
+ x, y : array_like
3360
+ Arrays of sample data. There must be at least three observations
3361
+ total.
3362
+ axis : int, optional
3363
+ The axis along which the samples are tested. `x` and `y` can be of
3364
+ different length along `axis`.
3365
+ If `axis` is None, `x` and `y` are flattened and the test is done on
3366
+ all values in the flattened arrays.
3367
+ alternative : {'two-sided', 'less', 'greater'}, optional
3368
+ Defines the alternative hypothesis. Default is 'two-sided'.
3369
+ The following options are available:
3370
+
3371
+ * 'two-sided': the scales of the distributions underlying `x` and `y`
3372
+ are different.
3373
+ * 'less': the scale of the distribution underlying `x` is less than
3374
+ the scale of the distribution underlying `y`.
3375
+ * 'greater': the scale of the distribution underlying `x` is greater
3376
+ than the scale of the distribution underlying `y`.
3377
+
3378
+ .. versionadded:: 1.7.0
3379
+
3380
+ Returns
3381
+ -------
3382
+ res : SignificanceResult
3383
+ An object containing attributes:
3384
+
3385
+ statistic : scalar or ndarray
3386
+ The z-score for the hypothesis test. For 1-D inputs a scalar is
3387
+ returned.
3388
+ pvalue : scalar ndarray
3389
+ The p-value for the hypothesis test.
3390
+
3391
+ See Also
3392
+ --------
3393
+ fligner : A non-parametric test for the equality of k variances
3394
+ ansari : A non-parametric test for the equality of 2 variances
3395
+ bartlett : A parametric test for equality of k variances in normal samples
3396
+ levene : A parametric test for equality of k variances
3397
+
3398
+ Notes
3399
+ -----
3400
+ The data are assumed to be drawn from probability distributions ``f(x)``
3401
+ and ``f(x/s) / s`` respectively, for some probability density function f.
3402
+ The null hypothesis is that ``s == 1``.
3403
+
3404
+ For multi-dimensional arrays, if the inputs are of shapes
3405
+ ``(n0, n1, n2, n3)`` and ``(n0, m1, n2, n3)``, then if ``axis=1``, the
3406
+ resulting z and p values will have shape ``(n0, n2, n3)``. Note that
3407
+ ``n1`` and ``m1`` don't have to be equal, but the other dimensions do.
3408
+
3409
+ References
3410
+ ----------
3411
+ [1] Mielke, Paul W. "Note on Some Squared Rank Tests with Existing Ties."
3412
+ Technometrics, vol. 9, no. 2, 1967, pp. 312-14. JSTOR,
3413
+ https://doi.org/10.2307/1266427. Accessed 18 May 2022.
3414
+
3415
+ Examples
3416
+ --------
3417
+ >>> import numpy as np
3418
+ >>> from scipy import stats
3419
+ >>> rng = np.random.default_rng()
3420
+ >>> x2 = rng.standard_normal((2, 45, 6, 7))
3421
+ >>> x1 = rng.standard_normal((2, 30, 6, 7))
3422
+ >>> res = stats.mood(x1, x2, axis=1)
3423
+ >>> res.pvalue.shape
3424
+ (2, 6, 7)
3425
+
3426
+ Find the number of points where the difference in scale is not significant:
3427
+
3428
+ >>> (res.pvalue > 0.1).sum()
3429
+ 78
3430
+
3431
+ Perform the test with different scales:
3432
+
3433
+ >>> x1 = rng.standard_normal((2, 30))
3434
+ >>> x2 = rng.standard_normal((2, 35)) * 10.0
3435
+ >>> stats.mood(x1, x2, axis=1)
3436
+ SignificanceResult(statistic=array([-5.76174136, -6.12650783]),
3437
+ pvalue=array([8.32505043e-09, 8.98287869e-10]))
3438
+
3439
+ """
3440
+ x = np.asarray(x, dtype=float)
3441
+ y = np.asarray(y, dtype=float)
3442
+
3443
+ if axis < 0:
3444
+ axis = x.ndim + axis
3445
+
3446
+ # Determine shape of the result arrays
3447
+ res_shape = tuple([x.shape[ax] for ax in range(len(x.shape)) if ax != axis])
3448
+ if not (res_shape == tuple([y.shape[ax] for ax in range(len(y.shape)) if
3449
+ ax != axis])):
3450
+ raise ValueError("Dimensions of x and y on all axes except `axis` "
3451
+ "should match")
3452
+
3453
+ n = x.shape[axis]
3454
+ m = y.shape[axis]
3455
+ N = m + n
3456
+ if N < 3:
3457
+ raise ValueError("Not enough observations.")
3458
+
3459
+ xy = np.concatenate((x, y), axis=axis)
3460
+ # determine if any of the samples contain ties
3461
+ sorted_xy = np.sort(xy, axis=axis)
3462
+ diffs = np.diff(sorted_xy, axis=axis)
3463
+ if 0 in diffs:
3464
+ z = np.asarray(_mood_inner_lc(xy, x, diffs, sorted_xy, n, m, N,
3465
+ axis=axis))
3466
+ else:
3467
+ if axis != 0:
3468
+ xy = np.moveaxis(xy, axis, 0)
3469
+
3470
+ xy = xy.reshape(xy.shape[0], -1)
3471
+ # Generalized to the n-dimensional case by adding the axis argument,
3472
+ # and using for loops, since rankdata is not vectorized. For improving
3473
+ # performance consider vectorizing rankdata function.
3474
+ all_ranks = np.empty_like(xy)
3475
+ for j in range(xy.shape[1]):
3476
+ all_ranks[:, j] = _stats_py.rankdata(xy[:, j])
3477
+
3478
+ Ri = all_ranks[:n]
3479
+ M = np.sum((Ri - (N + 1.0) / 2) ** 2, axis=0)
3480
+ # Approx stat.
3481
+ mnM = n * (N * N - 1.0) / 12
3482
+ varM = m * n * (N + 1.0) * (N + 2) * (N - 2) / 180
3483
+ z = (M - mnM) / sqrt(varM)
3484
+ pval = _get_pvalue(z, _SimpleNormal(), alternative, xp=np)
3485
+
3486
+ if res_shape == ():
3487
+ # Return scalars, not 0-D arrays
3488
+ z = z[0]
3489
+ pval = pval[0]
3490
+ else:
3491
+ z.shape = res_shape
3492
+ pval.shape = res_shape
3493
+ return SignificanceResult(z[()], pval[()])
3494
+
3495
+
3496
+ WilcoxonResult = _make_tuple_bunch('WilcoxonResult', ['statistic', 'pvalue'])
3497
+
3498
+
3499
+ def wilcoxon_result_unpacker(res, _):
3500
+ if hasattr(res, 'zstatistic'):
3501
+ return res.statistic, res.pvalue, res.zstatistic
3502
+ else:
3503
+ return res.statistic, res.pvalue
3504
+
3505
+
3506
+ def wilcoxon_result_object(statistic, pvalue, zstatistic=None):
3507
+ res = WilcoxonResult(statistic, pvalue)
3508
+ if zstatistic is not None:
3509
+ res.zstatistic = zstatistic
3510
+ return res
3511
+
3512
+
3513
+ def wilcoxon_outputs(kwds):
3514
+ method = kwds.get('method', 'auto')
3515
+ if method == 'asymptotic':
3516
+ return 3
3517
+ return 2
3518
+
3519
+
3520
+ @_rename_parameter("mode", "method")
3521
+ @_axis_nan_policy_factory(
3522
+ wilcoxon_result_object, paired=True,
3523
+ n_samples=lambda kwds: 2 if kwds.get('y', None) is not None else 1,
3524
+ result_to_tuple=wilcoxon_result_unpacker, n_outputs=wilcoxon_outputs,
3525
+ )
3526
+ def wilcoxon(x, y=None, zero_method="wilcox", correction=False,
3527
+ alternative="two-sided", method='auto', *, axis=0):
3528
+ """Calculate the Wilcoxon signed-rank test.
3529
+
3530
+ The Wilcoxon signed-rank test tests the null hypothesis that two
3531
+ related paired samples come from the same distribution. In particular,
3532
+ it tests whether the distribution of the differences ``x - y`` is symmetric
3533
+ about zero. It is a non-parametric version of the paired T-test.
3534
+
3535
+ Parameters
3536
+ ----------
3537
+ x : array_like
3538
+ Either the first set of measurements (in which case ``y`` is the second
3539
+ set of measurements), or the differences between two sets of
3540
+ measurements (in which case ``y`` is not to be specified.) Must be
3541
+ one-dimensional.
3542
+ y : array_like, optional
3543
+ Either the second set of measurements (if ``x`` is the first set of
3544
+ measurements), or not specified (if ``x`` is the differences between
3545
+ two sets of measurements.) Must be one-dimensional.
3546
+
3547
+ .. warning::
3548
+ When `y` is provided, `wilcoxon` calculates the test statistic
3549
+ based on the ranks of the absolute values of ``d = x - y``.
3550
+ Roundoff error in the subtraction can result in elements of ``d``
3551
+ being assigned different ranks even when they would be tied with
3552
+ exact arithmetic. Rather than passing `x` and `y` separately,
3553
+ consider computing the difference ``x - y``, rounding as needed to
3554
+ ensure that only truly unique elements are numerically distinct,
3555
+ and passing the result as `x`, leaving `y` at the default (None).
3556
+
3557
+ zero_method : {"wilcox", "pratt", "zsplit"}, optional
3558
+ There are different conventions for handling pairs of observations
3559
+ with equal values ("zero-differences", or "zeros").
3560
+
3561
+ * "wilcox": Discards all zero-differences (default); see [4]_.
3562
+ * "pratt": Includes zero-differences in the ranking process,
3563
+ but drops the ranks of the zeros (more conservative); see [3]_.
3564
+ In this case, the normal approximation is adjusted as in [5]_.
3565
+ * "zsplit": Includes zero-differences in the ranking process and
3566
+ splits the zero rank between positive and negative ones.
3567
+
3568
+ correction : bool, optional
3569
+ If True, apply continuity correction by adjusting the Wilcoxon rank
3570
+ statistic by 0.5 towards the mean value when computing the
3571
+ z-statistic if a normal approximation is used. Default is False.
3572
+ alternative : {"two-sided", "greater", "less"}, optional
3573
+ Defines the alternative hypothesis. Default is 'two-sided'.
3574
+ In the following, let ``d`` represent the difference between the paired
3575
+ samples: ``d = x - y`` if both ``x`` and ``y`` are provided, or
3576
+ ``d = x`` otherwise.
3577
+
3578
+ * 'two-sided': the distribution underlying ``d`` is not symmetric
3579
+ about zero.
3580
+ * 'less': the distribution underlying ``d`` is stochastically less
3581
+ than a distribution symmetric about zero.
3582
+ * 'greater': the distribution underlying ``d`` is stochastically
3583
+ greater than a distribution symmetric about zero.
3584
+
3585
+ method : {"auto", "exact", "asymptotic"} or `PermutationMethod` instance, optional
3586
+ Method to calculate the p-value, see Notes. Default is "auto".
3587
+
3588
+ axis : int or None, default: 0
3589
+ If an int, the axis of the input along which to compute the statistic.
3590
+ The statistic of each axis-slice (e.g. row) of the input will appear
3591
+ in a corresponding element of the output. If ``None``, the input will
3592
+ be raveled before computing the statistic.
3593
+
3594
+ Returns
3595
+ -------
3596
+ An object with the following attributes.
3597
+
3598
+ statistic : array_like
3599
+ If `alternative` is "two-sided", the sum of the ranks of the
3600
+ differences above or below zero, whichever is smaller.
3601
+ Otherwise the sum of the ranks of the differences above zero.
3602
+ pvalue : array_like
3603
+ The p-value for the test depending on `alternative` and `method`.
3604
+ zstatistic : array_like
3605
+ When ``method = 'asymptotic'``, this is the normalized z-statistic::
3606
+
3607
+ z = (T - mn - d) / se
3608
+
3609
+ where ``T`` is `statistic` as defined above, ``mn`` is the mean of the
3610
+ distribution under the null hypothesis, ``d`` is a continuity
3611
+ correction, and ``se`` is the standard error.
3612
+ When ``method != 'asymptotic'``, this attribute is not available.
3613
+
3614
+ See Also
3615
+ --------
3616
+ kruskal, mannwhitneyu
3617
+
3618
+ Notes
3619
+ -----
3620
+ In the following, let ``d`` represent the difference between the paired
3621
+ samples: ``d = x - y`` if both ``x`` and ``y`` are provided, or ``d = x``
3622
+ otherwise. Assume that all elements of ``d`` are independent and
3623
+ identically distributed observations, and all are distinct and nonzero.
3624
+
3625
+ - When ``len(d)`` is sufficiently large, the null distribution of the
3626
+ normalized test statistic (`zstatistic` above) is approximately normal,
3627
+ and ``method = 'asymptotic'`` can be used to compute the p-value.
3628
+
3629
+ - When ``len(d)`` is small, the normal approximation may not be accurate,
3630
+ and ``method='exact'`` is preferred (at the cost of additional
3631
+ execution time).
3632
+
3633
+ - The default, ``method='auto'``, selects between the two:
3634
+ ``method='exact'`` is used when ``len(d) <= 50``, and
3635
+ ``method='asymptotic'`` is used otherwise.
3636
+
3637
+ The presence of "ties" (i.e. not all elements of ``d`` are unique) or
3638
+ "zeros" (i.e. elements of ``d`` are zero) changes the null distribution
3639
+ of the test statistic, and ``method='exact'`` no longer calculates
3640
+ the exact p-value. If ``method='asymptotic'``, the z-statistic is adjusted
3641
+ for more accurate comparison against the standard normal, but still,
3642
+ for finite sample sizes, the standard normal is only an approximation of
3643
+ the true null distribution of the z-statistic. For such situations, the
3644
+ `method` parameter also accepts instances of `PermutationMethod`. In this
3645
+ case, the p-value is computed using `permutation_test` with the provided
3646
+ configuration options and other appropriate settings.
3647
+
3648
+ The presence of ties and zeros affects the resolution of ``method='auto'``
3649
+ accordingly: exhasutive permutations are performed when ``len(d) <= 13``,
3650
+ and the asymptotic method is used otherwise. Note that they asymptotic
3651
+ method may not be very accurate even for ``len(d) > 14``; the threshold
3652
+ was chosen as a compromise between execution time and accuracy under the
3653
+ constraint that the results must be deterministic. Consider providing an
3654
+ instance of `PermutationMethod` method manually, choosing the
3655
+ ``n_resamples`` parameter to balance time constraints and accuracy
3656
+ requirements.
3657
+
3658
+ Please also note that in the edge case that all elements of ``d`` are zero,
3659
+ the p-value relying on the normal approximaton cannot be computed (NaN)
3660
+ if ``zero_method='wilcox'`` or ``zero_method='pratt'``.
3661
+
3662
+ References
3663
+ ----------
3664
+ .. [1] https://en.wikipedia.org/wiki/Wilcoxon_signed-rank_test
3665
+ .. [2] Conover, W.J., Practical Nonparametric Statistics, 1971.
3666
+ .. [3] Pratt, J.W., Remarks on Zeros and Ties in the Wilcoxon Signed
3667
+ Rank Procedures, Journal of the American Statistical Association,
3668
+ Vol. 54, 1959, pp. 655-667. :doi:`10.1080/01621459.1959.10501526`
3669
+ .. [4] Wilcoxon, F., Individual Comparisons by Ranking Methods,
3670
+ Biometrics Bulletin, Vol. 1, 1945, pp. 80-83. :doi:`10.2307/3001968`
3671
+ .. [5] Cureton, E.E., The Normal Approximation to the Signed-Rank
3672
+ Sampling Distribution When Zero Differences are Present,
3673
+ Journal of the American Statistical Association, Vol. 62, 1967,
3674
+ pp. 1068-1069. :doi:`10.1080/01621459.1967.10500917`
3675
+
3676
+ Examples
3677
+ --------
3678
+ In [4]_, the differences in height between cross- and self-fertilized
3679
+ corn plants is given as follows:
3680
+
3681
+ >>> d = [6, 8, 14, 16, 23, 24, 28, 29, 41, -48, 49, 56, 60, -67, 75]
3682
+
3683
+ Cross-fertilized plants appear to be higher. To test the null
3684
+ hypothesis that there is no height difference, we can apply the
3685
+ two-sided test:
3686
+
3687
+ >>> from scipy.stats import wilcoxon
3688
+ >>> res = wilcoxon(d)
3689
+ >>> res.statistic, res.pvalue
3690
+ (24.0, 0.041259765625)
3691
+
3692
+ Hence, we would reject the null hypothesis at a confidence level of 5%,
3693
+ concluding that there is a difference in height between the groups.
3694
+ To confirm that the median of the differences can be assumed to be
3695
+ positive, we use:
3696
+
3697
+ >>> res = wilcoxon(d, alternative='greater')
3698
+ >>> res.statistic, res.pvalue
3699
+ (96.0, 0.0206298828125)
3700
+
3701
+ This shows that the null hypothesis that the median is negative can be
3702
+ rejected at a confidence level of 5% in favor of the alternative that
3703
+ the median is greater than zero. The p-values above are exact. Using the
3704
+ normal approximation gives very similar values:
3705
+
3706
+ >>> res = wilcoxon(d, method='asymptotic')
3707
+ >>> res.statistic, res.pvalue
3708
+ (24.0, 0.04088813291185591)
3709
+
3710
+ Note that the statistic changed to 96 in the one-sided case (the sum
3711
+ of ranks of positive differences) whereas it is 24 in the two-sided
3712
+ case (the minimum of sum of ranks above and below zero).
3713
+
3714
+ In the example above, the differences in height between paired plants are
3715
+ provided to `wilcoxon` directly. Alternatively, `wilcoxon` accepts two
3716
+ samples of equal length, calculates the differences between paired
3717
+ elements, then performs the test. Consider the samples ``x`` and ``y``:
3718
+
3719
+ >>> import numpy as np
3720
+ >>> x = np.array([0.5, 0.825, 0.375, 0.5])
3721
+ >>> y = np.array([0.525, 0.775, 0.325, 0.55])
3722
+ >>> res = wilcoxon(x, y, alternative='greater')
3723
+ >>> res
3724
+ WilcoxonResult(statistic=5.0, pvalue=0.5625)
3725
+
3726
+ Note that had we calculated the differences by hand, the test would have
3727
+ produced different results:
3728
+
3729
+ >>> d = [-0.025, 0.05, 0.05, -0.05]
3730
+ >>> ref = wilcoxon(d, alternative='greater')
3731
+ >>> ref
3732
+ WilcoxonResult(statistic=6.0, pvalue=0.5)
3733
+
3734
+ The substantial difference is due to roundoff error in the results of
3735
+ ``x-y``:
3736
+
3737
+ >>> d - (x-y)
3738
+ array([2.08166817e-17, 6.93889390e-17, 1.38777878e-17, 4.16333634e-17])
3739
+
3740
+ Even though we expected all the elements of ``(x-y)[1:]`` to have the same
3741
+ magnitude ``0.05``, they have slightly different magnitudes in practice,
3742
+ and therefore are assigned different ranks in the test. Before performing
3743
+ the test, consider calculating ``d`` and adjusting it as necessary to
3744
+ ensure that theoretically identically values are not numerically distinct.
3745
+ For example:
3746
+
3747
+ >>> d2 = np.around(x - y, decimals=3)
3748
+ >>> wilcoxon(d2, alternative='greater')
3749
+ WilcoxonResult(statistic=6.0, pvalue=0.5)
3750
+
3751
+ """
3752
+ # replace approx by asymptotic to ensure backwards compatability
3753
+ if method == "approx":
3754
+ method = "asymptotic"
3755
+ return _wilcoxon._wilcoxon_nd(x, y, zero_method, correction, alternative,
3756
+ method, axis)
3757
+
3758
+
3759
+ MedianTestResult = _make_tuple_bunch(
3760
+ 'MedianTestResult',
3761
+ ['statistic', 'pvalue', 'median', 'table'], []
3762
+ )
3763
+
3764
+
3765
+ def median_test(*samples, ties='below', correction=True, lambda_=1,
3766
+ nan_policy='propagate'):
3767
+ """Perform a Mood's median test.
3768
+
3769
+ Test that two or more samples come from populations with the same median.
3770
+
3771
+ Let ``n = len(samples)`` be the number of samples. The "grand median" of
3772
+ all the data is computed, and a contingency table is formed by
3773
+ classifying the values in each sample as being above or below the grand
3774
+ median. The contingency table, along with `correction` and `lambda_`,
3775
+ are passed to `scipy.stats.chi2_contingency` to compute the test statistic
3776
+ and p-value.
3777
+
3778
+ Parameters
3779
+ ----------
3780
+ sample1, sample2, ... : array_like
3781
+ The set of samples. There must be at least two samples.
3782
+ Each sample must be a one-dimensional sequence containing at least
3783
+ one value. The samples are not required to have the same length.
3784
+ ties : str, optional
3785
+ Determines how values equal to the grand median are classified in
3786
+ the contingency table. The string must be one of::
3787
+
3788
+ "below":
3789
+ Values equal to the grand median are counted as "below".
3790
+ "above":
3791
+ Values equal to the grand median are counted as "above".
3792
+ "ignore":
3793
+ Values equal to the grand median are not counted.
3794
+
3795
+ The default is "below".
3796
+ correction : bool, optional
3797
+ If True, *and* there are just two samples, apply Yates' correction
3798
+ for continuity when computing the test statistic associated with
3799
+ the contingency table. Default is True.
3800
+ lambda_ : float or str, optional
3801
+ By default, the statistic computed in this test is Pearson's
3802
+ chi-squared statistic. `lambda_` allows a statistic from the
3803
+ Cressie-Read power divergence family to be used instead. See
3804
+ `power_divergence` for details.
3805
+ Default is 1 (Pearson's chi-squared statistic).
3806
+ nan_policy : {'propagate', 'raise', 'omit'}, optional
3807
+ Defines how to handle when input contains nan. 'propagate' returns nan,
3808
+ 'raise' throws an error, 'omit' performs the calculations ignoring nan
3809
+ values. Default is 'propagate'.
3810
+
3811
+ Returns
3812
+ -------
3813
+ res : MedianTestResult
3814
+ An object containing attributes:
3815
+
3816
+ statistic : float
3817
+ The test statistic. The statistic that is returned is determined
3818
+ by `lambda_`. The default is Pearson's chi-squared statistic.
3819
+ pvalue : float
3820
+ The p-value of the test.
3821
+ median : float
3822
+ The grand median.
3823
+ table : ndarray
3824
+ The contingency table. The shape of the table is (2, n), where
3825
+ n is the number of samples. The first row holds the counts of the
3826
+ values above the grand median, and the second row holds the counts
3827
+ of the values below the grand median. The table allows further
3828
+ analysis with, for example, `scipy.stats.chi2_contingency`, or with
3829
+ `scipy.stats.fisher_exact` if there are two samples, without having
3830
+ to recompute the table. If ``nan_policy`` is "propagate" and there
3831
+ are nans in the input, the return value for ``table`` is ``None``.
3832
+
3833
+ See Also
3834
+ --------
3835
+ kruskal : Compute the Kruskal-Wallis H-test for independent samples.
3836
+ mannwhitneyu : Computes the Mann-Whitney rank test on samples x and y.
3837
+
3838
+ Notes
3839
+ -----
3840
+ .. versionadded:: 0.15.0
3841
+
3842
+ References
3843
+ ----------
3844
+ .. [1] Mood, A. M., Introduction to the Theory of Statistics. McGraw-Hill
3845
+ (1950), pp. 394-399.
3846
+ .. [2] Zar, J. H., Biostatistical Analysis, 5th ed. Prentice Hall (2010).
3847
+ See Sections 8.12 and 10.15.
3848
+
3849
+ Examples
3850
+ --------
3851
+ A biologist runs an experiment in which there are three groups of plants.
3852
+ Group 1 has 16 plants, group 2 has 15 plants, and group 3 has 17 plants.
3853
+ Each plant produces a number of seeds. The seed counts for each group
3854
+ are::
3855
+
3856
+ Group 1: 10 14 14 18 20 22 24 25 31 31 32 39 43 43 48 49
3857
+ Group 2: 28 30 31 33 34 35 36 40 44 55 57 61 91 92 99
3858
+ Group 3: 0 3 9 22 23 25 25 33 34 34 40 45 46 48 62 67 84
3859
+
3860
+ The following code applies Mood's median test to these samples.
3861
+
3862
+ >>> g1 = [10, 14, 14, 18, 20, 22, 24, 25, 31, 31, 32, 39, 43, 43, 48, 49]
3863
+ >>> g2 = [28, 30, 31, 33, 34, 35, 36, 40, 44, 55, 57, 61, 91, 92, 99]
3864
+ >>> g3 = [0, 3, 9, 22, 23, 25, 25, 33, 34, 34, 40, 45, 46, 48, 62, 67, 84]
3865
+ >>> from scipy.stats import median_test
3866
+ >>> res = median_test(g1, g2, g3)
3867
+
3868
+ The median is
3869
+
3870
+ >>> res.median
3871
+ 34.0
3872
+
3873
+ and the contingency table is
3874
+
3875
+ >>> res.table
3876
+ array([[ 5, 10, 7],
3877
+ [11, 5, 10]])
3878
+
3879
+ `p` is too large to conclude that the medians are not the same:
3880
+
3881
+ >>> res.pvalue
3882
+ 0.12609082774093244
3883
+
3884
+ The "G-test" can be performed by passing ``lambda_="log-likelihood"`` to
3885
+ `median_test`.
3886
+
3887
+ >>> res = median_test(g1, g2, g3, lambda_="log-likelihood")
3888
+ >>> res.pvalue
3889
+ 0.12224779737117837
3890
+
3891
+ The median occurs several times in the data, so we'll get a different
3892
+ result if, for example, ``ties="above"`` is used:
3893
+
3894
+ >>> res = median_test(g1, g2, g3, ties="above")
3895
+ >>> res.pvalue
3896
+ 0.063873276069553273
3897
+
3898
+ >>> res.table
3899
+ array([[ 5, 11, 9],
3900
+ [11, 4, 8]])
3901
+
3902
+ This example demonstrates that if the data set is not large and there
3903
+ are values equal to the median, the p-value can be sensitive to the
3904
+ choice of `ties`.
3905
+
3906
+ """
3907
+ if len(samples) < 2:
3908
+ raise ValueError('median_test requires two or more samples.')
3909
+
3910
+ ties_options = ['below', 'above', 'ignore']
3911
+ if ties not in ties_options:
3912
+ raise ValueError(f"invalid 'ties' option '{ties}'; 'ties' must be one "
3913
+ f"of: {str(ties_options)[1:-1]}")
3914
+
3915
+ data = [np.asarray(sample) for sample in samples]
3916
+
3917
+ # Validate the sizes and shapes of the arguments.
3918
+ for k, d in enumerate(data):
3919
+ if d.size == 0:
3920
+ raise ValueError(f"Sample {k + 1} is empty. All samples must "
3921
+ f"contain at least one value.")
3922
+ if d.ndim != 1:
3923
+ raise ValueError(f"Sample {k + 1} has {d.ndim} dimensions. "
3924
+ f"All samples must be one-dimensional sequences.")
3925
+
3926
+ cdata = np.concatenate(data)
3927
+ contains_nan = _contains_nan(cdata, nan_policy)
3928
+ if nan_policy == 'propagate' and contains_nan:
3929
+ return MedianTestResult(np.nan, np.nan, np.nan, None)
3930
+
3931
+ if contains_nan:
3932
+ grand_median = np.median(cdata[~np.isnan(cdata)])
3933
+ else:
3934
+ grand_median = np.median(cdata)
3935
+ # When the minimum version of numpy supported by scipy is 1.9.0,
3936
+ # the above if/else statement can be replaced by the single line:
3937
+ # grand_median = np.nanmedian(cdata)
3938
+
3939
+ # Create the contingency table.
3940
+ table = np.zeros((2, len(data)), dtype=np.int64)
3941
+ for k, sample in enumerate(data):
3942
+ sample = sample[~np.isnan(sample)]
3943
+
3944
+ nabove = count_nonzero(sample > grand_median)
3945
+ nbelow = count_nonzero(sample < grand_median)
3946
+ nequal = sample.size - (nabove + nbelow)
3947
+ table[0, k] += nabove
3948
+ table[1, k] += nbelow
3949
+ if ties == "below":
3950
+ table[1, k] += nequal
3951
+ elif ties == "above":
3952
+ table[0, k] += nequal
3953
+
3954
+ # Check that no row or column of the table is all zero.
3955
+ # Such a table can not be given to chi2_contingency, because it would have
3956
+ # a zero in the table of expected frequencies.
3957
+ rowsums = table.sum(axis=1)
3958
+ if rowsums[0] == 0:
3959
+ raise ValueError(f"All values are below the grand median ({grand_median}).")
3960
+ if rowsums[1] == 0:
3961
+ raise ValueError(f"All values are above the grand median ({grand_median}).")
3962
+ if ties == "ignore":
3963
+ # We already checked that each sample has at least one value, but it
3964
+ # is possible that all those values equal the grand median. If `ties`
3965
+ # is "ignore", that would result in a column of zeros in `table`. We
3966
+ # check for that case here.
3967
+ zero_cols = np.nonzero((table == 0).all(axis=0))[0]
3968
+ if len(zero_cols) > 0:
3969
+ raise ValueError(
3970
+ f"All values in sample {zero_cols[0] + 1} are equal to the grand "
3971
+ f"median ({grand_median!r}), so they are ignored, resulting in an "
3972
+ f"empty sample."
3973
+ )
3974
+
3975
+ stat, p, dof, expected = chi2_contingency(table, lambda_=lambda_,
3976
+ correction=correction)
3977
+ return MedianTestResult(stat, p, grand_median, table)
3978
+
3979
+
3980
+ def _circfuncs_common(samples, period, xp=None):
3981
+ xp = array_namespace(samples) if xp is None else xp
3982
+
3983
+ samples = xp_promote(samples, force_floating=True, xp=xp)
3984
+
3985
+ # Recast samples as radians that range between 0 and 2 pi and calculate
3986
+ # the sine and cosine
3987
+ scaled_samples = samples * ((2.0 * pi) / period)
3988
+ sin_samp = xp.sin(scaled_samples)
3989
+ cos_samp = xp.cos(scaled_samples)
3990
+
3991
+ return samples, sin_samp, cos_samp
3992
+
3993
+
3994
+ @_axis_nan_policy_factory(
3995
+ lambda x: x, n_outputs=1, default_axis=None,
3996
+ result_to_tuple=lambda x, _: (x,)
3997
+ )
3998
+ def circmean(samples, high=2*pi, low=0, axis=None, nan_policy='propagate'):
3999
+ r"""Compute the circular mean of a sample of angle observations.
4000
+
4001
+ Given :math:`n` angle observations :math:`x_1, \cdots, x_n` measured in
4002
+ radians, their *circular mean* is defined by ([1]_, Eq. 2.2.4)
4003
+
4004
+ .. math::
4005
+
4006
+ \mathrm{Arg} \left( \frac{1}{n} \sum_{k=1}^n e^{i x_k} \right)
4007
+
4008
+ where :math:`i` is the imaginary unit and :math:`\mathop{\mathrm{Arg}} z`
4009
+ gives the principal value of the argument of complex number :math:`z`,
4010
+ restricted to the range :math:`[0,2\pi]` by default. :math:`z` in the
4011
+ above expression is known as the `mean resultant vector`.
4012
+
4013
+ Parameters
4014
+ ----------
4015
+ samples : array_like
4016
+ Input array of angle observations. The value of a full angle is
4017
+ equal to ``(high - low)``.
4018
+ high : float, optional
4019
+ Upper boundary of the principal value of an angle. Default is ``2*pi``.
4020
+ low : float, optional
4021
+ Lower boundary of the principal value of an angle. Default is ``0``.
4022
+
4023
+ Returns
4024
+ -------
4025
+ circmean : float
4026
+ Circular mean, restricted to the range ``[low, high]``.
4027
+
4028
+ If the mean resultant vector is zero, an input-dependent,
4029
+ implementation-defined number between ``[low, high]`` is returned.
4030
+ If the input array is empty, ``np.nan`` is returned.
4031
+
4032
+ See Also
4033
+ --------
4034
+ circstd : Circular standard deviation.
4035
+ circvar : Circular variance.
4036
+
4037
+ References
4038
+ ----------
4039
+ .. [1] Mardia, K. V. and Jupp, P. E. *Directional Statistics*.
4040
+ John Wiley & Sons, 1999.
4041
+
4042
+ Examples
4043
+ --------
4044
+ For readability, all angles are printed out in degrees.
4045
+
4046
+ >>> import numpy as np
4047
+ >>> from scipy.stats import circmean
4048
+ >>> import matplotlib.pyplot as plt
4049
+ >>> angles = np.deg2rad(np.array([20, 30, 330]))
4050
+ >>> circmean = circmean(angles)
4051
+ >>> np.rad2deg(circmean)
4052
+ 7.294976657784009
4053
+
4054
+ >>> mean = angles.mean()
4055
+ >>> np.rad2deg(mean)
4056
+ 126.66666666666666
4057
+
4058
+ Plot and compare the circular mean against the arithmetic mean.
4059
+
4060
+ >>> plt.plot(np.cos(np.linspace(0, 2*np.pi, 500)),
4061
+ ... np.sin(np.linspace(0, 2*np.pi, 500)),
4062
+ ... c='k')
4063
+ >>> plt.scatter(np.cos(angles), np.sin(angles), c='k')
4064
+ >>> plt.scatter(np.cos(circmean), np.sin(circmean), c='b',
4065
+ ... label='circmean')
4066
+ >>> plt.scatter(np.cos(mean), np.sin(mean), c='r', label='mean')
4067
+ >>> plt.legend()
4068
+ >>> plt.axis('equal')
4069
+ >>> plt.show()
4070
+
4071
+ """
4072
+ xp = array_namespace(samples)
4073
+ # Needed for non-NumPy arrays to get appropriate NaN result
4074
+ # Apparently atan2(0, 0) is 0, even though it is mathematically undefined
4075
+ if xp_size(samples) == 0:
4076
+ return xp.mean(samples, axis=axis)
4077
+ period = high - low
4078
+ samples, sin_samp, cos_samp = _circfuncs_common(samples, period, xp=xp)
4079
+ sin_sum = xp.sum(sin_samp, axis=axis)
4080
+ cos_sum = xp.sum(cos_samp, axis=axis)
4081
+ res = xp.atan2(sin_sum, cos_sum)
4082
+
4083
+ res = res[()] if res.ndim == 0 else res
4084
+ return (res * (period / (2.0 * pi)) - low) % period + low
4085
+
4086
+
4087
+ @_axis_nan_policy_factory(
4088
+ lambda x: x, n_outputs=1, default_axis=None,
4089
+ result_to_tuple=lambda x, _: (x,)
4090
+ )
4091
+ def circvar(samples, high=2*pi, low=0, axis=None, nan_policy='propagate'):
4092
+ r"""Compute the circular variance of a sample of angle observations.
4093
+
4094
+ Given :math:`n` angle observations :math:`x_1, \cdots, x_n` measured in
4095
+ radians, their *circular variance* is defined by ([2]_, Eq. 2.3.3)
4096
+
4097
+ .. math::
4098
+
4099
+ 1 - \left| \frac{1}{n} \sum_{k=1}^n e^{i x_k} \right|
4100
+
4101
+ where :math:`i` is the imaginary unit and :math:`|z|` gives the length
4102
+ of the complex number :math:`z`. :math:`|z|` in the above expression
4103
+ is known as the `mean resultant length`.
4104
+
4105
+ Parameters
4106
+ ----------
4107
+ samples : array_like
4108
+ Input array of angle observations. The value of a full angle is
4109
+ equal to ``(high - low)``.
4110
+ high : float, optional
4111
+ Upper boundary of the principal value of an angle. Default is ``2*pi``.
4112
+ low : float, optional
4113
+ Lower boundary of the principal value of an angle. Default is ``0``.
4114
+
4115
+ Returns
4116
+ -------
4117
+ circvar : float
4118
+ Circular variance. The returned value is in the range ``[0, 1]``,
4119
+ where ``0`` indicates no variance and ``1`` indicates large variance.
4120
+
4121
+ If the input array is empty, ``np.nan`` is returned.
4122
+
4123
+ See Also
4124
+ --------
4125
+ circmean : Circular mean.
4126
+ circstd : Circular standard deviation.
4127
+
4128
+ Notes
4129
+ -----
4130
+ In the limit of small angles, the circular variance is close to
4131
+ half the 'linear' variance if measured in radians.
4132
+
4133
+ References
4134
+ ----------
4135
+ .. [1] Fisher, N.I. *Statistical analysis of circular data*. Cambridge
4136
+ University Press, 1993.
4137
+ .. [2] Mardia, K. V. and Jupp, P. E. *Directional Statistics*.
4138
+ John Wiley & Sons, 1999.
4139
+
4140
+ Examples
4141
+ --------
4142
+ >>> import numpy as np
4143
+ >>> from scipy.stats import circvar
4144
+ >>> import matplotlib.pyplot as plt
4145
+ >>> samples_1 = np.array([0.072, -0.158, 0.077, 0.108, 0.286,
4146
+ ... 0.133, -0.473, -0.001, -0.348, 0.131])
4147
+ >>> samples_2 = np.array([0.111, -0.879, 0.078, 0.733, 0.421,
4148
+ ... 0.104, -0.136, -0.867, 0.012, 0.105])
4149
+ >>> circvar_1 = circvar(samples_1)
4150
+ >>> circvar_2 = circvar(samples_2)
4151
+
4152
+ Plot the samples.
4153
+
4154
+ >>> fig, (left, right) = plt.subplots(ncols=2)
4155
+ >>> for image in (left, right):
4156
+ ... image.plot(np.cos(np.linspace(0, 2*np.pi, 500)),
4157
+ ... np.sin(np.linspace(0, 2*np.pi, 500)),
4158
+ ... c='k')
4159
+ ... image.axis('equal')
4160
+ ... image.axis('off')
4161
+ >>> left.scatter(np.cos(samples_1), np.sin(samples_1), c='k', s=15)
4162
+ >>> left.set_title(f"circular variance: {np.round(circvar_1, 2)!r}")
4163
+ >>> right.scatter(np.cos(samples_2), np.sin(samples_2), c='k', s=15)
4164
+ >>> right.set_title(f"circular variance: {np.round(circvar_2, 2)!r}")
4165
+ >>> plt.show()
4166
+
4167
+ """
4168
+ xp = array_namespace(samples)
4169
+ period = high - low
4170
+ samples, sin_samp, cos_samp = _circfuncs_common(samples, period, xp=xp)
4171
+ sin_mean = xp.mean(sin_samp, axis=axis)
4172
+ cos_mean = xp.mean(cos_samp, axis=axis)
4173
+ hypotenuse = (sin_mean**2. + cos_mean**2.)**0.5
4174
+ # hypotenuse can go slightly above 1 due to rounding errors
4175
+ R = xp.clip(hypotenuse, max=1.)
4176
+
4177
+ res = 1. - R
4178
+ return res
4179
+
4180
+
4181
+ @_axis_nan_policy_factory(
4182
+ lambda x: x, n_outputs=1, default_axis=None,
4183
+ result_to_tuple=lambda x, _: (x,)
4184
+ )
4185
+ def circstd(samples, high=2*pi, low=0, axis=None, nan_policy='propagate', *,
4186
+ normalize=False):
4187
+ r"""
4188
+ Compute the circular standard deviation of a sample of angle observations.
4189
+
4190
+ Given :math:`n` angle observations :math:`x_1, \cdots, x_n` measured in
4191
+ radians, their `circular standard deviation` is defined by
4192
+ ([2]_, Eq. 2.3.11)
4193
+
4194
+ .. math::
4195
+
4196
+ \sqrt{ -2 \log \left| \frac{1}{n} \sum_{k=1}^n e^{i x_k} \right| }
4197
+
4198
+ where :math:`i` is the imaginary unit and :math:`|z|` gives the length
4199
+ of the complex number :math:`z`. :math:`|z|` in the above expression
4200
+ is known as the `mean resultant length`.
4201
+
4202
+ Parameters
4203
+ ----------
4204
+ samples : array_like
4205
+ Input array of angle observations. The value of a full angle is
4206
+ equal to ``(high - low)``.
4207
+ high : float, optional
4208
+ Upper boundary of the principal value of an angle. Default is ``2*pi``.
4209
+ low : float, optional
4210
+ Lower boundary of the principal value of an angle. Default is ``0``.
4211
+ normalize : boolean, optional
4212
+ If ``False`` (the default), the return value is computed from the
4213
+ above formula with the input scaled by ``(2*pi)/(high-low)`` and
4214
+ the output scaled (back) by ``(high-low)/(2*pi)``. If ``True``,
4215
+ the output is not scaled and is returned directly.
4216
+
4217
+ Returns
4218
+ -------
4219
+ circstd : float
4220
+ Circular standard deviation, optionally normalized.
4221
+
4222
+ If the input array is empty, ``np.nan`` is returned.
4223
+
4224
+ See Also
4225
+ --------
4226
+ circmean : Circular mean.
4227
+ circvar : Circular variance.
4228
+
4229
+ Notes
4230
+ -----
4231
+ In the limit of small angles, the circular standard deviation is close
4232
+ to the 'linear' standard deviation if ``normalize`` is ``False``.
4233
+
4234
+ References
4235
+ ----------
4236
+ .. [1] Mardia, K. V. (1972). 2. In *Statistics of Directional Data*
4237
+ (pp. 18-24). Academic Press. :doi:`10.1016/C2013-0-07425-7`.
4238
+ .. [2] Mardia, K. V. and Jupp, P. E. *Directional Statistics*.
4239
+ John Wiley & Sons, 1999.
4240
+
4241
+ Examples
4242
+ --------
4243
+ >>> import numpy as np
4244
+ >>> from scipy.stats import circstd
4245
+ >>> import matplotlib.pyplot as plt
4246
+ >>> samples_1 = np.array([0.072, -0.158, 0.077, 0.108, 0.286,
4247
+ ... 0.133, -0.473, -0.001, -0.348, 0.131])
4248
+ >>> samples_2 = np.array([0.111, -0.879, 0.078, 0.733, 0.421,
4249
+ ... 0.104, -0.136, -0.867, 0.012, 0.105])
4250
+ >>> circstd_1 = circstd(samples_1)
4251
+ >>> circstd_2 = circstd(samples_2)
4252
+
4253
+ Plot the samples.
4254
+
4255
+ >>> fig, (left, right) = plt.subplots(ncols=2)
4256
+ >>> for image in (left, right):
4257
+ ... image.plot(np.cos(np.linspace(0, 2*np.pi, 500)),
4258
+ ... np.sin(np.linspace(0, 2*np.pi, 500)),
4259
+ ... c='k')
4260
+ ... image.axis('equal')
4261
+ ... image.axis('off')
4262
+ >>> left.scatter(np.cos(samples_1), np.sin(samples_1), c='k', s=15)
4263
+ >>> left.set_title(f"circular std: {np.round(circstd_1, 2)!r}")
4264
+ >>> right.plot(np.cos(np.linspace(0, 2*np.pi, 500)),
4265
+ ... np.sin(np.linspace(0, 2*np.pi, 500)),
4266
+ ... c='k')
4267
+ >>> right.scatter(np.cos(samples_2), np.sin(samples_2), c='k', s=15)
4268
+ >>> right.set_title(f"circular std: {np.round(circstd_2, 2)!r}")
4269
+ >>> plt.show()
4270
+
4271
+ """
4272
+ xp = array_namespace(samples)
4273
+ period = high - low
4274
+ samples, sin_samp, cos_samp = _circfuncs_common(samples, period, xp=xp)
4275
+ sin_mean = xp.mean(sin_samp, axis=axis) # [1] (2.2.3)
4276
+ cos_mean = xp.mean(cos_samp, axis=axis) # [1] (2.2.3)
4277
+ hypotenuse = (sin_mean**2. + cos_mean**2.)**0.5
4278
+ # hypotenuse can go slightly above 1 due to rounding errors
4279
+ R = xp.clip(hypotenuse, max=1.) # [1] (2.2.4)
4280
+
4281
+ res = (-2*xp.log(R))**0.5+0.0 # torch.pow returns -0.0 if R==1
4282
+ if not normalize:
4283
+ res *= (high-low)/(2.*pi) # [1] (2.3.14) w/ (2.3.7)
4284
+ return res
4285
+
4286
+
4287
+ class DirectionalStats:
4288
+ def __init__(self, mean_direction, mean_resultant_length):
4289
+ self.mean_direction = mean_direction
4290
+ self.mean_resultant_length = mean_resultant_length
4291
+
4292
+ def __repr__(self):
4293
+ return (f"DirectionalStats(mean_direction={self.mean_direction},"
4294
+ f" mean_resultant_length={self.mean_resultant_length})")
4295
+
4296
+
4297
+ def directional_stats(samples, *, axis=0, normalize=True):
4298
+ """
4299
+ Computes sample statistics for directional data.
4300
+
4301
+ Computes the directional mean (also called the mean direction vector) and
4302
+ mean resultant length of a sample of vectors.
4303
+
4304
+ The directional mean is a measure of "preferred direction" of vector data.
4305
+ It is analogous to the sample mean, but it is for use when the length of
4306
+ the data is irrelevant (e.g. unit vectors).
4307
+
4308
+ The mean resultant length is a value between 0 and 1 used to quantify the
4309
+ dispersion of directional data: the smaller the mean resultant length, the
4310
+ greater the dispersion. Several definitions of directional variance
4311
+ involving the mean resultant length are given in [1]_ and [2]_.
4312
+
4313
+ Parameters
4314
+ ----------
4315
+ samples : array_like
4316
+ Input array. Must be at least two-dimensional, and the last axis of the
4317
+ input must correspond with the dimensionality of the vector space.
4318
+ When the input is exactly two dimensional, this means that each row
4319
+ of the data is a vector observation.
4320
+ axis : int, default: 0
4321
+ Axis along which the directional mean is computed.
4322
+ normalize: boolean, default: True
4323
+ If True, normalize the input to ensure that each observation is a
4324
+ unit vector. It the observations are already unit vectors, consider
4325
+ setting this to False to avoid unnecessary computation.
4326
+
4327
+ Returns
4328
+ -------
4329
+ res : DirectionalStats
4330
+ An object containing attributes:
4331
+
4332
+ mean_direction : ndarray
4333
+ Directional mean.
4334
+ mean_resultant_length : ndarray
4335
+ The mean resultant length [1]_.
4336
+
4337
+ See Also
4338
+ --------
4339
+ circmean: circular mean; i.e. directional mean for 2D *angles*
4340
+ circvar: circular variance; i.e. directional variance for 2D *angles*
4341
+
4342
+ Notes
4343
+ -----
4344
+ This uses a definition of directional mean from [1]_.
4345
+ Assuming the observations are unit vectors, the calculation is as follows.
4346
+
4347
+ .. code-block:: python
4348
+
4349
+ mean = samples.mean(axis=0)
4350
+ mean_resultant_length = np.linalg.norm(mean)
4351
+ mean_direction = mean / mean_resultant_length
4352
+
4353
+ This definition is appropriate for *directional* data (i.e. vector data
4354
+ for which the magnitude of each observation is irrelevant) but not
4355
+ for *axial* data (i.e. vector data for which the magnitude and *sign* of
4356
+ each observation is irrelevant).
4357
+
4358
+ Several definitions of directional variance involving the mean resultant
4359
+ length ``R`` have been proposed, including ``1 - R`` [1]_, ``1 - R**2``
4360
+ [2]_, and ``2 * (1 - R)`` [2]_. Rather than choosing one, this function
4361
+ returns ``R`` as attribute `mean_resultant_length` so the user can compute
4362
+ their preferred measure of dispersion.
4363
+
4364
+ References
4365
+ ----------
4366
+ .. [1] Mardia, Jupp. (2000). *Directional Statistics*
4367
+ (p. 163). Wiley.
4368
+
4369
+ .. [2] https://en.wikipedia.org/wiki/Directional_statistics
4370
+
4371
+ Examples
4372
+ --------
4373
+ >>> import numpy as np
4374
+ >>> from scipy.stats import directional_stats
4375
+ >>> data = np.array([[3, 4], # first observation, 2D vector space
4376
+ ... [6, -8]]) # second observation
4377
+ >>> dirstats = directional_stats(data)
4378
+ >>> dirstats.mean_direction
4379
+ array([1., 0.])
4380
+
4381
+ In contrast, the regular sample mean of the vectors would be influenced
4382
+ by the magnitude of each observation. Furthermore, the result would not be
4383
+ a unit vector.
4384
+
4385
+ >>> data.mean(axis=0)
4386
+ array([4.5, -2.])
4387
+
4388
+ An exemplary use case for `directional_stats` is to find a *meaningful*
4389
+ center for a set of observations on a sphere, e.g. geographical locations.
4390
+
4391
+ >>> data = np.array([[0.8660254, 0.5, 0.],
4392
+ ... [0.8660254, -0.5, 0.]])
4393
+ >>> dirstats = directional_stats(data)
4394
+ >>> dirstats.mean_direction
4395
+ array([1., 0., 0.])
4396
+
4397
+ The regular sample mean on the other hand yields a result which does not
4398
+ lie on the surface of the sphere.
4399
+
4400
+ >>> data.mean(axis=0)
4401
+ array([0.8660254, 0., 0.])
4402
+
4403
+ The function also returns the mean resultant length, which
4404
+ can be used to calculate a directional variance. For example, using the
4405
+ definition ``Var(z) = 1 - R`` from [2]_ where ``R`` is the
4406
+ mean resultant length, we can calculate the directional variance of the
4407
+ vectors in the above example as:
4408
+
4409
+ >>> 1 - dirstats.mean_resultant_length
4410
+ 0.13397459716167093
4411
+ """
4412
+ xp = array_namespace(samples)
4413
+ samples = xp.asarray(samples)
4414
+
4415
+ if samples.ndim < 2:
4416
+ raise ValueError("samples must at least be two-dimensional. "
4417
+ f"Instead samples has shape: {tuple(samples.shape)}")
4418
+ samples = xp.moveaxis(samples, axis, 0)
4419
+ if normalize:
4420
+ vectornorms = xp_vector_norm(samples, axis=-1, keepdims=True, xp=xp)
4421
+ samples = samples/vectornorms
4422
+ mean = xp.mean(samples, axis=0)
4423
+ mean_resultant_length = xp_vector_norm(mean, axis=-1, keepdims=True, xp=xp)
4424
+ mean_direction = mean / mean_resultant_length
4425
+ mrl = xp.squeeze(mean_resultant_length, axis=-1)
4426
+ mean_resultant_length = mrl[()] if mrl.ndim == 0 else mrl
4427
+ return DirectionalStats(mean_direction, mean_resultant_length)
4428
+
4429
+
4430
+ def false_discovery_control(ps, *, axis=0, method='bh'):
4431
+ """Adjust p-values to control the false discovery rate.
4432
+
4433
+ The false discovery rate (FDR) is the expected proportion of rejected null
4434
+ hypotheses that are actually true.
4435
+ If the null hypothesis is rejected when the *adjusted* p-value falls below
4436
+ a specified level, the false discovery rate is controlled at that level.
4437
+
4438
+ Parameters
4439
+ ----------
4440
+ ps : 1D array_like
4441
+ The p-values to adjust. Elements must be real numbers between 0 and 1.
4442
+ axis : int
4443
+ The axis along which to perform the adjustment. The adjustment is
4444
+ performed independently along each axis-slice. If `axis` is None, `ps`
4445
+ is raveled before performing the adjustment.
4446
+ method : {'bh', 'by'}
4447
+ The false discovery rate control procedure to apply: ``'bh'`` is for
4448
+ Benjamini-Hochberg [1]_ (Eq. 1), ``'by'`` is for Benjaminini-Yekutieli
4449
+ [2]_ (Theorem 1.3). The latter is more conservative, but it is
4450
+ guaranteed to control the FDR even when the p-values are not from
4451
+ independent tests.
4452
+
4453
+ Returns
4454
+ -------
4455
+ ps_adusted : array_like
4456
+ The adjusted p-values. If the null hypothesis is rejected where these
4457
+ fall below a specified level, the false discovery rate is controlled
4458
+ at that level.
4459
+
4460
+ See Also
4461
+ --------
4462
+ combine_pvalues
4463
+ statsmodels.stats.multitest.multipletests
4464
+
4465
+ Notes
4466
+ -----
4467
+ In multiple hypothesis testing, false discovery control procedures tend to
4468
+ offer higher power than familywise error rate control procedures (e.g.
4469
+ Bonferroni correction [1]_).
4470
+
4471
+ If the p-values correspond with independent tests (or tests with
4472
+ "positive regression dependencies" [2]_), rejecting null hypotheses
4473
+ corresponding with Benjamini-Hochberg-adjusted p-values below :math:`q`
4474
+ controls the false discovery rate at a level less than or equal to
4475
+ :math:`q m_0 / m`, where :math:`m_0` is the number of true null hypotheses
4476
+ and :math:`m` is the total number of null hypotheses tested. The same is
4477
+ true even for dependent tests when the p-values are adjusted accorded to
4478
+ the more conservative Benjaminini-Yekutieli procedure.
4479
+
4480
+ The adjusted p-values produced by this function are comparable to those
4481
+ produced by the R function ``p.adjust`` and the statsmodels function
4482
+ `statsmodels.stats.multitest.multipletests`. Please consider the latter
4483
+ for more advanced methods of multiple comparison correction.
4484
+
4485
+ References
4486
+ ----------
4487
+ .. [1] Benjamini, Yoav, and Yosef Hochberg. "Controlling the false
4488
+ discovery rate: a practical and powerful approach to multiple
4489
+ testing." Journal of the Royal statistical society: series B
4490
+ (Methodological) 57.1 (1995): 289-300.
4491
+
4492
+ .. [2] Benjamini, Yoav, and Daniel Yekutieli. "The control of the false
4493
+ discovery rate in multiple testing under dependency." Annals of
4494
+ statistics (2001): 1165-1188.
4495
+
4496
+ .. [3] TileStats. FDR - Benjamini-Hochberg explained - Youtube.
4497
+ https://www.youtube.com/watch?v=rZKa4tW2NKs.
4498
+
4499
+ .. [4] Neuhaus, Karl-Ludwig, et al. "Improved thrombolysis in acute
4500
+ myocardial infarction with front-loaded administration of alteplase:
4501
+ results of the rt-PA-APSAC patency study (TAPS)." Journal of the
4502
+ American College of Cardiology 19.5 (1992): 885-891.
4503
+
4504
+ Examples
4505
+ --------
4506
+ We follow the example from [1]_.
4507
+
4508
+ Thrombolysis with recombinant tissue-type plasminogen activator (rt-PA)
4509
+ and anisoylated plasminogen streptokinase activator (APSAC) in
4510
+ myocardial infarction has been proved to reduce mortality. [4]_
4511
+ investigated the effects of a new front-loaded administration of rt-PA
4512
+ versus those obtained with a standard regimen of APSAC, in a randomized
4513
+ multicentre trial in 421 patients with acute myocardial infarction.
4514
+
4515
+ There were four families of hypotheses tested in the study, the last of
4516
+ which was "cardiac and other events after the start of thrombolitic
4517
+ treatment". FDR control may be desired in this family of hypotheses
4518
+ because it would not be appropriate to conclude that the front-loaded
4519
+ treatment is better if it is merely equivalent to the previous treatment.
4520
+
4521
+ The p-values corresponding with the 15 hypotheses in this family were
4522
+
4523
+ >>> ps = [0.0001, 0.0004, 0.0019, 0.0095, 0.0201, 0.0278, 0.0298, 0.0344,
4524
+ ... 0.0459, 0.3240, 0.4262, 0.5719, 0.6528, 0.7590, 1.000]
4525
+
4526
+ If the chosen significance level is 0.05, we may be tempted to reject the
4527
+ null hypotheses for the tests corresponding with the first nine p-values,
4528
+ as the first nine p-values fall below the chosen significance level.
4529
+ However, this would ignore the problem of "multiplicity": if we fail to
4530
+ correct for the fact that multiple comparisons are being performed, we
4531
+ are more likely to incorrectly reject true null hypotheses.
4532
+
4533
+ One approach to the multiplicity problem is to control the family-wise
4534
+ error rate (FWER), that is, the rate at which the null hypothesis is
4535
+ rejected when it is actually true. A common procedure of this kind is the
4536
+ Bonferroni correction [1]_. We begin by multiplying the p-values by the
4537
+ number of hypotheses tested.
4538
+
4539
+ >>> import numpy as np
4540
+ >>> np.array(ps) * len(ps)
4541
+ array([1.5000e-03, 6.0000e-03, 2.8500e-02, 1.4250e-01, 3.0150e-01,
4542
+ 4.1700e-01, 4.4700e-01, 5.1600e-01, 6.8850e-01, 4.8600e+00,
4543
+ 6.3930e+00, 8.5785e+00, 9.7920e+00, 1.1385e+01, 1.5000e+01])
4544
+
4545
+ To control the FWER at 5%, we reject only the hypotheses corresponding
4546
+ with adjusted p-values less than 0.05. In this case, only the hypotheses
4547
+ corresponding with the first three p-values can be rejected. According to
4548
+ [1]_, these three hypotheses concerned "allergic reaction" and "two
4549
+ different aspects of bleeding."
4550
+
4551
+ An alternative approach is to control the false discovery rate: the
4552
+ expected fraction of rejected null hypotheses that are actually true. The
4553
+ advantage of this approach is that it typically affords greater power: an
4554
+ increased rate of rejecting the null hypothesis when it is indeed false. To
4555
+ control the false discovery rate at 5%, we apply the Benjamini-Hochberg
4556
+ p-value adjustment.
4557
+
4558
+ >>> from scipy import stats
4559
+ >>> stats.false_discovery_control(ps)
4560
+ array([0.0015 , 0.003 , 0.0095 , 0.035625 , 0.0603 ,
4561
+ 0.06385714, 0.06385714, 0.0645 , 0.0765 , 0.486 ,
4562
+ 0.58118182, 0.714875 , 0.75323077, 0.81321429, 1. ])
4563
+
4564
+ Now, the first *four* adjusted p-values fall below 0.05, so we would reject
4565
+ the null hypotheses corresponding with these *four* p-values. Rejection
4566
+ of the fourth null hypothesis was particularly important to the original
4567
+ study as it led to the conclusion that the new treatment had a
4568
+ "substantially lower in-hospital mortality rate."
4569
+
4570
+ """
4571
+ # Input Validation and Special Cases
4572
+ ps = np.asarray(ps)
4573
+
4574
+ ps_in_range = (np.issubdtype(ps.dtype, np.number)
4575
+ and np.all(ps == np.clip(ps, 0, 1)))
4576
+ if not ps_in_range:
4577
+ raise ValueError("`ps` must include only numbers between 0 and 1.")
4578
+
4579
+ methods = {'bh', 'by'}
4580
+ if method.lower() not in methods:
4581
+ raise ValueError(f"Unrecognized `method` '{method}'."
4582
+ f"Method must be one of {methods}.")
4583
+ method = method.lower()
4584
+
4585
+ if axis is None:
4586
+ axis = 0
4587
+ ps = ps.ravel()
4588
+
4589
+ axis = np.asarray(axis)[()]
4590
+ if not np.issubdtype(axis.dtype, np.integer) or axis.size != 1:
4591
+ raise ValueError("`axis` must be an integer or `None`")
4592
+
4593
+ if ps.size <= 1 or ps.shape[axis] <= 1:
4594
+ return ps[()]
4595
+
4596
+ ps = np.moveaxis(ps, axis, -1)
4597
+ m = ps.shape[-1]
4598
+
4599
+ # Main Algorithm
4600
+ # Equivalent to the ideas of [1] and [2], except that this adjusts the
4601
+ # p-values as described in [3]. The results are similar to those produced
4602
+ # by R's p.adjust.
4603
+
4604
+ # "Let [ps] be the ordered observed p-values..."
4605
+ order = np.argsort(ps, axis=-1)
4606
+ ps = np.take_along_axis(ps, order, axis=-1) # this copies ps
4607
+
4608
+ # Equation 1 of [1] rearranged to reject when p is less than specified q
4609
+ i = np.arange(1, m+1)
4610
+ ps *= m / i
4611
+
4612
+ # Theorem 1.3 of [2]
4613
+ if method == 'by':
4614
+ ps *= np.sum(1 / i)
4615
+
4616
+ # accounts for rejecting all null hypotheses i for i < k, where k is
4617
+ # defined in Eq. 1 of either [1] or [2]. See [3]. Starting with the index j
4618
+ # of the second to last element, we replace element j with element j+1 if
4619
+ # the latter is smaller.
4620
+ np.minimum.accumulate(ps[..., ::-1], out=ps[..., ::-1], axis=-1)
4621
+
4622
+ # Restore original order of axes and data
4623
+ np.put_along_axis(ps, order, values=ps.copy(), axis=-1)
4624
+ ps = np.moveaxis(ps, -1, axis)
4625
+
4626
+ return np.clip(ps, 0, 1)