]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/math/distributions/detail/derived_accessors.hpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / boost / math / distributions / detail / derived_accessors.hpp
CommitLineData
7c673cae
FG
1// Copyright John Maddock 2006.
2// Use, modification and distribution are subject to the
3// Boost Software License, Version 1.0. (See accompanying file
4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef BOOST_STATS_DERIVED_HPP
7#define BOOST_STATS_DERIVED_HPP
8
9// This file implements various common properties of distributions
10// that can be implemented in terms of other properties:
11// variance OR standard deviation (see note below),
12// hazard, cumulative hazard (chf), coefficient_of_variation.
13//
14// Note that while both variance and standard_deviation are provided
15// here, each distribution MUST SPECIALIZE AT LEAST ONE OF THESE
16// otherwise these two versions will just call each other over and over
17// until stack space runs out ...
18
19// Of course there may be more efficient means of implementing these
20// that are specific to a particular distribution, but these generic
21// versions give these properties "for free" with most distributions.
22//
23// In order to make use of this header, it must be included AT THE END
24// of the distribution header, AFTER the distribution and its core
25// property accessors have been defined: this is so that compilers
26// that implement 2-phase lookup and early-type-checking of templates
f67539c2 27// can find the definitions referred to herein.
7c673cae
FG
28//
29
30#include <boost/type_traits/is_same.hpp>
31#include <boost/static_assert.hpp>
32
33#ifdef BOOST_MSVC
34# pragma warning(push)
35# pragma warning(disable: 4723) // potential divide by 0
36// Suppressing spurious warning in coefficient_of_variation
37#endif
38
39namespace boost{ namespace math{
40
41template <class Distribution>
42typename Distribution::value_type variance(const Distribution& dist);
43
44template <class Distribution>
45inline typename Distribution::value_type standard_deviation(const Distribution& dist)
46{
47 BOOST_MATH_STD_USING // ADL of sqrt.
48 return sqrt(variance(dist));
49}
50
51template <class Distribution>
52inline typename Distribution::value_type variance(const Distribution& dist)
53{
54 typename Distribution::value_type result = standard_deviation(dist);
55 return result * result;
56}
57
58template <class Distribution, class RealType>
59inline typename Distribution::value_type hazard(const Distribution& dist, const RealType& x)
60{ // hazard function
61 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda362.htm#HAZ
62 typedef typename Distribution::value_type value_type;
63 typedef typename Distribution::policy_type policy_type;
64 value_type p = cdf(complement(dist, x));
65 value_type d = pdf(dist, x);
66 if(d > p * tools::max_value<value_type>())
67 return policies::raise_overflow_error<value_type>(
68 "boost::math::hazard(const Distribution&, %1%)", 0, policy_type());
69 if(d == 0)
70 {
71 // This protects against 0/0, but is it the right thing to do?
72 return 0;
73 }
74 return d / p;
75}
76
77template <class Distribution, class RealType>
78inline typename Distribution::value_type chf(const Distribution& dist, const RealType& x)
79{ // cumulative hazard function.
80 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda362.htm#HAZ
81 BOOST_MATH_STD_USING
82 return -log(cdf(complement(dist, x)));
83}
84
85template <class Distribution>
86inline typename Distribution::value_type coefficient_of_variation(const Distribution& dist)
87{
88 typedef typename Distribution::value_type value_type;
89 typedef typename Distribution::policy_type policy_type;
90
91 using std::abs;
92
93 value_type m = mean(dist);
94 value_type d = standard_deviation(dist);
95 if((abs(m) < 1) && (d > abs(m) * tools::max_value<value_type>()))
96 { // Checks too that m is not zero,
97 return policies::raise_overflow_error<value_type>("boost::math::coefficient_of_variation(const Distribution&, %1%)", 0, policy_type());
98 }
99 return d / m; // so MSVC warning on zerodivide is spurious, and suppressed.
100}
101//
102// Next follow overloads of some of the standard accessors with mixed
103// argument types. We just use a typecast to forward on to the "real"
104// implementation with all arguments of the same type:
105//
106template <class Distribution, class RealType>
107inline typename Distribution::value_type pdf(const Distribution& dist, const RealType& x)
108{
109 typedef typename Distribution::value_type value_type;
110 return pdf(dist, static_cast<value_type>(x));
111}
112template <class Distribution, class RealType>
113inline typename Distribution::value_type cdf(const Distribution& dist, const RealType& x)
114{
115 typedef typename Distribution::value_type value_type;
116 return cdf(dist, static_cast<value_type>(x));
117}
118template <class Distribution, class RealType>
119inline typename Distribution::value_type quantile(const Distribution& dist, const RealType& x)
120{
121 typedef typename Distribution::value_type value_type;
122 return quantile(dist, static_cast<value_type>(x));
123}
124/*
125template <class Distribution, class RealType>
126inline typename Distribution::value_type chf(const Distribution& dist, const RealType& x)
127{
128 typedef typename Distribution::value_type value_type;
129 return chf(dist, static_cast<value_type>(x));
130}
131*/
132template <class Distribution, class RealType>
133inline typename Distribution::value_type cdf(const complemented2_type<Distribution, RealType>& c)
134{
135 typedef typename Distribution::value_type value_type;
136 return cdf(complement(c.dist, static_cast<value_type>(c.param)));
137}
138
139template <class Distribution, class RealType>
140inline typename Distribution::value_type quantile(const complemented2_type<Distribution, RealType>& c)
141{
142 typedef typename Distribution::value_type value_type;
143 return quantile(complement(c.dist, static_cast<value_type>(c.param)));
144}
145
146template <class Dist>
147inline typename Dist::value_type median(const Dist& d)
148{ // median - default definition for those distributions for which a
149 // simple closed form is not known,
150 // and for which a domain_error and/or NaN generating function is NOT defined.
151 typedef typename Dist::value_type value_type;
152 return quantile(d, static_cast<value_type>(0.5f));
153}
154
155} // namespace math
156} // namespace boost
157
158
159#ifdef BOOST_MSVC
160# pragma warning(pop)
161#endif
162
163#endif // BOOST_STATS_DERIVED_HPP