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