-
Notifications
You must be signed in to change notification settings - Fork 241
Add logit
, logistic_sigmoid
, and use in logistic
dist to support promotion policies
#1297
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
Merged
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
aa445f6
Implement logistic function
mborland 3ffbcfe
Add logistic function testing
mborland bb699fa
Implement logit function
mborland 2e66849
Rename
mborland c26f50f
Add missing pragma
mborland ce144e2
Add logit test set
mborland 064ca93
Explicitly cast to result
mborland b33d31a
Use new functions with logisitic distribution
mborland f3a823d
Add reproducer for github issue 1294
mborland a3d22ea
Adjust tolerances
mborland 6068d7a
Only adjust the FPU flags when we have access
mborland 488cd2f
Action review comments replacing fenv manipulation
mborland a453cb3
Ignore lines that should not be hit
mborland 4c086e6
Fix return types for concept tests
mborland 6893b74
Add and fix additional test case
mborland 79ab23f
Move sign position
mborland 24e59fd
Remove double negatives and now excess using statements
mborland b042910
Make power a promoted real type
mborland c49115c
Add make_forwarding_policy and make_forwarding_policy_t
mborland dc23aa3
Disable further promotion with forwarding policy
mborland d87ce89
Check validity of initial policy
mborland 067c6e7
Add documentation
mborland f87d826
Remove incorrect assertion
mborland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright Matt Borland 2025. | ||
// Use, modification and distribution are subject to the | ||
// Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt | ||
// or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef BOOST_MATH_SF_EXPIT_HPP | ||
#define BOOST_MATH_SF_EXPIT_HPP | ||
|
||
#include <boost/math/policies/policy.hpp> | ||
#include <boost/math/tools/precision.hpp> | ||
#include <cmath> | ||
|
||
namespace boost { | ||
namespace math { | ||
|
||
template <typename RealType, typename Policy> | ||
RealType logistic_sigmoid(RealType x, const Policy&) | ||
{ | ||
BOOST_MATH_STD_USING | ||
|
||
using promoted_real_type = typename policies::evaluation<RealType, Policy>::type; | ||
|
||
if(-x >= tools::log_max_value<RealType>()) | ||
{ | ||
return static_cast<RealType>(0); | ||
} | ||
if(-x <= -tools::log_max_value<RealType>()) | ||
{ | ||
return static_cast<RealType>(1); | ||
} | ||
|
||
const auto res {static_cast<RealType>(1 / (1 + exp(static_cast<promoted_real_type>(-x))))}; | ||
return res; | ||
} | ||
|
||
template <typename RealType> | ||
RealType logistic_sigmoid(RealType x) | ||
{ | ||
return logistic_sigmoid(x, policies::policy<>()); | ||
} | ||
|
||
} // namespace math | ||
} // namespace boost | ||
|
||
#endif // BOOST_MATH_SF_EXPIT_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright Matt Borland 2025. | ||
// Use, modification and distribution are subject to the | ||
// Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt | ||
// or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef BOOST_MATH_SF_LOGIT_HPP | ||
#define BOOST_MATH_SF_LOGIT_HPP | ||
|
||
#include <boost/math/tools/config.hpp> | ||
#include <boost/math/policies/policy.hpp> | ||
#include <boost/math/policies/error_handling.hpp> | ||
#include <cmath> | ||
#include <cfenv> | ||
|
||
namespace boost { | ||
namespace math { | ||
|
||
template <typename RealType, typename Policy> | ||
RealType logit(RealType p, const Policy&) | ||
{ | ||
BOOST_MATH_STD_USING | ||
using std::atanh; | ||
|
||
using promoted_real_type = typename policies::evaluation<RealType, Policy>::type; | ||
|
||
if (p < tools::min_value<RealType>()) | ||
{ | ||
return -policies::raise_overflow_error<RealType>("logit", "sub-normals will overflow ln(x/(1-x))", Policy()); | ||
} | ||
|
||
static const RealType crossover {RealType{1}/4}; | ||
const auto promoted_p {static_cast<promoted_real_type>(p)}; | ||
RealType result {}; | ||
if (p > crossover) | ||
{ | ||
result = static_cast<RealType>(2 * atanh(2 * promoted_p - 1)); | ||
} | ||
else | ||
{ | ||
result = static_cast<RealType>(log(promoted_p / (1 - promoted_p))); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
template <typename RealType> | ||
RealType logit(RealType p) | ||
{ | ||
return logit(p, policies::policy<>()); | ||
} | ||
|
||
} // namespace math | ||
} // namespace boost | ||
|
||
#endif // BOOST_MATH_SF_LOGIT_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2025 Matt Borland | ||
// Use, modification and distribution are subject to the | ||
// Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt | ||
// or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
// See: https://github.com/boostorg/math/issues/1294 | ||
|
||
#include <boost/math/distributions/logistic.hpp> | ||
#include "math_unit_test.hpp" | ||
|
||
int main() | ||
{ | ||
using namespace boost::math::policies; | ||
using boost::math::logistic_distribution; | ||
|
||
typedef policy< | ||
promote_float<true>, | ||
promote_double<true> | ||
> with_promotion; | ||
|
||
constexpr double p = 2049.0/4096; | ||
constexpr double ref = 9.76562577610225755e-04; | ||
|
||
logistic_distribution<double, with_promotion> dist_promote; | ||
const double x = quantile(dist_promote, p); | ||
|
||
// Previously we had: 9.76562577610170027e-04 | ||
// Which is an ULP distance of 256 | ||
CHECK_ULP_CLOSE(x, ref, 1); | ||
|
||
return boost::math::test::report_errors(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright Matt Borland 2025. | ||
// Use, modification and distribution are subject to the | ||
// Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt | ||
// or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <boost/math/special_functions/logistic_sigmoid.hpp> | ||
#include <boost/multiprecision/cpp_bin_float.hpp> | ||
#include <boost/multiprecision/cpp_dec_float.hpp> | ||
#include "math_unit_test.hpp" | ||
#include <array> | ||
#include <cfloat> | ||
#include <cfenv> | ||
|
||
#pragma STDC FENV_ACCESS ON | ||
|
||
template <typename RealType> | ||
void test() | ||
{ | ||
const std::array<RealType, 5> x_values = { | ||
0, | ||
1, | ||
1000, | ||
0.5, | ||
0.75 | ||
}; | ||
const std::array<RealType, 5> y_values = { | ||
static_cast<RealType>(1) / 2, | ||
static_cast<RealType>(0.73105857863000487925115924182183627436514464016505651927636590791904045307), | ||
static_cast<RealType>(1), | ||
static_cast<RealType>(0.62245933120185456463890056574550847875327936530891016305943716265854500), | ||
static_cast<RealType>(0.6791786991753929731596801157765790212342212482195760219829517436805) | ||
}; | ||
|
||
for (std::size_t i = 0; i < x_values.size(); ++i) | ||
{ | ||
const RealType test_value {boost::math::logistic_sigmoid(x_values[i])}; | ||
BOOST_MATH_IF_CONSTEXPR (std::is_same<RealType, float>::value || std::is_same<RealType, double>::value) | ||
{ | ||
CHECK_ULP_CLOSE(test_value, y_values[i], 1); | ||
} | ||
else | ||
{ | ||
RealType comparison_value = y_values[i]; | ||
CHECK_MOLLIFIED_CLOSE(test_value, comparison_value, static_cast<RealType>(1e-15)); | ||
} | ||
|
||
bool fe {false}; | ||
if (std::fetestexcept(FE_OVERFLOW)) | ||
{ | ||
fe = true; // LCOV_EXCL_LINE | ||
std::cerr << "FE_OVERFLOW" << std::endl; // LCOV_EXCL_LINE | ||
} | ||
if (std::fetestexcept(FE_UNDERFLOW)) | ||
{ | ||
fe = true; // LCOV_EXCL_LINE | ||
std::cerr << "FE_UNDERFLOW" << std::endl; // LCOV_EXCL_LINE | ||
} | ||
if (std::fetestexcept(FE_DIVBYZERO)) | ||
{ | ||
fe = true; // LCOV_EXCL_LINE | ||
std::cerr << "FE_DIVBYZERO" << std::endl; // LCOV_EXCL_LINE | ||
} | ||
if (std::fetestexcept(FE_INVALID)) | ||
{ | ||
fe = true; // LCOV_EXCL_LINE | ||
std::cerr << "FE_INVALID" << std::endl; // LCOV_EXCL_LINE | ||
} | ||
|
||
CHECK_EQUAL(fe, false); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
std::feclearexcept(FE_ALL_EXCEPT); | ||
test<float>(); | ||
|
||
std::feclearexcept(FE_ALL_EXCEPT); | ||
test<double>(); | ||
|
||
std::feclearexcept(FE_ALL_EXCEPT); | ||
test<long double>(); | ||
|
||
std::feclearexcept(FE_ALL_EXCEPT); | ||
test<boost::multiprecision::cpp_bin_float_quad>(); | ||
|
||
test<boost::multiprecision::cpp_dec_float_50>(); | ||
|
||
return boost::math::test::report_errors(); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.