univariate_statistics.qbk 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. [/
  2. Copyright 2018 Nick Thompson
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:univariate_statistics Univariate Statistics]
  8. [heading Synopsis]
  9. ``
  10. #include <boost/math/statistics/univariate_statistics.hpp>
  11. namespace boost{ namespace math{ namespace statistics {
  12. template<class Container>
  13. auto mean(Container const & c);
  14. template<class ForwardIterator>
  15. auto mean(ForwardIterator first, ForwardIterator last);
  16. template<class Container>
  17. auto variance(Container const & c);
  18. template<class ForwardIterator>
  19. auto variance(ForwardIterator first, ForwardIterator last);
  20. template<class Container>
  21. auto sample_variance(Container const & c);
  22. template<class ForwardIterator>
  23. auto sample_variance(ForwardIterator first, ForwardIterator last);
  24. template<class Container>
  25. auto mean_and_sample_variance(Container const & c);
  26. template<class Container>
  27. auto skewness(Container const & c);
  28. template<class ForwardIterator>
  29. auto skewness(ForwardIterator first, ForwardIterator last);
  30. template<class Container>
  31. auto kurtosis(Container const & c);
  32. template<class ForwardIterator>
  33. auto kurtosis(ForwardIterator first, ForwardIterator last);
  34. template<class Container>
  35. auto excess_kurtosis(Container const & c);
  36. template<class ForwardIterator>
  37. auto excess_kurtosis(ForwardIterator first, ForwardIterator last);
  38. template<class Container>
  39. auto first_four_moments(Container const & c);
  40. template<class ForwardIterator>
  41. auto first_four_moments(ForwardIterator first, ForwardIterator last);
  42. template<class Container>
  43. auto median(Container & c);
  44. template<class ForwardIterator>
  45. auto median(ForwardIterator first, ForwardIterator last);
  46. template<class RandomAccessIterator>
  47. auto median_absolute_deviation(ForwardIterator first, ForwardIterator last, typename std::iterator_traits<RandomAccessIterator>::value_type center=std::numeric_limits<Real>::quiet_NaN());
  48. template<class RandomAccessContainer>
  49. auto median_absolute_deviation(RandomAccessContainer v, typename RandomAccessContainer::value_type center=std::numeric_limits<Real>::quiet_NaN());
  50. template<class Container>
  51. auto gini_coefficient(Container & c);
  52. template<class ForwardIterator>
  53. auto gini_coefficient(ForwardIterator first, ForwardIterator last);
  54. template<class Container>
  55. auto sample_gini_coefficient(Container & c);
  56. template<class ForwardIterator>
  57. auto sample_gini_coefficient(ForwardIterator first, ForwardIterator last);
  58. }}}
  59. ``
  60. [heading Description]
  61. The file `boost/math/statistics/univariate_statistics.hpp` is a set of facilities for computing scalar values from vectors.
  62. Many of these functionals have trivial naive implementations, but experienced programmers will recognize that even trivial algorithms are easy to screw up, and that numerical instabilities often lurk in corner cases.
  63. We have attempted to do our "due diligence" to root out these problems-scouring the literature for numerically stable algorithms for even the simplest of functionals.
  64. /Nota bene/: Some similar functionality is provided in [@https://www.boost.org/doc/libs/1_68_0/doc/html/accumulators/user_s_guide.html Boost Accumulators Framework].
  65. These accumulators should be used in real-time applications; `univariate_statistics.hpp` should be used when CPU vectorization is needed.
  66. As a reminder, remember that to actually /get/ vectorization, compile with `-march=native -O3` flags.
  67. We now describe each functional in detail.
  68. Our examples use `std::vector<double>` to hold the data, but this not required.
  69. In general, you can store your data in an Eigen array, and Armadillo vector, `std::array`, and for many of the routines, a `std::forward_list`.
  70. These routines are usable in float, double, long double, and Boost.Multiprecision precision, as well as their complex extensions whenever the computation is well-defined.
  71. For certain operations (total variation, for example) integer inputs are supported.
  72. [heading Mean]
  73. std::vector<double> v{1,2,3,4,5};
  74. double mu = boost::math::statistics::mean(v.cbegin(), v.cend());
  75. // Alternative syntax if you want to use entire container:
  76. mu = boost::math::statistics::mean(v);
  77. The implementation follows [@https://doi.org/10.1137/1.9780898718027 Higham 1.6a].
  78. The data is not modified and must be forward iterable.
  79. Works with real and integer data.
  80. If the input is an integer type, the output is a double precision float.
  81. [heading Variance]
  82. std::vector<double> v{1,2,3,4,5};
  83. Real sigma_sq = boost::math::statistics::variance(v.cbegin(), v.cend());
  84. If you don't need to calculate on a subset of the input, then the range call is more terse:
  85. std::vector<double> v{1,2,3,4,5};
  86. Real sigma_sq = boost::math::statistics::variance(v);
  87. The implementation follows [@https://doi.org/10.1137/1.9780898718027 Higham 1.6b].
  88. The input data must be forward iterable and the range `[first, last)` must contain at least two elements.
  89. It is /not/ in general sensible to pass complex numbers to this routine.
  90. If integers are passed as input, then the output is a double precision float.
  91. `boost::math::statistics::variance` returns the population variance.
  92. If you want a sample variance, use
  93. std::vector<double> v{1,2,3,4,5};
  94. Real sn_sq = boost::math::statistics::sample_variance(v);
  95. [heading Skewness]
  96. Computes the skewness of a dataset:
  97. std::vector<double> v{1,2,3,4,5};
  98. double skewness = boost::math::statistics::skewness(v);
  99. // skewness = 0.
  100. The input vector is not modified, works with integral and real data.
  101. If the input data is integral, the output is a double precision float.
  102. For a dataset consisting of a single constant value, we take the skewness to be zero by definition.
  103. The implementation follows [@https://prod.sandia.gov/techlib-noauth/access-control.cgi/2008/086212.pdf Pebay].
  104. [heading Kurtosis]
  105. Computes the kurtosis of a dataset:
  106. std::vector<double> v{1,2,3,4,5};
  107. double kurtosis = boost::math::statistics::kurtosis(v);
  108. // kurtosis = 17/10
  109. The implementation follows [@https://prod.sandia.gov/techlib-noauth/access-control.cgi/2008/086212.pdf Pebay].
  110. The input data must be forward iterable and must consist of real or integral values.
  111. If the input data is integral, the output is a double precision float.
  112. Note that this is /not/ the excess kurtosis.
  113. If you require the excess kurtosis, use `boost::math::statistics::excess_kurtosis`.
  114. This function simply subtracts 3 from the kurtosis, but it makes eminently clear our definition of kurtosis.
  115. [heading First four moments]
  116. Simultaneously computes the first four [@https://en.wikipedia.org/wiki/Central_moment central moments] in a single pass through the data:
  117. std::vector<double> v{1,2,3,4,5};
  118. auto [M1, M2, M3, M4] = boost::math::statistics::first_four_moments(v);
  119. [heading Median]
  120. Computes the median of a dataset:
  121. std::vector<double> v{1,2,3,4,5};
  122. double m = boost::math::statistics::median(v.begin(), v.end());
  123. /Nota bene: The input vector is modified./
  124. The calculation of the median is a thin wrapper around the C++11 [@https://en.cppreference.com/w/cpp/algorithm/nth_element `nth_element`].
  125. Therefore, all requirements of `std::nth_element` are inherited by the median calculation.
  126. In particular, the container must allow random access.
  127. [heading Median Absolute Deviation]
  128. Computes the [@https://en.wikipedia.org/wiki/Median_absolute_deviation median absolute deviation] of a dataset:
  129. std::vector<double> v{1,2,3,4,5};
  130. double mad = boost::math::statistics::median_absolute_deviation(v);
  131. By default, the deviation from the median is used.
  132. If you have some prior that the median is zero, or wish to compute the median absolute deviation from the mean,
  133. use the following:
  134. // prior is that center is zero:
  135. double center = 0;
  136. double mad = boost::math::statistics::median_absolute_deviation(v, center);
  137. // compute median absolute deviation from the mean:
  138. double mu = boost::math::statistics::mean(v);
  139. double mad = boost::math::statistics::median_absolute_deviation(v, mu);
  140. /Nota bene:/ The input vector is modified.
  141. Again the vector is passed into a call to [@https://en.cppreference.com/w/cpp/algorithm/nth_element `nth_element`].
  142. [heading Gini Coefficient]
  143. Compute the Gini coefficient of a dataset:
  144. std::vector<double> v{1,0,0,0};
  145. double gini = boost::math::statistics::gini_coefficient(v);
  146. // gini = 3/4
  147. double s_gini = boost::math::statistics::sample_gini_coefficient(v);
  148. // s_gini = 1.
  149. std::vector<double> w{1,1,1,1};
  150. gini = boost::math::statistics::gini_coefficient(w.begin(), w.end());
  151. // gini = 0, as all elements are now equal.
  152. /Nota bene/: The input data is altered: in particular, it is sorted. Makes a call to `std::sort`, and as such requires random access iterators.
  153. The sample Gini coefficient lies in the range [0,1], whereas the population Gini coefficient is in the range [0, 1 - 1/ /n/].
  154. /Nota bene:/ There is essentially no reason to pass negative values to the Gini coefficient function.
  155. However, a use case (measuring wealth inequality when some people have negative wealth) exists, so we do not throw an exception when negative values are encountered.
  156. You should have /very/ good cause to pass negative values to the Gini coefficient calculator.
  157. Another use case is found in signal processing, but the sorting is by magnitude and hence has a different implementation.
  158. See `absolute_gini_coefficient` for details.
  159. [heading References]
  160. * Higham, Nicholas J. ['Accuracy and stability of numerical algorithms.] Vol. 80. Siam, 2002.
  161. * Philippe P. Pébay: ["Formulas for Robust, One-Pass Parallel Computation of Covariances and Arbitrary-Order Statistical Moments.] Technical Report SAND2008-6212, Sandia National Laboratories, September 2008.
  162. [endsect]
  163. [/section:univariate_statistics Univariate Statistics]