1
+ #include < test/unit/math/expect_near_rel.hpp>
1
2
#include < stan/math/prim.hpp>
2
3
#include < gtest/gtest.h>
3
4
#include < cmath>
4
5
#include < limits>
6
+ #include < vector>
5
7
6
8
TEST (MathFunctions, lbeta) {
7
9
using stan::math::lbeta;
@@ -21,3 +23,109 @@ TEST(MathFunctions, lbeta_nan) {
21
23
22
24
EXPECT_TRUE (std::isnan (stan::math::lbeta (nan , nan )));
23
25
}
26
+
27
+ namespace lbeta_test_internal {
28
+ struct TestValue {
29
+ double x;
30
+ double y;
31
+ double val;
32
+ };
33
+
34
+ // Test values generated in Mathematice, reproducible notebook at
35
+ // https://www.wolframcloud.com/obj/martin.modrak/Published/lbeta.nb
36
+ // Mathematica Code reproduced below for convenience:
37
+ //
38
+ // lbeta[x_,y_]:= LogGamma[x] + LogGamma[y] - LogGamma[x + y]
39
+ // out = OpenWrite["lbeta_test.txt"]
40
+ // xs= {8*10^-8,4*10^-3,1,1+10^-8,5,23, 19845};
41
+ // ys = {7*10^-11,2*10^-5,1+10^-12,1/2,2,1624};
42
+ // WriteString[out, "std::vector<TestValue> testValues = {"];
43
+ // For[i = 1, i <= Length[xs], i++, {
44
+ // For[j = 1, j <= Length[ys], j++, {
45
+ // cx = xs[[i]];
46
+ // cy = ys[[j]];
47
+
48
+ // val = N[lbeta[cx,cy],24];
49
+ // WriteString[out," {",CForm[cx],",",CForm[cy],",",
50
+ // CForm[val],"},"]
51
+ // }]
52
+ // }]
53
+ // extremeXs = {3*10^15,10^20};
54
+ // lowYs = {3, 100, 12895};
55
+ // For[i = 1, i <= Length[extremeXs], i++, {
56
+ // For[j = 1, j <= Length[lowYs], j++, {
57
+ // cx = extremeXs[[i]];
58
+ // cy = lowYs[[j]];
59
+ // val = N[lbeta[cx,cy],24];
60
+ // WriteString[out," {",CForm[cx],".0,",CForm[cy],",",
61
+ // CForm[val],"},"]
62
+ // }]
63
+ // }]
64
+ // WriteString[out,"};"];
65
+ // Close[out];
66
+ // FilePrint[%]
67
+ std::vector<TestValue> testValues = {
68
+ {8 .e -8 , 7 .e -11 , 23.3834004912898500586445 },
69
+ {8 .e -8 , 0.00002 , 16.3452312235394351410033 },
70
+ {8 .e -8 , 1.000000000001 , 16.3412392022725295437606 },
71
+ {8 .e -8 , 0.5 , 16.3412393131760679059067 },
72
+ {8 .e -8 , 2 , 16.3412391222725327438921 },
73
+ {8 .e -8 , 1624 , 16.3412385647081130254943 },
74
+ {0.004 , 7 .e -11 , 23.3825258913787298259023 },
75
+ {0.004 , 0.00002 , 10.8247656947117878792194 },
76
+ {0.004 , 1.000000000001 , 5.52146091786223987264715 },
77
+ {0.004 , 0.5 , 5.52697992926150653113797 },
78
+ {0.004 , 2 , 5.51746889659270898022044 },
79
+ {0.004 , 1624 , 5.48959582574332555214719 },
80
+ {1 , 7 .e -11 , 23.3825258738791892190926 },
81
+ {1 , 0.00002 , 10.8197782844102831106727 },
82
+ {1 , 1.000000000001 , -9.999999999995e-13 },
83
+ {1 , 0.5 , 0.693147180559945309417232 },
84
+ {1 , 2 , -0.693147180559945309417232 },
85
+ {1 , 1624 , -7.39264752072162326054032 },
86
+ {1.00000001 , 7 .e -11 , 23.3825258738791892179411 },
87
+ {1.00000001 , 0.00002 , 10.8197782844099541286699 },
88
+ {1.00000001 , 1.000000000001 , -1.00009999500064491739816e-8 },
89
+ {1.00000001 , 0.5 , 0.693147174422888956122731 },
90
+ {1.00000001 , 2 , -0.693147195559945246917232 },
91
+ {1.00000001 , 1624 , -7.39264760042333353631934 },
92
+ {5 , 7 .e -11 , 23.3825258737333558857627 },
93
+ {5 , 0.00002 , 10.8197366180283355258393 },
94
+ {5 , 1.000000000001 , -1.60943791243638370793409 },
95
+ {5 , 0.5 , -0.207395194346070587158746 },
96
+ {5 , 2 , -3.40119738166215537541324 },
97
+ {5 , 1624 , -33.7913357290267948074624 },
98
+ {23 , 7 .e -11 , 23.3825258736208322915813 },
99
+ {23 , 0.00002 , 10.819704468465374949026 },
100
+ {23 , 1.000000000001 , -3.13549421593288398231784 },
101
+ {23 , 0.5 , -0.989947810259228199543883 },
102
+ {23 , 2 , -6.31354804627709531045369 },
103
+ {23 , 1624 , -121.714785277510463870251 },
104
+ {19845 , 7 .e -11 , 23.3825258731460863706715 },
105
+ {19845 , 0.00002 , 10.8195688267825637640878 },
106
+ {19845 , 1.000000000001 , -9.89570736522763869861762 },
107
+ {19845 , 0.5 , -4.37548244086806082919414 },
108
+ {19845 , 2 , -19.7914651196913525680177 },
109
+ {19845 , 1624 , -5756.4146766727238501215 },
110
+ {3000000000000000.0 , 3 , -106.219018870176440545578 },
111
+ {3000000000000000.0 , 100 , -3204.60466298830574639047 },
112
+ {3000000000000000.0 , 12895 , -350396.988955562106921852 },
113
+ {100000000000000000000.0 , 3 , -137.461958399082795731692 },
114
+ {100000000000000000000.0 , 100 , -4246.03598061851596930944 },
115
+ {100000000000000000000.0 , 12895 , -484689.557363950217404711 },
116
+ };
117
+ } // namespace lbeta_test_internal
118
+
119
+ TEST (MathFunctions, lbeta_precomputed) {
120
+ using lbeta_test_internal::TestValue;
121
+ using lbeta_test_internal::testValues;
122
+ using stan::test::expect_near_rel;
123
+
124
+ for (TestValue t : testValues) {
125
+ std::ostringstream msg;
126
+ msg << std::setprecision (22 ) << " x = " << t.x << " , y = " << t.y ;
127
+
128
+ double val = stan::math::lbeta (t.x , t.y );
129
+ expect_near_rel (msg.str (), val, t.val );
130
+ }
131
+ }
0 commit comments