Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logpdf support #762

Merged
merged 20 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions include/boost/math/distributions/normal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,42 @@ inline RealType pdf(const normal_distribution<RealType, Policy>& dist, const Rea
return result;
} // pdf

template <class RealType, class Policy>
inline RealType logpdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
{
BOOST_MATH_STD_USING // for ADL of std functions

RealType sd = dist.standard_deviation();
RealType mean = dist.mean();

static const char* function = "boost::math::logpdf(const normal_distribution<%1%>&, %1%)";

RealType result = 0;
if(false == detail::check_scale(function, sd, &result, Policy()))
{
return result;
}
if(false == detail::check_location(function, mean, &result, Policy()))
{
return result;
}
if((boost::math::isinf)(x))
{
return 0; // pdf + and - infinity is zero.
}
if(false == detail::check_x(function, x, &result, Policy()))
{
return result;
}

const RealType pi = boost::math::constants::pi<RealType>();
const RealType half = boost::math::constants::half<RealType>();

result = -log(sd) - half*log(2*pi) - (x-mean)*(x-mean)/(2*sd*sd);

return result;
}

template <class RealType, class Policy>
inline RealType cdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
{
Expand Down
11 changes: 11 additions & 0 deletions test/test_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
using std::setprecision;
#include <limits>
using std::numeric_limits;
#include <type_traits>
using std::log;

template <class RealType>
RealType NaivePDF(RealType mean, RealType sd, RealType x)
Expand Down Expand Up @@ -219,6 +221,13 @@ void test_spots(RealType)
//
// Tests for logpdf
//
RealType temp_tol = tolerance;

BOOST_IF_CONSTEXPR (std::is_same<long double, RealType>::value)
{
tolerance *= 100;
}

BOOST_CHECK_CLOSE(
logpdf(normal_distribution<RealType>(), static_cast<RealType>(0)),
log(static_cast<RealType>(0.3989422804014326779399460599343818684759L)), // 1/sqrt(2*pi)
Expand All @@ -232,6 +241,8 @@ void test_spots(RealType)
log(static_cast<RealType>(0.3989422804014326779399460599343818684759L / 5)),
tolerance);

tolerance = temp_tol;

//
// Spot checks for mean = -5, sd = 6:
//
Expand Down