Real invertCDF(const Real input) const { Real x = a_; Real fx = evaluateCDF(x)-input; Real s = 0.0; Real xs = 0.0; Real a = 1.0; Real tmp = 0.0; for (int i = 0; i < 100; i++) { if ( std::abs(fx) < ROL_EPSILON ) { break; } s = -fx/evaluatePDF(x); a = 1.0; xs = x + a*s; tmp = fx; fx = evaluateCDF(xs)-input; while ( std::abs(fx) > (1.0 - 1.e-4*a)*std::abs(tmp) ) { a *= 0.5; xs = x + a*s; fx = evaluateCDF(xs)-input; } x = xs; } return x; }
Real invertCDF(const Real input) const { if ( input <= 0. ) { return 0.; } Real x = input*gamma_shape_; Real fx = evaluateCDF(x)-input; Real s = 0., xs = 0., a = 1., tmp = 0.; for (size_t i = 0; i < 100; i++) { if ( std::abs(fx) < ROL_EPSILON ) { break; } s = -fx/evaluatePDF(x); a = 1.0; xs = x + a*s; tmp = fx; fx = evaluateCDF(xs)-input; while ( std::abs(fx) > (1.0 - 1.e-4*a)*std::abs(tmp) ) { a *= 0.5; xs = x + a*s; fx = evaluateCDF(xs)-input; } x = xs; } return x; }
void test_centered(const Real x, std::ostream &outStream = std::cout) const { Real X = x, vx = 0., vy = 0., dv = 0., t = 1., diff = 0., err = 0.; try { vx = 0.0; vy = 0.0; dv = evaluatePDF(X); t = 1.0; diff = 0.0; err = 0.0; outStream << std::scientific << std::setprecision(11); outStream << std::right << std::setw(20) << "CHECK DENSITY: f(x) = cdf(x) with x = " << X << " is correct?" << std::endl; outStream << std::right << std::setw(20) << "t" << std::setw(20) << "f'(x)" << std::setw(20) << "(f(x+t)-f(x-t))/2t" << std::setw(20) << "Error" << std::endl; for (int i = 0; i < 13; i++) { vx = evaluateCDF(X+t); vy = evaluateCDF(X-t); diff = 0.5*(vx-vy)/t; err = std::abs(diff-dv); outStream << std::scientific << std::setprecision(11) << std::right << std::setw(20) << t << std::setw(20) << dv << std::setw(20) << diff << std::setw(20) << err << std::endl; t *= 0.1; } outStream << "\n"; } catch(std::exception &e) { outStream << "Either evaluateCDF or evaluatePDF is not implemented!" << std::endl << std::endl; } // CHECK INTCDF try { vx = 0.0; vy = 0.0; dv = evaluateCDF(X); t = 1.0; diff = 0.0; err = 0.0; outStream << std::scientific << std::setprecision(11); outStream << std::right << std::setw(20) << "CHECK DENSITY: f(x) = intcdf(x) with x = " << X << " is correct?" << std::endl; outStream << std::right << std::setw(20) << "t" << std::setw(20) << "f'(x)" << std::setw(20) << "(f(x+t)-f(x-t))/2t" << std::setw(20) << "Error" << std::endl; for (int i = 0; i < 13; i++) { vx = integrateCDF(X+t); vy = integrateCDF(X-t); diff = 0.5*(vx-vy)/t; err = std::abs(diff-dv); outStream << std::scientific << std::setprecision(11) << std::right << std::setw(20) << t << std::setw(20) << dv << std::setw(20) << diff << std::setw(20) << err << std::endl; t *= 0.1; } outStream << std::endl; } catch(std::exception &e) { outStream << "Either evaluateCDF or integrateCDF is not implemented!" << std::endl << std::endl; } // CHECK INVCDF try { vx = evaluateCDF(X); vy = invertCDF(vx); err = std::abs(X-vy); outStream << std::scientific << std::setprecision(11); outStream << std::right << std::setw(20) << "CHECK DENSITY: f(x) = invcdf(x) with x = " << X << " is correct?" << std::endl; outStream << std::right << std::setw(20) << "cdf(x)" << std::setw(20) << "invcdf(cdf(x))" << std::setw(20) << "Error" << std::endl; outStream << std::scientific << std::setprecision(11) << std::right << std::setw(20) << vx << std::setw(20) << vy << std::setw(20) << err << std::endl << std::endl; } catch(std::exception &e) { outStream << "Either evaluateCDF or invertCDF is not implemented!" << std::endl << std::endl; } }