weibull.hpp 15 KB

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