boost_math.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright John Maddock 2007.
  2. // Copyright Paul A. Bristow 2007, 2009, 2012.
  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. // boost_math.cpp This is the main DLL file.
  8. //#define BOOST_MATH_OVERFLOW_ERROR_POLICY errno_on_error
  9. //#define BOOST_MATH_ASSERT_UNDEFINED_POLICY false
  10. // These are now defined in project properties
  11. // to avoid complications with pre-compiled headers:
  12. // "BOOST_MATH_ASSERT_UNDEFINED_POLICY=0"
  13. // "BOOST_MATH_OVERFLOW_ERROR_POLICY="errno_on_error""
  14. // so command line shows:
  15. // /D "BOOST_MATH_ASSERT_UNDEFINED_POLICY=0"
  16. // /D "BOOST_MATH_OVERFLOW_ERROR_POLICY="errno_on_error""
  17. #include "stdafx.h"
  18. #ifdef _MSC_VER
  19. # pragma warning(disable: 4400) // 'const boost_math::any_distribution ^' : const/volatile qualifiers on this type are not supported
  20. # pragma warning(disable: 4244) // 'argument' : conversion from 'double' to 'unsigned int', possible loss of data
  21. # pragma warning(disable: 4512) // assignment operator could not be generated
  22. // hypergeometric expects integer parameters.
  23. # pragma warning(disable: 4127) // constant
  24. #endif
  25. #include "boost_math.h"
  26. namespace boost_math
  27. {
  28. any_distribution::any_distribution(int t, double arg1, double arg2, double arg3)
  29. {
  30. TRANSLATE_EXCEPTIONS_BEGIN
  31. // This is where all the work gets done:
  32. switch(t) // index of distribution to distribution_info distributions[]
  33. { // New entries must match distribution names, parameter name(s) and defaults defined below.
  34. case 0:
  35. this->reset(new concrete_distribution<boost::math::bernoulli>(boost::math::bernoulli(arg1)));
  36. break;
  37. case 1:
  38. this->reset(new concrete_distribution<boost::math::beta_distribution<> >(boost::math::beta_distribution<>(arg1, arg2)));
  39. break; // Note - no typedef, so need explicit type <> but rely on default = double.
  40. case 2:
  41. this->reset(new concrete_distribution<boost::math::binomial_distribution<> >(boost::math::binomial_distribution<>(arg1, arg2)));
  42. break; // Note - no typedef, so need explicit type <> but rely on default = double.
  43. case 3:
  44. this->reset(new concrete_distribution<boost::math::cauchy>(boost::math::cauchy(arg1, arg2)));
  45. break;
  46. case 4:
  47. this->reset(new concrete_distribution<boost::math::chi_squared>(boost::math::chi_squared(arg1)));
  48. break;
  49. case 5:
  50. this->reset(new concrete_distribution<boost::math::exponential>(boost::math::exponential(arg1)));
  51. break;
  52. case 6:
  53. this->reset(new concrete_distribution<boost::math::extreme_value>(boost::math::extreme_value(arg1)));
  54. break;
  55. case 7:
  56. this->reset(new concrete_distribution<boost::math::fisher_f >(boost::math::fisher_f(arg1, arg2)));
  57. break;
  58. case 8:
  59. this->reset(new concrete_distribution<boost::math::gamma_distribution<> >(boost::math::gamma_distribution<>(arg1, arg2)));
  60. break;
  61. case 9:
  62. this->reset(new concrete_distribution<boost::math::geometric_distribution<> >(boost::math::geometric_distribution<>(arg1)));
  63. break;
  64. case 10:
  65. this->reset(new concrete_distribution<boost::math::hypergeometric_distribution<> >(boost::math::hypergeometric_distribution<>(arg1, arg2, arg3)));
  66. break;
  67. case 11:
  68. this->reset(new concrete_distribution<boost::math::inverse_chi_squared_distribution<> >(boost::math::inverse_chi_squared_distribution<>(arg1, arg2)));
  69. break;
  70. case 12:
  71. this->reset(new concrete_distribution<boost::math::inverse_gamma_distribution<> >(boost::math::inverse_gamma_distribution<>(arg1, arg2)));
  72. break;
  73. case 13:
  74. this->reset(new concrete_distribution<boost::math::inverse_gaussian_distribution<> >(boost::math::inverse_gaussian_distribution<>(arg1, arg2)));
  75. break;
  76. case 14:
  77. this->reset(new concrete_distribution<boost::math::laplace_distribution<> >(boost::math::laplace_distribution<>(arg1, arg2)));
  78. break;
  79. case 15:
  80. this->reset(new concrete_distribution<boost::math::logistic_distribution<> >(boost::math::logistic_distribution<>(arg1, arg2)));
  81. break;
  82. case 16:
  83. this->reset(new concrete_distribution<boost::math::lognormal_distribution<> >(boost::math::lognormal_distribution<>(arg1, arg2)));
  84. break;
  85. case 17:
  86. this->reset(new concrete_distribution<boost::math::negative_binomial_distribution<> >(boost::math::negative_binomial_distribution<>(arg1, arg2)));
  87. break;
  88. case 18:
  89. this->reset(new concrete_distribution<boost::math::non_central_beta_distribution<> >(boost::math::non_central_beta_distribution<>(arg1, arg2, arg3)));
  90. break;
  91. case 19:
  92. this->reset(new concrete_distribution<boost::math::non_central_chi_squared_distribution<> >(boost::math::non_central_chi_squared_distribution<>(arg1, arg2)));
  93. break;
  94. case 20:
  95. this->reset(new concrete_distribution<boost::math::non_central_f_distribution<> >(boost::math::non_central_f_distribution<>(arg1, arg2, arg3)));
  96. break;
  97. case 21:
  98. this->reset(new concrete_distribution<boost::math::non_central_t_distribution<> >(boost::math::non_central_t_distribution<>(arg1, arg2)));
  99. break;
  100. case 22:
  101. this->reset(new concrete_distribution<boost::math::normal_distribution<> >(boost::math::normal_distribution<>(arg1, arg2)));
  102. break;
  103. case 23:
  104. this->reset(new concrete_distribution<boost::math::pareto>(boost::math::pareto(arg1, arg2)));
  105. break;
  106. case 24:
  107. this->reset(new concrete_distribution<boost::math::poisson>(boost::math::poisson(arg1)));
  108. break;
  109. case 25:
  110. this->reset(new concrete_distribution<boost::math::rayleigh>(boost::math::rayleigh(arg1)));
  111. break;
  112. case 26:
  113. this->reset(new concrete_distribution<boost::math::skew_normal>(boost::math::skew_normal(arg1, arg2, arg3)));
  114. break;
  115. case 27:
  116. this->reset(new concrete_distribution<boost::math::students_t>(boost::math::students_t(arg1)));
  117. break;
  118. case 28:
  119. this->reset(new concrete_distribution<boost::math::triangular>(boost::math::triangular(arg1, arg2, arg3)));
  120. break;
  121. case 29:
  122. this->reset(new concrete_distribution<boost::math::uniform>(boost::math::uniform(arg1, arg2)));
  123. break;
  124. case 30:
  125. this->reset(new concrete_distribution<boost::math::weibull>(boost::math::weibull(arg1, arg2)));
  126. break;
  127. default:
  128. // TODO Need some proper error handling here?
  129. BOOST_ASSERT(0);
  130. }
  131. TRANSLATE_EXCEPTIONS_END
  132. } // any_distribution constructor.
  133. struct distribution_info
  134. {
  135. const char* name; // of distribution.
  136. const char* first_param; // Parameters' name like "degrees of freedom",
  137. const char* second_param; // if required, else "",
  138. const char* third_param; // if required, else "".
  139. // triangular and non-centrals need 3 parameters.
  140. // (Only the Bi-Weibull would need 5 parameters?)
  141. double first_default; // distribution parameter value, often 0, 0.5 or 1.
  142. double second_default; // 0 if there isn't a second argument.
  143. // Note that defaults below follow default argument in constructors,
  144. // if any, but need not be the same.
  145. double third_default; // 0 if there isn't a third argument.
  146. };
  147. distribution_info distributions[] =
  148. { // distribution name, parameter name(s) and default(s)
  149. // Order must match any_distribution constructor above!
  150. // Null string "" and zero default for un-used arguments.
  151. { "Bernoulli", "Probability", "", "",0.5, 0, 0}, // case 0
  152. { "Beta", "Alpha", "Beta", "", 1, 1, 0}, // case 1
  153. { "Binomial", "Trials", "Probability of success", "", 1, 0.5, 0}, // case 2
  154. { "Cauchy", "Location", "Scale", "", 0, 1, 0}, // case 3
  155. { "Chi_squared", "Degrees of freedom", "", "", 1, 0, 0}, // case 4
  156. { "Exponential", "lambda", "", "", 1, 0, 0}, // case 5
  157. { "Extreme value", "Location", "Scale", "", 0, 1, 0}, // case 6
  158. { "Fisher-F", "Degrees of freedom 1", "Degrees of freedom 2", "", 1, 1, 0}, // case 7
  159. { "Gamma (Erlang)", "Shape", "Scale", "", 1, 1, 0}, // case 8
  160. { "Geometric", "Probability", "", "", 1, 0, 0}, // case 9
  161. { "HyperGeometric", "Defects", "Samples", "Objects", 1, 0, 1}, // case 10
  162. { "InverseChiSq", "Degrees of Freedom", "Scale", "", 1, 1, 0}, // case 11
  163. { "InverseGamma", "Shape", "Scale", "", 1, 1, 0}, // case 12
  164. { "InverseGaussian", "Mean", "Scale", "", 1, 1, 0}, // case 13
  165. { "Laplace", "Location", "Scale", "", 0, 1, 0}, // case 14
  166. { "Logistic", "Location", "Scale", "", 0, 1, 0}, // case 15
  167. { "LogNormal", "Location", "Scale", "", 0, 1, 0}, // case 16
  168. { "Negative Binomial", "Successes", "Probability of success", "", 1, 0.5, 0}, // case 17
  169. { "Noncentral Beta", "Shape alpha", "Shape beta", "Non-centrality", 1, 1, 0}, // case 18
  170. { "Noncentral ChiSquare", "Degrees of Freedom", "Non-centrality", "", 1, 0, 0}, // case 19
  171. { "Noncentral F", "Degrees of Freedom 1", "Degrees of Freedom 2", "Non-centrality", 1, 1, 0}, // case 20
  172. { "Noncentral t", "Degrees of Freedom", "Non-centrality", "", 1, 0, 0}, // case 21
  173. { "Normal (Gaussian)", "Mean", "Standard Deviation", "", 0, 1, 0}, // case 22
  174. { "Pareto", "Location", "Shape","", 1, 1, 0}, // case 23
  175. { "Poisson", "Mean", "", "", 1, 0, 0}, // case 24
  176. { "Rayleigh", "Shape", "", "", 1, 0, 0}, // case 25
  177. { "Skew Normal", "Location", "Shape", "Skew", 0, 1, 0}, // case 27 (defaults to Gaussian).
  178. { "Student's t", "Degrees of Freedom", "", "", 1, 0, 0}, // case 28
  179. { "Triangular", "Lower", "Mode", "Upper", -1, 0, +1 }, // case 29 3rd parameter!
  180. // 0, 0.5, 1 also said to be 'standard' but this is most like an approximation to Gaussian distribution.
  181. { "Uniform", "Lower", "Upper", "", 0, 1, 0}, // case 30
  182. { "Weibull", "Shape", "Scale", "", 1, 1, 0}, // case 31
  183. };
  184. // How many distributions are supported:
  185. int any_distribution::size()
  186. {
  187. return sizeof(distributions) / sizeof(distributions[0]);
  188. }
  189. // Display name of i'th distribution:
  190. System::String^ any_distribution::distribution_name(int i)
  191. {
  192. if(i >= size())
  193. return "";
  194. return gcnew System::String(distributions[i].name);
  195. }
  196. // Name of first distribution parameter, or null if not supported:
  197. System::String^ any_distribution::first_param_name(int i)
  198. {
  199. if(i >= size())
  200. return "";
  201. return gcnew System::String(distributions[i].first_param);
  202. }
  203. // Name of second distribution parameter, or null if not supported:
  204. System::String^ any_distribution::second_param_name(int i)
  205. {
  206. if(i >= size())
  207. return "";
  208. return gcnew System::String(distributions[i].second_param);
  209. }
  210. // Name of third distribution parameter, or null if not supported:
  211. System::String^ any_distribution::third_param_name(int i)
  212. {
  213. if(i >= size())
  214. return "";
  215. return gcnew System::String(distributions[i].third_param);
  216. }
  217. // default value for first parameter:
  218. double any_distribution::first_param_default(int i)
  219. {
  220. if(i >= size())
  221. return 0;
  222. return distributions[i].first_default;
  223. }
  224. // default value for second parameter:
  225. double any_distribution::second_param_default(int i)
  226. {
  227. if(i >= size())
  228. return 0;
  229. return distributions[i].second_default;
  230. }
  231. // default value for third parameter:
  232. double any_distribution::third_param_default(int i)
  233. {
  234. if(i >= size())
  235. return 0;
  236. return distributions[i].third_default;
  237. }
  238. } // namespace boost_math