weibull.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 logcdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
  180. {
  181. BOOST_MATH_STD_USING // for ADL of std functions
  182. static const char* function = "boost::math::logcdf(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_weibull_x(function, x, &result, Policy()))
  189. return result;
  190. result = log1p(-exp(-pow(x / scale, shape)), Policy());
  191. return result;
  192. }
  193. template <class RealType, class Policy>
  194. inline RealType quantile(const weibull_distribution<RealType, Policy>& dist, const RealType& p)
  195. {
  196. BOOST_MATH_STD_USING // for ADL of std functions
  197. static const char* function = "boost::math::quantile(const weibull_distribution<%1%>, %1%)";
  198. RealType shape = dist.shape();
  199. RealType scale = dist.scale();
  200. RealType result = 0;
  201. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  202. return result;
  203. if(false == detail::check_probability(function, p, &result, Policy()))
  204. return result;
  205. if(p == 1)
  206. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  207. result = scale * pow(-boost::math::log1p(-p, Policy()), 1 / shape);
  208. return result;
  209. }
  210. template <class RealType, class Policy>
  211. inline RealType cdf(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::cdf(const weibull_distribution<%1%>, %1%)";
  215. RealType shape = c.dist.shape();
  216. RealType scale = c.dist.scale();
  217. RealType result = 0;
  218. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  219. return result;
  220. if(false == detail::check_weibull_x(function, c.param, &result, Policy()))
  221. return result;
  222. result = exp(-pow(c.param / scale, shape));
  223. return result;
  224. }
  225. template <class RealType, class Policy>
  226. inline RealType logcdf(const complemented2_type<weibull_distribution<RealType, Policy>, RealType>& c)
  227. {
  228. BOOST_MATH_STD_USING // for ADL of std functions
  229. static const char* function = "boost::math::logcdf(const weibull_distribution<%1%>, %1%)";
  230. RealType shape = c.dist.shape();
  231. RealType scale = c.dist.scale();
  232. RealType result = 0;
  233. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  234. return result;
  235. if(false == detail::check_weibull_x(function, c.param, &result, Policy()))
  236. return result;
  237. result = -pow(c.param / scale, shape);
  238. return result;
  239. }
  240. template <class RealType, class Policy>
  241. inline RealType quantile(const complemented2_type<weibull_distribution<RealType, Policy>, RealType>& c)
  242. {
  243. BOOST_MATH_STD_USING // for ADL of std functions
  244. static const char* function = "boost::math::quantile(const weibull_distribution<%1%>, %1%)";
  245. RealType shape = c.dist.shape();
  246. RealType scale = c.dist.scale();
  247. RealType q = c.param;
  248. RealType result = 0;
  249. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  250. return result;
  251. if(false == detail::check_probability(function, q, &result, Policy()))
  252. return result;
  253. if(q == 0)
  254. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  255. result = scale * pow(-log(q), 1 / shape);
  256. return result;
  257. }
  258. template <class RealType, class Policy>
  259. inline RealType mean(const weibull_distribution<RealType, Policy>& dist)
  260. {
  261. BOOST_MATH_STD_USING // for ADL of std functions
  262. static const char* function = "boost::math::mean(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. return result;
  268. result = scale * boost::math::tgamma(1 + 1 / shape, Policy());
  269. return result;
  270. }
  271. template <class RealType, class Policy>
  272. inline RealType variance(const weibull_distribution<RealType, Policy>& dist)
  273. {
  274. RealType shape = dist.shape();
  275. RealType scale = dist.scale();
  276. static const char* function = "boost::math::variance(const weibull_distribution<%1%>)";
  277. RealType result = 0;
  278. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  279. {
  280. return result;
  281. }
  282. result = boost::math::tgamma(1 + 1 / shape, Policy());
  283. result *= -result;
  284. result += boost::math::tgamma(1 + 2 / shape, Policy());
  285. result *= scale * scale;
  286. return result;
  287. }
  288. template <class RealType, class Policy>
  289. inline RealType mode(const weibull_distribution<RealType, Policy>& dist)
  290. {
  291. BOOST_MATH_STD_USING // for ADL of std function pow.
  292. static const char* function = "boost::math::mode(const weibull_distribution<%1%>)";
  293. RealType shape = dist.shape();
  294. RealType scale = dist.scale();
  295. RealType result = 0;
  296. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  297. {
  298. return result;
  299. }
  300. if(shape <= 1)
  301. return 0;
  302. result = scale * pow((shape - 1) / shape, 1 / shape);
  303. return result;
  304. }
  305. template <class RealType, class Policy>
  306. inline RealType median(const weibull_distribution<RealType, Policy>& dist)
  307. {
  308. BOOST_MATH_STD_USING // for ADL of std function pow.
  309. static const char* function = "boost::math::median(const weibull_distribution<%1%>)";
  310. RealType shape = dist.shape(); // Wikipedia k
  311. RealType scale = dist.scale(); // Wikipedia lambda
  312. RealType result = 0;
  313. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  314. {
  315. return result;
  316. }
  317. using boost::math::constants::ln_two;
  318. result = scale * pow(ln_two<RealType>(), 1 / shape);
  319. return result;
  320. }
  321. template <class RealType, class Policy>
  322. inline RealType skewness(const weibull_distribution<RealType, Policy>& dist)
  323. {
  324. BOOST_MATH_STD_USING // for ADL of std functions
  325. static const char* function = "boost::math::skewness(const weibull_distribution<%1%>)";
  326. RealType shape = dist.shape();
  327. RealType scale = dist.scale();
  328. RealType result = 0;
  329. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  330. {
  331. return result;
  332. }
  333. RealType g1 = boost::math::tgamma(1 + 1 / shape, Policy());
  334. RealType g2 = boost::math::tgamma(1 + 2 / shape, Policy());
  335. RealType g3 = boost::math::tgamma(1 + 3 / shape, Policy());
  336. RealType d = pow(g2 - g1 * g1, RealType(1.5));
  337. result = (2 * g1 * g1 * g1 - 3 * g1 * g2 + g3) / d;
  338. return result;
  339. }
  340. template <class RealType, class Policy>
  341. inline RealType kurtosis_excess(const weibull_distribution<RealType, Policy>& dist)
  342. {
  343. BOOST_MATH_STD_USING // for ADL of std functions
  344. static const char* function = "boost::math::kurtosis_excess(const weibull_distribution<%1%>)";
  345. RealType shape = dist.shape();
  346. RealType scale = dist.scale();
  347. RealType result = 0;
  348. if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
  349. return result;
  350. RealType g1 = boost::math::tgamma(1 + 1 / shape, Policy());
  351. RealType g2 = boost::math::tgamma(1 + 2 / shape, Policy());
  352. RealType g3 = boost::math::tgamma(1 + 3 / shape, Policy());
  353. RealType g4 = boost::math::tgamma(1 + 4 / shape, Policy());
  354. RealType g1_2 = g1 * g1;
  355. RealType g1_4 = g1_2 * g1_2;
  356. RealType d = g2 - g1_2;
  357. d *= d;
  358. result = -6 * g1_4 + 12 * g1_2 * g2 - 3 * g2 * g2 - 4 * g1 * g3 + g4;
  359. result /= d;
  360. return result;
  361. }
  362. template <class RealType, class Policy>
  363. inline RealType kurtosis(const weibull_distribution<RealType, Policy>& dist)
  364. {
  365. return kurtosis_excess(dist) + 3;
  366. }
  367. template <class RealType, class Policy>
  368. inline RealType entropy(const weibull_distribution<RealType, Policy>& dist)
  369. {
  370. using std::log;
  371. RealType k = dist.shape();
  372. RealType lambda = dist.scale();
  373. return constants::euler<RealType>()*(1-1/k) + log(lambda/k) + 1;
  374. }
  375. } // namespace math
  376. } // namespace boost
  377. // This include must be at the end, *after* the accessors
  378. // for this distribution have been defined, in order to
  379. // keep compilers that support two-phase lookup happy.
  380. #include <boost/math/distributions/detail/derived_accessors.hpp>
  381. #endif // BOOST_STATS_WEIBULL_HPP