Automatic Differentiation
 
Loading...
Searching...
No Matches
log_modified_bessel_first_kind.hpp
Go to the documentation of this file.
1// Original code derived from Boost and is distributed here
2// under the Boost license (licenses/boost-license.txt)
3// Copyright (c) 2006 Xiaogang Zhang
4// Copyright (c) 2007, 2017 John Maddock
5// Secondary code copyright by its author and is distributed here
6// under the BSD-3 license (LICENSE.md)
7
8#ifndef STAN_MATH_PRIM_FUN_LOG_MODIFIED_BESSEL_FIRST_KIND_HPP
9#define STAN_MATH_PRIM_FUN_LOG_MODIFIED_BESSEL_FIRST_KIND_HPP
10
23#include <boost/math/tools/rational.hpp>
24#include <cmath>
25
26namespace stan {
27namespace math {
28
29/* Log of the modified Bessel function of the first kind,
30 * which is better known as the Bessel I function. See
31 * modified_bessel_first_kind.hpp for the function definition.
32 * The derivatives are known to be incorrect for v = 0 because a
33 * simple constant 0 is returned.
34 *
35 * @tparam T1 type of the order (v)
36 * @tparam T2 type of argument (z)
37 * @param v Order, can be a non-integer but must be at least -1
38 * @param z Real non-negative number
39 * @throws std::domain_error if either v or z is NaN, z is
40 * negative, or v is less than -1
41 * @return log of Bessel I function
42 */
43template <typename T1, typename T2,
44 require_all_stan_scalar_t<T1, T2>* = nullptr>
46 const T1 v, const T2 z) {
47 check_not_nan("log_modified_bessel_first_kind", "first argument (order)", v);
48 check_not_nan("log_modified_bessel_first_kind", "second argument (variable)",
49 z);
50 check_nonnegative("log_modified_bessel_first_kind",
51 "second argument (variable)", z);
52 check_greater_or_equal("log_modified_bessel_first_kind",
53 "first argument (order)", v, -1);
54
55 using boost::math::tools::evaluate_polynomial;
56 using std::log;
57 using std::pow;
58 using std::sqrt;
59
61
62 if (z == 0) {
63 if (v == 0) {
64 return 0.0;
65 }
66 if (v > 0) {
67 return NEGATIVE_INFTY;
68 }
69 return INFTY;
70 }
71 if (is_inf(z)) {
72 return z;
73 }
74 if (v == 0) {
75 // WARNING: will not autodiff for v = 0 correctly
76 // modified from Boost's bessel_i0_imp in the double precision case,
77 // which refers to:
78 // Modified Bessel function of the first kind of order zero
79 // we use the approximating forms derived in:
80 // "Rational Approximations for the Modified Bessel Function of the
81 // First Kind -- I0(x) for Computations with Double Precision"
82 // by Pavel Holoborodko, see
83 // http://www.advanpix.com/2015/11/11/rational-approximations-for-the-modified-bessel-function-of-the-first-kind-i0-computations-double-precision
84 // The actual coefficients used are [Boost's] own, and extend
85 // Pavel's work to precisions other than double.
86
87 if (z < 7.75) {
88 // Bessel I0 over[10 ^ -16, 7.75]
89 // Max error in interpolated form : 3.042e-18
90 // Max Error found at double precision = Poly : 5.106609e-16
91 // Cheb : 5.239199e-16
92 static constexpr double P[15]
93 = {1.00000000000000000e+00, 2.49999999999999909e-01,
94 2.77777777777782257e-02, 1.73611111111023792e-03,
95 6.94444444453352521e-05, 1.92901234513219920e-06,
96 3.93675991102510739e-08, 6.15118672704439289e-10,
97 7.59407002058973446e-12, 7.59389793369836367e-14,
98 6.27767773636292611e-16, 4.34709704153272287e-18,
99 2.63417742690109154e-20, 1.13943037744822825e-22,
100 9.07926920085624812e-25};
101 return log1p_exp(lmultiply(2, z) - log(4.0)
102 + log(evaluate_polynomial(P, 0.25 * square(z))));
103 }
104 if (z < 500) {
105 // Max error in interpolated form : 1.685e-16
106 // Max Error found at double precision = Poly : 2.575063e-16
107 // Cheb : 2.247615e+00
108 static constexpr double P[22]
109 = {3.98942280401425088e-01, 4.98677850604961985e-02,
110 2.80506233928312623e-02, 2.92211225166047873e-02,
111 4.44207299493659561e-02, 1.30970574605856719e-01,
112 -3.35052280231727022e+00, 2.33025711583514727e+02,
113 -1.13366350697172355e+04, 4.24057674317867331e+05,
114 -1.23157028595698731e+07, 2.80231938155267516e+08,
115 -5.01883999713777929e+09, 7.08029243015109113e+10,
116 -7.84261082124811106e+11, 6.76825737854096565e+12,
117 -4.49034849696138065e+13, 2.24155239966958995e+14,
118 -8.13426467865659318e+14, 2.02391097391687777e+15,
119 -3.08675715295370878e+15, 2.17587543863819074e+15};
120 return z + log(evaluate_polynomial(P, inv(z))) - lmultiply(0.5, z);
121 }
122 // Max error in interpolated form : 2.437e-18
123 // Max Error found at double precision = Poly : 1.216719e-16
124 static constexpr double P[5]
125 = {3.98942280401432905e-01, 4.98677850491434560e-02,
126 2.80506308916506102e-02, 2.92179096853915176e-02,
127 4.53371208762579442e-02};
128 return z + log(evaluate_polynomial(P, inv(z))) - lmultiply(0.5, z);
129 }
130 if (v == 1) { // WARNING: will not autodiff for v = 1 correctly
131 // modified from Boost's bessel_i1_imp in the double precision case
132 // see credits above in the v == 0 case
133 if (z < 7.75) {
134 // Bessel I0 over[10 ^ -16, 7.75]
135 // Max error in interpolated form: 5.639e-17
136 // Max Error found at double precision = Poly: 1.795559e-16
137
138 static constexpr double P[13]
139 = {8.333333333333333803e-02, 6.944444444444341983e-03,
140 3.472222222225921045e-04, 1.157407407354987232e-05,
141 2.755731926254790268e-07, 4.920949692800671435e-09,
142 6.834657311305621830e-11, 7.593969849687574339e-13,
143 6.904822652741917551e-15, 5.220157095351373194e-17,
144 3.410720494727771276e-19, 1.625212890947171108e-21,
145 1.332898928162290861e-23};
146 T a = square(z) * 0.25;
147 T Q[3] = {1, 0.5, evaluate_polynomial(P, a)};
148 return log(z) + log(evaluate_polynomial(Q, a)) - LOG_TWO;
149 }
150 if (z < 500) {
151 // Max error in interpolated form: 1.796e-16
152 // Max Error found at double precision = Poly: 2.898731e-16
153
154 static constexpr double P[22]
155 = {3.989422804014406054e-01, -1.496033551613111533e-01,
156 -4.675104253598537322e-02, -4.090895951581637791e-02,
157 -5.719036414430205390e-02, -1.528189554374492735e-01,
158 3.458284470977172076e+00, -2.426181371595021021e+02,
159 1.178785865993440669e+04, -4.404655582443487334e+05,
160 1.277677779341446497e+07, -2.903390398236656519e+08,
161 5.192386898222206474e+09, -7.313784438967834057e+10,
162 8.087824484994859552e+11, -6.967602516005787001e+12,
163 4.614040809616582764e+13, -2.298849639457172489e+14,
164 8.325554073334618015e+14, -2.067285045778906105e+15,
165 3.146401654361325073e+15, -2.213318202179221945e+15};
166 return z + log(evaluate_polynomial(P, inv(z))) - lmultiply(0.5, z);
167 }
168 // Max error in interpolated form: 1.320e-19
169 // Max Error found at double precision = Poly: 7.065357e-17
170 static constexpr double P[5]
171 = {3.989422804014314820e-01, -1.496033551467584157e-01,
172 -4.675105322571775911e-02, -4.090421597376992892e-02,
173 -5.843630344778927582e-02};
174 return z + log(evaluate_polynomial(P, inv(z))) - lmultiply(0.5, z);
175 }
176 if (z > 100) {
177 // Boost does something like this in asymptotic_bessel_i_large_x
178 T lim = pow((square(v) + 2.5) / (2 * z), 3) / 24;
179 if (lim < (EPSILON * 10)) {
180 T s = 1;
181 T mu = 4 * square(v);
182 T ex = 8 * z;
183 T num = mu - 1;
184 T denom = ex;
185 s -= num / denom;
186 num *= mu - 9;
187 denom *= ex * 2;
188 s += num / denom;
189 num *= mu - 25;
190 denom *= ex * 3;
191 s -= num / denom;
192 s = z - log(sqrt(z * TWO_PI)) + log(s);
193 return s;
194 }
195 }
196
197 return_type_t<T2> log_half_z = log(0.5 * z);
198 return_type_t<T1> lgam = v > -1 ? lgamma(v + 1.0) : 0;
199 T lcons = (2.0 + v) * log_half_z;
200 T out;
201 if (v > -1) {
202 out = log_sum_exp(v * log_half_z - lgam, lcons - lgamma(v + 2));
203 lgam += log1p(v);
204 } else {
205 out = lcons;
206 }
207
208 double m = 2;
209 double lfac = 0;
210 T old_out;
211 do {
212 lfac += log(m);
213 lgam += log(v + m);
214 lcons += 2 * log_half_z;
215 old_out = out;
216 out = log_sum_exp(out, lcons - lfac - lgam); // underflows eventually
217 m++;
218 } while (out > old_out || out < old_out);
219 return out;
220}
221
232template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr>
233inline auto log_modified_bessel_first_kind(T1&& a, T2&& b) {
234 return apply_scalar_binary(
235 [](auto&& c, auto&& d) {
236 return log_modified_bessel_first_kind(std::forward<decltype(c)>(c),
237 std::forward<decltype(d)>(d));
238 },
239 std::forward<T1>(a), std::forward<T2>(b));
240}
241
242} // namespace math
243} // namespace stan
244
245#endif
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
static constexpr double EPSILON
Smallest positive value.
Definition constants.hpp:39
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
auto apply_scalar_binary(F &&f, T1 &&x, T2 &&y)
Base template function for vectorization of binary scalar functions defined by applying a functor to ...
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
return_type_t< T1, T2, double > log_modified_bessel_first_kind(const T1 v, const T2 z)
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:49
fvar< T > lmultiply(const fvar< T > &x1, const fvar< T > &x2)
Definition lmultiply.hpp:14
static constexpr double LOG_TWO
The natural logarithm of 2, .
Definition constants.hpp:78
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not greater or equal than low.
fvar< T > log1p_exp(const fvar< T > &x)
Definition log1p_exp.hpp:14
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:18
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
static constexpr double TWO_PI
Twice the value of , .
Definition constants.hpp:60
int is_inf(const fvar< T > &x)
Returns 1 if the input's value is infinite and 0 otherwise.
Definition is_inf.hpp:21
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:13
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:44
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...