weibull.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_STATS_WEIBULL_HPP
  6. #define BOOST_STATS_WEIBULL_HPP
  7. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3668.htm
  8. // http://mathworld.wolfram.com/WeibullDistribution.html
  9. #include <boost/math/distributions/fwd.hpp>
  10. #include <boost/math/special_functions/gamma.hpp>
  11. #include <boost/math/special_functions/log1p.hpp>
  12. #include <boost/math/special_functions/expm1.hpp>
  13. #include <boost/math/distributions/detail/common_error_handling.hpp>
  14. #include <boost/math/distributions/complement.hpp>
  15. #include <utility>
  16. namespace boost{ namespace math
  17. {
  18. namespace detail{
  19. template <class RealType, class Policy>
  20. inline bool check_weibull_shape(
  21. const char* function,
  22. RealType shape,
  23. RealType* result, const Policy& pol)
  24. {
  25. if((shape <= 0) || !(boost::math::isfinite)(shape))
  26. {
  27. *result = policies::raise_domain_error<RealType>(
  28. function,
  29. "Shape parameter is %1%, but must be > 0 !", shape, pol);
  30. return false;
  31. }
  32. return true;
  33. }
  34. template <class RealType, class Policy>
  35. inline bool check_weibull_x(
  36. const char* function,
  37. RealType const& x,
  38. RealType* result, const Policy& pol)
  39. {
  40. if((x < 0) || !(boost::math::isfinite)(x))
  41. {
  42. *result = policies::raise_domain_error<RealType>(
  43. function,
  44. "Random variate is %1% but must be >= 0 !", x, pol);
  45. return false;
  46. }
  47. return true;
  48. }
  49. template <class RealType, class Policy>
  50. inline bool check_weibull(
  51. const char* function,
  52. RealType scale,
  53. RealType shape,
  54. RealType* result, const Policy& pol)
  55. {
  56. return check_scale(function, scale, result, pol) && check_weibull_shape(function, shape, result, pol);
  57. }
  58. } // namespace detail
  59. template <class RealType = double, class Policy = policies::policy<> >
  60. class weibull_distribution
  61. {
  62. public:
  63. typedef RealType value_type;
  64. typedef Policy policy_type;
  65. weibull_distribution(RealType l_shape, RealType l_scale = 1)
  66. : m_shape(l_shape), m_scale(l_scale)
  67. {
  68. RealType result;
  69. detail::check_weibull("boost::math::weibull_distribution<%1%>::weibull_distribution", l_scale, l_shape, &result, Policy());
  70. }
  71. RealType shape()const
  72. {
  73. return m_shape;
  74. }
  75. RealType scale()const
  76. {
  77. return m_scale;
  78. }
  79. private:
  80. //
  81. // Data members:
  82. //
  83. RealType m_shape; // distribution shape
  84. RealType m_scale; // distribution scale
  85. };
  86. typedef weibull_distribution<double> weibull;
  87. template <class RealType, class Policy>
  88. inline const std::pair<RealType, RealType> range(const weibull_distribution<RealType, Policy>& /*dist*/)
  89. { // Range of permissible values for random variable x.
  90. using boost::math::tools::max_value;
  91. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
  92. }
  93. template <class RealType, class Policy>
  94. inline const std::pair<RealType, RealType> support(const weibull_distribution<RealType, Policy>& /*dist*/)
  95. { // Range of supported values for random variable x.
  96. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  97. using boost::math::tools::max_value;
  98. using boost::math::tools::min_value;
  99. return std::pair<RealType, RealType>(min_value<RealType>(), max_value<RealType>());
  100. // A discontinuity at x == 0, so only support down to min_value.
  101. }
  102. template <class RealType, class Policy>
  103. inline RealType pdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
  104. {
  105. BOOST_MATH_STD_USING // for ADL of std functions
  106. static const char* function = "boost::math::pdf(const weibull_distribution<%1%>, %1%)";
  107. RealType shape = dist.shape();
  108. RealType scale = dist.scale();
  109. RealType result = 0;
  110. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  111. return result;
  112. if(false == detail::check_weibull_x(function, x, &result, Policy()))
  113. return result;
  114. if(x == 0)
  115. {
  116. if(shape == 1)
  117. {
  118. return 1 / scale;
  119. }
  120. if(shape > 1)
  121. {
  122. return 0;
  123. }
  124. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  125. }
  126. result = exp(-pow(x / scale, shape));
  127. result *= pow(x / scale, shape - 1) * shape / scale;
  128. return result;
  129. }
  130. template <class RealType, class Policy>
  131. inline RealType cdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
  132. {
  133. BOOST_MATH_STD_USING // for ADL of std functions
  134. static const char* function = "boost::math::cdf(const weibull_distribution<%1%>, %1%)";
  135. RealType shape = dist.shape();
  136. RealType scale = dist.scale();
  137. RealType result = 0;
  138. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  139. return result;
  140. if(false == detail::check_weibull_x(function, x, &result, Policy()))
  141. return result;
  142. result = -boost::math::expm1(-pow(x / scale, shape), Policy());
  143. return result;
  144. }
  145. template <class RealType, class Policy>
  146. inline RealType quantile(const weibull_distribution<RealType, Policy>& dist, const RealType& p)
  147. {
  148. BOOST_MATH_STD_USING // for ADL of std functions
  149. static const char* function = "boost::math::quantile(const weibull_distribution<%1%>, %1%)";
  150. RealType shape = dist.shape();
  151. RealType scale = dist.scale();
  152. RealType result = 0;
  153. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  154. return result;
  155. if(false == detail::check_probability(function, p, &result, Policy()))
  156. return result;
  157. if(p == 1)
  158. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  159. result = scale * pow(-boost::math::log1p(-p, Policy()), 1 / shape);
  160. return result;
  161. }
  162. template <class RealType, class Policy>
  163. inline RealType cdf(const complemented2_type<weibull_distribution<RealType, Policy>, RealType>& c)
  164. {
  165. BOOST_MATH_STD_USING // for ADL of std functions
  166. static const char* function = "boost::math::cdf(const weibull_distribution<%1%>, %1%)";
  167. RealType shape = c.dist.shape();
  168. RealType scale = c.dist.scale();
  169. RealType result = 0;
  170. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  171. return result;
  172. if(false == detail::check_weibull_x(function, c.param, &result, Policy()))
  173. return result;
  174. result = exp(-pow(c.param / scale, shape));
  175. return result;
  176. }
  177. template <class RealType, class Policy>
  178. inline RealType quantile(const complemented2_type<weibull_distribution<RealType, Policy>, RealType>& c)
  179. {
  180. BOOST_MATH_STD_USING // for ADL of std functions
  181. static const char* function = "boost::math::quantile(const weibull_distribution<%1%>, %1%)";
  182. RealType shape = c.dist.shape();
  183. RealType scale = c.dist.scale();
  184. RealType q = c.param;
  185. RealType result = 0;
  186. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  187. return result;
  188. if(false == detail::check_probability(function, q, &result, Policy()))
  189. return result;
  190. if(q == 0)
  191. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  192. result = scale * pow(-log(q), 1 / shape);
  193. return result;
  194. }
  195. template <class RealType, class Policy>
  196. inline RealType mean(const weibull_distribution<RealType, Policy>& dist)
  197. {
  198. BOOST_MATH_STD_USING // for ADL of std functions
  199. static const char* function = "boost::math::mean(const weibull_distribution<%1%>)";
  200. RealType shape = dist.shape();
  201. RealType scale = dist.scale();
  202. RealType result = 0;
  203. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  204. return result;
  205. result = scale * boost::math::tgamma(1 + 1 / shape, Policy());
  206. return result;
  207. }
  208. template <class RealType, class Policy>
  209. inline RealType variance(const weibull_distribution<RealType, Policy>& dist)
  210. {
  211. RealType shape = dist.shape();
  212. RealType scale = dist.scale();
  213. static const char* function = "boost::math::variance(const weibull_distribution<%1%>)";
  214. RealType result = 0;
  215. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  216. {
  217. return result;
  218. }
  219. result = boost::math::tgamma(1 + 1 / shape, Policy());
  220. result *= -result;
  221. result += boost::math::tgamma(1 + 2 / shape, Policy());
  222. result *= scale * scale;
  223. return result;
  224. }
  225. template <class RealType, class Policy>
  226. inline RealType mode(const weibull_distribution<RealType, Policy>& dist)
  227. {
  228. BOOST_MATH_STD_USING // for ADL of std function pow.
  229. static const char* function = "boost::math::mode(const weibull_distribution<%1%>)";
  230. RealType shape = dist.shape();
  231. RealType scale = dist.scale();
  232. RealType result = 0;
  233. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  234. {
  235. return result;
  236. }
  237. if(shape <= 1)
  238. return 0;
  239. result = scale * pow((shape - 1) / shape, 1 / shape);
  240. return result;
  241. }
  242. template <class RealType, class Policy>
  243. inline RealType median(const weibull_distribution<RealType, Policy>& dist)
  244. {
  245. BOOST_MATH_STD_USING // for ADL of std function pow.
  246. static const char* function = "boost::math::median(const weibull_distribution<%1%>)";
  247. RealType shape = dist.shape(); // Wikipedia k
  248. RealType scale = dist.scale(); // Wikipedia lambda
  249. RealType result = 0;
  250. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  251. {
  252. return result;
  253. }
  254. using boost::math::constants::ln_two;
  255. result = scale * pow(ln_two<RealType>(), 1 / shape);
  256. return result;
  257. }
  258. template <class RealType, class Policy>
  259. inline RealType skewness(const weibull_distribution<RealType, Policy>& dist)
  260. {
  261. BOOST_MATH_STD_USING // for ADL of std functions
  262. static const char* function = "boost::math::skewness(const weibull_distribution<%1%>)";
  263. RealType shape = dist.shape();
  264. RealType scale = dist.scale();
  265. RealType result = 0;
  266. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  267. {
  268. return result;
  269. }
  270. RealType g1, g2, g3, d;
  271. g1 = boost::math::tgamma(1 + 1 / shape, Policy());
  272. g2 = boost::math::tgamma(1 + 2 / shape, Policy());
  273. g3 = boost::math::tgamma(1 + 3 / shape, Policy());
  274. d = pow(g2 - g1 * g1, RealType(1.5));
  275. result = (2 * g1 * g1 * g1 - 3 * g1 * g2 + g3) / d;
  276. return result;
  277. }
  278. template <class RealType, class Policy>
  279. inline RealType kurtosis_excess(const weibull_distribution<RealType, Policy>& dist)
  280. {
  281. BOOST_MATH_STD_USING // for ADL of std functions
  282. static const char* function = "boost::math::kurtosis_excess(const weibull_distribution<%1%>)";
  283. RealType shape = dist.shape();
  284. RealType scale = dist.scale();
  285. RealType result = 0;
  286. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  287. return result;
  288. RealType g1, g2, g3, g4, d, g1_2, g1_4;
  289. g1 = boost::math::tgamma(1 + 1 / shape, Policy());
  290. g2 = boost::math::tgamma(1 + 2 / shape, Policy());
  291. g3 = boost::math::tgamma(1 + 3 / shape, Policy());
  292. g4 = boost::math::tgamma(1 + 4 / shape, Policy());
  293. g1_2 = g1 * g1;
  294. g1_4 = g1_2 * g1_2;
  295. d = g2 - g1_2;
  296. d *= d;
  297. result = -6 * g1_4 + 12 * g1_2 * g2 - 3 * g2 * g2 - 4 * g1 * g3 + g4;
  298. result /= d;
  299. return result;
  300. }
  301. template <class RealType, class Policy>
  302. inline RealType kurtosis(const weibull_distribution<RealType, Policy>& dist)
  303. {
  304. return kurtosis_excess(dist) + 3;
  305. }
  306. template <class RealType, class Policy>
  307. inline RealType entropy(const weibull_distribution<RealType, Policy>& dist)
  308. {
  309. using std::log;
  310. RealType k = dist.shape();
  311. RealType lambda = dist.scale();
  312. return constants::euler<RealType>()*(1-1/k) + log(lambda/k) + 1;
  313. }
  314. } // namespace math
  315. } // namespace boost
  316. // This include must be at the end, *after* the accessors
  317. // for this distribution have been defined, in order to
  318. // keep compilers that support two-phase lookup happy.
  319. #include <boost/math/distributions/detail/derived_accessors.hpp>
  320. #endif // BOOST_STATS_WEIBULL_HPP