-
Notifications
You must be signed in to change notification settings - Fork 58
/
incomplete_gamma_inv.hpp
271 lines (241 loc) · 7.44 KB
/
incomplete_gamma_inv.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*################################################################################
##
## Copyright (C) 2016-2024 Keith O'Hara
##
## This file is part of the GCE-Math C++ library.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
################################################################################*/
/*
* inverse of the incomplete gamma function
*/
#ifndef _gcem_incomplete_gamma_inv_HPP
#define _gcem_incomplete_gamma_inv_HPP
namespace internal
{
template<typename T>
constexpr T incomplete_gamma_inv_decision(const T value, const T a, const T p, const T direc, const T lg_val, const int iter_count) noexcept;
//
// initial value for Halley
template<typename T>
constexpr
T
incomplete_gamma_inv_t_val_1(const T p)
noexcept
{ // a > 1.0
return( p > T(0.5) ? sqrt(-2*log(T(1) - p)) : sqrt(-2*log(p)) );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_t_val_2(const T a)
noexcept
{ // a <= 1.0
return( T(1) - T(0.253) * a - T(0.12) * a*a );
}
//
template<typename T>
constexpr
T
incomplete_gamma_inv_initial_val_1_int_begin(const T t_val)
noexcept
{ // internal for a > 1.0
return( t_val - (T(2.515517L) + T(0.802853L)*t_val + T(0.010328L)*t_val*t_val) \
/ (T(1) + T(1.432788L)*t_val + T(0.189269L)*t_val*t_val + T(0.001308L)*t_val*t_val*t_val) );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_initial_val_1_int_end(const T value_inp, const T a)
noexcept
{ // internal for a > 1.0
return max( T(1E-04), a*pow(T(1) - T(1)/(9*a) - value_inp/(3*sqrt(a)), 3) );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_initial_val_1(const T a, const T t_val, const T sgn_term)
noexcept
{ // a > 1.0
return incomplete_gamma_inv_initial_val_1_int_end(sgn_term*incomplete_gamma_inv_initial_val_1_int_begin(t_val), a);
}
template<typename T>
constexpr
T
incomplete_gamma_inv_initial_val_2(const T a, const T p, const T t_val)
noexcept
{ // a <= 1.0
return( p < t_val ? \
// if
pow(p/t_val,T(1)/a) :
// else
T(1) - log(T(1) - (p - t_val)/(T(1) - t_val)) );
}
// initial value
template<typename T>
constexpr
T
incomplete_gamma_inv_initial_val(const T a, const T p)
noexcept
{
return( a > T(1) ? \
// if
incomplete_gamma_inv_initial_val_1(a,
incomplete_gamma_inv_t_val_1(p),
p > T(0.5) ? T(-1) : T(1)) :
// else
incomplete_gamma_inv_initial_val_2(a,p,
incomplete_gamma_inv_t_val_2(a)));
}
//
// Halley recursion
template<typename T>
constexpr
T
incomplete_gamma_inv_err_val(const T value, const T a, const T p)
noexcept
{ // err_val = f(x)
return( incomplete_gamma(a,value) - p );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_deriv_1(const T value, const T a, const T lg_val)
noexcept
{ // derivative of the incomplete gamma function w.r.t. x
return( exp( - value + (a - T(1))*log(value) - lg_val ) );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_deriv_2(const T value, const T a, const T deriv_1)
noexcept
{ // second derivative of the incomplete gamma function w.r.t. x
return( deriv_1*((a - T(1))/value - T(1)) );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_ratio_val_1(const T value, const T a, const T p, const T deriv_1)
noexcept
{
return( incomplete_gamma_inv_err_val(value,a,p) / deriv_1 );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_ratio_val_2(const T value, const T a, const T deriv_1)
noexcept
{
return( incomplete_gamma_inv_deriv_2(value,a,deriv_1) / deriv_1 );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_halley(const T ratio_val_1, const T ratio_val_2)
noexcept
{
return( ratio_val_1 / max( T(0.8), min( T(1.2), T(1) - T(0.5)*ratio_val_1*ratio_val_2 ) ) );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_recur(const T value, const T a, const T p, const T deriv_1, const T lg_val, const int iter_count)
noexcept
{
return incomplete_gamma_inv_decision(value, a, p,
incomplete_gamma_inv_halley(incomplete_gamma_inv_ratio_val_1(value,a,p,deriv_1),
incomplete_gamma_inv_ratio_val_2(value,a,deriv_1)),
lg_val, iter_count);
}
template<typename T>
constexpr
T
incomplete_gamma_inv_decision(const T value, const T a, const T p, const T direc, const T lg_val, const int iter_count)
noexcept
{
// return( abs(direc) > GCEM_INCML_GAMMA_INV_TOL ? incomplete_gamma_inv_recur(value - direc, a, p, incomplete_gamma_inv_deriv_1(value,a,lg_val), lg_val) : value - direc );
return( iter_count <= GCEM_INCML_GAMMA_INV_MAX_ITER ? \
// if
incomplete_gamma_inv_recur(value-direc,a,p,
incomplete_gamma_inv_deriv_1(value,a,lg_val),
lg_val,iter_count+1) :
// else
value - direc );
}
template<typename T>
constexpr
T
incomplete_gamma_inv_begin(const T initial_val, const T a, const T p, const T lg_val)
noexcept
{
return incomplete_gamma_inv_recur(initial_val,a,p,
incomplete_gamma_inv_deriv_1(initial_val,a,lg_val), lg_val,1);
}
template<typename T>
constexpr
T
incomplete_gamma_inv_check(const T a, const T p)
noexcept
{
return( // NaN check
any_nan(a, p) ? \
GCLIM<T>::quiet_NaN() :
//
GCLIM<T>::min() > p ? \
T(0) :
p > T(1) ? \
GCLIM<T>::quiet_NaN() :
GCLIM<T>::min() > abs(T(1) - p) ? \
GCLIM<T>::infinity() :
//
GCLIM<T>::min() > a ? \
T(0) :
// else
incomplete_gamma_inv_begin(incomplete_gamma_inv_initial_val(a,p),a,p,lgamma(a)) );
}
template<typename T1, typename T2, typename TC = common_return_t<T1,T2>>
constexpr
TC
incomplete_gamma_inv_type_check(const T1 a, const T2 p)
noexcept
{
return incomplete_gamma_inv_check(static_cast<TC>(a),
static_cast<TC>(p));
}
}
/**
* Compile-time inverse incomplete gamma function
*
* @param a a real-valued, non-negative input.
* @param p a real-valued input with values in the unit-interval.
*
* @return Computes the inverse incomplete gamma function, a value \f$ x \f$ such that
* \f[ f(x) := \frac{\gamma(a,x)}{\Gamma(a)} - p \f]
* equal to zero, for a given \c p.
* GCE-Math finds this root using Halley's method:
* \f[ x_{n+1} = x_n - \frac{f(x_n)/f'(x_n)}{1 - 0.5 \frac{f(x_n)}{f'(x_n)} \frac{f''(x_n)}{f'(x_n)} } \f]
* where
* \f[ \frac{\partial}{\partial x} \left(\frac{\gamma(a,x)}{\Gamma(a)}\right) = \frac{1}{\Gamma(a)} x^{a-1} \exp(-x) \f]
* \f[ \frac{\partial^2}{\partial x^2} \left(\frac{\gamma(a,x)}{\Gamma(a)}\right) = \frac{1}{\Gamma(a)} x^{a-1} \exp(-x) \left( \frac{a-1}{x} - 1 \right) \f]
*/
template<typename T1, typename T2>
constexpr
common_return_t<T1,T2>
incomplete_gamma_inv(const T1 a, const T2 p)
noexcept
{
return internal::incomplete_gamma_inv_type_check(a,p);
}
#endif