weibull.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. using value_type = RealType;
  64. using policy_type = Policy;
  65. explicit 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. using weibull = weibull_distribution<double>;
  87. #ifdef __cpp_deduction_guides
  88. template <class RealType>
  89. weibull_distribution(RealType)->weibull_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  90. template <class RealType>
  91. weibull_distribution(RealType,RealType)->weibull_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  92. #endif
  93. template <class RealType, class Policy>
  94. inline std::pair<RealType, RealType> range(const weibull_distribution<RealType, Policy>& /*dist*/)
  95. { // Range of permissible values for random variable x.
  96. using boost::math::tools::max_value;
  97. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
  98. }
  99. template <class RealType, class Policy>
  100. inline std::pair<RealType, RealType> support(const weibull_distribution<RealType, Policy>& /*dist*/)
  101. { // Range of supported values for random variable x.
  102. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  103. using boost::math::tools::max_value;
  104. using boost::math::tools::min_value;
  105. return std::pair<RealType, RealType>(min_value<RealType>(), max_value<RealType>());
  106. // A discontinuity at x == 0, so only support down to min_value.
  107. }
  108. template <class RealType, class Policy>
  109. inline RealType pdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
  110. {
  111. BOOST_MATH_STD_USING // for ADL of std functions
  112. static const char* function = "boost::math::pdf(const weibull_distribution<%1%>, %1%)";
  113. RealType shape = dist.shape();
  114. RealType scale = dist.scale();
  115. RealType result = 0;
  116. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  117. return result;
  118. if(false == detail::check_weibull_x(function, x, &result, Policy()))
  119. return result;
  120. if(x == 0)
  121. {
  122. if(shape == 1)
  123. {
  124. return 1 / scale;
  125. }
  126. if(shape > 1)
  127. {
  128. return 0;
  129. }
  130. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  131. }
  132. result = exp(-pow(x / scale, shape));
  133. result *= pow(x / scale, shape - 1) * shape / scale;
  134. return result;
  135. }
  136. template <class RealType, class Policy>
  137. inline RealType logpdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
  138. {
  139. BOOST_MATH_STD_USING // for ADL of std functions
  140. static const char* function = "boost::math::logpdf(const weibull_distribution<%1%>, %1%)";
  141. RealType shape = dist.shape();
  142. RealType scale = dist.scale();
  143. RealType result = 0;
  144. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  145. return result;
  146. if(false == detail::check_weibull_x(function, x, &result, Policy()))
  147. return result;
  148. if(x == 0)
  149. {
  150. if(shape == 1)
  151. {
  152. return log(1 / scale);
  153. }
  154. if(shape > 1)
  155. {
  156. return 0;
  157. }
  158. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  159. }
  160. result = log(shape) - shape * log(scale) + log(x) * (shape - 1) - pow(x / scale, shape);
  161. return result;
  162. }
  163. template <class RealType, class Policy>
  164. inline RealType cdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
  165. {
  166. BOOST_MATH_STD_USING // for ADL of std functions
  167. static const char* function = "boost::math::cdf(const weibull_distribution<%1%>, %1%)";
  168. RealType shape = dist.shape();
  169. RealType scale = dist.scale();
  170. RealType result = 0;
  171. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  172. return result;
  173. if(false == detail::check_weibull_x(function, x, &result, Policy()))
  174. return result;
  175. result = -boost::math::expm1(-pow(x / scale, shape), Policy());
  176. return result;
  177. }
  178. template <class RealType, class Policy>
  179. inline RealType quantile(const weibull_distribution<RealType, Policy>& dist, const RealType& p)
  180. {
  181. BOOST_MATH_STD_USING // for ADL of std functions
  182. static const char* function = "boost::math::quantile(const weibull_distribution<%1%>, %1%)";
  183. RealType shape = dist.shape();
  184. RealType scale = dist.scale();
  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, p, &result, Policy()))
  189. return result;
  190. if(p == 1)
  191. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  192. result = scale * pow(-boost::math::log1p(-p, Policy()), 1 / shape);
  193. return result;
  194. }
  195. template <class RealType, class Policy>
  196. inline RealType cdf(const complemented2_type<weibull_distribution<RealType, Policy>, RealType>& c)
  197. {
  198. BOOST_MATH_STD_USING // for ADL of std functions
  199. static const char* function = "boost::math::cdf(const weibull_distribution<%1%>, %1%)";
  200. RealType shape = c.dist.shape();
  201. RealType scale = c.dist.scale();
  202. RealType result = 0;
  203. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  204. return result;
  205. if(false == detail::check_weibull_x(function, c.param, &result, Policy()))
  206. return result;
  207. result = exp(-pow(c.param / scale, shape));
  208. return result;
  209. }
  210. template <class RealType, class Policy>
  211. inline RealType quantile(const complemented2_type<weibull_distribution<RealType, Policy>, RealType>& c)
  212. {
  213. BOOST_MATH_STD_USING // for ADL of std functions
  214. static const char* function = "boost::math::quantile(const weibull_distribution<%1%>, %1%)";
  215. RealType shape = c.dist.shape();
  216. RealType scale = c.dist.scale();
  217. RealType q = c.param;
  218. RealType result = 0;
  219. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  220. return result;
  221. if(false == detail::check_probability(function, q, &result, Policy()))
  222. return result;
  223. if(q == 0)
  224. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  225. result = scale * pow(-log(q), 1 / shape);
  226. return result;
  227. }
  228. template <class RealType, class Policy>
  229. inline RealType mean(const weibull_distribution<RealType, Policy>& dist)
  230. {
  231. BOOST_MATH_STD_USING // for ADL of std functions
  232. static const char* function = "boost::math::mean(const weibull_distribution<%1%>)";
  233. RealType shape = dist.shape();
  234. RealType scale = dist.scale();
  235. RealType result = 0;
  236. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  237. return result;
  238. result = scale * boost::math::tgamma(1 + 1 / shape, Policy());
  239. return result;
  240. }
  241. template <class RealType, class Policy>
  242. inline RealType variance(const weibull_distribution<RealType, Policy>& dist)
  243. {
  244. RealType shape = dist.shape();
  245. RealType scale = dist.scale();
  246. static const char* function = "boost::math::variance(const weibull_distribution<%1%>)";
  247. RealType result = 0;
  248. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  249. {
  250. return result;
  251. }
  252. result = boost::math::tgamma(1 + 1 / shape, Policy());
  253. result *= -result;
  254. result += boost::math::tgamma(1 + 2 / shape, Policy());
  255. result *= scale * scale;
  256. return result;
  257. }
  258. template <class RealType, class Policy>
  259. inline RealType mode(const weibull_distribution<RealType, Policy>& dist)
  260. {
  261. BOOST_MATH_STD_USING // for ADL of std function pow.
  262. static const char* function = "boost::math::mode(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. if(shape <= 1)
  271. return 0;
  272. result = scale * pow((shape - 1) / shape, 1 / shape);
  273. return result;
  274. }
  275. template <class RealType, class Policy>
  276. inline RealType median(const weibull_distribution<RealType, Policy>& dist)
  277. {
  278. BOOST_MATH_STD_USING // for ADL of std function pow.
  279. static const char* function = "boost::math::median(const weibull_distribution<%1%>)";
  280. RealType shape = dist.shape(); // Wikipedia k
  281. RealType scale = dist.scale(); // Wikipedia lambda
  282. RealType result = 0;
  283. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  284. {
  285. return result;
  286. }
  287. using boost::math::constants::ln_two;
  288. result = scale * pow(ln_two<RealType>(), 1 / shape);
  289. return result;
  290. }
  291. template <class RealType, class Policy>
  292. inline RealType skewness(const weibull_distribution<RealType, Policy>& dist)
  293. {
  294. BOOST_MATH_STD_USING // for ADL of std functions
  295. static const char* function = "boost::math::skewness(const weibull_distribution<%1%>)";
  296. RealType shape = dist.shape();
  297. RealType scale = dist.scale();
  298. RealType result = 0;
  299. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  300. {
  301. return result;
  302. }
  303. RealType g1 = boost::math::tgamma(1 + 1 / shape, Policy());
  304. RealType g2 = boost::math::tgamma(1 + 2 / shape, Policy());
  305. RealType g3 = boost::math::tgamma(1 + 3 / shape, Policy());
  306. RealType d = pow(g2 - g1 * g1, RealType(1.5));
  307. result = (2 * g1 * g1 * g1 - 3 * g1 * g2 + g3) / d;
  308. return result;
  309. }
  310. template <class RealType, class Policy>
  311. inline RealType kurtosis_excess(const weibull_distribution<RealType, Policy>& dist)
  312. {
  313. BOOST_MATH_STD_USING // for ADL of std functions
  314. static const char* function = "boost::math::kurtosis_excess(const weibull_distribution<%1%>)";
  315. RealType shape = dist.shape();
  316. RealType scale = dist.scale();
  317. RealType result = 0;
  318. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  319. return result;
  320. RealType g1 = boost::math::tgamma(1 + 1 / shape, Policy());
  321. RealType g2 = boost::math::tgamma(1 + 2 / shape, Policy());
  322. RealType g3 = boost::math::tgamma(1 + 3 / shape, Policy());
  323. RealType g4 = boost::math::tgamma(1 + 4 / shape, Policy());
  324. RealType g1_2 = g1 * g1;
  325. RealType g1_4 = g1_2 * g1_2;
  326. RealType d = g2 - g1_2;
  327. d *= d;
  328. result = -6 * g1_4 + 12 * g1_2 * g2 - 3 * g2 * g2 - 4 * g1 * g3 + g4;
  329. result /= d;
  330. return result;
  331. }
  332. template <class RealType, class Policy>
  333. inline RealType kurtosis(const weibull_distribution<RealType, Policy>& dist)
  334. {
  335. return kurtosis_excess(dist) + 3;
  336. }
  337. template <class RealType, class Policy>
  338. inline RealType entropy(const weibull_distribution<RealType, Policy>& dist)
  339. {
  340. using std::log;
  341. RealType k = dist.shape();
  342. RealType lambda = dist.scale();
  343. return constants::euler<RealType>()*(1-1/k) + log(lambda/k) + 1;
  344. }
  345. } // namespace math
  346. } // namespace boost
  347. // This include must be at the end, *after* the accessors
  348. // for this distribution have been defined, in order to
  349. // keep compilers that support two-phase lookup happy.
  350. #include <boost/math/distributions/detail/derived_accessors.hpp>
  351. #endif // BOOST_STATS_WEIBULL_HPP