hyperexponential_snips.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright John Maddock 2014.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // Caution: this file contains Quickbook markup as well as code
  7. // and comments, don't change any of the special comment markups!
  8. #ifdef _MSC_VER
  9. # pragma warning (disable : 4996) // disable -D_SCL_SECURE_NO_WARNINGS C++ 'Checked Iterators'
  10. #endif
  11. #include <boost/math/distributions/hyperexponential.hpp>
  12. #include <iostream>
  13. #ifndef BOOST_NO_CXX11_HDR_ARRAY
  14. #include <array>
  15. #endif
  16. int main()
  17. {
  18. {
  19. //[hyperexponential_snip1
  20. //=#include <boost/math/distributions/hyperexponential.hpp>
  21. //=#include <iostream>
  22. //=int main()
  23. //={
  24. const double rates[] = { 1.0 / 10.0, 1.0 / 12.0 };
  25. boost::math::hyperexponential he(rates);
  26. std::cout << "Average lifetime: "
  27. << boost::math::mean(he)
  28. << " years" << std::endl;
  29. std::cout << "Probability that the appliance will work for more than 15 years: "
  30. << boost::math::cdf(boost::math::complement(he, 15.0))
  31. << std::endl;
  32. //=}
  33. //]
  34. }
  35. using namespace boost::math;
  36. #ifndef BOOST_NO_CXX11_HDR_ARRAY
  37. {
  38. //[hyperexponential_snip2
  39. std::array<double, 2> phase_prob = { 0.5, 0.5 };
  40. std::array<double, 2> rates = { 1.0 / 10, 1.0 / 12 };
  41. hyperexponential he(phase_prob.begin(), phase_prob.end(), rates.begin(), rates.end());
  42. //]
  43. }
  44. {
  45. //[hyperexponential_snip3
  46. // We could be using any standard library container here... vector, deque, array, list etc:
  47. std::array<double, 2> phase_prob = { 0.5, 0.5 };
  48. std::array<double, 2> rates = { 1.0 / 10, 1.0 / 12 };
  49. hyperexponential he1(phase_prob, rates); // Construct from standard library container.
  50. double phase_probs2[] = { 0.5, 0.5 };
  51. double rates2[] = { 1.0 / 10, 1.0 / 12 };
  52. hyperexponential he2(phase_probs2, rates2); // Construct from native C++ array.
  53. //]
  54. }
  55. {
  56. //[hyperexponential_snip4
  57. // We could be using any standard library container here... vector, deque, array, list etc:
  58. std::array<double, 2> rates = { 1.0 / 10, 1.0 / 12 };
  59. hyperexponential he(rates.begin(), rates.end());
  60. BOOST_ASSERT(he.probabilities()[0] == 0.5); // Phase probabilities will be equal and normalised to unity.
  61. //]
  62. }
  63. {
  64. //[hyperexponential_snip5
  65. std::array<double, 2> rates = { 1.0 / 10, 1.0 / 12 };
  66. hyperexponential he(rates);
  67. BOOST_ASSERT(he.probabilities()[0] == 0.5); // Phase probabilities will be equal and normalised to unity.
  68. //]
  69. }
  70. #endif
  71. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !(defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION < 40500))
  72. {
  73. //[hyperexponential_snip6
  74. hyperexponential he = { { 0.5, 0.5 }, { 1.0 / 10, 1.0 / 12 } };
  75. //]
  76. }
  77. {
  78. //[hyperexponential_snip7
  79. hyperexponential he = { 1.0 / 10, 1.0 / 12 };
  80. BOOST_ASSERT(he.probabilities()[0] == 0.5);
  81. //]
  82. }
  83. #endif
  84. return 0;
  85. }