empirical_cdf.qbk 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. [/
  2. Copyright (c) 2019 Nick Thompson
  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. ]
  7. [section:empirical_cdf Empirical Cumulative Distribution Function]
  8. [heading Synopsis]
  9. ```
  10. #include <boost/math/distributions/empirical_cumulative_distribution_function.hpp>
  11. namespace boost{ namespace math{
  12. template <class RandomAccessContainer>
  13. class empirical_cumulative_distribution_function
  14. {
  15. public:
  16. using Real = typename RandomAccessContainer::value_type;
  17. empirical_cumulative_distribution_function(RandomAccessContainer && v, bool sorted = false);
  18. auto operator()(Real t) const;
  19. RandomAccessContainer&& return_data();
  20. };
  21. }}
  22. ```
  23. [heading Empirical Cumulative Distribution Function]
  24. The empirical cumulative distribution function is a step function constructed from observed data which converges to the true cumulative distribution function in the limit of infinite data.
  25. This function is a basic building block of hypothesis testing workflows that attempt to answer the question "does my data come from a given distribution?"
  26. These tests require computing quadratures over some function of the empirical CDF and the supposed CDF to create a distance measurement, and hence it is occasionally useful to construct a continuous callable from the data.
  27. An example usage is demonstrated below:
  28. ```
  29. #include <vector>
  30. #include <random>
  31. #include <boost/math/distributions/empirical_cumulative_distribution_function.hpp>
  32. using boost::math::empirical_cumulative_distribution_function;
  33. std::random_device rd;
  34. std::mt19937 gen{rd()};
  35. std::normal_distribution<double> dis(0, 1);
  36. size_t n = 128;
  37. std::vector<double> v(n);
  38. for (size_t i = 0; i < n; ++i) {
  39. v[i] = dis(gen);
  40. }
  41. auto ecdf = empirical_cumulative_distribution_function(std::move(v));
  42. std::cout << "ecdf(0.0) = " << ecdf(0.0) << "\n";
  43. // should print approximately 0.5 . . .
  44. ```
  45. The empirical distribution function requires sorted data.
  46. By default, the constructor sorts it for you at O(Nlog(N)) cost.
  47. If your data is already sorted, you can specify this and the constructor simply moves your data into the class:
  48. ```
  49. std::sort(v.begin(), v.end());
  50. auto ecdf = empirical_cumulative_distribution_function(std::move(v), /* already sorted = */ true);
  51. ```
  52. If you want your data back after being done with the object, use
  53. ```
  54. v = ecdf.return_data();
  55. ```
  56. This operation invalidates `ecdf`; it can no longer be used.
  57. The call operator complexity is O(log(N)), as it requires a call to `std::upper_bound`.
  58. Works with both integer and floating point types.
  59. If the input data consists of integers, the output of the call operator is a double. Requires C++17.
  60. [$../graphs/empiricial_cumulative_distribution_gauss.svg]
  61. [$../graphs/empiricial_cumulative_distribution_uniform.svg]
  62. [heading Performance]
  63. ```
  64. ------------------------------------------------------
  65. Benchmark Time
  66. ------------------------------------------------------
  67. ECDFConstructorSorted<double>/8 4.52 ns
  68. ECDFConstructorSorted<double>/16 5.20 ns
  69. ECDFConstructorSorted<double>/32 5.22 ns
  70. ECDFConstructorSorted<double>/64 7.37 ns
  71. ECDFConstructorSorted<double>/128 7.16 ns
  72. ECDFConstructorSorted<double>/256 8.97 ns
  73. ECDFConstructorSorted<double>/512 8.44 ns
  74. ECDFConstructorSorted<double>/1024 9.07 ns
  75. ECDFConstructorSorted<double>/2048 11.4 ns
  76. ECDFConstructorSorted<double>/4096 12.6 ns
  77. ECDFConstructorSorted<double>/8192 11.4 ns
  78. ECDFConstructorSorted<double>/16384 16.0 ns
  79. ECDFConstructorSorted<double>/32768 17.0 ns
  80. ECDFConstructorSorted<double>/65536 19.5 ns
  81. ECDFConstructorSorted<double>/131072 15.8 ns
  82. ECDFConstructorSorted<double>/262144 17.9 ns
  83. ECDFConstructorSorted<double>/524288 26.7 ns
  84. ECDFConstructorSorted<double>/1048576 29.5 ns
  85. ECDFConstructorSorted<double>/2097152 31.8 ns
  86. ECDFConstructorSorted<double>/4194304 32.8 ns
  87. ECDFConstructorSorted<double>/8388608 35.4 ns
  88. ECDFConstructorSorted<double>/16777216 30.4 ns
  89. ECDFConstructorSorted<double>_BigO 1.27 lgN
  90. ECDFConstructorSorted<double>_RMS 20 %
  91. ECDFConstructorUnsorted<double>/8 155 ns
  92. ECDFConstructorUnsorted<double>/64 2095 ns
  93. ECDFConstructorUnsorted<double>/512 22212 ns
  94. ECDFConstructorUnsorted<double>/4096 220821 ns
  95. ECDFConstructorUnsorted<double>/32768 1996380 ns
  96. ECDFConstructorUnsorted<double>/262144 18916039 ns
  97. ECDFConstructorUnsorted<double>/2097152 194250013 ns
  98. ECDFConstructorUnsorted<double>/16777216 2281469424 ns
  99. ECDFConstructorUnsorted<double>_BigO 5.65 NlgN
  100. ECDFConstructorUnsorted<double>_RMS 6 %
  101. Shuffle<double>/8 82.4 ns
  102. Shuffle<double>/64 731 ns
  103. Shuffle<double>/512 5876 ns
  104. Shuffle<double>/4096 46864 ns
  105. Shuffle<double>/32768 385265 ns
  106. Shuffle<double>/262144 4663866 ns
  107. Shuffle<double>/2097152 54686332 ns
  108. Shuffle<double>/16777216 875309099 ns
  109. Shuffle<double>_BigO 2.16 NlgN
  110. Shuffle<double>_RMS 12 %
  111. ECDFEvaluation<double>/8 48.6 ns
  112. ECDFEvaluation<double>/64 61.3 ns
  113. ECDFEvaluation<double>/512 85.1 ns
  114. ECDFEvaluation<double>/4096 105 ns
  115. ECDFEvaluation<double>/32768 131 ns
  116. ECDFEvaluation<double>/262144 196 ns
  117. ECDFEvaluation<double>/2097152 391 ns
  118. ECDFEvaluation<double>/16777216 715 ns
  119. ECDFEvaluation<double>_BigO 18.19 lgN
  120. ECDFEvaluation<double>_RMS 60 %
  121. ```
  122. The call to the unsorted constructor is in fact a little faster than indicated, as the data must be shuffled after being sorted in the benchmark.
  123. This is itself a fairly expensive operation.
  124. [endsect]
  125. [/section:empirical_cdf]