_morestats.py 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186
  1. from __future__ import annotations
  2. import math
  3. import warnings
  4. from collections import namedtuple
  5. import numpy as np
  6. from numpy import (isscalar, r_, log, around, unique, asarray, zeros,
  7. arange, sort, amin, amax, atleast_1d, sqrt, array,
  8. compress, pi, exp, ravel, count_nonzero, sin, cos,
  9. arctan2, hypot)
  10. from scipy import optimize
  11. from scipy import special
  12. from scipy._lib._bunch import _make_tuple_bunch
  13. from scipy._lib._util import _rename_parameter, _contains_nan
  14. from . import _statlib
  15. from . import _stats_py
  16. from ._fit import FitResult
  17. from ._stats_py import find_repeats, _normtest_finish, SignificanceResult
  18. from .contingency import chi2_contingency
  19. from . import distributions
  20. from ._distn_infrastructure import rv_generic
  21. from ._hypotests import _get_wilcoxon_distr
  22. from ._axis_nan_policy import _axis_nan_policy_factory
  23. from .._lib.deprecation import _deprecated
  24. __all__ = ['mvsdist',
  25. 'bayes_mvs', 'kstat', 'kstatvar', 'probplot', 'ppcc_max', 'ppcc_plot',
  26. 'boxcox_llf', 'boxcox', 'boxcox_normmax', 'boxcox_normplot',
  27. 'shapiro', 'anderson', 'ansari', 'bartlett', 'levene', 'binom_test',
  28. 'fligner', 'mood', 'wilcoxon', 'median_test',
  29. 'circmean', 'circvar', 'circstd', 'anderson_ksamp',
  30. 'yeojohnson_llf', 'yeojohnson', 'yeojohnson_normmax',
  31. 'yeojohnson_normplot', 'directional_stats'
  32. ]
  33. Mean = namedtuple('Mean', ('statistic', 'minmax'))
  34. Variance = namedtuple('Variance', ('statistic', 'minmax'))
  35. Std_dev = namedtuple('Std_dev', ('statistic', 'minmax'))
  36. def bayes_mvs(data, alpha=0.90):
  37. r"""
  38. Bayesian confidence intervals for the mean, var, and std.
  39. Parameters
  40. ----------
  41. data : array_like
  42. Input data, if multi-dimensional it is flattened to 1-D by `bayes_mvs`.
  43. Requires 2 or more data points.
  44. alpha : float, optional
  45. Probability that the returned confidence interval contains
  46. the true parameter.
  47. Returns
  48. -------
  49. mean_cntr, var_cntr, std_cntr : tuple
  50. The three results are for the mean, variance and standard deviation,
  51. respectively. Each result is a tuple of the form::
  52. (center, (lower, upper))
  53. with `center` the mean of the conditional pdf of the value given the
  54. data, and `(lower, upper)` a confidence interval, centered on the
  55. median, containing the estimate to a probability ``alpha``.
  56. See Also
  57. --------
  58. mvsdist
  59. Notes
  60. -----
  61. Each tuple of mean, variance, and standard deviation estimates represent
  62. the (center, (lower, upper)) with center the mean of the conditional pdf
  63. of the value given the data and (lower, upper) is a confidence interval
  64. centered on the median, containing the estimate to a probability
  65. ``alpha``.
  66. Converts data to 1-D and assumes all data has the same mean and variance.
  67. Uses Jeffrey's prior for variance and std.
  68. Equivalent to ``tuple((x.mean(), x.interval(alpha)) for x in mvsdist(dat))``
  69. References
  70. ----------
  71. T.E. Oliphant, "A Bayesian perspective on estimating mean, variance, and
  72. standard-deviation from data", https://scholarsarchive.byu.edu/facpub/278,
  73. 2006.
  74. Examples
  75. --------
  76. First a basic example to demonstrate the outputs:
  77. >>> from scipy import stats
  78. >>> data = [6, 9, 12, 7, 8, 8, 13]
  79. >>> mean, var, std = stats.bayes_mvs(data)
  80. >>> mean
  81. Mean(statistic=9.0, minmax=(7.103650222612533, 10.896349777387467))
  82. >>> var
  83. Variance(statistic=10.0, minmax=(3.176724206..., 24.45910382...))
  84. >>> std
  85. Std_dev(statistic=2.9724954732045084, minmax=(1.7823367265645143, 4.945614605014631))
  86. Now we generate some normally distributed random data, and get estimates of
  87. mean and standard deviation with 95% confidence intervals for those
  88. estimates:
  89. >>> n_samples = 100000
  90. >>> data = stats.norm.rvs(size=n_samples)
  91. >>> res_mean, res_var, res_std = stats.bayes_mvs(data, alpha=0.95)
  92. >>> import matplotlib.pyplot as plt
  93. >>> fig = plt.figure()
  94. >>> ax = fig.add_subplot(111)
  95. >>> ax.hist(data, bins=100, density=True, label='Histogram of data')
  96. >>> ax.vlines(res_mean.statistic, 0, 0.5, colors='r', label='Estimated mean')
  97. >>> ax.axvspan(res_mean.minmax[0],res_mean.minmax[1], facecolor='r',
  98. ... alpha=0.2, label=r'Estimated mean (95% limits)')
  99. >>> ax.vlines(res_std.statistic, 0, 0.5, colors='g', label='Estimated scale')
  100. >>> ax.axvspan(res_std.minmax[0],res_std.minmax[1], facecolor='g', alpha=0.2,
  101. ... label=r'Estimated scale (95% limits)')
  102. >>> ax.legend(fontsize=10)
  103. >>> ax.set_xlim([-4, 4])
  104. >>> ax.set_ylim([0, 0.5])
  105. >>> plt.show()
  106. """
  107. m, v, s = mvsdist(data)
  108. if alpha >= 1 or alpha <= 0:
  109. raise ValueError("0 < alpha < 1 is required, but alpha=%s was given."
  110. % alpha)
  111. m_res = Mean(m.mean(), m.interval(alpha))
  112. v_res = Variance(v.mean(), v.interval(alpha))
  113. s_res = Std_dev(s.mean(), s.interval(alpha))
  114. return m_res, v_res, s_res
  115. def mvsdist(data):
  116. """
  117. 'Frozen' distributions for mean, variance, and standard deviation of data.
  118. Parameters
  119. ----------
  120. data : array_like
  121. Input array. Converted to 1-D using ravel.
  122. Requires 2 or more data-points.
  123. Returns
  124. -------
  125. mdist : "frozen" distribution object
  126. Distribution object representing the mean of the data.
  127. vdist : "frozen" distribution object
  128. Distribution object representing the variance of the data.
  129. sdist : "frozen" distribution object
  130. Distribution object representing the standard deviation of the data.
  131. See Also
  132. --------
  133. bayes_mvs
  134. Notes
  135. -----
  136. The return values from ``bayes_mvs(data)`` is equivalent to
  137. ``tuple((x.mean(), x.interval(0.90)) for x in mvsdist(data))``.
  138. In other words, calling ``<dist>.mean()`` and ``<dist>.interval(0.90)``
  139. on the three distribution objects returned from this function will give
  140. the same results that are returned from `bayes_mvs`.
  141. References
  142. ----------
  143. T.E. Oliphant, "A Bayesian perspective on estimating mean, variance, and
  144. standard-deviation from data", https://scholarsarchive.byu.edu/facpub/278,
  145. 2006.
  146. Examples
  147. --------
  148. >>> from scipy import stats
  149. >>> data = [6, 9, 12, 7, 8, 8, 13]
  150. >>> mean, var, std = stats.mvsdist(data)
  151. We now have frozen distribution objects "mean", "var" and "std" that we can
  152. examine:
  153. >>> mean.mean()
  154. 9.0
  155. >>> mean.interval(0.95)
  156. (6.6120585482655692, 11.387941451734431)
  157. >>> mean.std()
  158. 1.1952286093343936
  159. """
  160. x = ravel(data)
  161. n = len(x)
  162. if n < 2:
  163. raise ValueError("Need at least 2 data-points.")
  164. xbar = x.mean()
  165. C = x.var()
  166. if n > 1000: # gaussian approximations for large n
  167. mdist = distributions.norm(loc=xbar, scale=math.sqrt(C / n))
  168. sdist = distributions.norm(loc=math.sqrt(C), scale=math.sqrt(C / (2. * n)))
  169. vdist = distributions.norm(loc=C, scale=math.sqrt(2.0 / n) * C)
  170. else:
  171. nm1 = n - 1
  172. fac = n * C / 2.
  173. val = nm1 / 2.
  174. mdist = distributions.t(nm1, loc=xbar, scale=math.sqrt(C / nm1))
  175. sdist = distributions.gengamma(val, -2, scale=math.sqrt(fac))
  176. vdist = distributions.invgamma(val, scale=fac)
  177. return mdist, vdist, sdist
  178. @_axis_nan_policy_factory(
  179. lambda x: x, result_to_tuple=lambda x: (x,), n_outputs=1, default_axis=None
  180. )
  181. def kstat(data, n=2):
  182. r"""
  183. Return the nth k-statistic (1<=n<=4 so far).
  184. The nth k-statistic k_n is the unique symmetric unbiased estimator of the
  185. nth cumulant kappa_n.
  186. Parameters
  187. ----------
  188. data : array_like
  189. Input array. Note that n-D input gets flattened.
  190. n : int, {1, 2, 3, 4}, optional
  191. Default is equal to 2.
  192. Returns
  193. -------
  194. kstat : float
  195. The nth k-statistic.
  196. See Also
  197. --------
  198. kstatvar: Returns an unbiased estimator of the variance of the k-statistic.
  199. moment: Returns the n-th central moment about the mean for a sample.
  200. Notes
  201. -----
  202. For a sample size n, the first few k-statistics are given by:
  203. .. math::
  204. k_{1} = \mu
  205. k_{2} = \frac{n}{n-1} m_{2}
  206. k_{3} = \frac{ n^{2} } {(n-1) (n-2)} m_{3}
  207. k_{4} = \frac{ n^{2} [(n + 1)m_{4} - 3(n - 1) m^2_{2}]} {(n-1) (n-2) (n-3)}
  208. where :math:`\mu` is the sample mean, :math:`m_2` is the sample
  209. variance, and :math:`m_i` is the i-th sample central moment.
  210. References
  211. ----------
  212. http://mathworld.wolfram.com/k-Statistic.html
  213. http://mathworld.wolfram.com/Cumulant.html
  214. Examples
  215. --------
  216. >>> from scipy import stats
  217. >>> from numpy.random import default_rng
  218. >>> rng = default_rng()
  219. As sample size increases, n-th moment and n-th k-statistic converge to the
  220. same number (although they aren't identical). In the case of the normal
  221. distribution, they converge to zero.
  222. >>> for n in [2, 3, 4, 5, 6, 7]:
  223. ... x = rng.normal(size=10**n)
  224. ... m, k = stats.moment(x, 3), stats.kstat(x, 3)
  225. ... print("%.3g %.3g %.3g" % (m, k, m-k))
  226. -0.631 -0.651 0.0194 # random
  227. 0.0282 0.0283 -8.49e-05
  228. -0.0454 -0.0454 1.36e-05
  229. 7.53e-05 7.53e-05 -2.26e-09
  230. 0.00166 0.00166 -4.99e-09
  231. -2.88e-06 -2.88e-06 8.63e-13
  232. """
  233. if n > 4 or n < 1:
  234. raise ValueError("k-statistics only supported for 1<=n<=4")
  235. n = int(n)
  236. S = np.zeros(n + 1, np.float64)
  237. data = ravel(data)
  238. N = data.size
  239. # raise ValueError on empty input
  240. if N == 0:
  241. raise ValueError("Data input must not be empty")
  242. # on nan input, return nan without warning
  243. if np.isnan(np.sum(data)):
  244. return np.nan
  245. for k in range(1, n + 1):
  246. S[k] = np.sum(data**k, axis=0)
  247. if n == 1:
  248. return S[1] * 1.0/N
  249. elif n == 2:
  250. return (N*S[2] - S[1]**2.0) / (N*(N - 1.0))
  251. elif n == 3:
  252. return (2*S[1]**3 - 3*N*S[1]*S[2] + N*N*S[3]) / (N*(N - 1.0)*(N - 2.0))
  253. elif n == 4:
  254. return ((-6*S[1]**4 + 12*N*S[1]**2 * S[2] - 3*N*(N-1.0)*S[2]**2 -
  255. 4*N*(N+1)*S[1]*S[3] + N*N*(N+1)*S[4]) /
  256. (N*(N-1.0)*(N-2.0)*(N-3.0)))
  257. else:
  258. raise ValueError("Should not be here.")
  259. @_axis_nan_policy_factory(
  260. lambda x: x, result_to_tuple=lambda x: (x,), n_outputs=1, default_axis=None
  261. )
  262. def kstatvar(data, n=2):
  263. r"""Return an unbiased estimator of the variance of the k-statistic.
  264. See `kstat` for more details of the k-statistic.
  265. Parameters
  266. ----------
  267. data : array_like
  268. Input array. Note that n-D input gets flattened.
  269. n : int, {1, 2}, optional
  270. Default is equal to 2.
  271. Returns
  272. -------
  273. kstatvar : float
  274. The nth k-statistic variance.
  275. See Also
  276. --------
  277. kstat: Returns the n-th k-statistic.
  278. moment: Returns the n-th central moment about the mean for a sample.
  279. Notes
  280. -----
  281. The variances of the first few k-statistics are given by:
  282. .. math::
  283. var(k_{1}) = \frac{\kappa^2}{n}
  284. var(k_{2}) = \frac{\kappa^4}{n} + \frac{2\kappa^2_{2}}{n - 1}
  285. var(k_{3}) = \frac{\kappa^6}{n} + \frac{9 \kappa_2 \kappa_4}{n - 1} +
  286. \frac{9 \kappa^2_{3}}{n - 1} +
  287. \frac{6 n \kappa^3_{2}}{(n-1) (n-2)}
  288. var(k_{4}) = \frac{\kappa^8}{n} + \frac{16 \kappa_2 \kappa_6}{n - 1} +
  289. \frac{48 \kappa_{3} \kappa_5}{n - 1} +
  290. \frac{34 \kappa^2_{4}}{n-1} + \frac{72 n \kappa^2_{2} \kappa_4}{(n - 1) (n - 2)} +
  291. \frac{144 n \kappa_{2} \kappa^2_{3}}{(n - 1) (n - 2)} +
  292. \frac{24 (n + 1) n \kappa^4_{2}}{(n - 1) (n - 2) (n - 3)}
  293. """
  294. data = ravel(data)
  295. N = len(data)
  296. if n == 1:
  297. return kstat(data, n=2) * 1.0/N
  298. elif n == 2:
  299. k2 = kstat(data, n=2)
  300. k4 = kstat(data, n=4)
  301. return (2*N*k2**2 + (N-1)*k4) / (N*(N+1))
  302. else:
  303. raise ValueError("Only n=1 or n=2 supported.")
  304. def _calc_uniform_order_statistic_medians(n):
  305. """Approximations of uniform order statistic medians.
  306. Parameters
  307. ----------
  308. n : int
  309. Sample size.
  310. Returns
  311. -------
  312. v : 1d float array
  313. Approximations of the order statistic medians.
  314. References
  315. ----------
  316. .. [1] James J. Filliben, "The Probability Plot Correlation Coefficient
  317. Test for Normality", Technometrics, Vol. 17, pp. 111-117, 1975.
  318. Examples
  319. --------
  320. Order statistics of the uniform distribution on the unit interval
  321. are marginally distributed according to beta distributions.
  322. The expectations of these order statistic are evenly spaced across
  323. the interval, but the distributions are skewed in a way that
  324. pushes the medians slightly towards the endpoints of the unit interval:
  325. >>> import numpy as np
  326. >>> n = 4
  327. >>> k = np.arange(1, n+1)
  328. >>> from scipy.stats import beta
  329. >>> a = k
  330. >>> b = n-k+1
  331. >>> beta.mean(a, b)
  332. array([0.2, 0.4, 0.6, 0.8])
  333. >>> beta.median(a, b)
  334. array([0.15910358, 0.38572757, 0.61427243, 0.84089642])
  335. The Filliben approximation uses the exact medians of the smallest
  336. and greatest order statistics, and the remaining medians are approximated
  337. by points spread evenly across a sub-interval of the unit interval:
  338. >>> from scipy.stats._morestats import _calc_uniform_order_statistic_medians
  339. >>> _calc_uniform_order_statistic_medians(n)
  340. array([0.15910358, 0.38545246, 0.61454754, 0.84089642])
  341. This plot shows the skewed distributions of the order statistics
  342. of a sample of size four from a uniform distribution on the unit interval:
  343. >>> import matplotlib.pyplot as plt
  344. >>> x = np.linspace(0.0, 1.0, num=50, endpoint=True)
  345. >>> pdfs = [beta.pdf(x, a[i], b[i]) for i in range(n)]
  346. >>> plt.figure()
  347. >>> plt.plot(x, pdfs[0], x, pdfs[1], x, pdfs[2], x, pdfs[3])
  348. """
  349. v = np.empty(n, dtype=np.float64)
  350. v[-1] = 0.5**(1.0 / n)
  351. v[0] = 1 - v[-1]
  352. i = np.arange(2, n)
  353. v[1:-1] = (i - 0.3175) / (n + 0.365)
  354. return v
  355. def _parse_dist_kw(dist, enforce_subclass=True):
  356. """Parse `dist` keyword.
  357. Parameters
  358. ----------
  359. dist : str or stats.distributions instance.
  360. Several functions take `dist` as a keyword, hence this utility
  361. function.
  362. enforce_subclass : bool, optional
  363. If True (default), `dist` needs to be a
  364. `_distn_infrastructure.rv_generic` instance.
  365. It can sometimes be useful to set this keyword to False, if a function
  366. wants to accept objects that just look somewhat like such an instance
  367. (for example, they have a ``ppf`` method).
  368. """
  369. if isinstance(dist, rv_generic):
  370. pass
  371. elif isinstance(dist, str):
  372. try:
  373. dist = getattr(distributions, dist)
  374. except AttributeError as e:
  375. raise ValueError("%s is not a valid distribution name" % dist) from e
  376. elif enforce_subclass:
  377. msg = ("`dist` should be a stats.distributions instance or a string "
  378. "with the name of such a distribution.")
  379. raise ValueError(msg)
  380. return dist
  381. def _add_axis_labels_title(plot, xlabel, ylabel, title):
  382. """Helper function to add axes labels and a title to stats plots."""
  383. try:
  384. if hasattr(plot, 'set_title'):
  385. # Matplotlib Axes instance or something that looks like it
  386. plot.set_title(title)
  387. plot.set_xlabel(xlabel)
  388. plot.set_ylabel(ylabel)
  389. else:
  390. # matplotlib.pyplot module
  391. plot.title(title)
  392. plot.xlabel(xlabel)
  393. plot.ylabel(ylabel)
  394. except Exception:
  395. # Not an MPL object or something that looks (enough) like it.
  396. # Don't crash on adding labels or title
  397. pass
  398. def probplot(x, sparams=(), dist='norm', fit=True, plot=None, rvalue=False):
  399. """
  400. Calculate quantiles for a probability plot, and optionally show the plot.
  401. Generates a probability plot of sample data against the quantiles of a
  402. specified theoretical distribution (the normal distribution by default).
  403. `probplot` optionally calculates a best-fit line for the data and plots the
  404. results using Matplotlib or a given plot function.
  405. Parameters
  406. ----------
  407. x : array_like
  408. Sample/response data from which `probplot` creates the plot.
  409. sparams : tuple, optional
  410. Distribution-specific shape parameters (shape parameters plus location
  411. and scale).
  412. dist : str or stats.distributions instance, optional
  413. Distribution or distribution function name. The default is 'norm' for a
  414. normal probability plot. Objects that look enough like a
  415. stats.distributions instance (i.e. they have a ``ppf`` method) are also
  416. accepted.
  417. fit : bool, optional
  418. Fit a least-squares regression (best-fit) line to the sample data if
  419. True (default).
  420. plot : object, optional
  421. If given, plots the quantiles.
  422. If given and `fit` is True, also plots the least squares fit.
  423. `plot` is an object that has to have methods "plot" and "text".
  424. The `matplotlib.pyplot` module or a Matplotlib Axes object can be used,
  425. or a custom object with the same methods.
  426. Default is None, which means that no plot is created.
  427. rvalue : bool, optional
  428. If `plot` is provided and `fit` is True, setting `rvalue` to True
  429. includes the coefficient of determination on the plot.
  430. Default is False.
  431. Returns
  432. -------
  433. (osm, osr) : tuple of ndarrays
  434. Tuple of theoretical quantiles (osm, or order statistic medians) and
  435. ordered responses (osr). `osr` is simply sorted input `x`.
  436. For details on how `osm` is calculated see the Notes section.
  437. (slope, intercept, r) : tuple of floats, optional
  438. Tuple containing the result of the least-squares fit, if that is
  439. performed by `probplot`. `r` is the square root of the coefficient of
  440. determination. If ``fit=False`` and ``plot=None``, this tuple is not
  441. returned.
  442. Notes
  443. -----
  444. Even if `plot` is given, the figure is not shown or saved by `probplot`;
  445. ``plt.show()`` or ``plt.savefig('figname.png')`` should be used after
  446. calling `probplot`.
  447. `probplot` generates a probability plot, which should not be confused with
  448. a Q-Q or a P-P plot. Statsmodels has more extensive functionality of this
  449. type, see ``statsmodels.api.ProbPlot``.
  450. The formula used for the theoretical quantiles (horizontal axis of the
  451. probability plot) is Filliben's estimate::
  452. quantiles = dist.ppf(val), for
  453. 0.5**(1/n), for i = n
  454. val = (i - 0.3175) / (n + 0.365), for i = 2, ..., n-1
  455. 1 - 0.5**(1/n), for i = 1
  456. where ``i`` indicates the i-th ordered value and ``n`` is the total number
  457. of values.
  458. Examples
  459. --------
  460. >>> import numpy as np
  461. >>> from scipy import stats
  462. >>> import matplotlib.pyplot as plt
  463. >>> nsample = 100
  464. >>> rng = np.random.default_rng()
  465. A t distribution with small degrees of freedom:
  466. >>> ax1 = plt.subplot(221)
  467. >>> x = stats.t.rvs(3, size=nsample, random_state=rng)
  468. >>> res = stats.probplot(x, plot=plt)
  469. A t distribution with larger degrees of freedom:
  470. >>> ax2 = plt.subplot(222)
  471. >>> x = stats.t.rvs(25, size=nsample, random_state=rng)
  472. >>> res = stats.probplot(x, plot=plt)
  473. A mixture of two normal distributions with broadcasting:
  474. >>> ax3 = plt.subplot(223)
  475. >>> x = stats.norm.rvs(loc=[0,5], scale=[1,1.5],
  476. ... size=(nsample//2,2), random_state=rng).ravel()
  477. >>> res = stats.probplot(x, plot=plt)
  478. A standard normal distribution:
  479. >>> ax4 = plt.subplot(224)
  480. >>> x = stats.norm.rvs(loc=0, scale=1, size=nsample, random_state=rng)
  481. >>> res = stats.probplot(x, plot=plt)
  482. Produce a new figure with a loggamma distribution, using the ``dist`` and
  483. ``sparams`` keywords:
  484. >>> fig = plt.figure()
  485. >>> ax = fig.add_subplot(111)
  486. >>> x = stats.loggamma.rvs(c=2.5, size=500, random_state=rng)
  487. >>> res = stats.probplot(x, dist=stats.loggamma, sparams=(2.5,), plot=ax)
  488. >>> ax.set_title("Probplot for loggamma dist with shape parameter 2.5")
  489. Show the results with Matplotlib:
  490. >>> plt.show()
  491. """
  492. x = np.asarray(x)
  493. if x.size == 0:
  494. if fit:
  495. return (x, x), (np.nan, np.nan, 0.0)
  496. else:
  497. return x, x
  498. osm_uniform = _calc_uniform_order_statistic_medians(len(x))
  499. dist = _parse_dist_kw(dist, enforce_subclass=False)
  500. if sparams is None:
  501. sparams = ()
  502. if isscalar(sparams):
  503. sparams = (sparams,)
  504. if not isinstance(sparams, tuple):
  505. sparams = tuple(sparams)
  506. osm = dist.ppf(osm_uniform, *sparams)
  507. osr = sort(x)
  508. if fit:
  509. # perform a linear least squares fit.
  510. slope, intercept, r, prob, _ = _stats_py.linregress(osm, osr)
  511. if plot is not None:
  512. plot.plot(osm, osr, 'bo')
  513. if fit:
  514. plot.plot(osm, slope*osm + intercept, 'r-')
  515. _add_axis_labels_title(plot, xlabel='Theoretical quantiles',
  516. ylabel='Ordered Values',
  517. title='Probability Plot')
  518. # Add R^2 value to the plot as text
  519. if fit and rvalue:
  520. xmin = amin(osm)
  521. xmax = amax(osm)
  522. ymin = amin(x)
  523. ymax = amax(x)
  524. posx = xmin + 0.70 * (xmax - xmin)
  525. posy = ymin + 0.01 * (ymax - ymin)
  526. plot.text(posx, posy, "$R^2=%1.4f$" % r**2)
  527. if fit:
  528. return (osm, osr), (slope, intercept, r)
  529. else:
  530. return osm, osr
  531. def ppcc_max(x, brack=(0.0, 1.0), dist='tukeylambda'):
  532. """Calculate the shape parameter that maximizes the PPCC.
  533. The probability plot correlation coefficient (PPCC) plot can be used
  534. to determine the optimal shape parameter for a one-parameter family
  535. of distributions. ``ppcc_max`` returns the shape parameter that would
  536. maximize the probability plot correlation coefficient for the given
  537. data to a one-parameter family of distributions.
  538. Parameters
  539. ----------
  540. x : array_like
  541. Input array.
  542. brack : tuple, optional
  543. Triple (a,b,c) where (a<b<c). If bracket consists of two numbers (a, c)
  544. then they are assumed to be a starting interval for a downhill bracket
  545. search (see `scipy.optimize.brent`).
  546. dist : str or stats.distributions instance, optional
  547. Distribution or distribution function name. Objects that look enough
  548. like a stats.distributions instance (i.e. they have a ``ppf`` method)
  549. are also accepted. The default is ``'tukeylambda'``.
  550. Returns
  551. -------
  552. shape_value : float
  553. The shape parameter at which the probability plot correlation
  554. coefficient reaches its max value.
  555. See Also
  556. --------
  557. ppcc_plot, probplot, boxcox
  558. Notes
  559. -----
  560. The brack keyword serves as a starting point which is useful in corner
  561. cases. One can use a plot to obtain a rough visual estimate of the location
  562. for the maximum to start the search near it.
  563. References
  564. ----------
  565. .. [1] J.J. Filliben, "The Probability Plot Correlation Coefficient Test
  566. for Normality", Technometrics, Vol. 17, pp. 111-117, 1975.
  567. .. [2] Engineering Statistics Handbook, NIST/SEMATEC,
  568. https://www.itl.nist.gov/div898/handbook/eda/section3/ppccplot.htm
  569. Examples
  570. --------
  571. First we generate some random data from a Weibull distribution
  572. with shape parameter 2.5:
  573. >>> import numpy as np
  574. >>> from scipy import stats
  575. >>> import matplotlib.pyplot as plt
  576. >>> rng = np.random.default_rng()
  577. >>> c = 2.5
  578. >>> x = stats.weibull_min.rvs(c, scale=4, size=2000, random_state=rng)
  579. Generate the PPCC plot for this data with the Weibull distribution.
  580. >>> fig, ax = plt.subplots(figsize=(8, 6))
  581. >>> res = stats.ppcc_plot(x, c/2, 2*c, dist='weibull_min', plot=ax)
  582. We calculate the value where the shape should reach its maximum and a
  583. red line is drawn there. The line should coincide with the highest
  584. point in the PPCC graph.
  585. >>> cmax = stats.ppcc_max(x, brack=(c/2, 2*c), dist='weibull_min')
  586. >>> ax.axvline(cmax, color='r')
  587. >>> plt.show()
  588. """
  589. dist = _parse_dist_kw(dist)
  590. osm_uniform = _calc_uniform_order_statistic_medians(len(x))
  591. osr = sort(x)
  592. # this function computes the x-axis values of the probability plot
  593. # and computes a linear regression (including the correlation)
  594. # and returns 1-r so that a minimization function maximizes the
  595. # correlation
  596. def tempfunc(shape, mi, yvals, func):
  597. xvals = func(mi, shape)
  598. r, prob = _stats_py.pearsonr(xvals, yvals)
  599. return 1 - r
  600. return optimize.brent(tempfunc, brack=brack,
  601. args=(osm_uniform, osr, dist.ppf))
  602. def ppcc_plot(x, a, b, dist='tukeylambda', plot=None, N=80):
  603. """Calculate and optionally plot probability plot correlation coefficient.
  604. The probability plot correlation coefficient (PPCC) plot can be used to
  605. determine the optimal shape parameter for a one-parameter family of
  606. distributions. It cannot be used for distributions without shape
  607. parameters
  608. (like the normal distribution) or with multiple shape parameters.
  609. By default a Tukey-Lambda distribution (`stats.tukeylambda`) is used. A
  610. Tukey-Lambda PPCC plot interpolates from long-tailed to short-tailed
  611. distributions via an approximately normal one, and is therefore
  612. particularly useful in practice.
  613. Parameters
  614. ----------
  615. x : array_like
  616. Input array.
  617. a, b : scalar
  618. Lower and upper bounds of the shape parameter to use.
  619. dist : str or stats.distributions instance, optional
  620. Distribution or distribution function name. Objects that look enough
  621. like a stats.distributions instance (i.e. they have a ``ppf`` method)
  622. are also accepted. The default is ``'tukeylambda'``.
  623. plot : object, optional
  624. If given, plots PPCC against the shape parameter.
  625. `plot` is an object that has to have methods "plot" and "text".
  626. The `matplotlib.pyplot` module or a Matplotlib Axes object can be used,
  627. or a custom object with the same methods.
  628. Default is None, which means that no plot is created.
  629. N : int, optional
  630. Number of points on the horizontal axis (equally distributed from
  631. `a` to `b`).
  632. Returns
  633. -------
  634. svals : ndarray
  635. The shape values for which `ppcc` was calculated.
  636. ppcc : ndarray
  637. The calculated probability plot correlation coefficient values.
  638. See Also
  639. --------
  640. ppcc_max, probplot, boxcox_normplot, tukeylambda
  641. References
  642. ----------
  643. J.J. Filliben, "The Probability Plot Correlation Coefficient Test for
  644. Normality", Technometrics, Vol. 17, pp. 111-117, 1975.
  645. Examples
  646. --------
  647. First we generate some random data from a Weibull distribution
  648. with shape parameter 2.5, and plot the histogram of the data:
  649. >>> import numpy as np
  650. >>> from scipy import stats
  651. >>> import matplotlib.pyplot as plt
  652. >>> rng = np.random.default_rng()
  653. >>> c = 2.5
  654. >>> x = stats.weibull_min.rvs(c, scale=4, size=2000, random_state=rng)
  655. Take a look at the histogram of the data.
  656. >>> fig1, ax = plt.subplots(figsize=(9, 4))
  657. >>> ax.hist(x, bins=50)
  658. >>> ax.set_title('Histogram of x')
  659. >>> plt.show()
  660. Now we explore this data with a PPCC plot as well as the related
  661. probability plot and Box-Cox normplot. A red line is drawn where we
  662. expect the PPCC value to be maximal (at the shape parameter ``c``
  663. used above):
  664. >>> fig2 = plt.figure(figsize=(12, 4))
  665. >>> ax1 = fig2.add_subplot(1, 3, 1)
  666. >>> ax2 = fig2.add_subplot(1, 3, 2)
  667. >>> ax3 = fig2.add_subplot(1, 3, 3)
  668. >>> res = stats.probplot(x, plot=ax1)
  669. >>> res = stats.boxcox_normplot(x, -4, 4, plot=ax2)
  670. >>> res = stats.ppcc_plot(x, c/2, 2*c, dist='weibull_min', plot=ax3)
  671. >>> ax3.axvline(c, color='r')
  672. >>> plt.show()
  673. """
  674. if b <= a:
  675. raise ValueError("`b` has to be larger than `a`.")
  676. svals = np.linspace(a, b, num=N)
  677. ppcc = np.empty_like(svals)
  678. for k, sval in enumerate(svals):
  679. _, r2 = probplot(x, sval, dist=dist, fit=True)
  680. ppcc[k] = r2[-1]
  681. if plot is not None:
  682. plot.plot(svals, ppcc, 'x')
  683. _add_axis_labels_title(plot, xlabel='Shape Values',
  684. ylabel='Prob Plot Corr. Coef.',
  685. title='(%s) PPCC Plot' % dist)
  686. return svals, ppcc
  687. def boxcox_llf(lmb, data):
  688. r"""The boxcox log-likelihood function.
  689. Parameters
  690. ----------
  691. lmb : scalar
  692. Parameter for Box-Cox transformation. See `boxcox` for details.
  693. data : array_like
  694. Data to calculate Box-Cox log-likelihood for. If `data` is
  695. multi-dimensional, the log-likelihood is calculated along the first
  696. axis.
  697. Returns
  698. -------
  699. llf : float or ndarray
  700. Box-Cox log-likelihood of `data` given `lmb`. A float for 1-D `data`,
  701. an array otherwise.
  702. See Also
  703. --------
  704. boxcox, probplot, boxcox_normplot, boxcox_normmax
  705. Notes
  706. -----
  707. The Box-Cox log-likelihood function is defined here as
  708. .. math::
  709. llf = (\lambda - 1) \sum_i(\log(x_i)) -
  710. N/2 \log(\sum_i (y_i - \bar{y})^2 / N),
  711. where ``y`` is the Box-Cox transformed input data ``x``.
  712. Examples
  713. --------
  714. >>> import numpy as np
  715. >>> from scipy import stats
  716. >>> import matplotlib.pyplot as plt
  717. >>> from mpl_toolkits.axes_grid1.inset_locator import inset_axes
  718. Generate some random variates and calculate Box-Cox log-likelihood values
  719. for them for a range of ``lmbda`` values:
  720. >>> rng = np.random.default_rng()
  721. >>> x = stats.loggamma.rvs(5, loc=10, size=1000, random_state=rng)
  722. >>> lmbdas = np.linspace(-2, 10)
  723. >>> llf = np.zeros(lmbdas.shape, dtype=float)
  724. >>> for ii, lmbda in enumerate(lmbdas):
  725. ... llf[ii] = stats.boxcox_llf(lmbda, x)
  726. Also find the optimal lmbda value with `boxcox`:
  727. >>> x_most_normal, lmbda_optimal = stats.boxcox(x)
  728. Plot the log-likelihood as function of lmbda. Add the optimal lmbda as a
  729. horizontal line to check that that's really the optimum:
  730. >>> fig = plt.figure()
  731. >>> ax = fig.add_subplot(111)
  732. >>> ax.plot(lmbdas, llf, 'b.-')
  733. >>> ax.axhline(stats.boxcox_llf(lmbda_optimal, x), color='r')
  734. >>> ax.set_xlabel('lmbda parameter')
  735. >>> ax.set_ylabel('Box-Cox log-likelihood')
  736. Now add some probability plots to show that where the log-likelihood is
  737. maximized the data transformed with `boxcox` looks closest to normal:
  738. >>> locs = [3, 10, 4] # 'lower left', 'center', 'lower right'
  739. >>> for lmbda, loc in zip([-1, lmbda_optimal, 9], locs):
  740. ... xt = stats.boxcox(x, lmbda=lmbda)
  741. ... (osm, osr), (slope, intercept, r_sq) = stats.probplot(xt)
  742. ... ax_inset = inset_axes(ax, width="20%", height="20%", loc=loc)
  743. ... ax_inset.plot(osm, osr, 'c.', osm, slope*osm + intercept, 'k-')
  744. ... ax_inset.set_xticklabels([])
  745. ... ax_inset.set_yticklabels([])
  746. ... ax_inset.set_title(r'$\lambda=%1.2f$' % lmbda)
  747. >>> plt.show()
  748. """
  749. data = np.asarray(data)
  750. N = data.shape[0]
  751. if N == 0:
  752. return np.nan
  753. logdata = np.log(data)
  754. # Compute the variance of the transformed data.
  755. if lmb == 0:
  756. variance = np.var(logdata, axis=0)
  757. else:
  758. # Transform without the constant offset 1/lmb. The offset does
  759. # not effect the variance, and the subtraction of the offset can
  760. # lead to loss of precision.
  761. variance = np.var(data**lmb / lmb, axis=0)
  762. return (lmb - 1) * np.sum(logdata, axis=0) - N/2 * np.log(variance)
  763. def _boxcox_conf_interval(x, lmax, alpha):
  764. # Need to find the lambda for which
  765. # f(x,lmbda) >= f(x,lmax) - 0.5*chi^2_alpha;1
  766. fac = 0.5 * distributions.chi2.ppf(1 - alpha, 1)
  767. target = boxcox_llf(lmax, x) - fac
  768. def rootfunc(lmbda, data, target):
  769. return boxcox_llf(lmbda, data) - target
  770. # Find positive endpoint of interval in which answer is to be found
  771. newlm = lmax + 0.5
  772. N = 0
  773. while (rootfunc(newlm, x, target) > 0.0) and (N < 500):
  774. newlm += 0.1
  775. N += 1
  776. if N == 500:
  777. raise RuntimeError("Could not find endpoint.")
  778. lmplus = optimize.brentq(rootfunc, lmax, newlm, args=(x, target))
  779. # Now find negative interval in the same way
  780. newlm = lmax - 0.5
  781. N = 0
  782. while (rootfunc(newlm, x, target) > 0.0) and (N < 500):
  783. newlm -= 0.1
  784. N += 1
  785. if N == 500:
  786. raise RuntimeError("Could not find endpoint.")
  787. lmminus = optimize.brentq(rootfunc, newlm, lmax, args=(x, target))
  788. return lmminus, lmplus
  789. def boxcox(x, lmbda=None, alpha=None, optimizer=None):
  790. r"""Return a dataset transformed by a Box-Cox power transformation.
  791. Parameters
  792. ----------
  793. x : ndarray
  794. Input array to be transformed.
  795. If `lmbda` is not None, this is an alias of
  796. `scipy.special.boxcox`.
  797. Returns nan if ``x < 0``; returns -inf if ``x == 0 and lmbda < 0``.
  798. If `lmbda` is None, array must be positive, 1-dimensional, and
  799. non-constant.
  800. lmbda : scalar, optional
  801. If `lmbda` is None (default), find the value of `lmbda` that maximizes
  802. the log-likelihood function and return it as the second output
  803. argument.
  804. If `lmbda` is not None, do the transformation for that value.
  805. alpha : float, optional
  806. If `lmbda` is None and `alpha` is not None (default), return the
  807. ``100 * (1-alpha)%`` confidence interval for `lmbda` as the third
  808. output argument. Must be between 0.0 and 1.0.
  809. If `lmbda` is not None, `alpha` is ignored.
  810. optimizer : callable, optional
  811. If `lmbda` is None, `optimizer` is the scalar optimizer used to find
  812. the value of `lmbda` that minimizes the negative log-likelihood
  813. function. `optimizer` is a callable that accepts one argument:
  814. fun : callable
  815. The objective function, which evaluates the negative
  816. log-likelihood function at a provided value of `lmbda`
  817. and returns an object, such as an instance of
  818. `scipy.optimize.OptimizeResult`, which holds the optimal value of
  819. `lmbda` in an attribute `x`.
  820. See the example in `boxcox_normmax` or the documentation of
  821. `scipy.optimize.minimize_scalar` for more information.
  822. If `lmbda` is not None, `optimizer` is ignored.
  823. Returns
  824. -------
  825. boxcox : ndarray
  826. Box-Cox power transformed array.
  827. maxlog : float, optional
  828. If the `lmbda` parameter is None, the second returned argument is
  829. the `lmbda` that maximizes the log-likelihood function.
  830. (min_ci, max_ci) : tuple of float, optional
  831. If `lmbda` parameter is None and `alpha` is not None, this returned
  832. tuple of floats represents the minimum and maximum confidence limits
  833. given `alpha`.
  834. See Also
  835. --------
  836. probplot, boxcox_normplot, boxcox_normmax, boxcox_llf
  837. Notes
  838. -----
  839. The Box-Cox transform is given by::
  840. y = (x**lmbda - 1) / lmbda, for lmbda != 0
  841. log(x), for lmbda = 0
  842. `boxcox` requires the input data to be positive. Sometimes a Box-Cox
  843. transformation provides a shift parameter to achieve this; `boxcox` does
  844. not. Such a shift parameter is equivalent to adding a positive constant to
  845. `x` before calling `boxcox`.
  846. The confidence limits returned when `alpha` is provided give the interval
  847. where:
  848. .. math::
  849. llf(\hat{\lambda}) - llf(\lambda) < \frac{1}{2}\chi^2(1 - \alpha, 1),
  850. with ``llf`` the log-likelihood function and :math:`\chi^2` the chi-squared
  851. function.
  852. References
  853. ----------
  854. G.E.P. Box and D.R. Cox, "An Analysis of Transformations", Journal of the
  855. Royal Statistical Society B, 26, 211-252 (1964).
  856. Examples
  857. --------
  858. >>> from scipy import stats
  859. >>> import matplotlib.pyplot as plt
  860. We generate some random variates from a non-normal distribution and make a
  861. probability plot for it, to show it is non-normal in the tails:
  862. >>> fig = plt.figure()
  863. >>> ax1 = fig.add_subplot(211)
  864. >>> x = stats.loggamma.rvs(5, size=500) + 5
  865. >>> prob = stats.probplot(x, dist=stats.norm, plot=ax1)
  866. >>> ax1.set_xlabel('')
  867. >>> ax1.set_title('Probplot against normal distribution')
  868. We now use `boxcox` to transform the data so it's closest to normal:
  869. >>> ax2 = fig.add_subplot(212)
  870. >>> xt, _ = stats.boxcox(x)
  871. >>> prob = stats.probplot(xt, dist=stats.norm, plot=ax2)
  872. >>> ax2.set_title('Probplot after Box-Cox transformation')
  873. >>> plt.show()
  874. """
  875. x = np.asarray(x)
  876. if lmbda is not None: # single transformation
  877. return special.boxcox(x, lmbda)
  878. if x.ndim != 1:
  879. raise ValueError("Data must be 1-dimensional.")
  880. if x.size == 0:
  881. return x
  882. if np.all(x == x[0]):
  883. raise ValueError("Data must not be constant.")
  884. if np.any(x <= 0):
  885. raise ValueError("Data must be positive.")
  886. # If lmbda=None, find the lmbda that maximizes the log-likelihood function.
  887. lmax = boxcox_normmax(x, method='mle', optimizer=optimizer)
  888. y = boxcox(x, lmax)
  889. if alpha is None:
  890. return y, lmax
  891. else:
  892. # Find confidence interval
  893. interval = _boxcox_conf_interval(x, lmax, alpha)
  894. return y, lmax, interval
  895. def boxcox_normmax(x, brack=None, method='pearsonr', optimizer=None):
  896. """Compute optimal Box-Cox transform parameter for input data.
  897. Parameters
  898. ----------
  899. x : array_like
  900. Input array.
  901. brack : 2-tuple, optional, default (-2.0, 2.0)
  902. The starting interval for a downhill bracket search for the default
  903. `optimize.brent` solver. Note that this is in most cases not
  904. critical; the final result is allowed to be outside this bracket.
  905. If `optimizer` is passed, `brack` must be None.
  906. method : str, optional
  907. The method to determine the optimal transform parameter (`boxcox`
  908. ``lmbda`` parameter). Options are:
  909. 'pearsonr' (default)
  910. Maximizes the Pearson correlation coefficient between
  911. ``y = boxcox(x)`` and the expected values for ``y`` if `x` would be
  912. normally-distributed.
  913. 'mle'
  914. Minimizes the log-likelihood `boxcox_llf`. This is the method used
  915. in `boxcox`.
  916. 'all'
  917. Use all optimization methods available, and return all results.
  918. Useful to compare different methods.
  919. optimizer : callable, optional
  920. `optimizer` is a callable that accepts one argument:
  921. fun : callable
  922. The objective function to be optimized. `fun` accepts one argument,
  923. the Box-Cox transform parameter `lmbda`, and returns the negative
  924. log-likelihood function at the provided value. The job of `optimizer`
  925. is to find the value of `lmbda` that minimizes `fun`.
  926. and returns an object, such as an instance of
  927. `scipy.optimize.OptimizeResult`, which holds the optimal value of
  928. `lmbda` in an attribute `x`.
  929. See the example below or the documentation of
  930. `scipy.optimize.minimize_scalar` for more information.
  931. Returns
  932. -------
  933. maxlog : float or ndarray
  934. The optimal transform parameter found. An array instead of a scalar
  935. for ``method='all'``.
  936. See Also
  937. --------
  938. boxcox, boxcox_llf, boxcox_normplot, scipy.optimize.minimize_scalar
  939. Examples
  940. --------
  941. >>> import numpy as np
  942. >>> from scipy import stats
  943. >>> import matplotlib.pyplot as plt
  944. We can generate some data and determine the optimal ``lmbda`` in various
  945. ways:
  946. >>> rng = np.random.default_rng()
  947. >>> x = stats.loggamma.rvs(5, size=30, random_state=rng) + 5
  948. >>> y, lmax_mle = stats.boxcox(x)
  949. >>> lmax_pearsonr = stats.boxcox_normmax(x)
  950. >>> lmax_mle
  951. 2.217563431465757
  952. >>> lmax_pearsonr
  953. 2.238318660200961
  954. >>> stats.boxcox_normmax(x, method='all')
  955. array([2.23831866, 2.21756343])
  956. >>> fig = plt.figure()
  957. >>> ax = fig.add_subplot(111)
  958. >>> prob = stats.boxcox_normplot(x, -10, 10, plot=ax)
  959. >>> ax.axvline(lmax_mle, color='r')
  960. >>> ax.axvline(lmax_pearsonr, color='g', ls='--')
  961. >>> plt.show()
  962. Alternatively, we can define our own `optimizer` function. Suppose we
  963. are only interested in values of `lmbda` on the interval [6, 7], we
  964. want to use `scipy.optimize.minimize_scalar` with ``method='bounded'``,
  965. and we want to use tighter tolerances when optimizing the log-likelihood
  966. function. To do this, we define a function that accepts positional argument
  967. `fun` and uses `scipy.optimize.minimize_scalar` to minimize `fun` subject
  968. to the provided bounds and tolerances:
  969. >>> from scipy import optimize
  970. >>> options = {'xatol': 1e-12} # absolute tolerance on `x`
  971. >>> def optimizer(fun):
  972. ... return optimize.minimize_scalar(fun, bounds=(6, 7),
  973. ... method="bounded", options=options)
  974. >>> stats.boxcox_normmax(x, optimizer=optimizer)
  975. 6.000...
  976. """
  977. # If optimizer is not given, define default 'brent' optimizer.
  978. if optimizer is None:
  979. # Set default value for `brack`.
  980. if brack is None:
  981. brack = (-2.0, 2.0)
  982. def _optimizer(func, args):
  983. return optimize.brent(func, args=args, brack=brack)
  984. # Otherwise check optimizer.
  985. else:
  986. if not callable(optimizer):
  987. raise ValueError("`optimizer` must be a callable")
  988. if brack is not None:
  989. raise ValueError("`brack` must be None if `optimizer` is given")
  990. # `optimizer` is expected to return a `OptimizeResult` object, we here
  991. # get the solution to the optimization problem.
  992. def _optimizer(func, args):
  993. def func_wrapped(x):
  994. return func(x, *args)
  995. return getattr(optimizer(func_wrapped), 'x', None)
  996. def _pearsonr(x):
  997. osm_uniform = _calc_uniform_order_statistic_medians(len(x))
  998. xvals = distributions.norm.ppf(osm_uniform)
  999. def _eval_pearsonr(lmbda, xvals, samps):
  1000. # This function computes the x-axis values of the probability plot
  1001. # and computes a linear regression (including the correlation) and
  1002. # returns ``1 - r`` so that a minimization function maximizes the
  1003. # correlation.
  1004. y = boxcox(samps, lmbda)
  1005. yvals = np.sort(y)
  1006. r, prob = _stats_py.pearsonr(xvals, yvals)
  1007. return 1 - r
  1008. return _optimizer(_eval_pearsonr, args=(xvals, x))
  1009. def _mle(x):
  1010. def _eval_mle(lmb, data):
  1011. # function to minimize
  1012. return -boxcox_llf(lmb, data)
  1013. return _optimizer(_eval_mle, args=(x,))
  1014. def _all(x):
  1015. maxlog = np.empty(2, dtype=float)
  1016. maxlog[0] = _pearsonr(x)
  1017. maxlog[1] = _mle(x)
  1018. return maxlog
  1019. methods = {'pearsonr': _pearsonr,
  1020. 'mle': _mle,
  1021. 'all': _all}
  1022. if method not in methods.keys():
  1023. raise ValueError("Method %s not recognized." % method)
  1024. optimfunc = methods[method]
  1025. res = optimfunc(x)
  1026. if res is None:
  1027. message = ("`optimizer` must return an object containing the optimal "
  1028. "`lmbda` in attribute `x`")
  1029. raise ValueError(message)
  1030. return res
  1031. def _normplot(method, x, la, lb, plot=None, N=80):
  1032. """Compute parameters for a Box-Cox or Yeo-Johnson normality plot,
  1033. optionally show it.
  1034. See `boxcox_normplot` or `yeojohnson_normplot` for details.
  1035. """
  1036. if method == 'boxcox':
  1037. title = 'Box-Cox Normality Plot'
  1038. transform_func = boxcox
  1039. else:
  1040. title = 'Yeo-Johnson Normality Plot'
  1041. transform_func = yeojohnson
  1042. x = np.asarray(x)
  1043. if x.size == 0:
  1044. return x
  1045. if lb <= la:
  1046. raise ValueError("`lb` has to be larger than `la`.")
  1047. if method == 'boxcox' and np.any(x <= 0):
  1048. raise ValueError("Data must be positive.")
  1049. lmbdas = np.linspace(la, lb, num=N)
  1050. ppcc = lmbdas * 0.0
  1051. for i, val in enumerate(lmbdas):
  1052. # Determine for each lmbda the square root of correlation coefficient
  1053. # of transformed x
  1054. z = transform_func(x, lmbda=val)
  1055. _, (_, _, r) = probplot(z, dist='norm', fit=True)
  1056. ppcc[i] = r
  1057. if plot is not None:
  1058. plot.plot(lmbdas, ppcc, 'x')
  1059. _add_axis_labels_title(plot, xlabel='$\\lambda$',
  1060. ylabel='Prob Plot Corr. Coef.',
  1061. title=title)
  1062. return lmbdas, ppcc
  1063. def boxcox_normplot(x, la, lb, plot=None, N=80):
  1064. """Compute parameters for a Box-Cox normality plot, optionally show it.
  1065. A Box-Cox normality plot shows graphically what the best transformation
  1066. parameter is to use in `boxcox` to obtain a distribution that is close
  1067. to normal.
  1068. Parameters
  1069. ----------
  1070. x : array_like
  1071. Input array.
  1072. la, lb : scalar
  1073. The lower and upper bounds for the ``lmbda`` values to pass to `boxcox`
  1074. for Box-Cox transformations. These are also the limits of the
  1075. horizontal axis of the plot if that is generated.
  1076. plot : object, optional
  1077. If given, plots the quantiles and least squares fit.
  1078. `plot` is an object that has to have methods "plot" and "text".
  1079. The `matplotlib.pyplot` module or a Matplotlib Axes object can be used,
  1080. or a custom object with the same methods.
  1081. Default is None, which means that no plot is created.
  1082. N : int, optional
  1083. Number of points on the horizontal axis (equally distributed from
  1084. `la` to `lb`).
  1085. Returns
  1086. -------
  1087. lmbdas : ndarray
  1088. The ``lmbda`` values for which a Box-Cox transform was done.
  1089. ppcc : ndarray
  1090. Probability Plot Correlelation Coefficient, as obtained from `probplot`
  1091. when fitting the Box-Cox transformed input `x` against a normal
  1092. distribution.
  1093. See Also
  1094. --------
  1095. probplot, boxcox, boxcox_normmax, boxcox_llf, ppcc_max
  1096. Notes
  1097. -----
  1098. Even if `plot` is given, the figure is not shown or saved by
  1099. `boxcox_normplot`; ``plt.show()`` or ``plt.savefig('figname.png')``
  1100. should be used after calling `probplot`.
  1101. Examples
  1102. --------
  1103. >>> from scipy import stats
  1104. >>> import matplotlib.pyplot as plt
  1105. Generate some non-normally distributed data, and create a Box-Cox plot:
  1106. >>> x = stats.loggamma.rvs(5, size=500) + 5
  1107. >>> fig = plt.figure()
  1108. >>> ax = fig.add_subplot(111)
  1109. >>> prob = stats.boxcox_normplot(x, -20, 20, plot=ax)
  1110. Determine and plot the optimal ``lmbda`` to transform ``x`` and plot it in
  1111. the same plot:
  1112. >>> _, maxlog = stats.boxcox(x)
  1113. >>> ax.axvline(maxlog, color='r')
  1114. >>> plt.show()
  1115. """
  1116. return _normplot('boxcox', x, la, lb, plot, N)
  1117. def yeojohnson(x, lmbda=None):
  1118. r"""Return a dataset transformed by a Yeo-Johnson power transformation.
  1119. Parameters
  1120. ----------
  1121. x : ndarray
  1122. Input array. Should be 1-dimensional.
  1123. lmbda : float, optional
  1124. If ``lmbda`` is ``None``, find the lambda that maximizes the
  1125. log-likelihood function and return it as the second output argument.
  1126. Otherwise the transformation is done for the given value.
  1127. Returns
  1128. -------
  1129. yeojohnson: ndarray
  1130. Yeo-Johnson power transformed array.
  1131. maxlog : float, optional
  1132. If the `lmbda` parameter is None, the second returned argument is
  1133. the lambda that maximizes the log-likelihood function.
  1134. See Also
  1135. --------
  1136. probplot, yeojohnson_normplot, yeojohnson_normmax, yeojohnson_llf, boxcox
  1137. Notes
  1138. -----
  1139. The Yeo-Johnson transform is given by::
  1140. y = ((x + 1)**lmbda - 1) / lmbda, for x >= 0, lmbda != 0
  1141. log(x + 1), for x >= 0, lmbda = 0
  1142. -((-x + 1)**(2 - lmbda) - 1) / (2 - lmbda), for x < 0, lmbda != 2
  1143. -log(-x + 1), for x < 0, lmbda = 2
  1144. Unlike `boxcox`, `yeojohnson` does not require the input data to be
  1145. positive.
  1146. .. versionadded:: 1.2.0
  1147. References
  1148. ----------
  1149. I. Yeo and R.A. Johnson, "A New Family of Power Transformations to
  1150. Improve Normality or Symmetry", Biometrika 87.4 (2000):
  1151. Examples
  1152. --------
  1153. >>> from scipy import stats
  1154. >>> import matplotlib.pyplot as plt
  1155. We generate some random variates from a non-normal distribution and make a
  1156. probability plot for it, to show it is non-normal in the tails:
  1157. >>> fig = plt.figure()
  1158. >>> ax1 = fig.add_subplot(211)
  1159. >>> x = stats.loggamma.rvs(5, size=500) + 5
  1160. >>> prob = stats.probplot(x, dist=stats.norm, plot=ax1)
  1161. >>> ax1.set_xlabel('')
  1162. >>> ax1.set_title('Probplot against normal distribution')
  1163. We now use `yeojohnson` to transform the data so it's closest to normal:
  1164. >>> ax2 = fig.add_subplot(212)
  1165. >>> xt, lmbda = stats.yeojohnson(x)
  1166. >>> prob = stats.probplot(xt, dist=stats.norm, plot=ax2)
  1167. >>> ax2.set_title('Probplot after Yeo-Johnson transformation')
  1168. >>> plt.show()
  1169. """
  1170. x = np.asarray(x)
  1171. if x.size == 0:
  1172. return x
  1173. if np.issubdtype(x.dtype, np.complexfloating):
  1174. raise ValueError('Yeo-Johnson transformation is not defined for '
  1175. 'complex numbers.')
  1176. if np.issubdtype(x.dtype, np.integer):
  1177. x = x.astype(np.float64, copy=False)
  1178. if lmbda is not None:
  1179. return _yeojohnson_transform(x, lmbda)
  1180. # if lmbda=None, find the lmbda that maximizes the log-likelihood function.
  1181. lmax = yeojohnson_normmax(x)
  1182. y = _yeojohnson_transform(x, lmax)
  1183. return y, lmax
  1184. def _yeojohnson_transform(x, lmbda):
  1185. """Returns `x` transformed by the Yeo-Johnson power transform with given
  1186. parameter `lmbda`.
  1187. """
  1188. out = np.zeros_like(x)
  1189. pos = x >= 0 # binary mask
  1190. # when x >= 0
  1191. if abs(lmbda) < np.spacing(1.):
  1192. out[pos] = np.log1p(x[pos])
  1193. else: # lmbda != 0
  1194. out[pos] = (np.power(x[pos] + 1, lmbda) - 1) / lmbda
  1195. # when x < 0
  1196. if abs(lmbda - 2) > np.spacing(1.):
  1197. out[~pos] = -(np.power(-x[~pos] + 1, 2 - lmbda) - 1) / (2 - lmbda)
  1198. else: # lmbda == 2
  1199. out[~pos] = -np.log1p(-x[~pos])
  1200. return out
  1201. def yeojohnson_llf(lmb, data):
  1202. r"""The yeojohnson log-likelihood function.
  1203. Parameters
  1204. ----------
  1205. lmb : scalar
  1206. Parameter for Yeo-Johnson transformation. See `yeojohnson` for
  1207. details.
  1208. data : array_like
  1209. Data to calculate Yeo-Johnson log-likelihood for. If `data` is
  1210. multi-dimensional, the log-likelihood is calculated along the first
  1211. axis.
  1212. Returns
  1213. -------
  1214. llf : float
  1215. Yeo-Johnson log-likelihood of `data` given `lmb`.
  1216. See Also
  1217. --------
  1218. yeojohnson, probplot, yeojohnson_normplot, yeojohnson_normmax
  1219. Notes
  1220. -----
  1221. The Yeo-Johnson log-likelihood function is defined here as
  1222. .. math::
  1223. llf = -N/2 \log(\hat{\sigma}^2) + (\lambda - 1)
  1224. \sum_i \text{ sign }(x_i)\log(|x_i| + 1)
  1225. where :math:`\hat{\sigma}^2` is estimated variance of the Yeo-Johnson
  1226. transformed input data ``x``.
  1227. .. versionadded:: 1.2.0
  1228. Examples
  1229. --------
  1230. >>> import numpy as np
  1231. >>> from scipy import stats
  1232. >>> import matplotlib.pyplot as plt
  1233. >>> from mpl_toolkits.axes_grid1.inset_locator import inset_axes
  1234. Generate some random variates and calculate Yeo-Johnson log-likelihood
  1235. values for them for a range of ``lmbda`` values:
  1236. >>> x = stats.loggamma.rvs(5, loc=10, size=1000)
  1237. >>> lmbdas = np.linspace(-2, 10)
  1238. >>> llf = np.zeros(lmbdas.shape, dtype=float)
  1239. >>> for ii, lmbda in enumerate(lmbdas):
  1240. ... llf[ii] = stats.yeojohnson_llf(lmbda, x)
  1241. Also find the optimal lmbda value with `yeojohnson`:
  1242. >>> x_most_normal, lmbda_optimal = stats.yeojohnson(x)
  1243. Plot the log-likelihood as function of lmbda. Add the optimal lmbda as a
  1244. horizontal line to check that that's really the optimum:
  1245. >>> fig = plt.figure()
  1246. >>> ax = fig.add_subplot(111)
  1247. >>> ax.plot(lmbdas, llf, 'b.-')
  1248. >>> ax.axhline(stats.yeojohnson_llf(lmbda_optimal, x), color='r')
  1249. >>> ax.set_xlabel('lmbda parameter')
  1250. >>> ax.set_ylabel('Yeo-Johnson log-likelihood')
  1251. Now add some probability plots to show that where the log-likelihood is
  1252. maximized the data transformed with `yeojohnson` looks closest to normal:
  1253. >>> locs = [3, 10, 4] # 'lower left', 'center', 'lower right'
  1254. >>> for lmbda, loc in zip([-1, lmbda_optimal, 9], locs):
  1255. ... xt = stats.yeojohnson(x, lmbda=lmbda)
  1256. ... (osm, osr), (slope, intercept, r_sq) = stats.probplot(xt)
  1257. ... ax_inset = inset_axes(ax, width="20%", height="20%", loc=loc)
  1258. ... ax_inset.plot(osm, osr, 'c.', osm, slope*osm + intercept, 'k-')
  1259. ... ax_inset.set_xticklabels([])
  1260. ... ax_inset.set_yticklabels([])
  1261. ... ax_inset.set_title(r'$\lambda=%1.2f$' % lmbda)
  1262. >>> plt.show()
  1263. """
  1264. data = np.asarray(data)
  1265. n_samples = data.shape[0]
  1266. if n_samples == 0:
  1267. return np.nan
  1268. trans = _yeojohnson_transform(data, lmb)
  1269. trans_var = trans.var(axis=0)
  1270. loglike = np.empty_like(trans_var)
  1271. # Avoid RuntimeWarning raised by np.log when the variance is too low
  1272. tiny_variance = trans_var < np.finfo(trans_var.dtype).tiny
  1273. loglike[tiny_variance] = np.inf
  1274. loglike[~tiny_variance] = (
  1275. -n_samples / 2 * np.log(trans_var[~tiny_variance]))
  1276. loglike[~tiny_variance] += (
  1277. (lmb - 1) * (np.sign(data) * np.log(np.abs(data) + 1)).sum(axis=0))
  1278. return loglike
  1279. def yeojohnson_normmax(x, brack=(-2, 2)):
  1280. """Compute optimal Yeo-Johnson transform parameter.
  1281. Compute optimal Yeo-Johnson transform parameter for input data, using
  1282. maximum likelihood estimation.
  1283. Parameters
  1284. ----------
  1285. x : array_like
  1286. Input array.
  1287. brack : 2-tuple, optional
  1288. The starting interval for a downhill bracket search with
  1289. `optimize.brent`. Note that this is in most cases not critical; the
  1290. final result is allowed to be outside this bracket.
  1291. Returns
  1292. -------
  1293. maxlog : float
  1294. The optimal transform parameter found.
  1295. See Also
  1296. --------
  1297. yeojohnson, yeojohnson_llf, yeojohnson_normplot
  1298. Notes
  1299. -----
  1300. .. versionadded:: 1.2.0
  1301. Examples
  1302. --------
  1303. >>> import numpy as np
  1304. >>> from scipy import stats
  1305. >>> import matplotlib.pyplot as plt
  1306. Generate some data and determine optimal ``lmbda``
  1307. >>> rng = np.random.default_rng()
  1308. >>> x = stats.loggamma.rvs(5, size=30, random_state=rng) + 5
  1309. >>> lmax = stats.yeojohnson_normmax(x)
  1310. >>> fig = plt.figure()
  1311. >>> ax = fig.add_subplot(111)
  1312. >>> prob = stats.yeojohnson_normplot(x, -10, 10, plot=ax)
  1313. >>> ax.axvline(lmax, color='r')
  1314. >>> plt.show()
  1315. """
  1316. def _neg_llf(lmbda, data):
  1317. llf = yeojohnson_llf(lmbda, data)
  1318. # reject likelihoods that are inf which are likely due to small
  1319. # variance in the transformed space
  1320. llf[np.isinf(llf)] = -np.inf
  1321. return -llf
  1322. with np.errstate(invalid='ignore'):
  1323. return optimize.brent(_neg_llf, brack=brack, args=(x,))
  1324. def yeojohnson_normplot(x, la, lb, plot=None, N=80):
  1325. """Compute parameters for a Yeo-Johnson normality plot, optionally show it.
  1326. A Yeo-Johnson normality plot shows graphically what the best
  1327. transformation parameter is to use in `yeojohnson` to obtain a
  1328. distribution that is close to normal.
  1329. Parameters
  1330. ----------
  1331. x : array_like
  1332. Input array.
  1333. la, lb : scalar
  1334. The lower and upper bounds for the ``lmbda`` values to pass to
  1335. `yeojohnson` for Yeo-Johnson transformations. These are also the
  1336. limits of the horizontal axis of the plot if that is generated.
  1337. plot : object, optional
  1338. If given, plots the quantiles and least squares fit.
  1339. `plot` is an object that has to have methods "plot" and "text".
  1340. The `matplotlib.pyplot` module or a Matplotlib Axes object can be used,
  1341. or a custom object with the same methods.
  1342. Default is None, which means that no plot is created.
  1343. N : int, optional
  1344. Number of points on the horizontal axis (equally distributed from
  1345. `la` to `lb`).
  1346. Returns
  1347. -------
  1348. lmbdas : ndarray
  1349. The ``lmbda`` values for which a Yeo-Johnson transform was done.
  1350. ppcc : ndarray
  1351. Probability Plot Correlelation Coefficient, as obtained from `probplot`
  1352. when fitting the Box-Cox transformed input `x` against a normal
  1353. distribution.
  1354. See Also
  1355. --------
  1356. probplot, yeojohnson, yeojohnson_normmax, yeojohnson_llf, ppcc_max
  1357. Notes
  1358. -----
  1359. Even if `plot` is given, the figure is not shown or saved by
  1360. `boxcox_normplot`; ``plt.show()`` or ``plt.savefig('figname.png')``
  1361. should be used after calling `probplot`.
  1362. .. versionadded:: 1.2.0
  1363. Examples
  1364. --------
  1365. >>> from scipy import stats
  1366. >>> import matplotlib.pyplot as plt
  1367. Generate some non-normally distributed data, and create a Yeo-Johnson plot:
  1368. >>> x = stats.loggamma.rvs(5, size=500) + 5
  1369. >>> fig = plt.figure()
  1370. >>> ax = fig.add_subplot(111)
  1371. >>> prob = stats.yeojohnson_normplot(x, -20, 20, plot=ax)
  1372. Determine and plot the optimal ``lmbda`` to transform ``x`` and plot it in
  1373. the same plot:
  1374. >>> _, maxlog = stats.yeojohnson(x)
  1375. >>> ax.axvline(maxlog, color='r')
  1376. >>> plt.show()
  1377. """
  1378. return _normplot('yeojohnson', x, la, lb, plot, N)
  1379. ShapiroResult = namedtuple('ShapiroResult', ('statistic', 'pvalue'))
  1380. def shapiro(x):
  1381. """Perform the Shapiro-Wilk test for normality.
  1382. The Shapiro-Wilk test tests the null hypothesis that the
  1383. data was drawn from a normal distribution.
  1384. Parameters
  1385. ----------
  1386. x : array_like
  1387. Array of sample data.
  1388. Returns
  1389. -------
  1390. statistic : float
  1391. The test statistic.
  1392. p-value : float
  1393. The p-value for the hypothesis test.
  1394. See Also
  1395. --------
  1396. anderson : The Anderson-Darling test for normality
  1397. kstest : The Kolmogorov-Smirnov test for goodness of fit.
  1398. Notes
  1399. -----
  1400. The algorithm used is described in [4]_ but censoring parameters as
  1401. described are not implemented. For N > 5000 the W test statistic is accurate
  1402. but the p-value may not be.
  1403. The chance of rejecting the null hypothesis when it is true is close to 5%
  1404. regardless of sample size.
  1405. References
  1406. ----------
  1407. .. [1] https://www.itl.nist.gov/div898/handbook/prc/section2/prc213.htm
  1408. .. [2] Shapiro, S. S. & Wilk, M.B (1965). An analysis of variance test for
  1409. normality (complete samples), Biometrika, Vol. 52, pp. 591-611.
  1410. .. [3] Razali, N. M. & Wah, Y. B. (2011) Power comparisons of Shapiro-Wilk,
  1411. Kolmogorov-Smirnov, Lilliefors and Anderson-Darling tests, Journal of
  1412. Statistical Modeling and Analytics, Vol. 2, pp. 21-33.
  1413. .. [4] ALGORITHM AS R94 APPL. STATIST. (1995) VOL. 44, NO. 4.
  1414. Examples
  1415. --------
  1416. >>> import numpy as np
  1417. >>> from scipy import stats
  1418. >>> rng = np.random.default_rng()
  1419. >>> x = stats.norm.rvs(loc=5, scale=3, size=100, random_state=rng)
  1420. >>> shapiro_test = stats.shapiro(x)
  1421. >>> shapiro_test
  1422. ShapiroResult(statistic=0.9813305735588074, pvalue=0.16855233907699585)
  1423. >>> shapiro_test.statistic
  1424. 0.9813305735588074
  1425. >>> shapiro_test.pvalue
  1426. 0.16855233907699585
  1427. """
  1428. x = np.ravel(x)
  1429. N = len(x)
  1430. if N < 3:
  1431. raise ValueError("Data must be at least length 3.")
  1432. x = x - np.median(x)
  1433. a = zeros(N, 'f')
  1434. init = 0
  1435. y = sort(x)
  1436. a, w, pw, ifault = _statlib.swilk(y, a[:N//2], init)
  1437. if ifault not in [0, 2]:
  1438. warnings.warn("Input data for shapiro has range zero. The results "
  1439. "may not be accurate.")
  1440. if N > 5000:
  1441. warnings.warn("p-value may not be accurate for N > 5000.")
  1442. return ShapiroResult(w, pw)
  1443. # Values from Stephens, M A, "EDF Statistics for Goodness of Fit and
  1444. # Some Comparisons", Journal of the American Statistical
  1445. # Association, Vol. 69, Issue 347, Sept. 1974, pp 730-737
  1446. _Avals_norm = array([0.576, 0.656, 0.787, 0.918, 1.092])
  1447. _Avals_expon = array([0.922, 1.078, 1.341, 1.606, 1.957])
  1448. # From Stephens, M A, "Goodness of Fit for the Extreme Value Distribution",
  1449. # Biometrika, Vol. 64, Issue 3, Dec. 1977, pp 583-588.
  1450. _Avals_gumbel = array([0.474, 0.637, 0.757, 0.877, 1.038])
  1451. # From Stephens, M A, "Tests of Fit for the Logistic Distribution Based
  1452. # on the Empirical Distribution Function.", Biometrika,
  1453. # Vol. 66, Issue 3, Dec. 1979, pp 591-595.
  1454. _Avals_logistic = array([0.426, 0.563, 0.660, 0.769, 0.906, 1.010])
  1455. AndersonResult = _make_tuple_bunch('AndersonResult',
  1456. ['statistic', 'critical_values',
  1457. 'significance_level'], ['fit_result'])
  1458. def anderson(x, dist='norm'):
  1459. """Anderson-Darling test for data coming from a particular distribution.
  1460. The Anderson-Darling test tests the null hypothesis that a sample is
  1461. drawn from a population that follows a particular distribution.
  1462. For the Anderson-Darling test, the critical values depend on
  1463. which distribution is being tested against. This function works
  1464. for normal, exponential, logistic, or Gumbel (Extreme Value
  1465. Type I) distributions.
  1466. Parameters
  1467. ----------
  1468. x : array_like
  1469. Array of sample data.
  1470. dist : {'norm', 'expon', 'logistic', 'gumbel', 'gumbel_l', 'gumbel_r', 'extreme1'}, optional
  1471. The type of distribution to test against. The default is 'norm'.
  1472. The names 'extreme1', 'gumbel_l' and 'gumbel' are synonyms for the
  1473. same distribution.
  1474. Returns
  1475. -------
  1476. result : AndersonResult
  1477. An object with the following attributes:
  1478. statistic : float
  1479. The Anderson-Darling test statistic.
  1480. critical_values : list
  1481. The critical values for this distribution.
  1482. significance_level : list
  1483. The significance levels for the corresponding critical values
  1484. in percents. The function returns critical values for a
  1485. differing set of significance levels depending on the
  1486. distribution that is being tested against.
  1487. fit_result : `~scipy.stats._result_classes.FitResult`
  1488. An object containing the results of fitting the distribution to
  1489. the data.
  1490. See Also
  1491. --------
  1492. kstest : The Kolmogorov-Smirnov test for goodness-of-fit.
  1493. Notes
  1494. -----
  1495. Critical values provided are for the following significance levels:
  1496. normal/exponential
  1497. 15%, 10%, 5%, 2.5%, 1%
  1498. logistic
  1499. 25%, 10%, 5%, 2.5%, 1%, 0.5%
  1500. Gumbel
  1501. 25%, 10%, 5%, 2.5%, 1%
  1502. If the returned statistic is larger than these critical values then
  1503. for the corresponding significance level, the null hypothesis that
  1504. the data come from the chosen distribution can be rejected.
  1505. The returned statistic is referred to as 'A2' in the references.
  1506. References
  1507. ----------
  1508. .. [1] https://www.itl.nist.gov/div898/handbook/prc/section2/prc213.htm
  1509. .. [2] Stephens, M. A. (1974). EDF Statistics for Goodness of Fit and
  1510. Some Comparisons, Journal of the American Statistical Association,
  1511. Vol. 69, pp. 730-737.
  1512. .. [3] Stephens, M. A. (1976). Asymptotic Results for Goodness-of-Fit
  1513. Statistics with Unknown Parameters, Annals of Statistics, Vol. 4,
  1514. pp. 357-369.
  1515. .. [4] Stephens, M. A. (1977). Goodness of Fit for the Extreme Value
  1516. Distribution, Biometrika, Vol. 64, pp. 583-588.
  1517. .. [5] Stephens, M. A. (1977). Goodness of Fit with Special Reference
  1518. to Tests for Exponentiality , Technical Report No. 262,
  1519. Department of Statistics, Stanford University, Stanford, CA.
  1520. .. [6] Stephens, M. A. (1979). Tests of Fit for the Logistic Distribution
  1521. Based on the Empirical Distribution Function, Biometrika, Vol. 66,
  1522. pp. 591-595.
  1523. Examples
  1524. --------
  1525. Test the null hypothesis that a random sample was drawn from a normal
  1526. distribution (with unspecified mean and standard deviation).
  1527. >>> import numpy as np
  1528. >>> from scipy.stats import anderson
  1529. >>> rng = np.random.default_rng()
  1530. >>> data = rng.random(size=35)
  1531. >>> res = anderson(data)
  1532. >>> res.statistic
  1533. 0.8398018749744764
  1534. >>> res.critical_values
  1535. array([0.527, 0.6 , 0.719, 0.839, 0.998])
  1536. >>> res.significance_level
  1537. array([15. , 10. , 5. , 2.5, 1. ])
  1538. The value of the statistic (barely) exceeds the critical value associated
  1539. with a significance level of 2.5%, so the null hypothesis may be rejected
  1540. at a significance level of 2.5%, but not at a significance level of 1%.
  1541. """ # noqa
  1542. dist = dist.lower()
  1543. if dist in {'extreme1', 'gumbel'}:
  1544. dist = 'gumbel_l'
  1545. dists = {'norm', 'expon', 'gumbel_l', 'gumbel_r', 'logistic'}
  1546. if dist not in dists:
  1547. raise ValueError(f"Invalid distribution; dist must be in {dists}.")
  1548. y = sort(x)
  1549. xbar = np.mean(x, axis=0)
  1550. N = len(y)
  1551. if dist == 'norm':
  1552. s = np.std(x, ddof=1, axis=0)
  1553. w = (y - xbar) / s
  1554. fit_params = xbar, s
  1555. logcdf = distributions.norm.logcdf(w)
  1556. logsf = distributions.norm.logsf(w)
  1557. sig = array([15, 10, 5, 2.5, 1])
  1558. critical = around(_Avals_norm / (1.0 + 4.0/N - 25.0/N/N), 3)
  1559. elif dist == 'expon':
  1560. w = y / xbar
  1561. fit_params = 0, xbar
  1562. logcdf = distributions.expon.logcdf(w)
  1563. logsf = distributions.expon.logsf(w)
  1564. sig = array([15, 10, 5, 2.5, 1])
  1565. critical = around(_Avals_expon / (1.0 + 0.6/N), 3)
  1566. elif dist == 'logistic':
  1567. def rootfunc(ab, xj, N):
  1568. a, b = ab
  1569. tmp = (xj - a) / b
  1570. tmp2 = exp(tmp)
  1571. val = [np.sum(1.0/(1+tmp2), axis=0) - 0.5*N,
  1572. np.sum(tmp*(1.0-tmp2)/(1+tmp2), axis=0) + N]
  1573. return array(val)
  1574. sol0 = array([xbar, np.std(x, ddof=1, axis=0)])
  1575. sol = optimize.fsolve(rootfunc, sol0, args=(x, N), xtol=1e-5)
  1576. w = (y - sol[0]) / sol[1]
  1577. fit_params = sol
  1578. logcdf = distributions.logistic.logcdf(w)
  1579. logsf = distributions.logistic.logsf(w)
  1580. sig = array([25, 10, 5, 2.5, 1, 0.5])
  1581. critical = around(_Avals_logistic / (1.0 + 0.25/N), 3)
  1582. elif dist == 'gumbel_r':
  1583. xbar, s = distributions.gumbel_r.fit(x)
  1584. w = (y - xbar) / s
  1585. fit_params = xbar, s
  1586. logcdf = distributions.gumbel_r.logcdf(w)
  1587. logsf = distributions.gumbel_r.logsf(w)
  1588. sig = array([25, 10, 5, 2.5, 1])
  1589. critical = around(_Avals_gumbel / (1.0 + 0.2/sqrt(N)), 3)
  1590. elif dist == 'gumbel_l':
  1591. xbar, s = distributions.gumbel_l.fit(x)
  1592. w = (y - xbar) / s
  1593. fit_params = xbar, s
  1594. logcdf = distributions.gumbel_l.logcdf(w)
  1595. logsf = distributions.gumbel_l.logsf(w)
  1596. sig = array([25, 10, 5, 2.5, 1])
  1597. critical = around(_Avals_gumbel / (1.0 + 0.2/sqrt(N)), 3)
  1598. i = arange(1, N + 1)
  1599. A2 = -N - np.sum((2*i - 1.0) / N * (logcdf + logsf[::-1]), axis=0)
  1600. # FitResult initializer expects an optimize result, so let's work with it
  1601. message = '`anderson` successfully fit the distribution to the data.'
  1602. res = optimize.OptimizeResult(success=True, message=message)
  1603. res.x = np.array(fit_params)
  1604. fit_result = FitResult(getattr(distributions, dist), y,
  1605. discrete=False, res=res)
  1606. return AndersonResult(A2, critical, sig, fit_result=fit_result)
  1607. def _anderson_ksamp_midrank(samples, Z, Zstar, k, n, N):
  1608. """Compute A2akN equation 7 of Scholz and Stephens.
  1609. Parameters
  1610. ----------
  1611. samples : sequence of 1-D array_like
  1612. Array of sample arrays.
  1613. Z : array_like
  1614. Sorted array of all observations.
  1615. Zstar : array_like
  1616. Sorted array of unique observations.
  1617. k : int
  1618. Number of samples.
  1619. n : array_like
  1620. Number of observations in each sample.
  1621. N : int
  1622. Total number of observations.
  1623. Returns
  1624. -------
  1625. A2aKN : float
  1626. The A2aKN statistics of Scholz and Stephens 1987.
  1627. """
  1628. A2akN = 0.
  1629. Z_ssorted_left = Z.searchsorted(Zstar, 'left')
  1630. if N == Zstar.size:
  1631. lj = 1.
  1632. else:
  1633. lj = Z.searchsorted(Zstar, 'right') - Z_ssorted_left
  1634. Bj = Z_ssorted_left + lj / 2.
  1635. for i in arange(0, k):
  1636. s = np.sort(samples[i])
  1637. s_ssorted_right = s.searchsorted(Zstar, side='right')
  1638. Mij = s_ssorted_right.astype(float)
  1639. fij = s_ssorted_right - s.searchsorted(Zstar, 'left')
  1640. Mij -= fij / 2.
  1641. inner = lj / float(N) * (N*Mij - Bj*n[i])**2 / (Bj*(N - Bj) - N*lj/4.)
  1642. A2akN += inner.sum() / n[i]
  1643. A2akN *= (N - 1.) / N
  1644. return A2akN
  1645. def _anderson_ksamp_right(samples, Z, Zstar, k, n, N):
  1646. """Compute A2akN equation 6 of Scholz & Stephens.
  1647. Parameters
  1648. ----------
  1649. samples : sequence of 1-D array_like
  1650. Array of sample arrays.
  1651. Z : array_like
  1652. Sorted array of all observations.
  1653. Zstar : array_like
  1654. Sorted array of unique observations.
  1655. k : int
  1656. Number of samples.
  1657. n : array_like
  1658. Number of observations in each sample.
  1659. N : int
  1660. Total number of observations.
  1661. Returns
  1662. -------
  1663. A2KN : float
  1664. The A2KN statistics of Scholz and Stephens 1987.
  1665. """
  1666. A2kN = 0.
  1667. lj = Z.searchsorted(Zstar[:-1], 'right') - Z.searchsorted(Zstar[:-1],
  1668. 'left')
  1669. Bj = lj.cumsum()
  1670. for i in arange(0, k):
  1671. s = np.sort(samples[i])
  1672. Mij = s.searchsorted(Zstar[:-1], side='right')
  1673. inner = lj / float(N) * (N * Mij - Bj * n[i])**2 / (Bj * (N - Bj))
  1674. A2kN += inner.sum() / n[i]
  1675. return A2kN
  1676. Anderson_ksampResult = _make_tuple_bunch(
  1677. 'Anderson_ksampResult',
  1678. ['statistic', 'critical_values', 'pvalue'], []
  1679. )
  1680. def anderson_ksamp(samples, midrank=True):
  1681. """The Anderson-Darling test for k-samples.
  1682. The k-sample Anderson-Darling test is a modification of the
  1683. one-sample Anderson-Darling test. It tests the null hypothesis
  1684. that k-samples are drawn from the same population without having
  1685. to specify the distribution function of that population. The
  1686. critical values depend on the number of samples.
  1687. Parameters
  1688. ----------
  1689. samples : sequence of 1-D array_like
  1690. Array of sample data in arrays.
  1691. midrank : bool, optional
  1692. Type of Anderson-Darling test which is computed. Default
  1693. (True) is the midrank test applicable to continuous and
  1694. discrete populations. If False, the right side empirical
  1695. distribution is used.
  1696. Returns
  1697. -------
  1698. res : Anderson_ksampResult
  1699. An object containing attributes:
  1700. statistic : float
  1701. Normalized k-sample Anderson-Darling test statistic.
  1702. critical_values : array
  1703. The critical values for significance levels 25%, 10%, 5%, 2.5%, 1%,
  1704. 0.5%, 0.1%.
  1705. pvalue : float
  1706. The approximate p-value of the test. The value is floored / capped
  1707. at 0.1% / 25%.
  1708. Raises
  1709. ------
  1710. ValueError
  1711. If less than 2 samples are provided, a sample is empty, or no
  1712. distinct observations are in the samples.
  1713. See Also
  1714. --------
  1715. ks_2samp : 2 sample Kolmogorov-Smirnov test
  1716. anderson : 1 sample Anderson-Darling test
  1717. Notes
  1718. -----
  1719. [1]_ defines three versions of the k-sample Anderson-Darling test:
  1720. one for continuous distributions and two for discrete
  1721. distributions, in which ties between samples may occur. The
  1722. default of this routine is to compute the version based on the
  1723. midrank empirical distribution function. This test is applicable
  1724. to continuous and discrete data. If midrank is set to False, the
  1725. right side empirical distribution is used for a test for discrete
  1726. data. According to [1]_, the two discrete test statistics differ
  1727. only slightly if a few collisions due to round-off errors occur in
  1728. the test not adjusted for ties between samples.
  1729. The critical values corresponding to the significance levels from 0.01
  1730. to 0.25 are taken from [1]_. p-values are floored / capped
  1731. at 0.1% / 25%. Since the range of critical values might be extended in
  1732. future releases, it is recommended not to test ``p == 0.25``, but rather
  1733. ``p >= 0.25`` (analogously for the lower bound).
  1734. .. versionadded:: 0.14.0
  1735. References
  1736. ----------
  1737. .. [1] Scholz, F. W and Stephens, M. A. (1987), K-Sample
  1738. Anderson-Darling Tests, Journal of the American Statistical
  1739. Association, Vol. 82, pp. 918-924.
  1740. Examples
  1741. --------
  1742. >>> import numpy as np
  1743. >>> from scipy import stats
  1744. >>> rng = np.random.default_rng()
  1745. >>> res = stats.anderson_ksamp([rng.normal(size=50),
  1746. ... rng.normal(loc=0.5, size=30)])
  1747. >>> res.statistic, res.pvalue
  1748. (1.974403288713695, 0.04991293614572478)
  1749. >>> res.critical_values
  1750. array([0.325, 1.226, 1.961, 2.718, 3.752, 4.592, 6.546])
  1751. The null hypothesis that the two random samples come from the same
  1752. distribution can be rejected at the 5% level because the returned
  1753. test value is greater than the critical value for 5% (1.961) but
  1754. not at the 2.5% level. The interpolation gives an approximate
  1755. p-value of 4.99%.
  1756. >>> res = stats.anderson_ksamp([rng.normal(size=50),
  1757. ... rng.normal(size=30), rng.normal(size=20)])
  1758. >>> res.statistic, res.pvalue
  1759. (-0.29103725200789504, 0.25)
  1760. >>> res.critical_values
  1761. array([ 0.44925884, 1.3052767 , 1.9434184 , 2.57696569, 3.41634856,
  1762. 4.07210043, 5.56419101])
  1763. The null hypothesis cannot be rejected for three samples from an
  1764. identical distribution. The reported p-value (25%) has been capped and
  1765. may not be very accurate (since it corresponds to the value 0.449
  1766. whereas the statistic is -0.291).
  1767. """
  1768. k = len(samples)
  1769. if (k < 2):
  1770. raise ValueError("anderson_ksamp needs at least two samples")
  1771. samples = list(map(np.asarray, samples))
  1772. Z = np.sort(np.hstack(samples))
  1773. N = Z.size
  1774. Zstar = np.unique(Z)
  1775. if Zstar.size < 2:
  1776. raise ValueError("anderson_ksamp needs more than one distinct "
  1777. "observation")
  1778. n = np.array([sample.size for sample in samples])
  1779. if np.any(n == 0):
  1780. raise ValueError("anderson_ksamp encountered sample without "
  1781. "observations")
  1782. if midrank:
  1783. A2kN = _anderson_ksamp_midrank(samples, Z, Zstar, k, n, N)
  1784. else:
  1785. A2kN = _anderson_ksamp_right(samples, Z, Zstar, k, n, N)
  1786. H = (1. / n).sum()
  1787. hs_cs = (1. / arange(N - 1, 1, -1)).cumsum()
  1788. h = hs_cs[-1] + 1
  1789. g = (hs_cs / arange(2, N)).sum()
  1790. a = (4*g - 6) * (k - 1) + (10 - 6*g)*H
  1791. b = (2*g - 4)*k**2 + 8*h*k + (2*g - 14*h - 4)*H - 8*h + 4*g - 6
  1792. c = (6*h + 2*g - 2)*k**2 + (4*h - 4*g + 6)*k + (2*h - 6)*H + 4*h
  1793. d = (2*h + 6)*k**2 - 4*h*k
  1794. sigmasq = (a*N**3 + b*N**2 + c*N + d) / ((N - 1.) * (N - 2.) * (N - 3.))
  1795. m = k - 1
  1796. A2 = (A2kN - m) / math.sqrt(sigmasq)
  1797. # The b_i values are the interpolation coefficients from Table 2
  1798. # of Scholz and Stephens 1987
  1799. b0 = np.array([0.675, 1.281, 1.645, 1.96, 2.326, 2.573, 3.085])
  1800. b1 = np.array([-0.245, 0.25, 0.678, 1.149, 1.822, 2.364, 3.615])
  1801. b2 = np.array([-0.105, -0.305, -0.362, -0.391, -0.396, -0.345, -0.154])
  1802. critical = b0 + b1 / math.sqrt(m) + b2 / m
  1803. sig = np.array([0.25, 0.1, 0.05, 0.025, 0.01, 0.005, 0.001])
  1804. if A2 < critical.min():
  1805. p = sig.max()
  1806. warnings.warn("p-value capped: true value larger than {}".format(p),
  1807. stacklevel=2)
  1808. elif A2 > critical.max():
  1809. p = sig.min()
  1810. warnings.warn("p-value floored: true value smaller than {}".format(p),
  1811. stacklevel=2)
  1812. else:
  1813. # interpolation of probit of significance level
  1814. pf = np.polyfit(critical, log(sig), 2)
  1815. p = math.exp(np.polyval(pf, A2))
  1816. # create result object with alias for backward compatibility
  1817. res = Anderson_ksampResult(A2, critical, p)
  1818. res.significance_level = p
  1819. return res
  1820. AnsariResult = namedtuple('AnsariResult', ('statistic', 'pvalue'))
  1821. class _ABW:
  1822. """Distribution of Ansari-Bradley W-statistic under the null hypothesis."""
  1823. # TODO: calculate exact distribution considering ties
  1824. # We could avoid summing over more than half the frequencies,
  1825. # but inititally it doesn't seem worth the extra complexity
  1826. def __init__(self):
  1827. """Minimal initializer."""
  1828. self.m = None
  1829. self.n = None
  1830. self.astart = None
  1831. self.total = None
  1832. self.freqs = None
  1833. def _recalc(self, n, m):
  1834. """When necessary, recalculate exact distribution."""
  1835. if n != self.n or m != self.m:
  1836. self.n, self.m = n, m
  1837. # distribution is NOT symmetric when m + n is odd
  1838. # n is len(x), m is len(y), and ratio of scales is defined x/y
  1839. astart, a1, _ = _statlib.gscale(n, m)
  1840. self.astart = astart # minimum value of statistic
  1841. # Exact distribution of test statistic under null hypothesis
  1842. # expressed as frequencies/counts/integers to maintain precision.
  1843. # Stored as floats to avoid overflow of sums.
  1844. self.freqs = a1.astype(np.float64)
  1845. self.total = self.freqs.sum() # could calculate from m and n
  1846. # probability mass is self.freqs / self.total;
  1847. def pmf(self, k, n, m):
  1848. """Probability mass function."""
  1849. self._recalc(n, m)
  1850. # The convention here is that PMF at k = 12.5 is the same as at k = 12,
  1851. # -> use `floor` in case of ties.
  1852. ind = np.floor(k - self.astart).astype(int)
  1853. return self.freqs[ind] / self.total
  1854. def cdf(self, k, n, m):
  1855. """Cumulative distribution function."""
  1856. self._recalc(n, m)
  1857. # Null distribution derived without considering ties is
  1858. # approximate. Round down to avoid Type I error.
  1859. ind = np.ceil(k - self.astart).astype(int)
  1860. return self.freqs[:ind+1].sum() / self.total
  1861. def sf(self, k, n, m):
  1862. """Survival function."""
  1863. self._recalc(n, m)
  1864. # Null distribution derived without considering ties is
  1865. # approximate. Round down to avoid Type I error.
  1866. ind = np.floor(k - self.astart).astype(int)
  1867. return self.freqs[ind:].sum() / self.total
  1868. # Maintain state for faster repeat calls to ansari w/ method='exact'
  1869. _abw_state = _ABW()
  1870. def ansari(x, y, alternative='two-sided'):
  1871. """Perform the Ansari-Bradley test for equal scale parameters.
  1872. The Ansari-Bradley test ([1]_, [2]_) is a non-parametric test
  1873. for the equality of the scale parameter of the distributions
  1874. from which two samples were drawn. The null hypothesis states that
  1875. the ratio of the scale of the distribution underlying `x` to the scale
  1876. of the distribution underlying `y` is 1.
  1877. Parameters
  1878. ----------
  1879. x, y : array_like
  1880. Arrays of sample data.
  1881. alternative : {'two-sided', 'less', 'greater'}, optional
  1882. Defines the alternative hypothesis. Default is 'two-sided'.
  1883. The following options are available:
  1884. * 'two-sided': the ratio of scales is not equal to 1.
  1885. * 'less': the ratio of scales is less than 1.
  1886. * 'greater': the ratio of scales is greater than 1.
  1887. .. versionadded:: 1.7.0
  1888. Returns
  1889. -------
  1890. statistic : float
  1891. The Ansari-Bradley test statistic.
  1892. pvalue : float
  1893. The p-value of the hypothesis test.
  1894. See Also
  1895. --------
  1896. fligner : A non-parametric test for the equality of k variances
  1897. mood : A non-parametric test for the equality of two scale parameters
  1898. Notes
  1899. -----
  1900. The p-value given is exact when the sample sizes are both less than
  1901. 55 and there are no ties, otherwise a normal approximation for the
  1902. p-value is used.
  1903. References
  1904. ----------
  1905. .. [1] Ansari, A. R. and Bradley, R. A. (1960) Rank-sum tests for
  1906. dispersions, Annals of Mathematical Statistics, 31, 1174-1189.
  1907. .. [2] Sprent, Peter and N.C. Smeeton. Applied nonparametric
  1908. statistical methods. 3rd ed. Chapman and Hall/CRC. 2001.
  1909. Section 5.8.2.
  1910. .. [3] Nathaniel E. Helwig "Nonparametric Dispersion and Equality
  1911. Tests" at http://users.stat.umn.edu/~helwig/notes/npde-Notes.pdf
  1912. Examples
  1913. --------
  1914. >>> import numpy as np
  1915. >>> from scipy.stats import ansari
  1916. >>> rng = np.random.default_rng()
  1917. For these examples, we'll create three random data sets. The first
  1918. two, with sizes 35 and 25, are drawn from a normal distribution with
  1919. mean 0 and standard deviation 2. The third data set has size 25 and
  1920. is drawn from a normal distribution with standard deviation 1.25.
  1921. >>> x1 = rng.normal(loc=0, scale=2, size=35)
  1922. >>> x2 = rng.normal(loc=0, scale=2, size=25)
  1923. >>> x3 = rng.normal(loc=0, scale=1.25, size=25)
  1924. First we apply `ansari` to `x1` and `x2`. These samples are drawn
  1925. from the same distribution, so we expect the Ansari-Bradley test
  1926. should not lead us to conclude that the scales of the distributions
  1927. are different.
  1928. >>> ansari(x1, x2)
  1929. AnsariResult(statistic=541.0, pvalue=0.9762532927399098)
  1930. With a p-value close to 1, we cannot conclude that there is a
  1931. significant difference in the scales (as expected).
  1932. Now apply the test to `x1` and `x3`:
  1933. >>> ansari(x1, x3)
  1934. AnsariResult(statistic=425.0, pvalue=0.0003087020407974518)
  1935. The probability of observing such an extreme value of the statistic
  1936. under the null hypothesis of equal scales is only 0.03087%. We take this
  1937. as evidence against the null hypothesis in favor of the alternative:
  1938. the scales of the distributions from which the samples were drawn
  1939. are not equal.
  1940. We can use the `alternative` parameter to perform a one-tailed test.
  1941. In the above example, the scale of `x1` is greater than `x3` and so
  1942. the ratio of scales of `x1` and `x3` is greater than 1. This means
  1943. that the p-value when ``alternative='greater'`` should be near 0 and
  1944. hence we should be able to reject the null hypothesis:
  1945. >>> ansari(x1, x3, alternative='greater')
  1946. AnsariResult(statistic=425.0, pvalue=0.0001543510203987259)
  1947. As we can see, the p-value is indeed quite low. Use of
  1948. ``alternative='less'`` should thus yield a large p-value:
  1949. >>> ansari(x1, x3, alternative='less')
  1950. AnsariResult(statistic=425.0, pvalue=0.9998643258449039)
  1951. """
  1952. if alternative not in {'two-sided', 'greater', 'less'}:
  1953. raise ValueError("'alternative' must be 'two-sided',"
  1954. " 'greater', or 'less'.")
  1955. x, y = asarray(x), asarray(y)
  1956. n = len(x)
  1957. m = len(y)
  1958. if m < 1:
  1959. raise ValueError("Not enough other observations.")
  1960. if n < 1:
  1961. raise ValueError("Not enough test observations.")
  1962. N = m + n
  1963. xy = r_[x, y] # combine
  1964. rank = _stats_py.rankdata(xy)
  1965. symrank = amin(array((rank, N - rank + 1)), 0)
  1966. AB = np.sum(symrank[:n], axis=0)
  1967. uxy = unique(xy)
  1968. repeats = (len(uxy) != len(xy))
  1969. exact = ((m < 55) and (n < 55) and not repeats)
  1970. if repeats and (m < 55 or n < 55):
  1971. warnings.warn("Ties preclude use of exact statistic.")
  1972. if exact:
  1973. if alternative == 'two-sided':
  1974. pval = 2.0 * np.minimum(_abw_state.cdf(AB, n, m),
  1975. _abw_state.sf(AB, n, m))
  1976. elif alternative == 'greater':
  1977. # AB statistic is _smaller_ when ratio of scales is larger,
  1978. # so this is the opposite of the usual calculation
  1979. pval = _abw_state.cdf(AB, n, m)
  1980. else:
  1981. pval = _abw_state.sf(AB, n, m)
  1982. return AnsariResult(AB, min(1.0, pval))
  1983. # otherwise compute normal approximation
  1984. if N % 2: # N odd
  1985. mnAB = n * (N+1.0)**2 / 4.0 / N
  1986. varAB = n * m * (N+1.0) * (3+N**2) / (48.0 * N**2)
  1987. else:
  1988. mnAB = n * (N+2.0) / 4.0
  1989. varAB = m * n * (N+2) * (N-2.0) / 48 / (N-1.0)
  1990. if repeats: # adjust variance estimates
  1991. # compute np.sum(tj * rj**2,axis=0)
  1992. fac = np.sum(symrank**2, axis=0)
  1993. if N % 2: # N odd
  1994. varAB = m * n * (16*N*fac - (N+1)**4) / (16.0 * N**2 * (N-1))
  1995. else: # N even
  1996. varAB = m * n * (16*fac - N*(N+2)**2) / (16.0 * N * (N-1))
  1997. # Small values of AB indicate larger dispersion for the x sample.
  1998. # Large values of AB indicate larger dispersion for the y sample.
  1999. # This is opposite to the way we define the ratio of scales. see [1]_.
  2000. z = (mnAB - AB) / sqrt(varAB)
  2001. z, pval = _normtest_finish(z, alternative)
  2002. return AnsariResult(AB, pval)
  2003. BartlettResult = namedtuple('BartlettResult', ('statistic', 'pvalue'))
  2004. def bartlett(*samples):
  2005. """Perform Bartlett's test for equal variances.
  2006. Bartlett's test tests the null hypothesis that all input samples
  2007. are from populations with equal variances. For samples
  2008. from significantly non-normal populations, Levene's test
  2009. `levene` is more robust.
  2010. Parameters
  2011. ----------
  2012. sample1, sample2, ... : array_like
  2013. arrays of sample data. Only 1d arrays are accepted, they may have
  2014. different lengths.
  2015. Returns
  2016. -------
  2017. statistic : float
  2018. The test statistic.
  2019. pvalue : float
  2020. The p-value of the test.
  2021. See Also
  2022. --------
  2023. fligner : A non-parametric test for the equality of k variances
  2024. levene : A robust parametric test for equality of k variances
  2025. Notes
  2026. -----
  2027. Conover et al. (1981) examine many of the existing parametric and
  2028. nonparametric tests by extensive simulations and they conclude that the
  2029. tests proposed by Fligner and Killeen (1976) and Levene (1960) appear to be
  2030. superior in terms of robustness of departures from normality and power
  2031. ([3]_).
  2032. References
  2033. ----------
  2034. .. [1] https://www.itl.nist.gov/div898/handbook/eda/section3/eda357.htm
  2035. .. [2] Snedecor, George W. and Cochran, William G. (1989), Statistical
  2036. Methods, Eighth Edition, Iowa State University Press.
  2037. .. [3] Park, C. and Lindsay, B. G. (1999). Robust Scale Estimation and
  2038. Hypothesis Testing based on Quadratic Inference Function. Technical
  2039. Report #99-03, Center for Likelihood Studies, Pennsylvania State
  2040. University.
  2041. .. [4] Bartlett, M. S. (1937). Properties of Sufficiency and Statistical
  2042. Tests. Proceedings of the Royal Society of London. Series A,
  2043. Mathematical and Physical Sciences, Vol. 160, No.901, pp. 268-282.
  2044. Examples
  2045. --------
  2046. Test whether or not the lists `a`, `b` and `c` come from populations
  2047. with equal variances.
  2048. >>> import numpy as np
  2049. >>> from scipy.stats import bartlett
  2050. >>> a = [8.88, 9.12, 9.04, 8.98, 9.00, 9.08, 9.01, 8.85, 9.06, 8.99]
  2051. >>> b = [8.88, 8.95, 9.29, 9.44, 9.15, 9.58, 8.36, 9.18, 8.67, 9.05]
  2052. >>> c = [8.95, 9.12, 8.95, 8.85, 9.03, 8.84, 9.07, 8.98, 8.86, 8.98]
  2053. >>> stat, p = bartlett(a, b, c)
  2054. >>> p
  2055. 1.1254782518834628e-05
  2056. The very small p-value suggests that the populations do not have equal
  2057. variances.
  2058. This is not surprising, given that the sample variance of `b` is much
  2059. larger than that of `a` and `c`:
  2060. >>> [np.var(x, ddof=1) for x in [a, b, c]]
  2061. [0.007054444444444413, 0.13073888888888888, 0.008890000000000002]
  2062. """
  2063. # Handle empty input and input that is not 1d
  2064. for sample in samples:
  2065. if np.asanyarray(sample).size == 0:
  2066. return BartlettResult(np.nan, np.nan)
  2067. if np.asanyarray(sample).ndim > 1:
  2068. raise ValueError('Samples must be one-dimensional.')
  2069. k = len(samples)
  2070. if k < 2:
  2071. raise ValueError("Must enter at least two input sample vectors.")
  2072. Ni = np.empty(k)
  2073. ssq = np.empty(k, 'd')
  2074. for j in range(k):
  2075. Ni[j] = len(samples[j])
  2076. ssq[j] = np.var(samples[j], ddof=1)
  2077. Ntot = np.sum(Ni, axis=0)
  2078. spsq = np.sum((Ni - 1)*ssq, axis=0) / (1.0*(Ntot - k))
  2079. numer = (Ntot*1.0 - k) * log(spsq) - np.sum((Ni - 1.0)*log(ssq), axis=0)
  2080. denom = 1.0 + 1.0/(3*(k - 1)) * ((np.sum(1.0/(Ni - 1.0), axis=0)) -
  2081. 1.0/(Ntot - k))
  2082. T = numer / denom
  2083. pval = distributions.chi2.sf(T, k - 1) # 1 - cdf
  2084. return BartlettResult(T, pval)
  2085. LeveneResult = namedtuple('LeveneResult', ('statistic', 'pvalue'))
  2086. def levene(*samples, center='median', proportiontocut=0.05):
  2087. """Perform Levene test for equal variances.
  2088. The Levene test tests the null hypothesis that all input samples
  2089. are from populations with equal variances. Levene's test is an
  2090. alternative to Bartlett's test `bartlett` in the case where
  2091. there are significant deviations from normality.
  2092. Parameters
  2093. ----------
  2094. sample1, sample2, ... : array_like
  2095. The sample data, possibly with different lengths. Only one-dimensional
  2096. samples are accepted.
  2097. center : {'mean', 'median', 'trimmed'}, optional
  2098. Which function of the data to use in the test. The default
  2099. is 'median'.
  2100. proportiontocut : float, optional
  2101. When `center` is 'trimmed', this gives the proportion of data points
  2102. to cut from each end. (See `scipy.stats.trim_mean`.)
  2103. Default is 0.05.
  2104. Returns
  2105. -------
  2106. statistic : float
  2107. The test statistic.
  2108. pvalue : float
  2109. The p-value for the test.
  2110. Notes
  2111. -----
  2112. Three variations of Levene's test are possible. The possibilities
  2113. and their recommended usages are:
  2114. * 'median' : Recommended for skewed (non-normal) distributions>
  2115. * 'mean' : Recommended for symmetric, moderate-tailed distributions.
  2116. * 'trimmed' : Recommended for heavy-tailed distributions.
  2117. The test version using the mean was proposed in the original article
  2118. of Levene ([2]_) while the median and trimmed mean have been studied by
  2119. Brown and Forsythe ([3]_), sometimes also referred to as Brown-Forsythe
  2120. test.
  2121. References
  2122. ----------
  2123. .. [1] https://www.itl.nist.gov/div898/handbook/eda/section3/eda35a.htm
  2124. .. [2] Levene, H. (1960). In Contributions to Probability and Statistics:
  2125. Essays in Honor of Harold Hotelling, I. Olkin et al. eds.,
  2126. Stanford University Press, pp. 278-292.
  2127. .. [3] Brown, M. B. and Forsythe, A. B. (1974), Journal of the American
  2128. Statistical Association, 69, 364-367
  2129. Examples
  2130. --------
  2131. Test whether or not the lists `a`, `b` and `c` come from populations
  2132. with equal variances.
  2133. >>> import numpy as np
  2134. >>> from scipy.stats import levene
  2135. >>> a = [8.88, 9.12, 9.04, 8.98, 9.00, 9.08, 9.01, 8.85, 9.06, 8.99]
  2136. >>> b = [8.88, 8.95, 9.29, 9.44, 9.15, 9.58, 8.36, 9.18, 8.67, 9.05]
  2137. >>> c = [8.95, 9.12, 8.95, 8.85, 9.03, 8.84, 9.07, 8.98, 8.86, 8.98]
  2138. >>> stat, p = levene(a, b, c)
  2139. >>> p
  2140. 0.002431505967249681
  2141. The small p-value suggests that the populations do not have equal
  2142. variances.
  2143. This is not surprising, given that the sample variance of `b` is much
  2144. larger than that of `a` and `c`:
  2145. >>> [np.var(x, ddof=1) for x in [a, b, c]]
  2146. [0.007054444444444413, 0.13073888888888888, 0.008890000000000002]
  2147. """
  2148. if center not in ['mean', 'median', 'trimmed']:
  2149. raise ValueError("center must be 'mean', 'median' or 'trimmed'.")
  2150. k = len(samples)
  2151. if k < 2:
  2152. raise ValueError("Must enter at least two input sample vectors.")
  2153. # check for 1d input
  2154. for j in range(k):
  2155. if np.asanyarray(samples[j]).ndim > 1:
  2156. raise ValueError('Samples must be one-dimensional.')
  2157. Ni = np.empty(k)
  2158. Yci = np.empty(k, 'd')
  2159. if center == 'median':
  2160. func = lambda x: np.median(x, axis=0)
  2161. elif center == 'mean':
  2162. func = lambda x: np.mean(x, axis=0)
  2163. else: # center == 'trimmed'
  2164. samples = tuple(_stats_py.trimboth(np.sort(sample), proportiontocut)
  2165. for sample in samples)
  2166. func = lambda x: np.mean(x, axis=0)
  2167. for j in range(k):
  2168. Ni[j] = len(samples[j])
  2169. Yci[j] = func(samples[j])
  2170. Ntot = np.sum(Ni, axis=0)
  2171. # compute Zij's
  2172. Zij = [None] * k
  2173. for i in range(k):
  2174. Zij[i] = abs(asarray(samples[i]) - Yci[i])
  2175. # compute Zbari
  2176. Zbari = np.empty(k, 'd')
  2177. Zbar = 0.0
  2178. for i in range(k):
  2179. Zbari[i] = np.mean(Zij[i], axis=0)
  2180. Zbar += Zbari[i] * Ni[i]
  2181. Zbar /= Ntot
  2182. numer = (Ntot - k) * np.sum(Ni * (Zbari - Zbar)**2, axis=0)
  2183. # compute denom_variance
  2184. dvar = 0.0
  2185. for i in range(k):
  2186. dvar += np.sum((Zij[i] - Zbari[i])**2, axis=0)
  2187. denom = (k - 1.0) * dvar
  2188. W = numer / denom
  2189. pval = distributions.f.sf(W, k-1, Ntot-k) # 1 - cdf
  2190. return LeveneResult(W, pval)
  2191. @_deprecated("'binom_test' is deprecated in favour of"
  2192. " 'binomtest' from version 1.7.0 and will"
  2193. " be removed in Scipy 1.12.0.")
  2194. def binom_test(x, n=None, p=0.5, alternative='two-sided'):
  2195. """Perform a test that the probability of success is p.
  2196. This is an exact, two-sided test of the null hypothesis
  2197. that the probability of success in a Bernoulli experiment
  2198. is `p`.
  2199. .. deprecated:: 1.10.0
  2200. `binom_test` is deprecated in favour of `binomtest` and will
  2201. be removed in Scipy 1.12.0.
  2202. Parameters
  2203. ----------
  2204. x : int or array_like
  2205. The number of successes, or if x has length 2, it is the
  2206. number of successes and the number of failures.
  2207. n : int
  2208. The number of trials. This is ignored if x gives both the
  2209. number of successes and failures.
  2210. p : float, optional
  2211. The hypothesized probability of success. ``0 <= p <= 1``. The
  2212. default value is ``p = 0.5``.
  2213. alternative : {'two-sided', 'greater', 'less'}, optional
  2214. Indicates the alternative hypothesis. The default value is
  2215. 'two-sided'.
  2216. Returns
  2217. -------
  2218. p-value : float
  2219. The p-value of the hypothesis test.
  2220. References
  2221. ----------
  2222. .. [1] https://en.wikipedia.org/wiki/Binomial_test
  2223. Examples
  2224. --------
  2225. >>> from scipy import stats
  2226. A car manufacturer claims that no more than 10% of their cars are unsafe.
  2227. 15 cars are inspected for safety, 3 were found to be unsafe. Test the
  2228. manufacturer's claim:
  2229. >>> stats.binom_test(3, n=15, p=0.1, alternative='greater')
  2230. 0.18406106910639114
  2231. The null hypothesis cannot be rejected at the 5% level of significance
  2232. because the returned p-value is greater than the critical value of 5%.
  2233. """
  2234. x = atleast_1d(x).astype(np.int_)
  2235. if len(x) == 2:
  2236. n = x[1] + x[0]
  2237. x = x[0]
  2238. elif len(x) == 1:
  2239. x = x[0]
  2240. if n is None or n < x:
  2241. raise ValueError("n must be >= x")
  2242. n = np.int_(n)
  2243. else:
  2244. raise ValueError("Incorrect length for x.")
  2245. if (p > 1.0) or (p < 0.0):
  2246. raise ValueError("p must be in range [0,1]")
  2247. if alternative not in ('two-sided', 'less', 'greater'):
  2248. raise ValueError("alternative not recognized\n"
  2249. "should be 'two-sided', 'less' or 'greater'")
  2250. if alternative == 'less':
  2251. pval = distributions.binom.cdf(x, n, p)
  2252. return pval
  2253. if alternative == 'greater':
  2254. pval = distributions.binom.sf(x-1, n, p)
  2255. return pval
  2256. # if alternative was neither 'less' nor 'greater', then it's 'two-sided'
  2257. d = distributions.binom.pmf(x, n, p)
  2258. rerr = 1 + 1e-7
  2259. if x == p * n:
  2260. # special case as shortcut, would also be handled by `else` below
  2261. pval = 1.
  2262. elif x < p * n:
  2263. i = np.arange(np.ceil(p * n), n+1)
  2264. y = np.sum(distributions.binom.pmf(i, n, p) <= d*rerr, axis=0)
  2265. pval = (distributions.binom.cdf(x, n, p) +
  2266. distributions.binom.sf(n - y, n, p))
  2267. else:
  2268. i = np.arange(np.floor(p*n) + 1)
  2269. y = np.sum(distributions.binom.pmf(i, n, p) <= d*rerr, axis=0)
  2270. pval = (distributions.binom.cdf(y-1, n, p) +
  2271. distributions.binom.sf(x-1, n, p))
  2272. return min(1.0, pval)
  2273. def _apply_func(x, g, func):
  2274. # g is list of indices into x
  2275. # separating x into different groups
  2276. # func should be applied over the groups
  2277. g = unique(r_[0, g, len(x)])
  2278. output = [func(x[g[k]:g[k+1]]) for k in range(len(g) - 1)]
  2279. return asarray(output)
  2280. FlignerResult = namedtuple('FlignerResult', ('statistic', 'pvalue'))
  2281. def fligner(*samples, center='median', proportiontocut=0.05):
  2282. """Perform Fligner-Killeen test for equality of variance.
  2283. Fligner's test tests the null hypothesis that all input samples
  2284. are from populations with equal variances. Fligner-Killeen's test is
  2285. distribution free when populations are identical [2]_.
  2286. Parameters
  2287. ----------
  2288. sample1, sample2, ... : array_like
  2289. Arrays of sample data. Need not be the same length.
  2290. center : {'mean', 'median', 'trimmed'}, optional
  2291. Keyword argument controlling which function of the data is used in
  2292. computing the test statistic. The default is 'median'.
  2293. proportiontocut : float, optional
  2294. When `center` is 'trimmed', this gives the proportion of data points
  2295. to cut from each end. (See `scipy.stats.trim_mean`.)
  2296. Default is 0.05.
  2297. Returns
  2298. -------
  2299. statistic : float
  2300. The test statistic.
  2301. pvalue : float
  2302. The p-value for the hypothesis test.
  2303. See Also
  2304. --------
  2305. bartlett : A parametric test for equality of k variances in normal samples
  2306. levene : A robust parametric test for equality of k variances
  2307. Notes
  2308. -----
  2309. As with Levene's test there are three variants of Fligner's test that
  2310. differ by the measure of central tendency used in the test. See `levene`
  2311. for more information.
  2312. Conover et al. (1981) examine many of the existing parametric and
  2313. nonparametric tests by extensive simulations and they conclude that the
  2314. tests proposed by Fligner and Killeen (1976) and Levene (1960) appear to be
  2315. superior in terms of robustness of departures from normality and power [3]_.
  2316. References
  2317. ----------
  2318. .. [1] Park, C. and Lindsay, B. G. (1999). Robust Scale Estimation and
  2319. Hypothesis Testing based on Quadratic Inference Function. Technical
  2320. Report #99-03, Center for Likelihood Studies, Pennsylvania State
  2321. University.
  2322. https://cecas.clemson.edu/~cspark/cv/paper/qif/draftqif2.pdf
  2323. .. [2] Fligner, M.A. and Killeen, T.J. (1976). Distribution-free two-sample
  2324. tests for scale. 'Journal of the American Statistical Association.'
  2325. 71(353), 210-213.
  2326. .. [3] Park, C. and Lindsay, B. G. (1999). Robust Scale Estimation and
  2327. Hypothesis Testing based on Quadratic Inference Function. Technical
  2328. Report #99-03, Center for Likelihood Studies, Pennsylvania State
  2329. University.
  2330. .. [4] Conover, W. J., Johnson, M. E. and Johnson M. M. (1981). A
  2331. comparative study of tests for homogeneity of variances, with
  2332. applications to the outer continental shelf biding data.
  2333. Technometrics, 23(4), 351-361.
  2334. Examples
  2335. --------
  2336. Test whether or not the lists `a`, `b` and `c` come from populations
  2337. with equal variances.
  2338. >>> import numpy as np
  2339. >>> from scipy.stats import fligner
  2340. >>> a = [8.88, 9.12, 9.04, 8.98, 9.00, 9.08, 9.01, 8.85, 9.06, 8.99]
  2341. >>> b = [8.88, 8.95, 9.29, 9.44, 9.15, 9.58, 8.36, 9.18, 8.67, 9.05]
  2342. >>> c = [8.95, 9.12, 8.95, 8.85, 9.03, 8.84, 9.07, 8.98, 8.86, 8.98]
  2343. >>> stat, p = fligner(a, b, c)
  2344. >>> p
  2345. 0.00450826080004775
  2346. The small p-value suggests that the populations do not have equal
  2347. variances.
  2348. This is not surprising, given that the sample variance of `b` is much
  2349. larger than that of `a` and `c`:
  2350. >>> [np.var(x, ddof=1) for x in [a, b, c]]
  2351. [0.007054444444444413, 0.13073888888888888, 0.008890000000000002]
  2352. """
  2353. if center not in ['mean', 'median', 'trimmed']:
  2354. raise ValueError("center must be 'mean', 'median' or 'trimmed'.")
  2355. # Handle empty input
  2356. for sample in samples:
  2357. if np.asanyarray(sample).size == 0:
  2358. return FlignerResult(np.nan, np.nan)
  2359. k = len(samples)
  2360. if k < 2:
  2361. raise ValueError("Must enter at least two input sample vectors.")
  2362. if center == 'median':
  2363. func = lambda x: np.median(x, axis=0)
  2364. elif center == 'mean':
  2365. func = lambda x: np.mean(x, axis=0)
  2366. else: # center == 'trimmed'
  2367. samples = tuple(_stats_py.trimboth(sample, proportiontocut)
  2368. for sample in samples)
  2369. func = lambda x: np.mean(x, axis=0)
  2370. Ni = asarray([len(samples[j]) for j in range(k)])
  2371. Yci = asarray([func(samples[j]) for j in range(k)])
  2372. Ntot = np.sum(Ni, axis=0)
  2373. # compute Zij's
  2374. Zij = [abs(asarray(samples[i]) - Yci[i]) for i in range(k)]
  2375. allZij = []
  2376. g = [0]
  2377. for i in range(k):
  2378. allZij.extend(list(Zij[i]))
  2379. g.append(len(allZij))
  2380. ranks = _stats_py.rankdata(allZij)
  2381. sample = distributions.norm.ppf(ranks / (2*(Ntot + 1.0)) + 0.5)
  2382. # compute Aibar
  2383. Aibar = _apply_func(sample, g, np.sum) / Ni
  2384. anbar = np.mean(sample, axis=0)
  2385. varsq = np.var(sample, axis=0, ddof=1)
  2386. Xsq = np.sum(Ni * (asarray(Aibar) - anbar)**2.0, axis=0) / varsq
  2387. pval = distributions.chi2.sf(Xsq, k - 1) # 1 - cdf
  2388. return FlignerResult(Xsq, pval)
  2389. @_axis_nan_policy_factory(lambda x1: (x1,), n_samples=4, n_outputs=1)
  2390. def _mood_inner_lc(xy, x, diffs, sorted_xy, n, m, N) -> float:
  2391. # Obtain the unique values and their frequencies from the pooled samples.
  2392. # "a_j, + b_j, = t_j, for j = 1, ... k" where `k` is the number of unique
  2393. # classes, and "[t]he number of values associated with the x's and y's in
  2394. # the jth class will be denoted by a_j, and b_j respectively."
  2395. # (Mielke, 312)
  2396. # Reuse previously computed sorted array and `diff` arrays to obtain the
  2397. # unique values and counts. Prepend `diffs` with a non-zero to indicate
  2398. # that the first element should be marked as not matching what preceded it.
  2399. diffs_prep = np.concatenate(([1], diffs))
  2400. # Unique elements are where the was a difference between elements in the
  2401. # sorted array
  2402. uniques = sorted_xy[diffs_prep != 0]
  2403. # The count of each element is the bin size for each set of consecutive
  2404. # differences where the difference is zero. Replace nonzero differences
  2405. # with 1 and then use the cumulative sum to count the indices.
  2406. t = np.bincount(np.cumsum(np.asarray(diffs_prep != 0, dtype=int)))[1:]
  2407. k = len(uniques)
  2408. js = np.arange(1, k + 1, dtype=int)
  2409. # the `b` array mentioned in the paper is not used, outside of the
  2410. # calculation of `t`, so we do not need to calculate it separately. Here
  2411. # we calculate `a`. In plain language, `a[j]` is the number of values in
  2412. # `x` that equal `uniques[j]`.
  2413. sorted_xyx = np.sort(np.concatenate((xy, x)))
  2414. diffs = np.diff(sorted_xyx)
  2415. diffs_prep = np.concatenate(([1], diffs))
  2416. diff_is_zero = np.asarray(diffs_prep != 0, dtype=int)
  2417. xyx_counts = np.bincount(np.cumsum(diff_is_zero))[1:]
  2418. a = xyx_counts - t
  2419. # "Define .. a_0 = b_0 = t_0 = S_0 = 0" (Mielke 312) so we shift `a`
  2420. # and `t` arrays over 1 to allow a first element of 0 to accommodate this
  2421. # indexing.
  2422. t = np.concatenate(([0], t))
  2423. a = np.concatenate(([0], a))
  2424. # S is built from `t`, so it does not need a preceding zero added on.
  2425. S = np.cumsum(t)
  2426. # define a copy of `S` with a prepending zero for later use to avoid
  2427. # the need for indexing.
  2428. S_i_m1 = np.concatenate(([0], S[:-1]))
  2429. # Psi, as defined by the 6th unnumbered equation on page 313 (Mielke).
  2430. # Note that in the paper there is an error where the denominator `2` is
  2431. # squared when it should be the entire equation.
  2432. def psi(indicator):
  2433. return (indicator - (N + 1)/2)**2
  2434. # define summation range for use in calculation of phi, as seen in sum
  2435. # in the unnumbered equation on the bottom of page 312 (Mielke).
  2436. s_lower = S[js - 1] + 1
  2437. s_upper = S[js] + 1
  2438. phi_J = [np.arange(s_lower[idx], s_upper[idx]) for idx in range(k)]
  2439. # for every range in the above array, determine the sum of psi(I) for
  2440. # every element in the range. Divide all the sums by `t`. Following the
  2441. # last unnumbered equation on page 312.
  2442. phis = [np.sum(psi(I_j)) for I_j in phi_J] / t[js]
  2443. # `T` is equal to a[j] * phi[j], per the first unnumbered equation on
  2444. # page 312. `phis` is already in the order based on `js`, so we index
  2445. # into `a` with `js` as well.
  2446. T = sum(phis * a[js])
  2447. # The approximate statistic
  2448. E_0_T = n * (N * N - 1) / 12
  2449. varM = (m * n * (N + 1.0) * (N ** 2 - 4) / 180 -
  2450. m * n / (180 * N * (N - 1)) * np.sum(
  2451. t * (t**2 - 1) * (t**2 - 4 + (15 * (N - S - S_i_m1) ** 2))
  2452. ))
  2453. return ((T - E_0_T) / np.sqrt(varM),)
  2454. def mood(x, y, axis=0, alternative="two-sided"):
  2455. """Perform Mood's test for equal scale parameters.
  2456. Mood's two-sample test for scale parameters is a non-parametric
  2457. test for the null hypothesis that two samples are drawn from the
  2458. same distribution with the same scale parameter.
  2459. Parameters
  2460. ----------
  2461. x, y : array_like
  2462. Arrays of sample data.
  2463. axis : int, optional
  2464. The axis along which the samples are tested. `x` and `y` can be of
  2465. different length along `axis`.
  2466. If `axis` is None, `x` and `y` are flattened and the test is done on
  2467. all values in the flattened arrays.
  2468. alternative : {'two-sided', 'less', 'greater'}, optional
  2469. Defines the alternative hypothesis. Default is 'two-sided'.
  2470. The following options are available:
  2471. * 'two-sided': the scales of the distributions underlying `x` and `y`
  2472. are different.
  2473. * 'less': the scale of the distribution underlying `x` is less than
  2474. the scale of the distribution underlying `y`.
  2475. * 'greater': the scale of the distribution underlying `x` is greater
  2476. than the scale of the distribution underlying `y`.
  2477. .. versionadded:: 1.7.0
  2478. Returns
  2479. -------
  2480. res : SignificanceResult
  2481. An object containing attributes:
  2482. statistic : scalar or ndarray
  2483. The z-score for the hypothesis test. For 1-D inputs a scalar is
  2484. returned.
  2485. pvalue : scalar ndarray
  2486. The p-value for the hypothesis test.
  2487. See Also
  2488. --------
  2489. fligner : A non-parametric test for the equality of k variances
  2490. ansari : A non-parametric test for the equality of 2 variances
  2491. bartlett : A parametric test for equality of k variances in normal samples
  2492. levene : A parametric test for equality of k variances
  2493. Notes
  2494. -----
  2495. The data are assumed to be drawn from probability distributions ``f(x)``
  2496. and ``f(x/s) / s`` respectively, for some probability density function f.
  2497. The null hypothesis is that ``s == 1``.
  2498. For multi-dimensional arrays, if the inputs are of shapes
  2499. ``(n0, n1, n2, n3)`` and ``(n0, m1, n2, n3)``, then if ``axis=1``, the
  2500. resulting z and p values will have shape ``(n0, n2, n3)``. Note that
  2501. ``n1`` and ``m1`` don't have to be equal, but the other dimensions do.
  2502. References
  2503. ----------
  2504. [1] Mielke, Paul W. "Note on Some Squared Rank Tests with Existing Ties."
  2505. Technometrics, vol. 9, no. 2, 1967, pp. 312-14. JSTOR,
  2506. https://doi.org/10.2307/1266427. Accessed 18 May 2022.
  2507. Examples
  2508. --------
  2509. >>> import numpy as np
  2510. >>> from scipy import stats
  2511. >>> rng = np.random.default_rng()
  2512. >>> x2 = rng.standard_normal((2, 45, 6, 7))
  2513. >>> x1 = rng.standard_normal((2, 30, 6, 7))
  2514. >>> res = stats.mood(x1, x2, axis=1)
  2515. >>> res.pvalue.shape
  2516. (2, 6, 7)
  2517. Find the number of points where the difference in scale is not significant:
  2518. >>> (res.pvalue > 0.1).sum()
  2519. 78
  2520. Perform the test with different scales:
  2521. >>> x1 = rng.standard_normal((2, 30))
  2522. >>> x2 = rng.standard_normal((2, 35)) * 10.0
  2523. >>> stats.mood(x1, x2, axis=1)
  2524. SignificanceResult(statistic=array([-5.76174136, -6.12650783]),
  2525. pvalue=array([8.32505043e-09, 8.98287869e-10]))
  2526. """
  2527. x = np.asarray(x, dtype=float)
  2528. y = np.asarray(y, dtype=float)
  2529. if axis is None:
  2530. x = x.flatten()
  2531. y = y.flatten()
  2532. axis = 0
  2533. if axis < 0:
  2534. axis = x.ndim + axis
  2535. # Determine shape of the result arrays
  2536. res_shape = tuple([x.shape[ax] for ax in range(len(x.shape)) if ax != axis])
  2537. if not (res_shape == tuple([y.shape[ax] for ax in range(len(y.shape)) if
  2538. ax != axis])):
  2539. raise ValueError("Dimensions of x and y on all axes except `axis` "
  2540. "should match")
  2541. n = x.shape[axis]
  2542. m = y.shape[axis]
  2543. N = m + n
  2544. if N < 3:
  2545. raise ValueError("Not enough observations.")
  2546. xy = np.concatenate((x, y), axis=axis)
  2547. # determine if any of the samples contain ties
  2548. sorted_xy = np.sort(xy, axis=axis)
  2549. diffs = np.diff(sorted_xy, axis=axis)
  2550. if 0 in diffs:
  2551. z = np.asarray(_mood_inner_lc(xy, x, diffs, sorted_xy, n, m, N,
  2552. axis=axis))
  2553. else:
  2554. if axis != 0:
  2555. xy = np.moveaxis(xy, axis, 0)
  2556. xy = xy.reshape(xy.shape[0], -1)
  2557. # Generalized to the n-dimensional case by adding the axis argument,
  2558. # and using for loops, since rankdata is not vectorized. For improving
  2559. # performance consider vectorizing rankdata function.
  2560. all_ranks = np.empty_like(xy)
  2561. for j in range(xy.shape[1]):
  2562. all_ranks[:, j] = _stats_py.rankdata(xy[:, j])
  2563. Ri = all_ranks[:n]
  2564. M = np.sum((Ri - (N + 1.0) / 2) ** 2, axis=0)
  2565. # Approx stat.
  2566. mnM = n * (N * N - 1.0) / 12
  2567. varM = m * n * (N + 1.0) * (N + 2) * (N - 2) / 180
  2568. z = (M - mnM) / sqrt(varM)
  2569. z, pval = _normtest_finish(z, alternative)
  2570. if res_shape == ():
  2571. # Return scalars, not 0-D arrays
  2572. z = z[0]
  2573. pval = pval[0]
  2574. else:
  2575. z.shape = res_shape
  2576. pval.shape = res_shape
  2577. return SignificanceResult(z, pval)
  2578. WilcoxonResult = _make_tuple_bunch('WilcoxonResult', ['statistic', 'pvalue'])
  2579. def wilcoxon_result_unpacker(res):
  2580. if hasattr(res, 'zstatistic'):
  2581. return res.statistic, res.pvalue, res.zstatistic
  2582. else:
  2583. return res.statistic, res.pvalue
  2584. def wilcoxon_result_object(statistic, pvalue, zstatistic=None):
  2585. res = WilcoxonResult(statistic, pvalue)
  2586. if zstatistic is not None:
  2587. res.zstatistic = zstatistic
  2588. return res
  2589. def wilcoxon_outputs(kwds):
  2590. method = kwds.get('method', 'auto')
  2591. if method == 'approx':
  2592. return 3
  2593. return 2
  2594. @_rename_parameter("mode", "method")
  2595. @_axis_nan_policy_factory(
  2596. wilcoxon_result_object, paired=True,
  2597. n_samples=lambda kwds: 2 if kwds.get('y', None) is not None else 1,
  2598. result_to_tuple=wilcoxon_result_unpacker, n_outputs=wilcoxon_outputs,
  2599. )
  2600. def wilcoxon(x, y=None, zero_method="wilcox", correction=False,
  2601. alternative="two-sided", method='auto'):
  2602. """Calculate the Wilcoxon signed-rank test.
  2603. The Wilcoxon signed-rank test tests the null hypothesis that two
  2604. related paired samples come from the same distribution. In particular,
  2605. it tests whether the distribution of the differences ``x - y`` is symmetric
  2606. about zero. It is a non-parametric version of the paired T-test.
  2607. Parameters
  2608. ----------
  2609. x : array_like
  2610. Either the first set of measurements (in which case ``y`` is the second
  2611. set of measurements), or the differences between two sets of
  2612. measurements (in which case ``y`` is not to be specified.) Must be
  2613. one-dimensional.
  2614. y : array_like, optional
  2615. Either the second set of measurements (if ``x`` is the first set of
  2616. measurements), or not specified (if ``x`` is the differences between
  2617. two sets of measurements.) Must be one-dimensional.
  2618. zero_method : {"wilcox", "pratt", "zsplit"}, optional
  2619. There are different conventions for handling pairs of observations
  2620. with equal values ("zero-differences", or "zeros").
  2621. * "wilcox": Discards all zero-differences (default); see [4]_.
  2622. * "pratt": Includes zero-differences in the ranking process,
  2623. but drops the ranks of the zeros (more conservative); see [3]_.
  2624. In this case, the normal approximation is adjusted as in [5]_.
  2625. * "zsplit": Includes zero-differences in the ranking process and
  2626. splits the zero rank between positive and negative ones.
  2627. correction : bool, optional
  2628. If True, apply continuity correction by adjusting the Wilcoxon rank
  2629. statistic by 0.5 towards the mean value when computing the
  2630. z-statistic if a normal approximation is used. Default is False.
  2631. alternative : {"two-sided", "greater", "less"}, optional
  2632. Defines the alternative hypothesis. Default is 'two-sided'.
  2633. In the following, let ``d`` represent the difference between the paired
  2634. samples: ``d = x - y`` if both ``x`` and ``y`` are provided, or
  2635. ``d = x`` otherwise.
  2636. * 'two-sided': the distribution underlying ``d`` is not symmetric
  2637. about zero.
  2638. * 'less': the distribution underlying ``d`` is stochastically less
  2639. than a distribution symmetric about zero.
  2640. * 'greater': the distribution underlying ``d`` is stochastically
  2641. greater than a distribution symmetric about zero.
  2642. method : {"auto", "exact", "approx"}, optional
  2643. Method to calculate the p-value, see Notes. Default is "auto".
  2644. Returns
  2645. -------
  2646. An object with the following attributes.
  2647. statistic : array_like
  2648. If `alternative` is "two-sided", the sum of the ranks of the
  2649. differences above or below zero, whichever is smaller.
  2650. Otherwise the sum of the ranks of the differences above zero.
  2651. pvalue : array_like
  2652. The p-value for the test depending on `alternative` and `method`.
  2653. zstatistic : array_like
  2654. When ``method = 'approx'``, this is the normalized z-statistic::
  2655. z = (T - mn - d) / se
  2656. where ``T`` is `statistic` as defined above, ``mn`` is the mean of the
  2657. distribution under the null hypothesis, ``d`` is a continuity
  2658. correction, and ``se`` is the standard error.
  2659. When ``method != 'approx'``, this attribute is not available.
  2660. See Also
  2661. --------
  2662. kruskal, mannwhitneyu
  2663. Notes
  2664. -----
  2665. In the following, let ``d`` represent the difference between the paired
  2666. samples: ``d = x - y`` if both ``x`` and ``y`` are provided, or ``d = x``
  2667. otherwise. Assume that all elements of ``d`` are independent and
  2668. identically distributed observations, and all are distinct and nonzero.
  2669. - When ``len(d)`` is sufficiently large, the null distribution of the
  2670. normalized test statistic (`zstatistic` above) is approximately normal,
  2671. and ``method = 'approx'`` can be used to compute the p-value.
  2672. - When ``len(d)`` is small, the normal approximation may not be accurate,
  2673. and ``method='exact'`` is preferred (at the cost of additional
  2674. execution time).
  2675. - The default, ``method='auto'``, selects between the two: when
  2676. ``len(d) <= 50``, the exact method is used; otherwise, the approximate
  2677. method is used.
  2678. The presence of "ties" (i.e. not all elements of ``d`` are unique) and
  2679. "zeros" (i.e. elements of ``d`` are zero) changes the null distribution
  2680. of the test statistic, and ``method='exact'`` no longer calculates
  2681. the exact p-value. If ``method='approx'``, the z-statistic is adjusted
  2682. for more accurate comparison against the standard normal, but still,
  2683. for finite sample sizes, the standard normal is only an approximation of
  2684. the true null distribution of the z-statistic. There is no clear
  2685. consensus among references on which method most accurately approximates
  2686. the p-value for small samples in the presence of zeros and/or ties. In any
  2687. case, this is the behavior of `wilcoxon` when ``method='auto':
  2688. ``method='exact'`` is used when ``len(d) <= 50`` *and there are no zeros*;
  2689. otherwise, ``method='approx'`` is used.
  2690. References
  2691. ----------
  2692. .. [1] https://en.wikipedia.org/wiki/Wilcoxon_signed-rank_test
  2693. .. [2] Conover, W.J., Practical Nonparametric Statistics, 1971.
  2694. .. [3] Pratt, J.W., Remarks on Zeros and Ties in the Wilcoxon Signed
  2695. Rank Procedures, Journal of the American Statistical Association,
  2696. Vol. 54, 1959, pp. 655-667. :doi:`10.1080/01621459.1959.10501526`
  2697. .. [4] Wilcoxon, F., Individual Comparisons by Ranking Methods,
  2698. Biometrics Bulletin, Vol. 1, 1945, pp. 80-83. :doi:`10.2307/3001968`
  2699. .. [5] Cureton, E.E., The Normal Approximation to the Signed-Rank
  2700. Sampling Distribution When Zero Differences are Present,
  2701. Journal of the American Statistical Association, Vol. 62, 1967,
  2702. pp. 1068-1069. :doi:`10.1080/01621459.1967.10500917`
  2703. Examples
  2704. --------
  2705. In [4]_, the differences in height between cross- and self-fertilized
  2706. corn plants is given as follows:
  2707. >>> d = [6, 8, 14, 16, 23, 24, 28, 29, 41, -48, 49, 56, 60, -67, 75]
  2708. Cross-fertilized plants appear to be higher. To test the null
  2709. hypothesis that there is no height difference, we can apply the
  2710. two-sided test:
  2711. >>> from scipy.stats import wilcoxon
  2712. >>> res = wilcoxon(d)
  2713. >>> res.statistic, res.pvalue
  2714. (24.0, 0.041259765625)
  2715. Hence, we would reject the null hypothesis at a confidence level of 5%,
  2716. concluding that there is a difference in height between the groups.
  2717. To confirm that the median of the differences can be assumed to be
  2718. positive, we use:
  2719. >>> res = wilcoxon(d, alternative='greater')
  2720. >>> res.statistic, res.pvalue
  2721. (96.0, 0.0206298828125)
  2722. This shows that the null hypothesis that the median is negative can be
  2723. rejected at a confidence level of 5% in favor of the alternative that
  2724. the median is greater than zero. The p-values above are exact. Using the
  2725. normal approximation gives very similar values:
  2726. >>> res = wilcoxon(d, method='approx')
  2727. >>> res.statistic, res.pvalue
  2728. (24.0, 0.04088813291185591)
  2729. Note that the statistic changed to 96 in the one-sided case (the sum
  2730. of ranks of positive differences) whereas it is 24 in the two-sided
  2731. case (the minimum of sum of ranks above and below zero).
  2732. """
  2733. mode = method
  2734. if mode not in ["auto", "approx", "exact"]:
  2735. raise ValueError("mode must be either 'auto', 'approx' or 'exact'")
  2736. if zero_method not in ["wilcox", "pratt", "zsplit"]:
  2737. raise ValueError("Zero method must be either 'wilcox' "
  2738. "or 'pratt' or 'zsplit'")
  2739. if alternative not in ["two-sided", "less", "greater"]:
  2740. raise ValueError("Alternative must be either 'two-sided', "
  2741. "'greater' or 'less'")
  2742. if y is None:
  2743. d = asarray(x)
  2744. if d.ndim > 1:
  2745. raise ValueError('Sample x must be one-dimensional.')
  2746. else:
  2747. x, y = map(asarray, (x, y))
  2748. if x.ndim > 1 or y.ndim > 1:
  2749. raise ValueError('Samples x and y must be one-dimensional.')
  2750. if len(x) != len(y):
  2751. raise ValueError('The samples x and y must have the same length.')
  2752. d = x - y
  2753. if len(d) == 0:
  2754. res = WilcoxonResult(np.nan, np.nan)
  2755. if method == 'approx':
  2756. res.zstatistic = np.nan
  2757. return res
  2758. if mode == "auto":
  2759. if len(d) <= 50:
  2760. mode = "exact"
  2761. else:
  2762. mode = "approx"
  2763. n_zero = np.sum(d == 0)
  2764. if n_zero > 0 and mode == "exact":
  2765. mode = "approx"
  2766. warnings.warn("Exact p-value calculation does not work if there are "
  2767. "zeros. Switching to normal approximation.")
  2768. if mode == "approx":
  2769. if zero_method in ["wilcox", "pratt"]:
  2770. if n_zero == len(d):
  2771. raise ValueError("zero_method 'wilcox' and 'pratt' do not "
  2772. "work if x - y is zero for all elements.")
  2773. if zero_method == "wilcox":
  2774. # Keep all non-zero differences
  2775. d = compress(np.not_equal(d, 0), d)
  2776. count = len(d)
  2777. if count < 10 and mode == "approx":
  2778. warnings.warn("Sample size too small for normal approximation.")
  2779. r = _stats_py.rankdata(abs(d))
  2780. r_plus = np.sum((d > 0) * r)
  2781. r_minus = np.sum((d < 0) * r)
  2782. if zero_method == "zsplit":
  2783. r_zero = np.sum((d == 0) * r)
  2784. r_plus += r_zero / 2.
  2785. r_minus += r_zero / 2.
  2786. # return min for two-sided test, but r_plus for one-sided test
  2787. # the literature is not consistent here
  2788. # r_plus is more informative since r_plus + r_minus = count*(count+1)/2,
  2789. # i.e. the sum of the ranks, so r_minus and the min can be inferred
  2790. # (If alternative='pratt', r_plus + r_minus = count*(count+1)/2 - r_zero.)
  2791. # [3] uses the r_plus for the one-sided test, keep min for two-sided test
  2792. # to keep backwards compatibility
  2793. if alternative == "two-sided":
  2794. T = min(r_plus, r_minus)
  2795. else:
  2796. T = r_plus
  2797. if mode == "approx":
  2798. mn = count * (count + 1.) * 0.25
  2799. se = count * (count + 1.) * (2. * count + 1.)
  2800. if zero_method == "pratt":
  2801. r = r[d != 0]
  2802. # normal approximation needs to be adjusted, see Cureton (1967)
  2803. mn -= n_zero * (n_zero + 1.) * 0.25
  2804. se -= n_zero * (n_zero + 1.) * (2. * n_zero + 1.)
  2805. replist, repnum = find_repeats(r)
  2806. if repnum.size != 0:
  2807. # Correction for repeated elements.
  2808. se -= 0.5 * (repnum * (repnum * repnum - 1)).sum()
  2809. se = sqrt(se / 24)
  2810. # apply continuity correction if applicable
  2811. d = 0
  2812. if correction:
  2813. if alternative == "two-sided":
  2814. d = 0.5 * np.sign(T - mn)
  2815. elif alternative == "less":
  2816. d = -0.5
  2817. else:
  2818. d = 0.5
  2819. # compute statistic and p-value using normal approximation
  2820. z = (T - mn - d) / se
  2821. if alternative == "two-sided":
  2822. prob = 2. * distributions.norm.sf(abs(z))
  2823. elif alternative == "greater":
  2824. # large T = r_plus indicates x is greater than y; i.e.
  2825. # accept alternative in that case and return small p-value (sf)
  2826. prob = distributions.norm.sf(z)
  2827. else:
  2828. prob = distributions.norm.cdf(z)
  2829. elif mode == "exact":
  2830. # get pmf of the possible positive ranksums r_plus
  2831. pmf = _get_wilcoxon_distr(count)
  2832. # note: r_plus is int (ties not allowed), need int for slices below
  2833. r_plus = int(r_plus)
  2834. if alternative == "two-sided":
  2835. if r_plus == (len(pmf) - 1) // 2:
  2836. # r_plus is the center of the distribution.
  2837. prob = 1.0
  2838. else:
  2839. p_less = np.sum(pmf[:r_plus + 1])
  2840. p_greater = np.sum(pmf[r_plus:])
  2841. prob = 2*min(p_greater, p_less)
  2842. elif alternative == "greater":
  2843. prob = np.sum(pmf[r_plus:])
  2844. else:
  2845. prob = np.sum(pmf[:r_plus + 1])
  2846. prob = np.clip(prob, 0, 1)
  2847. res = WilcoxonResult(T, prob)
  2848. if method == 'approx':
  2849. res.zstatistic = z
  2850. return res
  2851. MedianTestResult = _make_tuple_bunch(
  2852. 'MedianTestResult',
  2853. ['statistic', 'pvalue', 'median', 'table'], []
  2854. )
  2855. def median_test(*samples, ties='below', correction=True, lambda_=1,
  2856. nan_policy='propagate'):
  2857. """Perform a Mood's median test.
  2858. Test that two or more samples come from populations with the same median.
  2859. Let ``n = len(samples)`` be the number of samples. The "grand median" of
  2860. all the data is computed, and a contingency table is formed by
  2861. classifying the values in each sample as being above or below the grand
  2862. median. The contingency table, along with `correction` and `lambda_`,
  2863. are passed to `scipy.stats.chi2_contingency` to compute the test statistic
  2864. and p-value.
  2865. Parameters
  2866. ----------
  2867. sample1, sample2, ... : array_like
  2868. The set of samples. There must be at least two samples.
  2869. Each sample must be a one-dimensional sequence containing at least
  2870. one value. The samples are not required to have the same length.
  2871. ties : str, optional
  2872. Determines how values equal to the grand median are classified in
  2873. the contingency table. The string must be one of::
  2874. "below":
  2875. Values equal to the grand median are counted as "below".
  2876. "above":
  2877. Values equal to the grand median are counted as "above".
  2878. "ignore":
  2879. Values equal to the grand median are not counted.
  2880. The default is "below".
  2881. correction : bool, optional
  2882. If True, *and* there are just two samples, apply Yates' correction
  2883. for continuity when computing the test statistic associated with
  2884. the contingency table. Default is True.
  2885. lambda_ : float or str, optional
  2886. By default, the statistic computed in this test is Pearson's
  2887. chi-squared statistic. `lambda_` allows a statistic from the
  2888. Cressie-Read power divergence family to be used instead. See
  2889. `power_divergence` for details.
  2890. Default is 1 (Pearson's chi-squared statistic).
  2891. nan_policy : {'propagate', 'raise', 'omit'}, optional
  2892. Defines how to handle when input contains nan. 'propagate' returns nan,
  2893. 'raise' throws an error, 'omit' performs the calculations ignoring nan
  2894. values. Default is 'propagate'.
  2895. Returns
  2896. -------
  2897. res : MedianTestResult
  2898. An object containing attributes:
  2899. statistic : float
  2900. The test statistic. The statistic that is returned is determined
  2901. by `lambda_`. The default is Pearson's chi-squared statistic.
  2902. pvalue : float
  2903. The p-value of the test.
  2904. median : float
  2905. The grand median.
  2906. table : ndarray
  2907. The contingency table. The shape of the table is (2, n), where
  2908. n is the number of samples. The first row holds the counts of the
  2909. values above the grand median, and the second row holds the counts
  2910. of the values below the grand median. The table allows further
  2911. analysis with, for example, `scipy.stats.chi2_contingency`, or with
  2912. `scipy.stats.fisher_exact` if there are two samples, without having
  2913. to recompute the table. If ``nan_policy`` is "propagate" and there
  2914. are nans in the input, the return value for ``table`` is ``None``.
  2915. See Also
  2916. --------
  2917. kruskal : Compute the Kruskal-Wallis H-test for independent samples.
  2918. mannwhitneyu : Computes the Mann-Whitney rank test on samples x and y.
  2919. Notes
  2920. -----
  2921. .. versionadded:: 0.15.0
  2922. References
  2923. ----------
  2924. .. [1] Mood, A. M., Introduction to the Theory of Statistics. McGraw-Hill
  2925. (1950), pp. 394-399.
  2926. .. [2] Zar, J. H., Biostatistical Analysis, 5th ed. Prentice Hall (2010).
  2927. See Sections 8.12 and 10.15.
  2928. Examples
  2929. --------
  2930. A biologist runs an experiment in which there are three groups of plants.
  2931. Group 1 has 16 plants, group 2 has 15 plants, and group 3 has 17 plants.
  2932. Each plant produces a number of seeds. The seed counts for each group
  2933. are::
  2934. Group 1: 10 14 14 18 20 22 24 25 31 31 32 39 43 43 48 49
  2935. Group 2: 28 30 31 33 34 35 36 40 44 55 57 61 91 92 99
  2936. Group 3: 0 3 9 22 23 25 25 33 34 34 40 45 46 48 62 67 84
  2937. The following code applies Mood's median test to these samples.
  2938. >>> g1 = [10, 14, 14, 18, 20, 22, 24, 25, 31, 31, 32, 39, 43, 43, 48, 49]
  2939. >>> g2 = [28, 30, 31, 33, 34, 35, 36, 40, 44, 55, 57, 61, 91, 92, 99]
  2940. >>> g3 = [0, 3, 9, 22, 23, 25, 25, 33, 34, 34, 40, 45, 46, 48, 62, 67, 84]
  2941. >>> from scipy.stats import median_test
  2942. >>> res = median_test(g1, g2, g3)
  2943. The median is
  2944. >>> res.median
  2945. 34.0
  2946. and the contingency table is
  2947. >>> res.table
  2948. array([[ 5, 10, 7],
  2949. [11, 5, 10]])
  2950. `p` is too large to conclude that the medians are not the same:
  2951. >>> res.pvalue
  2952. 0.12609082774093244
  2953. The "G-test" can be performed by passing ``lambda_="log-likelihood"`` to
  2954. `median_test`.
  2955. >>> res = median_test(g1, g2, g3, lambda_="log-likelihood")
  2956. >>> res.pvalue
  2957. 0.12224779737117837
  2958. The median occurs several times in the data, so we'll get a different
  2959. result if, for example, ``ties="above"`` is used:
  2960. >>> res = median_test(g1, g2, g3, ties="above")
  2961. >>> res.pvalue
  2962. 0.063873276069553273
  2963. >>> res.table
  2964. array([[ 5, 11, 9],
  2965. [11, 4, 8]])
  2966. This example demonstrates that if the data set is not large and there
  2967. are values equal to the median, the p-value can be sensitive to the
  2968. choice of `ties`.
  2969. """
  2970. if len(samples) < 2:
  2971. raise ValueError('median_test requires two or more samples.')
  2972. ties_options = ['below', 'above', 'ignore']
  2973. if ties not in ties_options:
  2974. raise ValueError("invalid 'ties' option '%s'; 'ties' must be one "
  2975. "of: %s" % (ties, str(ties_options)[1:-1]))
  2976. data = [np.asarray(sample) for sample in samples]
  2977. # Validate the sizes and shapes of the arguments.
  2978. for k, d in enumerate(data):
  2979. if d.size == 0:
  2980. raise ValueError("Sample %d is empty. All samples must "
  2981. "contain at least one value." % (k + 1))
  2982. if d.ndim != 1:
  2983. raise ValueError("Sample %d has %d dimensions. All "
  2984. "samples must be one-dimensional sequences." %
  2985. (k + 1, d.ndim))
  2986. cdata = np.concatenate(data)
  2987. contains_nan, nan_policy = _contains_nan(cdata, nan_policy)
  2988. if contains_nan and nan_policy == 'propagate':
  2989. return MedianTestResult(np.nan, np.nan, np.nan, None)
  2990. if contains_nan:
  2991. grand_median = np.median(cdata[~np.isnan(cdata)])
  2992. else:
  2993. grand_median = np.median(cdata)
  2994. # When the minimum version of numpy supported by scipy is 1.9.0,
  2995. # the above if/else statement can be replaced by the single line:
  2996. # grand_median = np.nanmedian(cdata)
  2997. # Create the contingency table.
  2998. table = np.zeros((2, len(data)), dtype=np.int64)
  2999. for k, sample in enumerate(data):
  3000. sample = sample[~np.isnan(sample)]
  3001. nabove = count_nonzero(sample > grand_median)
  3002. nbelow = count_nonzero(sample < grand_median)
  3003. nequal = sample.size - (nabove + nbelow)
  3004. table[0, k] += nabove
  3005. table[1, k] += nbelow
  3006. if ties == "below":
  3007. table[1, k] += nequal
  3008. elif ties == "above":
  3009. table[0, k] += nequal
  3010. # Check that no row or column of the table is all zero.
  3011. # Such a table can not be given to chi2_contingency, because it would have
  3012. # a zero in the table of expected frequencies.
  3013. rowsums = table.sum(axis=1)
  3014. if rowsums[0] == 0:
  3015. raise ValueError("All values are below the grand median (%r)." %
  3016. grand_median)
  3017. if rowsums[1] == 0:
  3018. raise ValueError("All values are above the grand median (%r)." %
  3019. grand_median)
  3020. if ties == "ignore":
  3021. # We already checked that each sample has at least one value, but it
  3022. # is possible that all those values equal the grand median. If `ties`
  3023. # is "ignore", that would result in a column of zeros in `table`. We
  3024. # check for that case here.
  3025. zero_cols = np.nonzero((table == 0).all(axis=0))[0]
  3026. if len(zero_cols) > 0:
  3027. msg = ("All values in sample %d are equal to the grand "
  3028. "median (%r), so they are ignored, resulting in an "
  3029. "empty sample." % (zero_cols[0] + 1, grand_median))
  3030. raise ValueError(msg)
  3031. stat, p, dof, expected = chi2_contingency(table, lambda_=lambda_,
  3032. correction=correction)
  3033. return MedianTestResult(stat, p, grand_median, table)
  3034. def _circfuncs_common(samples, high, low, nan_policy='propagate'):
  3035. # Ensure samples are array-like and size is not zero
  3036. samples = np.asarray(samples)
  3037. if samples.size == 0:
  3038. return np.nan, np.asarray(np.nan), np.asarray(np.nan), None
  3039. # Recast samples as radians that range between 0 and 2 pi and calculate
  3040. # the sine and cosine
  3041. sin_samp = sin((samples - low)*2.*pi / (high - low))
  3042. cos_samp = cos((samples - low)*2.*pi / (high - low))
  3043. # Apply the NaN policy
  3044. contains_nan, nan_policy = _contains_nan(samples, nan_policy)
  3045. if contains_nan and nan_policy == 'omit':
  3046. mask = np.isnan(samples)
  3047. # Set the sines and cosines that are NaN to zero
  3048. sin_samp[mask] = 0.0
  3049. cos_samp[mask] = 0.0
  3050. else:
  3051. mask = None
  3052. return samples, sin_samp, cos_samp, mask
  3053. def circmean(samples, high=2*pi, low=0, axis=None, nan_policy='propagate'):
  3054. """Compute the circular mean for samples in a range.
  3055. Parameters
  3056. ----------
  3057. samples : array_like
  3058. Input array.
  3059. high : float or int, optional
  3060. High boundary for the sample range. Default is ``2*pi``.
  3061. low : float or int, optional
  3062. Low boundary for the sample range. Default is 0.
  3063. axis : int, optional
  3064. Axis along which means are computed. The default is to compute
  3065. the mean of the flattened array.
  3066. nan_policy : {'propagate', 'raise', 'omit'}, optional
  3067. Defines how to handle when input contains nan. 'propagate' returns nan,
  3068. 'raise' throws an error, 'omit' performs the calculations ignoring nan
  3069. values. Default is 'propagate'.
  3070. Returns
  3071. -------
  3072. circmean : float
  3073. Circular mean.
  3074. See Also
  3075. --------
  3076. circstd : Circular standard deviation.
  3077. circvar : Circular variance.
  3078. Examples
  3079. --------
  3080. For simplicity, all angles are printed out in degrees.
  3081. >>> import numpy as np
  3082. >>> from scipy.stats import circmean
  3083. >>> import matplotlib.pyplot as plt
  3084. >>> angles = np.deg2rad(np.array([20, 30, 330]))
  3085. >>> circmean = circmean(angles)
  3086. >>> np.rad2deg(circmean)
  3087. 7.294976657784009
  3088. >>> mean = angles.mean()
  3089. >>> np.rad2deg(mean)
  3090. 126.66666666666666
  3091. Plot and compare the circular mean against the arithmetic mean.
  3092. >>> plt.plot(np.cos(np.linspace(0, 2*np.pi, 500)),
  3093. ... np.sin(np.linspace(0, 2*np.pi, 500)),
  3094. ... c='k')
  3095. >>> plt.scatter(np.cos(angles), np.sin(angles), c='k')
  3096. >>> plt.scatter(np.cos(circmean), np.sin(circmean), c='b',
  3097. ... label='circmean')
  3098. >>> plt.scatter(np.cos(mean), np.sin(mean), c='r', label='mean')
  3099. >>> plt.legend()
  3100. >>> plt.axis('equal')
  3101. >>> plt.show()
  3102. """
  3103. samples, sin_samp, cos_samp, nmask = _circfuncs_common(samples, high, low,
  3104. nan_policy=nan_policy)
  3105. sin_sum = sin_samp.sum(axis=axis)
  3106. cos_sum = cos_samp.sum(axis=axis)
  3107. res = arctan2(sin_sum, cos_sum)
  3108. mask_nan = ~np.isnan(res)
  3109. if mask_nan.ndim > 0:
  3110. mask = res[mask_nan] < 0
  3111. else:
  3112. mask = res < 0
  3113. if mask.ndim > 0:
  3114. mask_nan[mask_nan] = mask
  3115. res[mask_nan] += 2*pi
  3116. elif mask:
  3117. res += 2*pi
  3118. # Set output to NaN if no samples went into the mean
  3119. if nmask is not None:
  3120. if nmask.all():
  3121. res = np.full(shape=res.shape, fill_value=np.nan)
  3122. else:
  3123. # Find out if any of the axis that are being averaged consist
  3124. # entirely of NaN. If one exists, set the result (res) to NaN
  3125. nshape = 0 if axis is None else axis
  3126. smask = nmask.shape[nshape] == nmask.sum(axis=axis)
  3127. if smask.any():
  3128. res[smask] = np.nan
  3129. return res*(high - low)/2.0/pi + low
  3130. def circvar(samples, high=2*pi, low=0, axis=None, nan_policy='propagate'):
  3131. """Compute the circular variance for samples assumed to be in a range.
  3132. Parameters
  3133. ----------
  3134. samples : array_like
  3135. Input array.
  3136. high : float or int, optional
  3137. High boundary for the sample range. Default is ``2*pi``.
  3138. low : float or int, optional
  3139. Low boundary for the sample range. Default is 0.
  3140. axis : int, optional
  3141. Axis along which variances are computed. The default is to compute
  3142. the variance of the flattened array.
  3143. nan_policy : {'propagate', 'raise', 'omit'}, optional
  3144. Defines how to handle when input contains nan. 'propagate' returns nan,
  3145. 'raise' throws an error, 'omit' performs the calculations ignoring nan
  3146. values. Default is 'propagate'.
  3147. Returns
  3148. -------
  3149. circvar : float
  3150. Circular variance.
  3151. See Also
  3152. --------
  3153. circmean : Circular mean.
  3154. circstd : Circular standard deviation.
  3155. Notes
  3156. -----
  3157. This uses the following definition of circular variance: ``1-R``, where
  3158. ``R`` is the mean resultant vector. The
  3159. returned value is in the range [0, 1], 0 standing for no variance, and 1
  3160. for a large variance. In the limit of small angles, this value is similar
  3161. to half the 'linear' variance.
  3162. References
  3163. ----------
  3164. .. [1] Fisher, N.I. *Statistical analysis of circular data*. Cambridge
  3165. University Press, 1993.
  3166. Examples
  3167. --------
  3168. >>> import numpy as np
  3169. >>> from scipy.stats import circvar
  3170. >>> import matplotlib.pyplot as plt
  3171. >>> samples_1 = np.array([0.072, -0.158, 0.077, 0.108, 0.286,
  3172. ... 0.133, -0.473, -0.001, -0.348, 0.131])
  3173. >>> samples_2 = np.array([0.111, -0.879, 0.078, 0.733, 0.421,
  3174. ... 0.104, -0.136, -0.867, 0.012, 0.105])
  3175. >>> circvar_1 = circvar(samples_1)
  3176. >>> circvar_2 = circvar(samples_2)
  3177. Plot the samples.
  3178. >>> fig, (left, right) = plt.subplots(ncols=2)
  3179. >>> for image in (left, right):
  3180. ... image.plot(np.cos(np.linspace(0, 2*np.pi, 500)),
  3181. ... np.sin(np.linspace(0, 2*np.pi, 500)),
  3182. ... c='k')
  3183. ... image.axis('equal')
  3184. ... image.axis('off')
  3185. >>> left.scatter(np.cos(samples_1), np.sin(samples_1), c='k', s=15)
  3186. >>> left.set_title(f"circular variance: {np.round(circvar_1, 2)!r}")
  3187. >>> right.scatter(np.cos(samples_2), np.sin(samples_2), c='k', s=15)
  3188. >>> right.set_title(f"circular variance: {np.round(circvar_2, 2)!r}")
  3189. >>> plt.show()
  3190. """
  3191. samples, sin_samp, cos_samp, mask = _circfuncs_common(samples, high, low,
  3192. nan_policy=nan_policy)
  3193. if mask is None:
  3194. sin_mean = sin_samp.mean(axis=axis)
  3195. cos_mean = cos_samp.mean(axis=axis)
  3196. else:
  3197. nsum = np.asarray(np.sum(~mask, axis=axis).astype(float))
  3198. nsum[nsum == 0] = np.nan
  3199. sin_mean = sin_samp.sum(axis=axis) / nsum
  3200. cos_mean = cos_samp.sum(axis=axis) / nsum
  3201. # hypot can go slightly above 1 due to rounding errors
  3202. with np.errstate(invalid='ignore'):
  3203. R = np.minimum(1, hypot(sin_mean, cos_mean))
  3204. res = 1. - R
  3205. return res
  3206. def circstd(samples, high=2*pi, low=0, axis=None, nan_policy='propagate', *,
  3207. normalize=False):
  3208. """
  3209. Compute the circular standard deviation for samples assumed to be in the
  3210. range [low to high].
  3211. Parameters
  3212. ----------
  3213. samples : array_like
  3214. Input array.
  3215. high : float or int, optional
  3216. High boundary for the sample range. Default is ``2*pi``.
  3217. low : float or int, optional
  3218. Low boundary for the sample range. Default is 0.
  3219. axis : int, optional
  3220. Axis along which standard deviations are computed. The default is
  3221. to compute the standard deviation of the flattened array.
  3222. nan_policy : {'propagate', 'raise', 'omit'}, optional
  3223. Defines how to handle when input contains nan. 'propagate' returns nan,
  3224. 'raise' throws an error, 'omit' performs the calculations ignoring nan
  3225. values. Default is 'propagate'.
  3226. normalize : boolean, optional
  3227. If True, the returned value is equal to ``sqrt(-2*log(R))`` and does
  3228. not depend on the variable units. If False (default), the returned
  3229. value is scaled by ``((high-low)/(2*pi))``.
  3230. Returns
  3231. -------
  3232. circstd : float
  3233. Circular standard deviation.
  3234. See Also
  3235. --------
  3236. circmean : Circular mean.
  3237. circvar : Circular variance.
  3238. Notes
  3239. -----
  3240. This uses a definition of circular standard deviation from [1]_.
  3241. Essentially, the calculation is as follows.
  3242. .. code-block:: python
  3243. import numpy as np
  3244. C = np.cos(samples).mean()
  3245. S = np.sin(samples).mean()
  3246. R = np.sqrt(C**2 + S**2)
  3247. l = 2*np.pi / (high-low)
  3248. circstd = np.sqrt(-2*np.log(R)) / l
  3249. In the limit of small angles, it returns a number close to the 'linear'
  3250. standard deviation.
  3251. References
  3252. ----------
  3253. .. [1] Mardia, K. V. (1972). 2. In *Statistics of Directional Data*
  3254. (pp. 18-24). Academic Press. :doi:`10.1016/C2013-0-07425-7`.
  3255. Examples
  3256. --------
  3257. >>> import numpy as np
  3258. >>> from scipy.stats import circstd
  3259. >>> import matplotlib.pyplot as plt
  3260. >>> samples_1 = np.array([0.072, -0.158, 0.077, 0.108, 0.286,
  3261. ... 0.133, -0.473, -0.001, -0.348, 0.131])
  3262. >>> samples_2 = np.array([0.111, -0.879, 0.078, 0.733, 0.421,
  3263. ... 0.104, -0.136, -0.867, 0.012, 0.105])
  3264. >>> circstd_1 = circstd(samples_1)
  3265. >>> circstd_2 = circstd(samples_2)
  3266. Plot the samples.
  3267. >>> fig, (left, right) = plt.subplots(ncols=2)
  3268. >>> for image in (left, right):
  3269. ... image.plot(np.cos(np.linspace(0, 2*np.pi, 500)),
  3270. ... np.sin(np.linspace(0, 2*np.pi, 500)),
  3271. ... c='k')
  3272. ... image.axis('equal')
  3273. ... image.axis('off')
  3274. >>> left.scatter(np.cos(samples_1), np.sin(samples_1), c='k', s=15)
  3275. >>> left.set_title(f"circular std: {np.round(circstd_1, 2)!r}")
  3276. >>> right.plot(np.cos(np.linspace(0, 2*np.pi, 500)),
  3277. ... np.sin(np.linspace(0, 2*np.pi, 500)),
  3278. ... c='k')
  3279. >>> right.scatter(np.cos(samples_2), np.sin(samples_2), c='k', s=15)
  3280. >>> right.set_title(f"circular std: {np.round(circstd_2, 2)!r}")
  3281. >>> plt.show()
  3282. """
  3283. samples, sin_samp, cos_samp, mask = _circfuncs_common(samples, high, low,
  3284. nan_policy=nan_policy)
  3285. if mask is None:
  3286. sin_mean = sin_samp.mean(axis=axis) # [1] (2.2.3)
  3287. cos_mean = cos_samp.mean(axis=axis) # [1] (2.2.3)
  3288. else:
  3289. nsum = np.asarray(np.sum(~mask, axis=axis).astype(float))
  3290. nsum[nsum == 0] = np.nan
  3291. sin_mean = sin_samp.sum(axis=axis) / nsum
  3292. cos_mean = cos_samp.sum(axis=axis) / nsum
  3293. # hypot can go slightly above 1 due to rounding errors
  3294. with np.errstate(invalid='ignore'):
  3295. R = np.minimum(1, hypot(sin_mean, cos_mean)) # [1] (2.2.4)
  3296. res = sqrt(-2*log(R))
  3297. if not normalize:
  3298. res *= (high-low)/(2.*pi) # [1] (2.3.14) w/ (2.3.7)
  3299. return res
  3300. class DirectionalStats:
  3301. def __init__(self, mean_direction, mean_resultant_length):
  3302. self.mean_direction = mean_direction
  3303. self.mean_resultant_length = mean_resultant_length
  3304. def __repr__(self):
  3305. return (f"DirectionalStats(mean_direction={self.mean_direction},"
  3306. f" mean_resultant_length={self.mean_resultant_length})")
  3307. def directional_stats(samples, *, axis=0, normalize=True):
  3308. """
  3309. Computes sample statistics for directional data.
  3310. Computes the directional mean (also called the mean direction vector) and
  3311. mean resultant length of a sample of vectors.
  3312. The directional mean is a measure of "preferred direction" of vector data.
  3313. It is analogous to the sample mean, but it is for use when the length of
  3314. the data is irrelevant (e.g. unit vectors).
  3315. The mean resultant length is a value between 0 and 1 used to quantify the
  3316. dispersion of directional data: the smaller the mean resultant length, the
  3317. greater the dispersion. Several definitions of directional variance
  3318. involving the mean resultant length are given in [1]_ and [2]_.
  3319. Parameters
  3320. ----------
  3321. samples : array_like
  3322. Input array. Must be at least two-dimensional, and the last axis of the
  3323. input must correspond with the dimensionality of the vector space.
  3324. When the input is exactly two dimensional, this means that each row
  3325. of the data is a vector observation.
  3326. axis : int, default: 0
  3327. Axis along which the directional mean is computed.
  3328. normalize: boolean, default: True
  3329. If True, normalize the input to ensure that each observation is a
  3330. unit vector. It the observations are already unit vectors, consider
  3331. setting this to False to avoid unnecessary computation.
  3332. Returns
  3333. -------
  3334. res : DirectionalStats
  3335. An object containing attributes:
  3336. mean_direction : ndarray
  3337. Directional mean.
  3338. mean_resultant_length : ndarray
  3339. The mean resultant length [1]_.
  3340. See also
  3341. --------
  3342. circmean: circular mean; i.e. directional mean for 2D *angles*
  3343. circvar: circular variance; i.e. directional variance for 2D *angles*
  3344. Notes
  3345. -----
  3346. This uses a definition of directional mean from [1]_.
  3347. Assuming the observations are unit vectors, the calculation is as follows.
  3348. .. code-block:: python
  3349. mean = samples.mean(axis=0)
  3350. mean_resultant_length = np.linalg.norm(mean)
  3351. mean_direction = mean / mean_resultant_length
  3352. This definition is appropriate for *directional* data (i.e. vector data
  3353. for which the magnitude of each observation is irrelevant) but not
  3354. for *axial* data (i.e. vector data for which the magnitude and *sign* of
  3355. each observation is irrelevant).
  3356. Several definitions of directional variance involving the mean resultant
  3357. length ``R`` have been proposed, including ``1 - R`` [1]_, ``1 - R**2``
  3358. [2]_, and ``2 * (1 - R)`` [2]_. Rather than choosing one, this function
  3359. returns ``R`` as attribute `mean_resultant_length` so the user can compute
  3360. their preferred measure of dispersion.
  3361. References
  3362. ----------
  3363. .. [1] Mardia, Jupp. (2000). *Directional Statistics*
  3364. (p. 163). Wiley.
  3365. .. [2] https://en.wikipedia.org/wiki/Directional_statistics
  3366. Examples
  3367. --------
  3368. >>> import numpy as np
  3369. >>> from scipy.stats import directional_stats
  3370. >>> data = np.array([[3, 4], # first observation, 2D vector space
  3371. ... [6, -8]]) # second observation
  3372. >>> dirstats = directional_stats(data)
  3373. >>> dirstats.mean_direction
  3374. array([1., 0.])
  3375. In contrast, the regular sample mean of the vectors would be influenced
  3376. by the magnitude of each observation. Furthermore, the result would not be
  3377. a unit vector.
  3378. >>> data.mean(axis=0)
  3379. array([4.5, -2.])
  3380. An exemplary use case for `directional_stats` is to find a *meaningful*
  3381. center for a set of observations on a sphere, e.g. geographical locations.
  3382. >>> data = np.array([[0.8660254, 0.5, 0.],
  3383. ... [0.8660254, -0.5, 0.]])
  3384. >>> dirstats = directional_stats(data)
  3385. >>> dirstats.mean_direction
  3386. array([1., 0., 0.])
  3387. The regular sample mean on the other hand yields a result which does not
  3388. lie on the surface of the sphere.
  3389. >>> data.mean(axis=0)
  3390. array([0.8660254, 0., 0.])
  3391. The function also returns the mean resultant length, which
  3392. can be used to calculate a directional variance. For example, using the
  3393. definition ``Var(z) = 1 - R`` from [2]_ where ``R`` is the
  3394. mean resultant length, we can calculate the directional variance of the
  3395. vectors in the above example as:
  3396. >>> 1 - dirstats.mean_resultant_length
  3397. 0.13397459716167093
  3398. """
  3399. samples = np.asarray(samples)
  3400. if samples.ndim < 2:
  3401. raise ValueError("samples must at least be two-dimensional. "
  3402. f"Instead samples has shape: {samples.shape!r}")
  3403. samples = np.moveaxis(samples, axis, 0)
  3404. if normalize:
  3405. vectornorms = np.linalg.norm(samples, axis=-1, keepdims=True)
  3406. samples = samples/vectornorms
  3407. mean = np.mean(samples, axis=0)
  3408. mean_resultant_length = np.linalg.norm(mean, axis=-1, keepdims=True)
  3409. mean_direction = mean / mean_resultant_length
  3410. return DirectionalStats(mean_direction,
  3411. mean_resultant_length.squeeze(-1)[()])