2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Add logpdf to weibull distribution

This commit is contained in:
Matt Borland
2022-02-24 15:59:11 +01:00
parent 7681a4eded
commit 2a3cb313ec
2 changed files with 80 additions and 0 deletions

View File

@@ -157,6 +157,40 @@ inline RealType pdf(const weibull_distribution<RealType, Policy>& dist, const Re
return result;
}
template <class RealType, class Policy>
inline RealType logpdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
{
BOOST_MATH_STD_USING // for ADL of std functions
static const char* function = "boost::math::logpdf(const weibull_distribution<%1%>, %1%)";
RealType shape = dist.shape();
RealType scale = dist.scale();
RealType result = 0;
if(false == detail::check_weibull(function, scale, shape, &result, Policy()))
return result;
if(false == detail::check_weibull_x(function, x, &result, Policy()))
return result;
if(x == 0)
{
if(shape == 1)
{
return log(1 / scale);
}
if(shape > 1)
{
return 0;
}
return policies::raise_overflow_error<RealType>(function, 0, Policy());
}
result = log(shape * pow(scale, -shape) * pow(x, shape - 1)) - pow(x / scale, shape);
return result;
}
template <class RealType, class Policy>
inline RealType cdf(const weibull_distribution<RealType, Policy>& dist, const RealType& x)
{

View File

@@ -29,6 +29,8 @@
using std::setprecision;
#include <limits>
using std::numeric_limits;
#include <cmath>
using std::log;
template <class RealType>
void check_weibull(RealType shape, RealType scale, RealType x, RealType p, RealType q, RealType tol)
@@ -244,6 +246,50 @@ void test_spots(RealType)
static_cast<RealType>(0.551819),
tolerance);
//
// Tests for logpdf
//
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(0.25, 0.5), static_cast<RealType>(0.1)),
log(static_cast<RealType>(0.856579)),
tolerance);
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(0.25, 0.5), static_cast<RealType>(0.5)),
log(static_cast<RealType>(0.183940)),
tolerance);
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(0.25, 0.5), static_cast<RealType>(5)),
log(static_cast<RealType>(0.015020)),
tolerance * 10); // fewer digits in test value
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(0.5, 2), static_cast<RealType>(0.1)),
log(static_cast<RealType>(0.894013)),
tolerance);
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(0.5, 2), static_cast<RealType>(0.5)),
log(static_cast<RealType>(0.303265)),
tolerance);
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(0.5, 2), static_cast<RealType>(1)),
log(static_cast<RealType>(0.174326)),
tolerance);
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(2, 0.25), static_cast<RealType>(0.1)),
log(static_cast<RealType>(2.726860)),
tolerance);
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(2, 0.25), static_cast<RealType>(0.5)),
log(static_cast<RealType>(0.293050)),
tolerance);
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(3, 2), static_cast<RealType>(1)),
log(static_cast<RealType>(0.330936)),
tolerance);
BOOST_CHECK_CLOSE(
logpdf(weibull_distribution<RealType>(3, 2), static_cast<RealType>(2)),
log(static_cast<RealType>(0.551819)),
tolerance);
//
// These test values were obtained using the formulas at
// http://en.wikipedia.org/wiki/Weibull_distribution