Skip to content

Commit 63e1700

Browse files
committed
fix finite_diff_stepsize and lower tolerance for AD tests on inv_Phi, mdivide_left_test, and mdivide_right_test
1 parent 7720c7a commit 63e1700

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

stan/math/mix/functor/wolfe_line_search.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ template <typename Eval, typename WolfeT, typename Option>
287287
inline bool check_armijo(const Eval& eval, const WolfeT& prev,
288288
const Option& opt) {
289289
return check_armijo(eval.obj(), prev.obj(), eval.alpha(), prev.dir(), opt);
290-
};
290+
}
291291

292292
template <typename Option>
293293
inline auto check_wolfe_curve(double dir_deriv_next, double dir_deriv_init,
@@ -404,7 +404,7 @@ struct Eval {
404404
inline const auto& dir() const { return dir_; }
405405
constexpr Eval(double alpha, double obj, double dir)
406406
: alpha_(alpha), obj_(obj), dir_(dir) {}
407-
constexpr explicit Eval() = default;
407+
constexpr Eval() = default;
408408
};
409409

410410
/**

stan/math/prim/fun/finite_diff_stepsize.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33

44
#include <stan/math/prim/fun/constants.hpp>
55
#include <stan/math/prim/fun/fabs.hpp>
6+
#include <limits>
67
#include <cmath>
78

89
namespace stan {
910
namespace math {
1011

11-
#include <cmath>
12-
#include <limits>
13-
#include <algorithm>
14-
1512
namespace internal {
1613
template <std::size_t StencilOrder = 2, typename T>
1714
inline constexpr auto eps_root_calc() {
@@ -74,13 +71,15 @@ inline constexpr auto eps_root_calc() {
7471
* differentiable) behavior along the perturbed coordinate.
7572
*/
7673
template <std::size_t StencilOrder = 2, typename T>
77-
inline T finite_diff_stepsize(T u, T c = T(1)) {
78-
const T scale = std::max(T(1), std::abs(u));
79-
const T eps_root = internal::eps_root_calc<StencilOrder, T>();
80-
const T h = c * eps_root * scale;
74+
inline auto finite_diff_stepsize(T u_, T c = T(1)) {
75+
using fp_t = std::conditional_t<std::is_integral_v<T>, double, T>;
76+
const fp_t u = static_cast<fp_t>(u_);
77+
const fp_t scale = std::max(fp_t{1}, std::abs(u));
78+
const fp_t eps_root = internal::eps_root_calc<StencilOrder, fp_t>();
79+
const fp_t h = static_cast<fp_t>(c) * eps_root * scale;
8180
// Ensure perturbation isn’t rounded away at u.
8281
if (u + h == u) {
83-
const T next = std::nextafter(u, std::numeric_limits<T>::infinity());
82+
const fp_t next = std::nextafter(u, std::numeric_limits<fp_t>::infinity());
8483
return std::max(h, next - u);
8584
}
8685
return h;

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stan/math/prim.hpp>
22
#include <gtest/gtest.h>
3+
#include <array>
34
#include <limits>
45

56
TEST(MathFunctions, inv_Phi) {
@@ -23,14 +24,24 @@ TEST(MathFunctions, inv_Phi) {
2324
TEST(MathFunctions, Equal) {
2425
using stan::math::inv_Phi;
2526
// test output generated with WolframAlpha
26-
27-
double p[] = {0.000000001, 0.0000001, 0.00001, 0.001, 0.05, 0.15, 0.25,
27+
constexpr std::array p_tails{
28+
0.000000001,
29+
0.0000001,
30+
0.99999,
31+
0.9999999,
32+
0.999999999
33+
};
34+
constexpr std::array p{0.00001, 0.001, 0.05, 0.15, 0.25,
2835
0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95,
29-
0.999, 0.99999, 0.9999999, 0.999999999};
30-
31-
double exact[]
32-
= {-5.9978070150076868715623102049115374195951202210145432633059905935,
33-
-5.199337582192816931587347266962336866509737160238716454318950531,
36+
0.999};
37+
constexpr std::array exact_tails{
38+
-5.9978070150076868715623102049115374195951202210145432633059905935,
39+
-5.199337582192816931587347266962336866509737160238716454318950531,
40+
4.2648907939228246284985246989063446293560532226954907262010508062,
41+
5.1993375821928169315873472669623368665097371602387164543189505310,
42+
5.9978070150076868715623102049115374195951202210145432633059905935
43+
};
44+
constexpr std::array exact{
3445
-4.264890793922824628498524698906344629356053222695490726201050806,
3546
-3.090232306167813541540399830107379205491008491865808855697171108,
3647
-1.644853626951472714863848907991632136083195744275322071769672094,
@@ -43,15 +54,16 @@ TEST(MathFunctions, Equal) {
4354
0.6744897501960817432022270145413071853869044150498618956620937885,
4455
1.0364333894937895797132440746735033661347405959859176279044548663,
4556
1.6448536269514727148638489079916321360831957442753220717696720944,
46-
3.0902323061678135415403998301073792054910084918658088556971711085,
47-
4.2648907939228246284985246989063446293560532226954907262010508062,
48-
5.1993375821928169315873472669623368665097371602387164543189505310,
49-
5.9978070150076868715623102049115374195951202210145432633059905935};
57+
3.0902323061678135415403998301073792054910084918658088556971711085};
5058

51-
int numValues = sizeof(p) / sizeof(double);
5259

53-
for (int i = 0; i < numValues; ++i) {
54-
EXPECT_NEAR(exact[i], inv_Phi(p[i]), 1.5e-15);
60+
for (int i = 0; i < p_tails.size(); ++i) {
61+
EXPECT_NEAR(exact_tails[i], inv_Phi(p_tails[i]), 4.6e-9) <<
62+
"at p_tails(" << i << ") = " << p_tails[i];
63+
}
64+
for (int i = 0; i < p.size(); ++i) {
65+
EXPECT_NEAR(exact[i], inv_Phi(p[i]), 1.5e-15) <<
66+
"at p(" << i << ") = " << p[i];
5567
}
5668
}
5769

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ TEST(MathMatrixPrim, mdivide_left_val) {
77
Ad << 2.0, 3.0, 5.0, 7.0;
88

99
stan::math::matrix_d I = Eigen::MatrixXd::Identity(2, 2);
10-
EXPECT_MATRIX_FLOAT_EQ(I, stan::math::mdivide_left(Ad, Ad));
10+
EXPECT_MATRIX_NEAR(I, stan::math::mdivide_left(Ad, Ad), 1e-15);
1111
}
1212

1313
TEST(MathMatrixPrim, mdivide_left_val2) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ TEST(MathMatrixPrim, mdivide_right_val) {
77
Ad << 2.0, 3.0, 5.0, 7.0;
88

99
stan::math::matrix_d I = Eigen::MatrixXd::Identity(2, 2);
10-
EXPECT_MATRIX_FLOAT_EQ(I, stan::math::mdivide_left(Ad, Ad));
10+
EXPECT_MATRIX_NEAR(I, stan::math::mdivide_left(Ad, Ad), 1e-15);
1111
}
1212

1313
TEST(MathMatrixPrim, mdivide_right_val2) {

0 commit comments

Comments
 (0)