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)
6 #ifndef BOOST_STATS_WEIBULL_HPP
7 #define BOOST_STATS_WEIBULL_HPP
9 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3668.htm
10 // http://mathworld.wolfram.com/WeibullDistribution.html
12 #include <boost/math/distributions/fwd.hpp>
13 #include <boost/math/special_functions/gamma.hpp>
14 #include <boost/math/special_functions/log1p.hpp>
15 #include <boost/math/special_functions/expm1.hpp>
16 #include <boost/math/distributions/detail/common_error_handling.hpp>
17 #include <boost/math/distributions/complement.hpp>
21 namespace boost{ namespace math
25 template <class RealType, class Policy>
26 inline bool check_weibull_shape(
29 RealType* result, const Policy& pol)
31 if((shape <= 0) || !(boost::math::isfinite)(shape))
33 *result = policies::raise_domain_error<RealType>(
35 "Shape parameter is %1%, but must be > 0 !", shape, pol);
41 template <class RealType, class Policy>
42 inline bool check_weibull_x(
45 RealType* result, const Policy& pol)
47 if((x < 0) || !(boost::math::isfinite)(x))
49 *result = policies::raise_domain_error<RealType>(
51 "Random variate is %1% but must be >= 0 !", x, pol);
57 template <class RealType, class Policy>
58 inline bool check_weibull(
62 RealType* result, const Policy& pol)
64 return check_scale(function, scale, result, pol) && check_weibull_shape(function, shape, result, pol);
69 template <class RealType = double, class Policy = policies::policy<> >
70 class weibull_distribution
73 typedef RealType value_type;
74 typedef Policy policy_type;
76 weibull_distribution(RealType l_shape, RealType l_scale = 1)
77 : m_shape(l_shape), m_scale(l_scale)
80 detail::check_weibull("boost::math::weibull_distribution<%1%>::weibull_distribution", l_scale, l_shape, &result, Policy());
96 RealType m_shape; // distribution shape
97 RealType m_scale; // distribution scale
100 typedef weibull_distribution<double> weibull;
102 template <class RealType, class Policy>
103 inline const std::pair<RealType, RealType> range(const weibull_distribution<RealType, Policy>& /*dist*/)
104 { // Range of permissible values for random variable x.
105 using boost::math::tools::max_value;
106 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
109 template <class RealType, class Policy>
110 inline const std::pair<RealType, RealType> support(const weibull_distribution<RealType, Policy>& /*dist*/)
111 { // Range of supported values for random variable x.
112 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
113 using boost::math::tools::max_value;
114 using boost::math::tools::min_value;
115 return std::pair<RealType, RealType>(min_value<RealType>(), max_value<RealType>());
116 // A discontinuity at x == 0, so only support down to min_value.
119 template <class RealType, class Policy>
120 inline RealType pdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
122 BOOST_MATH_STD_USING // for ADL of std functions
124 static const char* function = "boost::math::pdf(const weibull_distribution<%1%>, %1%)";
126 RealType shape = dist.shape();
127 RealType scale = dist.scale();
130 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
132 if(false == detail::check_weibull_x(function, x, &result, Policy()))
145 return policies::raise_overflow_error<RealType>(function, 0, Policy());
147 result = exp(-pow(x / scale, shape));
148 result *= pow(x / scale, shape - 1) * shape / scale;
153 template <class RealType, class Policy>
154 inline RealType cdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
156 BOOST_MATH_STD_USING // for ADL of std functions
158 static const char* function = "boost::math::cdf(const weibull_distribution<%1%>, %1%)";
160 RealType shape = dist.shape();
161 RealType scale = dist.scale();
164 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
166 if(false == detail::check_weibull_x(function, x, &result, Policy()))
169 result = -boost::math::expm1(-pow(x / scale, shape), Policy());
174 template <class RealType, class Policy>
175 inline RealType quantile(const weibull_distribution<RealType, Policy>& dist, const RealType& p)
177 BOOST_MATH_STD_USING // for ADL of std functions
179 static const char* function = "boost::math::quantile(const weibull_distribution<%1%>, %1%)";
181 RealType shape = dist.shape();
182 RealType scale = dist.scale();
185 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
187 if(false == detail::check_probability(function, p, &result, Policy()))
191 return policies::raise_overflow_error<RealType>(function, 0, Policy());
193 result = scale * pow(-boost::math::log1p(-p, Policy()), 1 / shape);
198 template <class RealType, class Policy>
199 inline RealType cdf(const complemented2_type<weibull_distribution<RealType, Policy>, RealType>& c)
201 BOOST_MATH_STD_USING // for ADL of std functions
203 static const char* function = "boost::math::cdf(const weibull_distribution<%1%>, %1%)";
205 RealType shape = c.dist.shape();
206 RealType scale = c.dist.scale();
209 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
211 if(false == detail::check_weibull_x(function, c.param, &result, Policy()))
214 result = exp(-pow(c.param / scale, shape));
219 template <class RealType, class Policy>
220 inline RealType quantile(const complemented2_type<weibull_distribution<RealType, Policy>, RealType>& c)
222 BOOST_MATH_STD_USING // for ADL of std functions
224 static const char* function = "boost::math::quantile(const weibull_distribution<%1%>, %1%)";
226 RealType shape = c.dist.shape();
227 RealType scale = c.dist.scale();
228 RealType q = c.param;
231 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
233 if(false == detail::check_probability(function, q, &result, Policy()))
237 return policies::raise_overflow_error<RealType>(function, 0, Policy());
239 result = scale * pow(-log(q), 1 / shape);
244 template <class RealType, class Policy>
245 inline RealType mean(const weibull_distribution<RealType, Policy>& dist)
247 BOOST_MATH_STD_USING // for ADL of std functions
249 static const char* function = "boost::math::mean(const weibull_distribution<%1%>)";
251 RealType shape = dist.shape();
252 RealType scale = dist.scale();
255 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
258 result = scale * boost::math::tgamma(1 + 1 / shape, Policy());
262 template <class RealType, class Policy>
263 inline RealType variance(const weibull_distribution<RealType, Policy>& dist)
265 RealType shape = dist.shape();
266 RealType scale = dist.scale();
268 static const char* function = "boost::math::variance(const weibull_distribution<%1%>)";
271 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
275 result = boost::math::tgamma(1 + 1 / shape, Policy());
277 result += boost::math::tgamma(1 + 2 / shape, Policy());
278 result *= scale * scale;
282 template <class RealType, class Policy>
283 inline RealType mode(const weibull_distribution<RealType, Policy>& dist)
285 BOOST_MATH_STD_USING // for ADL of std function pow.
287 static const char* function = "boost::math::mode(const weibull_distribution<%1%>)";
289 RealType shape = dist.shape();
290 RealType scale = dist.scale();
293 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
299 result = scale * pow((shape - 1) / shape, 1 / shape);
303 template <class RealType, class Policy>
304 inline RealType median(const weibull_distribution<RealType, Policy>& dist)
306 BOOST_MATH_STD_USING // for ADL of std function pow.
308 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
314 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
318 using boost::math::constants::ln_two;
319 result = scale * pow(ln_two<RealType>(), 1 / shape);
323 template <class RealType, class Policy>
324 inline RealType skewness(const weibull_distribution<RealType, Policy>& dist)
326 BOOST_MATH_STD_USING // for ADL of std functions
328 static const char* function = "boost::math::skewness(const weibull_distribution<%1%>)";
330 RealType shape = dist.shape();
331 RealType scale = dist.scale();
334 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
338 RealType g1, g2, g3, d;
340 g1 = boost::math::tgamma(1 + 1 / shape, Policy());
341 g2 = boost::math::tgamma(1 + 2 / shape, Policy());
342 g3 = boost::math::tgamma(1 + 3 / shape, Policy());
343 d = pow(g2 - g1 * g1, RealType(1.5));
345 result = (2 * g1 * g1 * g1 - 3 * g1 * g2 + g3) / d;
349 template <class RealType, class Policy>
350 inline RealType kurtosis_excess(const weibull_distribution<RealType, Policy>& dist)
352 BOOST_MATH_STD_USING // for ADL of std functions
354 static const char* function = "boost::math::kurtosis_excess(const weibull_distribution<%1%>)";
356 RealType shape = dist.shape();
357 RealType scale = dist.scale();
360 if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
363 RealType g1, g2, g3, g4, d, g1_2, g1_4;
365 g1 = boost::math::tgamma(1 + 1 / shape, Policy());
366 g2 = boost::math::tgamma(1 + 2 / shape, Policy());
367 g3 = boost::math::tgamma(1 + 3 / shape, Policy());
368 g4 = boost::math::tgamma(1 + 4 / shape, Policy());
374 result = -6 * g1_4 + 12 * g1_2 * g2 - 3 * g2 * g2 - 4 * g1 * g3 + g4;
379 template <class RealType, class Policy>
380 inline RealType kurtosis(const weibull_distribution<RealType, Policy>& dist)
382 return kurtosis_excess(dist) + 3;
388 // This include must be at the end, *after* the accessors
389 // for this distribution have been defined, in order to
390 // keep compilers that support two-phase lookup happy.
391 #include <boost/math/distributions/detail/derived_accessors.hpp>
393 #endif // BOOST_STATS_WEIBULL_HPP