find_scale_example.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // find_scale.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. // Example of finding scale (standard deviation) for normal (Gaussian).
  8. // Note that this file contains Quickbook mark-up as well as code
  9. // and comments, don't change any of the special comment mark-ups!
  10. //[find_scale1
  11. /*`
  12. First we need some includes to access the __normal_distrib,
  13. the algorithms to find scale (and some std output of course).
  14. */
  15. #include <boost/math/distributions/normal.hpp> // for normal_distribution
  16. using boost::math::normal; // typedef provides default type is double.
  17. #include <boost/math/distributions/find_scale.hpp>
  18. using boost::math::find_scale;
  19. using boost::math::complement; // Needed if you want to use the complement version.
  20. using boost::math::policies::policy; // Needed to specify the error handling policy.
  21. #include <iostream>
  22. using std::cout; using std::endl;
  23. #include <iomanip>
  24. using std::setw; using std::setprecision;
  25. #include <limits>
  26. using std::numeric_limits;
  27. //] [/find_scale1]
  28. int main()
  29. {
  30. cout << "Example: Find scale (standard deviation)." << endl;
  31. try
  32. {
  33. //[find_scale2
  34. /*`
  35. For this example, we will use the standard __normal_distrib,
  36. with location (mean) zero and standard deviation (scale) unity.
  37. Conveniently, this is also the default for this implementation's constructor.
  38. */
  39. normal N01; // Default 'standard' normal distribution with zero mean
  40. double sd = 1.; // and standard deviation is 1.
  41. /*`Suppose we want to find a different normal distribution with standard deviation
  42. so that only fraction p (here 0.001 or 0.1%) are below a certain chosen limit
  43. (here -2. standard deviations).
  44. */
  45. double z = -2.; // z to give prob p
  46. double p = 0.001; // only 0.1% below z = -2
  47. cout << "Normal distribution with mean = " << N01.location() // aka N01.mean()
  48. << ", standard deviation " << N01.scale() // aka N01.standard_deviation()
  49. << ", has " << "fraction <= " << z
  50. << ", p = " << cdf(N01, z) << endl;
  51. cout << "Normal distribution with mean = " << N01.location()
  52. << ", standard deviation " << N01.scale()
  53. << ", has " << "fraction > " << z
  54. << ", p = " << cdf(complement(N01, z)) << endl; // Note: uses complement.
  55. /*`
  56. [pre
  57. Normal distribution with mean = 0 has fraction <= -2, p = 0.0227501
  58. Normal distribution with mean = 0 has fraction > -2, p = 0.97725
  59. ]
  60. Noting that p = 0.02 instead of our target of 0.001,
  61. we can now use `find_scale` to give a new standard deviation.
  62. */
  63. double l = N01.location();
  64. double s = find_scale<normal>(z, p, l);
  65. cout << "scale (standard deviation) = " << s << endl;
  66. /*`
  67. that outputs:
  68. [pre
  69. scale (standard deviation) = 0.647201
  70. ]
  71. showing that we need to reduce the standard deviation from 1. to 0.65.
  72. Then we can check that we have achieved our objective
  73. by constructing a new distribution
  74. with the new standard deviation (but same zero mean):
  75. */
  76. normal np001pc(N01.location(), s);
  77. /*`
  78. And re-calculating the fraction below (and above) our chosen limit.
  79. */
  80. cout << "Normal distribution with mean = " << l
  81. << " has " << "fraction <= " << z
  82. << ", p = " << cdf(np001pc, z) << endl;
  83. cout << "Normal distribution with mean = " << l
  84. << " has " << "fraction > " << z
  85. << ", p = " << cdf(complement(np001pc, z)) << endl;
  86. /*`
  87. [pre
  88. Normal distribution with mean = 0 has fraction <= -2, p = 0.001
  89. Normal distribution with mean = 0 has fraction > -2, p = 0.999
  90. ]
  91. [h4 Controlling how Errors from find_scale are handled]
  92. We can also control the policy for handling various errors.
  93. For example, we can define a new (possibly unwise)
  94. policy to ignore domain errors ('bad' arguments).
  95. Unless we are using the boost::math namespace, we will need:
  96. */
  97. using boost::math::policies::policy;
  98. using boost::math::policies::domain_error;
  99. using boost::math::policies::ignore_error;
  100. /*`
  101. Using a typedef is convenient, especially if it is re-used,
  102. although it is not required, as the various examples below show.
  103. */
  104. typedef policy<domain_error<ignore_error> > ignore_domain_policy;
  105. // find_scale with new policy, using typedef.
  106. l = find_scale<normal>(z, p, l, ignore_domain_policy());
  107. // Default policy policy<>, needs using boost::math::policies::policy;
  108. l = find_scale<normal>(z, p, l, policy<>());
  109. // Default policy, fully specified.
  110. l = find_scale<normal>(z, p, l, boost::math::policies::policy<>());
  111. // New policy, without typedef.
  112. l = find_scale<normal>(z, p, l, policy<domain_error<ignore_error> >());
  113. /*`
  114. If we want to express a probability, say 0.999, that is a complement, `1 - p`
  115. we should not even think of writing `find_scale<normal>(z, 1 - p, l)`,
  116. but use the __complements version (see __why_complements).
  117. */
  118. z = -2.;
  119. double q = 0.999; // = 1 - p; // complement of 0.001.
  120. sd = find_scale<normal>(complement(z, q, l));
  121. normal np95pc(l, sd); // Same standard_deviation (scale) but with mean(scale) shifted
  122. cout << "Normal distribution with mean = " << l << " has "
  123. << "fraction <= " << z << " = " << cdf(np95pc, z) << endl;
  124. cout << "Normal distribution with mean = " << l << " has "
  125. << "fraction > " << z << " = " << cdf(complement(np95pc, z)) << endl;
  126. /*`
  127. Sadly, it is all too easy to get probabilities the wrong way round,
  128. when you may get a warning like this:
  129. [pre
  130. Message from thrown exception was:
  131. Error in function boost::math::find_scale<Dist, Policy>(complement(double, double, double, Policy)):
  132. Computed scale (-0.48043523852179076) is <= 0! Was the complement intended?
  133. ]
  134. The default error handling policy is to throw an exception with this message,
  135. but if you chose a policy to ignore the error,
  136. the (impossible) negative scale is quietly returned.
  137. */
  138. //] [/find_scale2]
  139. }
  140. catch(const std::exception& e)
  141. { // Always useful to include try & catch blocks because default policies
  142. // are to throw exceptions on arguments that cause errors like underflow, overflow.
  143. // Lacking try & catch blocks, the program will abort without a message below,
  144. // which may give some helpful clues as to the cause of the exception.
  145. std::cout <<
  146. "\n""Message from thrown exception was:\n " << e.what() << std::endl;
  147. }
  148. return 0;
  149. } // int main()
  150. //[find_scale_example_output
  151. /*`
  152. [pre
  153. Example: Find scale (standard deviation).
  154. Normal distribution with mean = 0, standard deviation 1, has fraction <= -2, p = 0.0227501
  155. Normal distribution with mean = 0, standard deviation 1, has fraction > -2, p = 0.97725
  156. scale (standard deviation) = 0.647201
  157. Normal distribution with mean = 0 has fraction <= -2, p = 0.001
  158. Normal distribution with mean = 0 has fraction > -2, p = 0.999
  159. Normal distribution with mean = 0.946339 has fraction <= -2 = 0.001
  160. Normal distribution with mean = 0.946339 has fraction > -2 = 0.999
  161. ]
  162. */
  163. //] [/find_scale_example_output]