16 #ifndef POPS_GAMMA_KERNEL_HPP
17 #define POPS_GAMMA_KERNEL_HPP
45 throw std::invalid_argument(
46 "alpha and theta must greater than or equal to 0");
56 template<
class Generator>
71 throw std::invalid_argument(
"x must greater than or equal to 0");
85 double beta = 1.0 /
theta;
86 for (
int i = 0; i <
alpha; i++) {
88 sum += (1.0 / std::tgamma(i + 1)) * exp(-beta * x) * pow(beta * x, i);
106 if (x <= 0 || x >= 1) {
107 throw std::invalid_argument(
"icdf: x must be between 0.0 and 1.0");
111 double guess = lognormal.
icdf(x);
112 double check =
cdf(guess);
113 double numiterations = 1000;
114 double precision = 0.001;
115 for (
int i = 0; i < numiterations; i++) {
116 if (check < (x - precision) || check > (x + precision)) {
117 double dif = check - x;
120 double past_guess = guess;
121 double derivative = dif /
pdf(guess);
123 guess = std::max(guess / 10, std::min(guess * 10, guess - derivative));
128 while ((std::abs(dif) < std::abs(check - x)) && run) {
129 guess = (guess + past_guess) / 2.0;
141 throw std::invalid_argument(
"unable to find solution to gamma icdf ");
148 #endif // POPS_GAMMA_KERNEL_HPP