123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- #ifndef BOOST_STATS_INVERSE_GAUSSIAN_HPP
- #define BOOST_STATS_INVERSE_GAUSSIAN_HPP
- #ifdef _MSC_VER
- #pragma warning(disable: 4512)
- #endif
- #include <boost/math/special_functions/erf.hpp>
- #include <boost/math/distributions/complement.hpp>
- #include <boost/math/distributions/detail/common_error_handling.hpp>
- #include <boost/math/distributions/normal.hpp>
- #include <boost/math/distributions/gamma.hpp>
- #include <boost/math/tools/tuple.hpp>
- #include <boost/math/tools/roots.hpp>
- #include <utility>
- namespace boost{ namespace math{
- template <class RealType = double, class Policy = policies::policy<> >
- class inverse_gaussian_distribution
- {
- public:
- using value_type = RealType;
- using policy_type = Policy;
- explicit inverse_gaussian_distribution(RealType l_mean = 1, RealType l_scale = 1)
- : m_mean(l_mean), m_scale(l_scale)
- {
- static const char* function = "boost::math::inverse_gaussian_distribution<%1%>::inverse_gaussian_distribution";
- RealType result;
- detail::check_scale(function, l_scale, &result, Policy());
- detail::check_location(function, l_mean, &result, Policy());
- detail::check_x_gt0(function, l_mean, &result, Policy());
- }
- RealType mean()const
- {
- return m_mean;
- }
-
- RealType location()const
- {
- return m_mean;
- }
- RealType scale()const
- {
- return m_scale;
- }
- RealType shape()const
- {
- return m_scale / m_mean;
- }
- private:
-
-
-
- RealType m_mean;
- RealType m_scale;
- };
- using inverse_gaussian = inverse_gaussian_distribution<double>;
- #ifdef __cpp_deduction_guides
- template <class RealType>
- inverse_gaussian_distribution(RealType)->inverse_gaussian_distribution<typename boost::math::tools::promote_args<RealType>::type>;
- template <class RealType>
- inverse_gaussian_distribution(RealType,RealType)->inverse_gaussian_distribution<typename boost::math::tools::promote_args<RealType>::type>;
- #endif
- template <class RealType, class Policy>
- inline std::pair<RealType, RealType> range(const inverse_gaussian_distribution<RealType, Policy>& )
- {
- using boost::math::tools::max_value;
- return std::pair<RealType, RealType>(static_cast<RealType>(0.), max_value<RealType>());
- }
- template <class RealType, class Policy>
- inline std::pair<RealType, RealType> support(const inverse_gaussian_distribution<RealType, Policy>& )
- {
-
- using boost::math::tools::max_value;
- return std::pair<RealType, RealType>(static_cast<RealType>(0.), max_value<RealType>());
- }
- template <class RealType, class Policy>
- inline RealType pdf(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& x)
- {
- BOOST_MATH_STD_USING
- RealType scale = dist.scale();
- RealType mean = dist.mean();
- RealType result = 0;
- static const char* function = "boost::math::pdf(const inverse_gaussian_distribution<%1%>&, %1%)";
- if(false == detail::check_scale(function, scale, &result, Policy()))
- {
- return result;
- }
- if(false == detail::check_location(function, mean, &result, Policy()))
- {
- return result;
- }
- if(false == detail::check_x_gt0(function, mean, &result, Policy()))
- {
- return result;
- }
- if(false == detail::check_positive_x(function, x, &result, Policy()))
- {
- return result;
- }
- if (x == 0)
- {
- return 0;
- }
- result =
- sqrt(scale / (constants::two_pi<RealType>() * x * x * x))
- * exp(-scale * (x - mean) * (x - mean) / (2 * x * mean * mean));
- return result;
- }
- template <class RealType, class Policy>
- inline RealType logpdf(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& x)
- {
- BOOST_MATH_STD_USING
- RealType scale = dist.scale();
- RealType mean = dist.mean();
- RealType result = -std::numeric_limits<RealType>::infinity();
- static const char* function = "boost::math::logpdf(const inverse_gaussian_distribution<%1%>&, %1%)";
- if(false == detail::check_scale(function, scale, &result, Policy()))
- {
- return result;
- }
- if(false == detail::check_location(function, mean, &result, Policy()))
- {
- return result;
- }
- if(false == detail::check_x_gt0(function, mean, &result, Policy()))
- {
- return result;
- }
- if(false == detail::check_positive_x(function, x, &result, Policy()))
- {
- return result;
- }
- if (x == 0)
- {
- return std::numeric_limits<RealType>::quiet_NaN();
- }
- const RealType two_pi = boost::math::constants::two_pi<RealType>();
-
- result = (-scale*pow(mean - x, RealType(2))/(mean*mean*x) + log(scale) - 3*log(x) - log(two_pi)) / 2;
- return result;
- }
- template <class RealType, class Policy>
- inline RealType cdf(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& x)
- {
- BOOST_MATH_STD_USING
- RealType scale = dist.scale();
- RealType mean = dist.mean();
- static const char* function = "boost::math::cdf(const inverse_gaussian_distribution<%1%>&, %1%)";
- RealType result = 0;
- if(false == detail::check_scale(function, scale, &result, Policy()))
- {
- return result;
- }
- if(false == detail::check_location(function, mean, &result, Policy()))
- {
- return result;
- }
- if (false == detail::check_x_gt0(function, mean, &result, Policy()))
- {
- return result;
- }
- if(false == detail::check_positive_x(function, x, &result, Policy()))
- {
- return result;
- }
- if (x == 0)
- {
- return 0;
- }
-
-
-
- normal_distribution<RealType> n01;
- RealType n0 = sqrt(scale / x);
- n0 *= ((x / mean) -1);
- RealType n1 = cdf(n01, n0);
- RealType expfactor = exp(2 * scale / mean);
- RealType n3 = - sqrt(scale / x);
- n3 *= (x / mean) + 1;
- RealType n4 = cdf(n01, n3);
- result = n1 + expfactor * n4;
- return result;
- }
- template <class RealType, class Policy>
- struct inverse_gaussian_quantile_functor
- {
- inverse_gaussian_quantile_functor(const boost::math::inverse_gaussian_distribution<RealType, Policy> dist, RealType const& p)
- : distribution(dist), prob(p)
- {
- }
- boost::math::tuple<RealType, RealType> operator()(RealType const& x)
- {
- RealType c = cdf(distribution, x);
- RealType fx = c - prob;
- RealType dx = pdf(distribution, x);
-
- return boost::math::make_tuple(fx, dx);
- }
- private:
- const boost::math::inverse_gaussian_distribution<RealType, Policy> distribution;
- RealType prob;
- };
- template <class RealType, class Policy>
- struct inverse_gaussian_quantile_complement_functor
- {
- inverse_gaussian_quantile_complement_functor(const boost::math::inverse_gaussian_distribution<RealType, Policy> dist, RealType const& p)
- : distribution(dist), prob(p)
- {
- }
- boost::math::tuple<RealType, RealType> operator()(RealType const& x)
- {
- RealType c = cdf(complement(distribution, x));
- RealType fx = c - prob;
- RealType dx = -pdf(distribution, x);
-
-
- return boost::math::make_tuple(fx, dx);
- }
- private:
- const boost::math::inverse_gaussian_distribution<RealType, Policy> distribution;
- RealType prob;
- };
- namespace detail
- {
- template <class RealType>
- inline RealType guess_ig(RealType p, RealType mu = 1, RealType lambda = 1)
- {
- BOOST_MATH_STD_USING
- using boost::math::policies::policy;
-
- using boost::math::policies::overflow_error;
-
- using boost::math::policies::ignore_error;
- using no_overthrow_policy = policy<overflow_error<ignore_error>>;
- RealType x;
- RealType phi = lambda / mu;
- if (phi > 2.)
- {
-
-
-
-
-
-
- normal_distribution<RealType, no_overthrow_policy> n01;
- x = mu * exp(quantile(n01, p) / sqrt(phi) - 1/(2 * phi));
- }
- else
- {
-
- using boost::math::gamma_distribution;
-
- using gamma_nooverflow = gamma_distribution<RealType, no_overthrow_policy>;
- gamma_nooverflow g(static_cast<RealType>(0.5), static_cast<RealType>(1.));
-
- RealType qg = quantile(complement(g, p));
- x = lambda / (qg * 2);
-
- if (x > mu/2)
- {
-
- RealType q = quantile(g, p);
-
-
- x = mu * exp(q / sqrt(phi) - 1/(2 * phi));
- }
- }
- return x;
- }
- }
- template <class RealType, class Policy>
- inline RealType quantile(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& p)
- {
- BOOST_MATH_STD_USING
-
- RealType mean = dist.mean();
- RealType scale = dist.scale();
- static const char* function = "boost::math::quantile(const inverse_gaussian_distribution<%1%>&, %1%)";
- RealType result = 0;
- if(false == detail::check_scale(function, scale, &result, Policy()))
- return result;
- if(false == detail::check_location(function, mean, &result, Policy()))
- return result;
- if (false == detail::check_x_gt0(function, mean, &result, Policy()))
- return result;
- if(false == detail::check_probability(function, p, &result, Policy()))
- return result;
- if (p == 0)
- {
- return 0;
- }
- if (p == 1)
- {
- result = policies::raise_overflow_error<RealType>(function,
- "probability parameter is 1, but must be < 1!", Policy());
- return result;
- }
- RealType guess = detail::guess_ig(p, dist.mean(), dist.scale());
- using boost::math::tools::max_value;
- RealType min = static_cast<RealType>(0);
- RealType max = max_value<RealType>();
-
-
-
- int get_digits = policies::digits<RealType, Policy>();
- std::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
- using boost::math::tools::newton_raphson_iterate;
- result =
- newton_raphson_iterate(inverse_gaussian_quantile_functor<RealType, Policy>(dist, p), guess, min, max, get_digits, max_iter);
- if (max_iter >= policies::get_max_root_iterations<Policy>())
- {
- return policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
- " either there is no answer to quantile or the answer is infinite. Current best guess is %1%", result, Policy());
- }
- return result;
- }
- template <class RealType, class Policy>
- inline RealType cdf(const complemented2_type<inverse_gaussian_distribution<RealType, Policy>, RealType>& c)
- {
- BOOST_MATH_STD_USING
- RealType scale = c.dist.scale();
- RealType mean = c.dist.mean();
- RealType x = c.param;
- static const char* function = "boost::math::cdf(const complement(inverse_gaussian_distribution<%1%>&), %1%)";
- RealType result = 0;
- if(false == detail::check_scale(function, scale, &result, Policy()))
- return result;
- if(false == detail::check_location(function, mean, &result, Policy()))
- return result;
- if (false == detail::check_x_gt0(function, mean, &result, Policy()))
- return result;
- if(false == detail::check_positive_x(function, x, &result, Policy()))
- return result;
- normal_distribution<RealType> n01;
- RealType n0 = sqrt(scale / x);
- n0 *= ((x / mean) -1);
- RealType cdf_1 = cdf(complement(n01, n0));
- RealType expfactor = exp(2 * scale / mean);
- RealType n3 = - sqrt(scale / x);
- n3 *= (x / mean) + 1;
-
- RealType n6 = cdf(complement(n01, +sqrt(scale/x) * ((x /mean) + 1)));
-
- result = cdf_1 - expfactor * n6;
- return result;
- }
- template <class RealType, class Policy>
- inline RealType quantile(const complemented2_type<inverse_gaussian_distribution<RealType, Policy>, RealType>& c)
- {
- BOOST_MATH_STD_USING
- RealType scale = c.dist.scale();
- RealType mean = c.dist.mean();
- static const char* function = "boost::math::quantile(const complement(inverse_gaussian_distribution<%1%>&), %1%)";
- RealType result = 0;
- if(false == detail::check_scale(function, scale, &result, Policy()))
- return result;
- if(false == detail::check_location(function, mean, &result, Policy()))
- return result;
- if (false == detail::check_x_gt0(function, mean, &result, Policy()))
- return result;
- RealType q = c.param;
- if(false == detail::check_probability(function, q, &result, Policy()))
- return result;
- RealType guess = detail::guess_ig(q, mean, scale);
-
- using boost::math::tools::max_value;
- RealType min = static_cast<RealType>(0);
- RealType max = max_value<RealType>();
-
-
- int get_digits = policies::digits<RealType, Policy>();
- std::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
- using boost::math::tools::newton_raphson_iterate;
- result = newton_raphson_iterate(inverse_gaussian_quantile_complement_functor<RealType, Policy>(c.dist, q), guess, min, max, get_digits, max_iter);
- if (max_iter >= policies::get_max_root_iterations<Policy>())
- {
- return policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
- " either there is no answer to quantile or the answer is infinite. Current best guess is %1%", result, Policy());
- }
- return result;
- }
- template <class RealType, class Policy>
- inline RealType mean(const inverse_gaussian_distribution<RealType, Policy>& dist)
- {
- return dist.mean();
- }
- template <class RealType, class Policy>
- inline RealType scale(const inverse_gaussian_distribution<RealType, Policy>& dist)
- {
- return dist.scale();
- }
- template <class RealType, class Policy>
- inline RealType shape(const inverse_gaussian_distribution<RealType, Policy>& dist)
- {
- return dist.shape();
- }
- template <class RealType, class Policy>
- inline RealType standard_deviation(const inverse_gaussian_distribution<RealType, Policy>& dist)
- {
- BOOST_MATH_STD_USING
- RealType scale = dist.scale();
- RealType mean = dist.mean();
- RealType result = sqrt(mean * mean * mean / scale);
- return result;
- }
- template <class RealType, class Policy>
- inline RealType mode(const inverse_gaussian_distribution<RealType, Policy>& dist)
- {
- BOOST_MATH_STD_USING
- RealType scale = dist.scale();
- RealType mean = dist.mean();
- RealType result = mean * (sqrt(1 + (9 * mean * mean)/(4 * scale * scale))
- - 3 * mean / (2 * scale));
- return result;
- }
- template <class RealType, class Policy>
- inline RealType skewness(const inverse_gaussian_distribution<RealType, Policy>& dist)
- {
- BOOST_MATH_STD_USING
- RealType scale = dist.scale();
- RealType mean = dist.mean();
- RealType result = 3 * sqrt(mean/scale);
- return result;
- }
- template <class RealType, class Policy>
- inline RealType kurtosis(const inverse_gaussian_distribution<RealType, Policy>& dist)
- {
- RealType scale = dist.scale();
- RealType mean = dist.mean();
- RealType result = 15 * mean / scale -3;
- return result;
- }
- template <class RealType, class Policy>
- inline RealType kurtosis_excess(const inverse_gaussian_distribution<RealType, Policy>& dist)
- {
- RealType scale = dist.scale();
- RealType mean = dist.mean();
- RealType result = 15 * mean / scale;
- return result;
- }
- }
- }
- #include <boost/math/distributions/detail/derived_accessors.hpp>
- #endif
|