Skip to content

Commit bdb3c86

Browse files
authored
Merge pull request #2798 from andrjohns/issue-2796-log_inv_logit_diff-inf
Bugfix: Handling of Inf x argument to `log_inv_logit_diff`
2 parents 2e174d2 + d7772f4 commit bdb3c86

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

stan/math/prim/fun/log_inv_logit_diff.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define STAN_MATH_PRIM_FUN_LOG_INV_LOGIT_DIFF_HPP
33

44
#include <stan/math/prim/meta.hpp>
5+
#include <stan/math/prim/fun/is_inf.hpp>
56
#include <stan/math/prim/fun/log1m_exp.hpp>
67
#include <stan/math/prim/fun/log1p_exp.hpp>
78
#include <stan/math/prim/functor/apply_scalar_binary.hpp>
@@ -34,6 +35,9 @@ namespace math {
3435
*/
3536
template <typename T1, typename T2, require_all_arithmetic_t<T1, T2>* = nullptr>
3637
inline return_type_t<T1, T2> log_inv_logit_diff(const T1& x, const T2& y) {
38+
if (is_inf(x) && x >= 0) {
39+
return -log1p_exp(y);
40+
}
3741
return x - log1p_exp(x) + log1m_exp(y - x) - log1p_exp(y);
3842
}
3943

test/unit/math/prim/fun/log_inv_logit_diff_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
#include <gtest/gtest.h>
44
#include <cmath>
55

6+
TEST(MathFunctions, log_inv_logit_diff_inf) {
7+
using stan::math::INFTY;
8+
using stan::math::log_inv_logit_diff;
9+
10+
EXPECT_FLOAT_EQ(-0.0916494, log_inv_logit_diff(INFTY, -2.34361));
11+
EXPECT_TRUE(std::isnan(log_inv_logit_diff(-INFTY, -2.34361)));
12+
}
13+
614
TEST(MathFunctions, log_inv_logit_diff) {
715
using stan::math::log_inv_logit_diff;
816

0 commit comments

Comments
 (0)