univariate_statistics.hpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. // (C) Copyright Nick Thompson 2018.
  2. // (C) Copyright Matt Borland 2020.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_STATISTICS_UNIVARIATE_STATISTICS_HPP
  7. #define BOOST_MATH_STATISTICS_UNIVARIATE_STATISTICS_HPP
  8. #include <boost/math/statistics/detail/single_pass.hpp>
  9. #include <boost/config.hpp>
  10. #include <boost/assert.hpp>
  11. #include <algorithm>
  12. #include <iterator>
  13. #include <tuple>
  14. #include <cmath>
  15. #include <vector>
  16. #include <type_traits>
  17. #include <utility>
  18. #include <numeric>
  19. #include <list>
  20. // Support compilers with P0024R2 implemented without linking TBB
  21. // https://en.cppreference.com/w/cpp/compiler_support
  22. #ifndef BOOST_NO_CXX17_HDR_EXECUTION
  23. #include <execution>
  24. namespace boost::math::statistics {
  25. template<class ExecutionPolicy, class ForwardIterator>
  26. inline auto mean(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  27. {
  28. using Real = typename std::iterator_traits<ForwardIterator>::value_type;
  29. BOOST_ASSERT_MSG(first != last, "At least one sample is required to compute the mean.");
  30. if constexpr (std::is_integral_v<Real>)
  31. {
  32. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  33. {
  34. return detail::mean_sequential_impl<double>(first, last);
  35. }
  36. else
  37. {
  38. return std::reduce(exec, first, last, 0.0) / std::distance(first, last);
  39. }
  40. }
  41. else
  42. {
  43. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  44. {
  45. return detail::mean_sequential_impl<Real>(first, last);
  46. }
  47. else
  48. {
  49. return std::reduce(exec, first, last, Real(0.0)) / Real(std::distance(first, last));
  50. }
  51. }
  52. }
  53. template<class ExecutionPolicy, class Container>
  54. inline auto mean(ExecutionPolicy&& exec, Container const & v)
  55. {
  56. return mean(exec, std::cbegin(v), std::cend(v));
  57. }
  58. template<class ForwardIterator>
  59. inline auto mean(ForwardIterator first, ForwardIterator last)
  60. {
  61. return mean(std::execution::seq, first, last);
  62. }
  63. template<class Container>
  64. inline auto mean(Container const & v)
  65. {
  66. return mean(std::execution::seq, std::cbegin(v), std::cend(v));
  67. }
  68. template<class ExecutionPolicy, class ForwardIterator>
  69. inline auto variance(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  70. {
  71. using Real = typename std::iterator_traits<ForwardIterator>::value_type;
  72. if constexpr (std::is_integral_v<Real>)
  73. {
  74. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  75. {
  76. return std::get<2>(detail::variance_sequential_impl<std::tuple<double, double, double, double>>(first, last));
  77. }
  78. else
  79. {
  80. const auto results = detail::first_four_moments_parallel_impl<std::tuple<double, double, double, double, double>>(first, last);
  81. return std::get<1>(results) / std::get<4>(results);
  82. }
  83. }
  84. else
  85. {
  86. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  87. {
  88. return std::get<2>(detail::variance_sequential_impl<std::tuple<Real, Real, Real, Real>>(first, last));
  89. }
  90. else
  91. {
  92. const auto results = detail::first_four_moments_parallel_impl<std::tuple<Real, Real, Real, Real, Real>>(first, last);
  93. return std::get<1>(results) / std::get<4>(results);
  94. }
  95. }
  96. }
  97. template<class ExecutionPolicy, class Container>
  98. inline auto variance(ExecutionPolicy&& exec, Container const & v)
  99. {
  100. return variance(exec, std::cbegin(v), std::cend(v));
  101. }
  102. template<class ForwardIterator>
  103. inline auto variance(ForwardIterator first, ForwardIterator last)
  104. {
  105. return variance(std::execution::seq, first, last);
  106. }
  107. template<class Container>
  108. inline auto variance(Container const & v)
  109. {
  110. return variance(std::execution::seq, std::cbegin(v), std::cend(v));
  111. }
  112. template<class ExecutionPolicy, class ForwardIterator>
  113. inline auto sample_variance(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  114. {
  115. const auto n = std::distance(first, last);
  116. BOOST_ASSERT_MSG(n > 1, "At least two samples are required to compute the sample variance.");
  117. return n*variance(exec, first, last)/(n-1);
  118. }
  119. template<class ExecutionPolicy, class Container>
  120. inline auto sample_variance(ExecutionPolicy&& exec, Container const & v)
  121. {
  122. return sample_variance(exec, std::cbegin(v), std::cend(v));
  123. }
  124. template<class ForwardIterator>
  125. inline auto sample_variance(ForwardIterator first, ForwardIterator last)
  126. {
  127. return sample_variance(std::execution::seq, first, last);
  128. }
  129. template<class Container>
  130. inline auto sample_variance(Container const & v)
  131. {
  132. return sample_variance(std::execution::seq, std::cbegin(v), std::cend(v));
  133. }
  134. template<class ExecutionPolicy, class ForwardIterator>
  135. inline auto mean_and_sample_variance(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  136. {
  137. using Real = typename std::iterator_traits<ForwardIterator>::value_type;
  138. if constexpr (std::is_integral_v<Real>)
  139. {
  140. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  141. {
  142. const auto results = detail::variance_sequential_impl<std::tuple<double, double, double, double>>(first, last);
  143. return std::make_pair(std::get<0>(results), std::get<2>(results)*std::get<3>(results)/(std::get<3>(results)-1.0));
  144. }
  145. else
  146. {
  147. const auto results = detail::first_four_moments_parallel_impl<std::tuple<double, double, double, double, double>>(first, last);
  148. return std::make_pair(std::get<0>(results), std::get<1>(results) / (std::get<4>(results)-1.0));
  149. }
  150. }
  151. else
  152. {
  153. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  154. {
  155. const auto results = detail::variance_sequential_impl<std::tuple<Real, Real, Real, Real>>(first, last);
  156. return std::make_pair(std::get<0>(results), std::get<2>(results)*std::get<3>(results)/(std::get<3>(results)-Real(1)));
  157. }
  158. else
  159. {
  160. const auto results = detail::first_four_moments_parallel_impl<std::tuple<Real, Real, Real, Real, Real>>(first, last);
  161. return std::make_pair(std::get<0>(results), std::get<1>(results) / (std::get<4>(results)-Real(1)));
  162. }
  163. }
  164. }
  165. template<class ExecutionPolicy, class Container>
  166. inline auto mean_and_sample_variance(ExecutionPolicy&& exec, Container const & v)
  167. {
  168. return mean_and_sample_variance(exec, std::cbegin(v), std::cend(v));
  169. }
  170. template<class ForwardIterator>
  171. inline auto mean_and_sample_variance(ForwardIterator first, ForwardIterator last)
  172. {
  173. return mean_and_sample_variance(std::execution::seq, first, last);
  174. }
  175. template<class Container>
  176. inline auto mean_and_sample_variance(Container const & v)
  177. {
  178. return mean_and_sample_variance(std::execution::seq, std::cbegin(v), std::cend(v));
  179. }
  180. template<class ExecutionPolicy, class ForwardIterator>
  181. inline auto first_four_moments(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  182. {
  183. using Real = typename std::iterator_traits<ForwardIterator>::value_type;
  184. if constexpr (std::is_integral_v<Real>)
  185. {
  186. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  187. {
  188. const auto results = detail::first_four_moments_sequential_impl<std::tuple<double, double, double, double, double>>(first, last);
  189. return std::make_tuple(std::get<0>(results), std::get<1>(results) / std::get<4>(results), std::get<2>(results) / std::get<4>(results),
  190. std::get<3>(results) / std::get<4>(results));
  191. }
  192. else
  193. {
  194. const auto results = detail::first_four_moments_parallel_impl<std::tuple<double, double, double, double, double>>(first, last);
  195. return std::make_tuple(std::get<0>(results), std::get<1>(results) / std::get<4>(results), std::get<2>(results) / std::get<4>(results),
  196. std::get<3>(results) / std::get<4>(results));
  197. }
  198. }
  199. else
  200. {
  201. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  202. {
  203. const auto results = detail::first_four_moments_sequential_impl<std::tuple<Real, Real, Real, Real, Real>>(first, last);
  204. return std::make_tuple(std::get<0>(results), std::get<1>(results) / std::get<4>(results), std::get<2>(results) / std::get<4>(results),
  205. std::get<3>(results) / std::get<4>(results));
  206. }
  207. else
  208. {
  209. const auto results = detail::first_four_moments_parallel_impl<std::tuple<Real, Real, Real, Real, Real>>(first, last);
  210. return std::make_tuple(std::get<0>(results), std::get<1>(results) / std::get<4>(results), std::get<2>(results) / std::get<4>(results),
  211. std::get<3>(results) / std::get<4>(results));
  212. }
  213. }
  214. }
  215. template<class ExecutionPolicy, class Container>
  216. inline auto first_four_moments(ExecutionPolicy&& exec, Container const & v)
  217. {
  218. return first_four_moments(exec, std::cbegin(v), std::cend(v));
  219. }
  220. template<class ForwardIterator>
  221. inline auto first_four_moments(ForwardIterator first, ForwardIterator last)
  222. {
  223. return first_four_moments(std::execution::seq, first, last);
  224. }
  225. template<class Container>
  226. inline auto first_four_moments(Container const & v)
  227. {
  228. return first_four_moments(std::execution::seq, std::cbegin(v), std::cend(v));
  229. }
  230. // https://prod.sandia.gov/techlib-noauth/access-control.cgi/2008/086212.pdf
  231. template<class ExecutionPolicy, class ForwardIterator>
  232. inline auto skewness(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  233. {
  234. using Real = typename std::iterator_traits<ForwardIterator>::value_type;
  235. using std::sqrt;
  236. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  237. {
  238. if constexpr (std::is_integral_v<Real>)
  239. {
  240. return detail::skewness_sequential_impl<double>(first, last);
  241. }
  242. else
  243. {
  244. return detail::skewness_sequential_impl<Real>(first, last);
  245. }
  246. }
  247. else
  248. {
  249. const auto [M1, M2, M3, M4] = first_four_moments(exec, first, last);
  250. const auto n = std::distance(first, last);
  251. const auto var = M2/(n-1);
  252. if (M2 == 0)
  253. {
  254. // The limit is technically undefined, but the interpretation here is clear:
  255. // A constant dataset has no skewness.
  256. if constexpr (std::is_integral_v<Real>)
  257. {
  258. return double(0);
  259. }
  260. else
  261. {
  262. return Real(0);
  263. }
  264. }
  265. else
  266. {
  267. return M3/(M2*sqrt(var)) / Real(2);
  268. }
  269. }
  270. }
  271. template<class ExecutionPolicy, class Container>
  272. inline auto skewness(ExecutionPolicy&& exec, Container & v)
  273. {
  274. return skewness(exec, std::cbegin(v), std::cend(v));
  275. }
  276. template<class ForwardIterator>
  277. inline auto skewness(ForwardIterator first, ForwardIterator last)
  278. {
  279. return skewness(std::execution::seq, first, last);
  280. }
  281. template<class Container>
  282. inline auto skewness(Container const & v)
  283. {
  284. return skewness(std::execution::seq, std::cbegin(v), std::cend(v));
  285. }
  286. // Follows equation 1.6 of:
  287. // https://prod.sandia.gov/techlib-noauth/access-control.cgi/2008/086212.pdf
  288. template<class ExecutionPolicy, class ForwardIterator>
  289. inline auto kurtosis(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  290. {
  291. const auto [M1, M2, M3, M4] = first_four_moments(exec, first, last);
  292. if (M2 == 0)
  293. {
  294. return M2;
  295. }
  296. return M4/(M2*M2);
  297. }
  298. template<class ExecutionPolicy, class Container>
  299. inline auto kurtosis(ExecutionPolicy&& exec, Container const & v)
  300. {
  301. return kurtosis(exec, std::cbegin(v), std::cend(v));
  302. }
  303. template<class ForwardIterator>
  304. inline auto kurtosis(ForwardIterator first, ForwardIterator last)
  305. {
  306. return kurtosis(std::execution::seq, first, last);
  307. }
  308. template<class Container>
  309. inline auto kurtosis(Container const & v)
  310. {
  311. return kurtosis(std::execution::seq, std::cbegin(v), std::cend(v));
  312. }
  313. template<class ExecutionPolicy, class ForwardIterator>
  314. inline auto excess_kurtosis(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  315. {
  316. return kurtosis(exec, first, last) - 3;
  317. }
  318. template<class ExecutionPolicy, class Container>
  319. inline auto excess_kurtosis(ExecutionPolicy&& exec, Container const & v)
  320. {
  321. return excess_kurtosis(exec, std::cbegin(v), std::cend(v));
  322. }
  323. template<class ForwardIterator>
  324. inline auto excess_kurtosis(ForwardIterator first, ForwardIterator last)
  325. {
  326. return excess_kurtosis(std::execution::seq, first, last);
  327. }
  328. template<class Container>
  329. inline auto excess_kurtosis(Container const & v)
  330. {
  331. return excess_kurtosis(std::execution::seq, std::cbegin(v), std::cend(v));
  332. }
  333. template<class ExecutionPolicy, class RandomAccessIterator>
  334. auto median(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last)
  335. {
  336. const auto num_elems = std::distance(first, last);
  337. BOOST_ASSERT_MSG(num_elems > 0, "The median of a zero length vector is undefined.");
  338. if (num_elems & 1)
  339. {
  340. auto middle = first + (num_elems - 1)/2;
  341. std::nth_element(exec, first, middle, last);
  342. return *middle;
  343. }
  344. else
  345. {
  346. auto middle = first + num_elems/2 - 1;
  347. std::nth_element(exec, first, middle, last);
  348. std::nth_element(exec, middle, middle+1, last);
  349. return (*middle + *(middle+1))/2;
  350. }
  351. }
  352. template<class ExecutionPolicy, class RandomAccessContainer>
  353. inline auto median(ExecutionPolicy&& exec, RandomAccessContainer & v)
  354. {
  355. return median(exec, std::begin(v), std::end(v));
  356. }
  357. template<class RandomAccessIterator>
  358. inline auto median(RandomAccessIterator first, RandomAccessIterator last)
  359. {
  360. return median(std::execution::seq, first, last);
  361. }
  362. template<class RandomAccessContainer>
  363. inline auto median(RandomAccessContainer & v)
  364. {
  365. return median(std::execution::seq, std::begin(v), std::end(v));
  366. }
  367. #if 0
  368. //
  369. // Parallel gini calculation is curently broken, see:
  370. // https://github.com/boostorg/math/issues/585
  371. // We will fix this at a later date, for now just use a serial implementation:
  372. //
  373. template<class ExecutionPolicy, class RandomAccessIterator>
  374. inline auto gini_coefficient(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last)
  375. {
  376. using Real = typename std::iterator_traits<RandomAccessIterator>::value_type;
  377. if(!std::is_sorted(exec, first, last))
  378. {
  379. std::sort(exec, first, last);
  380. }
  381. if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>)
  382. {
  383. if constexpr (std::is_integral_v<Real>)
  384. {
  385. return detail::gini_coefficient_sequential_impl<double>(first, last);
  386. }
  387. else
  388. {
  389. return detail::gini_coefficient_sequential_impl<Real>(first, last);
  390. }
  391. }
  392. else if constexpr (std::is_integral_v<Real>)
  393. {
  394. return detail::gini_coefficient_parallel_impl<double>(exec, first, last);
  395. }
  396. else
  397. {
  398. return detail::gini_coefficient_parallel_impl<Real>(exec, first, last);
  399. }
  400. }
  401. #else
  402. template<class ExecutionPolicy, class RandomAccessIterator>
  403. inline auto gini_coefficient(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last)
  404. {
  405. using Real = typename std::iterator_traits<RandomAccessIterator>::value_type;
  406. if (!std::is_sorted(exec, first, last))
  407. {
  408. std::sort(exec, first, last);
  409. }
  410. if constexpr (std::is_integral_v<Real>)
  411. {
  412. return detail::gini_coefficient_sequential_impl<double>(first, last);
  413. }
  414. else
  415. {
  416. return detail::gini_coefficient_sequential_impl<Real>(first, last);
  417. }
  418. }
  419. #endif
  420. template<class ExecutionPolicy, class RandomAccessContainer>
  421. inline auto gini_coefficient(ExecutionPolicy&& exec, RandomAccessContainer & v)
  422. {
  423. return gini_coefficient(exec, std::begin(v), std::end(v));
  424. }
  425. template<class RandomAccessIterator>
  426. inline auto gini_coefficient(RandomAccessIterator first, RandomAccessIterator last)
  427. {
  428. return gini_coefficient(std::execution::seq, first, last);
  429. }
  430. template<class RandomAccessContainer>
  431. inline auto gini_coefficient(RandomAccessContainer & v)
  432. {
  433. return gini_coefficient(std::execution::seq, std::begin(v), std::end(v));
  434. }
  435. template<class ExecutionPolicy, class RandomAccessIterator>
  436. inline auto sample_gini_coefficient(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last)
  437. {
  438. const auto n = std::distance(first, last);
  439. return n*gini_coefficient(exec, first, last)/(n-1);
  440. }
  441. template<class ExecutionPolicy, class RandomAccessContainer>
  442. inline auto sample_gini_coefficient(ExecutionPolicy&& exec, RandomAccessContainer & v)
  443. {
  444. return sample_gini_coefficient(exec, std::begin(v), std::end(v));
  445. }
  446. template<class RandomAccessIterator>
  447. inline auto sample_gini_coefficient(RandomAccessIterator first, RandomAccessIterator last)
  448. {
  449. return sample_gini_coefficient(std::execution::seq, first, last);
  450. }
  451. template<class RandomAccessContainer>
  452. inline auto sample_gini_coefficient(RandomAccessContainer & v)
  453. {
  454. return sample_gini_coefficient(std::execution::seq, std::begin(v), std::end(v));
  455. }
  456. template<class ExecutionPolicy, class RandomAccessIterator>
  457. auto median_absolute_deviation(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last,
  458. typename std::iterator_traits<RandomAccessIterator>::value_type center=std::numeric_limits<typename std::iterator_traits<RandomAccessIterator>::value_type>::quiet_NaN())
  459. {
  460. using std::abs;
  461. using Real = typename std::iterator_traits<RandomAccessIterator>::value_type;
  462. using std::isnan;
  463. if (isnan(center))
  464. {
  465. center = boost::math::statistics::median(exec, first, last);
  466. }
  467. const auto num_elems = std::distance(first, last);
  468. BOOST_ASSERT_MSG(num_elems > 0, "The median of a zero-length vector is undefined.");
  469. auto comparator = [&center](Real a, Real b) { return abs(a-center) < abs(b-center);};
  470. if (num_elems & 1)
  471. {
  472. auto middle = first + (num_elems - 1)/2;
  473. std::nth_element(exec, first, middle, last, comparator);
  474. return abs(*middle);
  475. }
  476. else
  477. {
  478. auto middle = first + num_elems/2 - 1;
  479. std::nth_element(exec, first, middle, last, comparator);
  480. std::nth_element(exec, middle, middle+1, last, comparator);
  481. return (abs(*middle) + abs(*(middle+1)))/abs(static_cast<Real>(2));
  482. }
  483. }
  484. template<class ExecutionPolicy, class RandomAccessContainer>
  485. inline auto median_absolute_deviation(ExecutionPolicy&& exec, RandomAccessContainer & v,
  486. typename RandomAccessContainer::value_type center=std::numeric_limits<typename RandomAccessContainer::value_type>::quiet_NaN())
  487. {
  488. return median_absolute_deviation(exec, std::begin(v), std::end(v), center);
  489. }
  490. template<class RandomAccessIterator>
  491. inline auto median_absolute_deviation(RandomAccessIterator first, RandomAccessIterator last,
  492. typename RandomAccessIterator::value_type center=std::numeric_limits<typename RandomAccessIterator::value_type>::quiet_NaN())
  493. {
  494. return median_absolute_deviation(std::execution::seq, first, last, center);
  495. }
  496. template<class RandomAccessContainer>
  497. inline auto median_absolute_deviation(RandomAccessContainer & v,
  498. typename RandomAccessContainer::value_type center=std::numeric_limits<typename RandomAccessContainer::value_type>::quiet_NaN())
  499. {
  500. return median_absolute_deviation(std::execution::seq, std::begin(v), std::end(v), center);
  501. }
  502. template<class ExecutionPolicy, class ForwardIterator>
  503. auto interquartile_range(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  504. {
  505. using Real = typename std::iterator_traits<ForwardIterator>::value_type;
  506. static_assert(!std::is_integral_v<Real>, "Integer values have not yet been implemented.");
  507. auto m = std::distance(first,last);
  508. BOOST_ASSERT_MSG(m >= 3, "At least 3 samples are required to compute the interquartile range.");
  509. auto k = m/4;
  510. auto j = m - (4*k);
  511. // m = 4k+j.
  512. // If j = 0 or j = 1, then there are an even number of samples below the median, and an even number above the median.
  513. // Then we must average adjacent elements to get the quartiles.
  514. // If j = 2 or j = 3, there are an odd number of samples above and below the median, these elements may be directly extracted to get the quartiles.
  515. if (j==2 || j==3)
  516. {
  517. auto q1 = first + k;
  518. auto q3 = first + 3*k + j - 1;
  519. std::nth_element(exec, first, q1, last);
  520. Real Q1 = *q1;
  521. std::nth_element(exec, q1, q3, last);
  522. Real Q3 = *q3;
  523. return Q3 - Q1;
  524. } else {
  525. // j == 0 or j==1:
  526. auto q1 = first + k - 1;
  527. auto q3 = first + 3*k - 1 + j;
  528. std::nth_element(exec, first, q1, last);
  529. Real a = *q1;
  530. std::nth_element(exec, q1, q1 + 1, last);
  531. Real b = *(q1 + 1);
  532. Real Q1 = (a+b)/2;
  533. std::nth_element(exec, q1, q3, last);
  534. a = *q3;
  535. std::nth_element(exec, q3, q3 + 1, last);
  536. b = *(q3 + 1);
  537. Real Q3 = (a+b)/2;
  538. return Q3 - Q1;
  539. }
  540. }
  541. template<class ExecutionPolicy, class RandomAccessContainer>
  542. inline auto interquartile_range(ExecutionPolicy&& exec, RandomAccessContainer & v)
  543. {
  544. return interquartile_range(exec, std::begin(v), std::end(v));
  545. }
  546. template<class RandomAccessIterator>
  547. inline auto interquartile_range(RandomAccessIterator first, RandomAccessIterator last)
  548. {
  549. return interquartile_range(std::execution::seq, first, last);
  550. }
  551. template<class RandomAccessContainer>
  552. inline auto interquartile_range(RandomAccessContainer & v)
  553. {
  554. return interquartile_range(std::execution::seq, std::begin(v), std::end(v));
  555. }
  556. template<class ExecutionPolicy, class ForwardIterator, class OutputIterator>
  557. inline OutputIterator mode(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, OutputIterator output)
  558. {
  559. if(!std::is_sorted(exec, first, last))
  560. {
  561. if constexpr (std::is_same_v<typename std::iterator_traits<ForwardIterator>::iterator_category(), std::random_access_iterator_tag>)
  562. {
  563. std::sort(exec, first, last);
  564. }
  565. else
  566. {
  567. BOOST_ASSERT("Data must be sorted for sequential mode calculation");
  568. }
  569. }
  570. return detail::mode_impl(first, last, output);
  571. }
  572. template<class ExecutionPolicy, class Container, class OutputIterator>
  573. inline OutputIterator mode(ExecutionPolicy&& exec, Container & v, OutputIterator output)
  574. {
  575. return mode(exec, std::begin(v), std::end(v), output);
  576. }
  577. template<class ForwardIterator, class OutputIterator>
  578. inline OutputIterator mode(ForwardIterator first, ForwardIterator last, OutputIterator output)
  579. {
  580. return mode(std::execution::seq, first, last, output);
  581. }
  582. // Requires enable_if_t to not clash with impl that returns std::list
  583. // Very ugly. std::is_execution_policy_v returns false for the std::execution objects and decltype of the objects (e.g. std::execution::seq)
  584. template<class Container, class OutputIterator, std::enable_if_t<!std::is_convertible_v<std::execution::sequenced_policy, Container> &&
  585. !std::is_convertible_v<std::execution::parallel_unsequenced_policy, Container> &&
  586. !std::is_convertible_v<std::execution::parallel_policy, Container>
  587. #if __cpp_lib_execution > 201900
  588. && !std::is_convertible_v<std::execution::unsequenced_policy, Container>
  589. #endif
  590. , bool> = true>
  591. inline OutputIterator mode(Container & v, OutputIterator output)
  592. {
  593. return mode(std::execution::seq, std::begin(v), std::end(v), output);
  594. }
  595. // std::list is the return type for the proposed STL stats library
  596. template<class ExecutionPolicy, class ForwardIterator, class Real = typename std::iterator_traits<ForwardIterator>::value_type>
  597. inline auto mode(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last)
  598. {
  599. std::list<Real> modes;
  600. mode(exec, first, last, std::inserter(modes, modes.begin()));
  601. return modes;
  602. }
  603. template<class ExecutionPolicy, class Container>
  604. inline auto mode(ExecutionPolicy&& exec, Container & v)
  605. {
  606. return mode(exec, std::begin(v), std::end(v));
  607. }
  608. template<class ForwardIterator>
  609. inline auto mode(ForwardIterator first, ForwardIterator last)
  610. {
  611. return mode(std::execution::seq, first, last);
  612. }
  613. template<class Container>
  614. inline auto mode(Container & v)
  615. {
  616. return mode(std::execution::seq, std::begin(v), std::end(v));
  617. }
  618. } // Namespace boost::math::statistics
  619. #else // Backwards compatible bindings for C++11
  620. namespace boost { namespace math { namespace statistics {
  621. template<bool B, class T = void>
  622. using enable_if_t = typename std::enable_if<B, T>::type;
  623. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  624. enable_if_t<std::is_integral<Real>::value, bool> = true>
  625. inline double mean(const ForwardIterator first, const ForwardIterator last)
  626. {
  627. BOOST_ASSERT_MSG(first != last, "At least one sample is required to compute the mean.");
  628. return detail::mean_sequential_impl<double>(first, last);
  629. }
  630. template<class Container, typename Real = typename Container::value_type,
  631. enable_if_t<std::is_integral<Real>::value, bool> = true>
  632. inline double mean(const Container& c)
  633. {
  634. return mean(std::begin(c), std::end(c));
  635. }
  636. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  637. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  638. inline Real mean(const ForwardIterator first, const ForwardIterator last)
  639. {
  640. BOOST_ASSERT_MSG(first != last, "At least one sample is required to compute the mean.");
  641. return detail::mean_sequential_impl<Real>(first, last);
  642. }
  643. template<class Container, typename Real = typename Container::value_type,
  644. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  645. inline Real mean(const Container& c)
  646. {
  647. return mean(std::begin(c), std::end(c));
  648. }
  649. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  650. enable_if_t<std::is_integral<Real>::value, bool> = true>
  651. inline double variance(const ForwardIterator first, const ForwardIterator last)
  652. {
  653. return std::get<2>(detail::variance_sequential_impl<std::tuple<double, double, double, double>>(first, last));
  654. }
  655. template<class Container, typename Real = typename Container::value_type,
  656. enable_if_t<std::is_integral<Real>::value, bool> = true>
  657. inline double variance(const Container& c)
  658. {
  659. return variance(std::begin(c), std::end(c));
  660. }
  661. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  662. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  663. inline Real variance(const ForwardIterator first, const ForwardIterator last)
  664. {
  665. return std::get<2>(detail::variance_sequential_impl<std::tuple<Real, Real, Real, Real>>(first, last));
  666. }
  667. template<class Container, typename Real = typename Container::value_type,
  668. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  669. inline Real variance(const Container& c)
  670. {
  671. return variance(std::begin(c), std::end(c));
  672. }
  673. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  674. enable_if_t<std::is_integral<Real>::value, bool> = true>
  675. inline double sample_variance(const ForwardIterator first, const ForwardIterator last)
  676. {
  677. const auto n = std::distance(first, last);
  678. BOOST_ASSERT_MSG(n > 1, "At least two samples are required to compute the sample variance.");
  679. return n*variance(first, last)/(n-1);
  680. }
  681. template<class Container, typename Real = typename Container::value_type,
  682. enable_if_t<std::is_integral<Real>::value, bool> = true>
  683. inline double sample_variance(const Container& c)
  684. {
  685. return sample_variance(std::begin(c), std::end(c));
  686. }
  687. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  688. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  689. inline Real sample_variance(const ForwardIterator first, const ForwardIterator last)
  690. {
  691. const auto n = std::distance(first, last);
  692. BOOST_ASSERT_MSG(n > 1, "At least two samples are required to compute the sample variance.");
  693. return n*variance(first, last)/(n-1);
  694. }
  695. template<class Container, typename Real = typename Container::value_type,
  696. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  697. inline Real sample_variance(const Container& c)
  698. {
  699. return sample_variance(std::begin(c), std::end(c));
  700. }
  701. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  702. enable_if_t<std::is_integral<Real>::value, bool> = true>
  703. inline std::pair<double, double> mean_and_sample_variance(const ForwardIterator first, const ForwardIterator last)
  704. {
  705. const auto results = detail::variance_sequential_impl<std::tuple<double, double, double, double>>(first, last);
  706. return std::make_pair(std::get<0>(results), std::get<3>(results)*std::get<2>(results)/(std::get<3>(results)-1.0));
  707. }
  708. template<class Container, typename Real = typename Container::value_type,
  709. enable_if_t<std::is_integral<Real>::value, bool> = true>
  710. inline std::pair<double, double> mean_and_sample_variance(const Container& c)
  711. {
  712. return mean_and_sample_variance(std::begin(c), std::end(c));
  713. }
  714. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  715. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  716. inline std::pair<Real, Real> mean_and_sample_variance(const ForwardIterator first, const ForwardIterator last)
  717. {
  718. const auto results = detail::variance_sequential_impl<std::tuple<Real, Real, Real, Real>>(first, last);
  719. return std::make_pair(std::get<0>(results), std::get<3>(results)*std::get<2>(results)/(std::get<3>(results)-Real(1)));
  720. }
  721. template<class Container, typename Real = typename Container::value_type,
  722. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  723. inline std::pair<Real, Real> mean_and_sample_variance(const Container& c)
  724. {
  725. return mean_and_sample_variance(std::begin(c), std::end(c));
  726. }
  727. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  728. enable_if_t<std::is_integral<Real>::value, bool> = true>
  729. inline std::tuple<double, double, double, double> first_four_moments(const ForwardIterator first, const ForwardIterator last)
  730. {
  731. const auto results = detail::first_four_moments_sequential_impl<std::tuple<double, double, double, double, double>>(first, last);
  732. return std::make_tuple(std::get<0>(results), std::get<1>(results) / std::get<4>(results), std::get<2>(results) / std::get<4>(results),
  733. std::get<3>(results) / std::get<4>(results));
  734. }
  735. template<class Container, typename Real = typename Container::value_type,
  736. enable_if_t<std::is_integral<Real>::value, bool> = true>
  737. inline std::tuple<double, double, double, double> first_four_moments(const Container& c)
  738. {
  739. return first_four_moments(std::begin(c), std::end(c));
  740. }
  741. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  742. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  743. inline std::tuple<Real, Real, Real, Real> first_four_moments(const ForwardIterator first, const ForwardIterator last)
  744. {
  745. const auto results = detail::first_four_moments_sequential_impl<std::tuple<Real, Real, Real, Real, Real>>(first, last);
  746. return std::make_tuple(std::get<0>(results), std::get<1>(results) / std::get<4>(results), std::get<2>(results) / std::get<4>(results),
  747. std::get<3>(results) / std::get<4>(results));
  748. }
  749. template<class Container, typename Real = typename Container::value_type,
  750. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  751. inline std::tuple<Real, Real, Real, Real> first_four_moments(const Container& c)
  752. {
  753. return first_four_moments(std::begin(c), std::end(c));
  754. }
  755. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  756. enable_if_t<std::is_integral<Real>::value, bool> = true>
  757. inline double skewness(const ForwardIterator first, const ForwardIterator last)
  758. {
  759. return detail::skewness_sequential_impl<double>(first, last);
  760. }
  761. template<class Container, typename Real = typename Container::value_type,
  762. enable_if_t<std::is_integral<Real>::value, bool> = true>
  763. inline double skewness(const Container& c)
  764. {
  765. return skewness(std::begin(c), std::end(c));
  766. }
  767. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  768. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  769. inline Real skewness(const ForwardIterator first, const ForwardIterator last)
  770. {
  771. return detail::skewness_sequential_impl<Real>(first, last);
  772. }
  773. template<class Container, typename Real = typename Container::value_type,
  774. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  775. inline Real skewness(const Container& c)
  776. {
  777. return skewness(std::begin(c), std::end(c));
  778. }
  779. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  780. enable_if_t<std::is_integral<Real>::value, bool> = true>
  781. inline double kurtosis(const ForwardIterator first, const ForwardIterator last)
  782. {
  783. std::tuple<double, double, double, double> M = first_four_moments(first, last);
  784. if(std::get<1>(M) == 0)
  785. {
  786. return std::get<1>(M);
  787. }
  788. else
  789. {
  790. return std::get<3>(M)/(std::get<1>(M)*std::get<1>(M));
  791. }
  792. }
  793. template<class Container, typename Real = typename Container::value_type,
  794. enable_if_t<std::is_integral<Real>::value, bool> = true>
  795. inline double kurtosis(const Container& c)
  796. {
  797. return kurtosis(std::begin(c), std::end(c));
  798. }
  799. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  800. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  801. inline Real kurtosis(const ForwardIterator first, const ForwardIterator last)
  802. {
  803. std::tuple<Real, Real, Real, Real> M = first_four_moments(first, last);
  804. if(std::get<1>(M) == 0)
  805. {
  806. return std::get<1>(M);
  807. }
  808. else
  809. {
  810. return std::get<3>(M)/(std::get<1>(M)*std::get<1>(M));
  811. }
  812. }
  813. template<class Container, typename Real = typename Container::value_type,
  814. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  815. inline Real kurtosis(const Container& c)
  816. {
  817. return kurtosis(std::begin(c), std::end(c));
  818. }
  819. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  820. enable_if_t<std::is_integral<Real>::value, bool> = true>
  821. inline double excess_kurtosis(const ForwardIterator first, const ForwardIterator last)
  822. {
  823. return kurtosis(first, last) - 3;
  824. }
  825. template<class Container, typename Real = typename Container::value_type,
  826. enable_if_t<std::is_integral<Real>::value, bool> = true>
  827. inline double excess_kurtosis(const Container& c)
  828. {
  829. return excess_kurtosis(std::begin(c), std::end(c));
  830. }
  831. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type,
  832. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  833. inline Real excess_kurtosis(const ForwardIterator first, const ForwardIterator last)
  834. {
  835. return kurtosis(first, last) - 3;
  836. }
  837. template<class Container, typename Real = typename Container::value_type,
  838. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  839. inline Real excess_kurtosis(const Container& c)
  840. {
  841. return excess_kurtosis(std::begin(c), std::end(c));
  842. }
  843. template<class RandomAccessIterator, typename Real = typename std::iterator_traits<RandomAccessIterator>::value_type>
  844. Real median(RandomAccessIterator first, RandomAccessIterator last)
  845. {
  846. const auto num_elems = std::distance(first, last);
  847. BOOST_ASSERT_MSG(num_elems > 0, "The median of a zero length vector is undefined.");
  848. if (num_elems & 1)
  849. {
  850. auto middle = first + (num_elems - 1)/2;
  851. std::nth_element(first, middle, last);
  852. return *middle;
  853. }
  854. else
  855. {
  856. auto middle = first + num_elems/2 - 1;
  857. std::nth_element(first, middle, last);
  858. std::nth_element(middle, middle+1, last);
  859. return (*middle + *(middle+1))/2;
  860. }
  861. }
  862. template<class RandomAccessContainer, typename Real = typename RandomAccessContainer::value_type>
  863. inline Real median(RandomAccessContainer& c)
  864. {
  865. return median(std::begin(c), std::end(c));
  866. }
  867. template<class RandomAccessIterator, typename Real = typename std::iterator_traits<RandomAccessIterator>::value_type,
  868. enable_if_t<std::is_integral<Real>::value, bool> = true>
  869. inline double gini_coefficient(RandomAccessIterator first, RandomAccessIterator last)
  870. {
  871. if(!std::is_sorted(first, last))
  872. {
  873. std::sort(first, last);
  874. }
  875. return detail::gini_coefficient_sequential_impl<double>(first, last);
  876. }
  877. template<class RandomAccessContainer, typename Real = typename RandomAccessContainer::value_type,
  878. enable_if_t<std::is_integral<Real>::value, bool> = true>
  879. inline double gini_coefficient(RandomAccessContainer& c)
  880. {
  881. return gini_coefficient(std::begin(c), std::end(c));
  882. }
  883. template<class RandomAccessIterator, typename Real = typename std::iterator_traits<RandomAccessIterator>::value_type,
  884. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  885. inline Real gini_coefficient(RandomAccessIterator first, RandomAccessIterator last)
  886. {
  887. if(!std::is_sorted(first, last))
  888. {
  889. std::sort(first, last);
  890. }
  891. return detail::gini_coefficient_sequential_impl<Real>(first, last);
  892. }
  893. template<class RandomAccessContainer, typename Real = typename RandomAccessContainer::value_type,
  894. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  895. inline Real gini_coefficient(RandomAccessContainer& c)
  896. {
  897. return gini_coefficient(std::begin(c), std::end(c));
  898. }
  899. template<class RandomAccessIterator, typename Real = typename std::iterator_traits<RandomAccessIterator>::value_type,
  900. enable_if_t<std::is_integral<Real>::value, bool> = true>
  901. inline double sample_gini_coefficient(RandomAccessIterator first, RandomAccessIterator last)
  902. {
  903. const auto n = std::distance(first, last);
  904. return n*gini_coefficient(first, last)/(n-1);
  905. }
  906. template<class RandomAccessContainer, typename Real = typename RandomAccessContainer::value_type,
  907. enable_if_t<std::is_integral<Real>::value, bool> = true>
  908. inline double sample_gini_coefficient(RandomAccessContainer& c)
  909. {
  910. return sample_gini_coefficient(std::begin(c), std::end(c));
  911. }
  912. template<class RandomAccessIterator, typename Real = typename std::iterator_traits<RandomAccessIterator>::value_type,
  913. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  914. inline Real sample_gini_coefficient(RandomAccessIterator first, RandomAccessIterator last)
  915. {
  916. const auto n = std::distance(first, last);
  917. return n*gini_coefficient(first, last)/(n-1);
  918. }
  919. template<class RandomAccessContainer, typename Real = typename RandomAccessContainer::value_type,
  920. enable_if_t<!std::is_integral<Real>::value, bool> = true>
  921. inline Real sample_gini_coefficient(RandomAccessContainer& c)
  922. {
  923. return sample_gini_coefficient(std::begin(c), std::end(c));
  924. }
  925. template<class RandomAccessIterator, typename Real = typename std::iterator_traits<RandomAccessIterator>::value_type>
  926. Real median_absolute_deviation(RandomAccessIterator first, RandomAccessIterator last,
  927. typename std::iterator_traits<RandomAccessIterator>::value_type center=std::numeric_limits<typename std::iterator_traits<RandomAccessIterator>::value_type>::quiet_NaN())
  928. {
  929. using std::abs;
  930. using std::isnan;
  931. if (isnan(center))
  932. {
  933. center = boost::math::statistics::median(first, last);
  934. }
  935. const auto num_elems = std::distance(first, last);
  936. BOOST_ASSERT_MSG(num_elems > 0, "The median of a zero-length vector is undefined.");
  937. auto comparator = [&center](Real a, Real b) { return abs(a-center) < abs(b-center);};
  938. if (num_elems & 1)
  939. {
  940. auto middle = first + (num_elems - 1)/2;
  941. std::nth_element(first, middle, last, comparator);
  942. return abs(*middle);
  943. }
  944. else
  945. {
  946. auto middle = first + num_elems/2 - 1;
  947. std::nth_element(first, middle, last, comparator);
  948. std::nth_element(middle, middle+1, last, comparator);
  949. return (abs(*middle) + abs(*(middle+1)))/abs(static_cast<Real>(2));
  950. }
  951. }
  952. template<class RandomAccessContainer, typename Real = typename RandomAccessContainer::value_type>
  953. inline Real median_absolute_deviation(RandomAccessContainer& c,
  954. typename RandomAccessContainer::value_type center=std::numeric_limits<typename RandomAccessContainer::value_type>::quiet_NaN())
  955. {
  956. return median_absolute_deviation(std::begin(c), std::end(c), center);
  957. }
  958. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type>
  959. Real interquartile_range(ForwardIterator first, ForwardIterator last)
  960. {
  961. static_assert(!std::is_integral<Real>::value, "Integer values have not yet been implemented.");
  962. auto m = std::distance(first,last);
  963. BOOST_ASSERT_MSG(m >= 3, "At least 3 samples are required to compute the interquartile range.");
  964. auto k = m/4;
  965. auto j = m - (4*k);
  966. // m = 4k+j.
  967. // If j = 0 or j = 1, then there are an even number of samples below the median, and an even number above the median.
  968. // Then we must average adjacent elements to get the quartiles.
  969. // If j = 2 or j = 3, there are an odd number of samples above and below the median, these elements may be directly extracted to get the quartiles.
  970. if (j==2 || j==3)
  971. {
  972. auto q1 = first + k;
  973. auto q3 = first + 3*k + j - 1;
  974. std::nth_element(first, q1, last);
  975. Real Q1 = *q1;
  976. std::nth_element(q1, q3, last);
  977. Real Q3 = *q3;
  978. return Q3 - Q1;
  979. }
  980. else
  981. {
  982. // j == 0 or j==1:
  983. auto q1 = first + k - 1;
  984. auto q3 = first + 3*k - 1 + j;
  985. std::nth_element(first, q1, last);
  986. Real a = *q1;
  987. std::nth_element(q1, q1 + 1, last);
  988. Real b = *(q1 + 1);
  989. Real Q1 = (a+b)/2;
  990. std::nth_element(q1, q3, last);
  991. a = *q3;
  992. std::nth_element(q3, q3 + 1, last);
  993. b = *(q3 + 1);
  994. Real Q3 = (a+b)/2;
  995. return Q3 - Q1;
  996. }
  997. }
  998. template<class Container, typename Real = typename Container::value_type>
  999. Real interquartile_range(Container& c)
  1000. {
  1001. return interquartile_range(std::begin(c), std::end(c));
  1002. }
  1003. template<class ForwardIterator, class OutputIterator,
  1004. enable_if_t<std::is_same<typename std::iterator_traits<ForwardIterator>::iterator_category(), std::random_access_iterator_tag>::value, bool> = true>
  1005. inline OutputIterator mode(ForwardIterator first, ForwardIterator last, OutputIterator output)
  1006. {
  1007. if(!std::is_sorted(first, last))
  1008. {
  1009. std::sort(first, last);
  1010. }
  1011. return detail::mode_impl(first, last, output);
  1012. }
  1013. template<class ForwardIterator, class OutputIterator,
  1014. enable_if_t<!std::is_same<typename std::iterator_traits<ForwardIterator>::iterator_category(), std::random_access_iterator_tag>::value, bool> = true>
  1015. inline OutputIterator mode(ForwardIterator first, ForwardIterator last, OutputIterator output)
  1016. {
  1017. if(!std::is_sorted(first, last))
  1018. {
  1019. BOOST_ASSERT("Data must be sorted for mode calculation");
  1020. }
  1021. return detail::mode_impl(first, last, output);
  1022. }
  1023. template<class Container, class OutputIterator>
  1024. inline OutputIterator mode(Container& c, OutputIterator output)
  1025. {
  1026. return mode(std::begin(c), std::end(c), output);
  1027. }
  1028. template<class ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type>
  1029. inline std::list<Real> mode(ForwardIterator first, ForwardIterator last)
  1030. {
  1031. std::list<Real> modes;
  1032. mode(first, last, std::inserter(modes, modes.begin()));
  1033. return modes;
  1034. }
  1035. template<class Container, typename Real = typename Container::value_type>
  1036. inline std::list<Real> mode(Container& c)
  1037. {
  1038. return mode(std::begin(c), std::end(c));
  1039. }
  1040. }}}
  1041. #endif
  1042. #endif // BOOST_MATH_STATISTICS_UNIVARIATE_STATISTICS_HPP