PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/math/example/distribution_construction.cpp

http://hadesmem.googlecode.com/
C++ | 199 lines | 37 code | 28 blank | 134 comment | 0 complexity | 7a5ede83b97fae210f0137edef5ca2b2 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, Apache-2.0, LGPL-3.0
  1. // distribution_construction.cpp
  2. // Copyright Paul A. Bristow 2007, 2010.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // Caution: this file contains Quickbook markup as well as code
  8. // and comments, don't change any of the special comment markups!
  9. //[distribution_construction1
  10. /*`
  11. The structure of distributions is rather different from some other statistical libraries,
  12. for example in less object-oriented language like FORTRAN and C,
  13. that provide a few arguments to each free function.
  14. This library provides each distribution as a template C++ class.
  15. A distribution is constructed with a few arguments, and then
  16. member and non-member functions are used to find values of the
  17. distribution, often a function of a random variate.
  18. First we need some includes to access the negative binomial distribution
  19. (and the binomial, beta and gamma too).
  20. */
  21. #include <boost/math/distributions/negative_binomial.hpp> // for negative_binomial_distribution
  22. using boost::math::negative_binomial_distribution; // default type is double.
  23. using boost::math::negative_binomial; // typedef provides default type is double.
  24. #include <boost/math/distributions/binomial.hpp> // for binomial_distribution.
  25. #include <boost/math/distributions/beta.hpp> // for beta_distribution.
  26. #include <boost/math/distributions/gamma.hpp> // for gamma_distribution.
  27. #include <boost/math/distributions/normal.hpp> // for normal_distribution.
  28. /*`
  29. Several examples of constructing distributions follow:
  30. */
  31. //] [/distribution_construction1 end of Quickbook in C++ markup]
  32. int main()
  33. {
  34. //[distribution_construction2
  35. /*`
  36. First, a negative binomial distribution with 8 successes
  37. and a success fraction 0.25, 25% or 1 in 4, is constructed like this:
  38. */
  39. boost::math::negative_binomial_distribution<double> mydist0(8., 0.25);
  40. /*`
  41. But this is inconveniently long, so we might be tempted to write
  42. */
  43. using namespace boost::math;
  44. /*`
  45. but this might risk ambiguity with names in std random so
  46. *much better is explicit `using boost::math:: ` * ... statements like
  47. */
  48. using boost::math::negative_binomial_distribution;
  49. /*`
  50. and we can still reduce typing.
  51. Since the vast majority of applications use will be using double precision,
  52. the template argument to the distribution (RealType) defaults
  53. to type double, so we can also write:
  54. */
  55. negative_binomial_distribution<> mydist9(8., 0.25); // Uses default RealType = double.
  56. /*`
  57. But the name "negative_binomial_distribution" is still inconveniently long,
  58. so for most distributions, a convenience typedef is provided, for example:
  59. typedef negative_binomial_distribution<double> negative_binomial; // Reserved name of type double.
  60. [caution
  61. This convenience typedef is /not/ provided if a clash would occur
  62. with the name of a function: currently only "beta" and "gamma"
  63. fall into this category.
  64. ]
  65. So, after a using statement,
  66. */
  67. using boost::math::negative_binomial;
  68. /*`
  69. we have a convenient typedef to `negative_binomial_distribution<double>`:
  70. */
  71. negative_binomial mydist(8., 0.25);
  72. /*`
  73. Some more examples using the convenience typedef:
  74. */
  75. negative_binomial mydist10(5., 0.4); // Both arguments double.
  76. /*`
  77. And automatic conversion takes place, so you can use integers and floats:
  78. */
  79. negative_binomial mydist11(5, 0.4); // Using provided typedef double, int and double arguments.
  80. /*`
  81. This is probably the most common usage.
  82. */
  83. negative_binomial mydist12(5., 0.4F); // Double and float arguments.
  84. negative_binomial mydist13(5, 1); // Both arguments integer.
  85. /*`
  86. Similarly for most other distributions like the binomial.
  87. */
  88. binomial mybinomial(1, 0.5); // is more concise than
  89. binomial_distribution<> mybinomd1(1, 0.5);
  90. /*`
  91. For cases when the typdef distribution name would clash with a math special function
  92. (currently only beta and gamma)
  93. the typedef is deliberately not provided, and the longer version of the name
  94. must be used. For example do not use:
  95. using boost::math::beta;
  96. beta mybetad0(1, 0.5); // Error beta is a math FUNCTION!
  97. Which produces the error messages:
  98. [pre
  99. error C2146: syntax error : missing ';' before identifier 'mybetad0'
  100. warning C4551: function call missing argument list
  101. error C3861: 'mybetad0': identifier not found
  102. ]
  103. Instead you should use:
  104. */
  105. using boost::math::beta_distribution;
  106. beta_distribution<> mybetad1(1, 0.5);
  107. /*`
  108. or for the gamma distribution:
  109. */
  110. gamma_distribution<> mygammad1(1, 0.5);
  111. /*`
  112. We can, of course, still provide the type explicitly thus:
  113. */
  114. // Explicit double precision:
  115. negative_binomial_distribution<double> mydist1(8., 0.25);
  116. // Explicit float precision, double arguments are truncated to float:
  117. negative_binomial_distribution<float> mydist2(8., 0.25);
  118. // Explicit float precision, integer & double arguments converted to float.
  119. negative_binomial_distribution<float> mydist3(8, 0.25);
  120. // Explicit float precision, float arguments, so no conversion:
  121. negative_binomial_distribution<float> mydist4(8.F, 0.25F);
  122. // Explicit float precision, integer arguments promoted to float.
  123. negative_binomial_distribution<float> mydist5(8, 1);
  124. // Explicit double precision:
  125. negative_binomial_distribution<double> mydist6(8., 0.25);
  126. // Explicit long double precision:
  127. negative_binomial_distribution<long double> mydist7(8., 0.25);
  128. /*`
  129. And if you have your own RealType called MyFPType,
  130. for example NTL RR (an arbitrary precision type), then we can write:
  131. negative_binomial_distribution<MyFPType> mydist6(8, 1); // Integer arguments -> MyFPType.
  132. [heading Default arguments to distribution constructors.]
  133. Note that default constructor arguments are only provided for some distributions.
  134. So if you wrongly assume a default argument you will get an error message, for example:
  135. negative_binomial_distribution<> mydist8;
  136. [pre error C2512 no appropriate default constructor available.]
  137. No default constructors are provided for the negative binomial,
  138. because it is difficult to chose any sensible default values for this distribution.
  139. For other distributions, like the normal distribution,
  140. it is obviously very useful to provide 'standard'
  141. defaults for the mean and standard deviation thus:
  142. normal_distribution(RealType mean = 0, RealType sd = 1);
  143. So in this case we can write:
  144. */
  145. using boost::math::normal;
  146. normal norm1; // Standard normal distribution.
  147. normal norm2(2); // Mean = 2, std deviation = 1.
  148. normal norm3(2, 3); // Mean = 2, std deviation = 3.
  149. return 0;
  150. } // int main()
  151. /*`There is no useful output from this program, of course. */
  152. //] [/end of distribution_construction2]