Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/math/distributions/extreme_value.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children | c530137014c0 |
comparison
equal
deleted
inserted
replaced
15:663ca0da4350 | 16:2665513ce2d3 |
---|---|
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_EXTREME_VALUE_HPP | |
7 #define BOOST_STATS_EXTREME_VALUE_HPP | |
8 | |
9 #include <boost/math/distributions/fwd.hpp> | |
10 #include <boost/math/constants/constants.hpp> | |
11 #include <boost/math/special_functions/log1p.hpp> | |
12 #include <boost/math/special_functions/expm1.hpp> | |
13 #include <boost/math/distributions/complement.hpp> | |
14 #include <boost/math/distributions/detail/common_error_handling.hpp> | |
15 #include <boost/config/no_tr1/cmath.hpp> | |
16 | |
17 // | |
18 // This is the maximum extreme value distribution, see | |
19 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366g.htm | |
20 // and http://mathworld.wolfram.com/ExtremeValueDistribution.html | |
21 // Also known as a Fisher-Tippett distribution, a log-Weibull | |
22 // distribution or a Gumbel distribution. | |
23 | |
24 #include <utility> | |
25 | |
26 #ifdef BOOST_MSVC | |
27 # pragma warning(push) | |
28 # pragma warning(disable: 4702) // unreachable code (return after domain_error throw). | |
29 #endif | |
30 | |
31 namespace boost{ namespace math{ | |
32 | |
33 namespace detail{ | |
34 // | |
35 // Error check: | |
36 // | |
37 template <class RealType, class Policy> | |
38 inline bool verify_scale_b(const char* function, RealType b, RealType* presult, const Policy& pol) | |
39 { | |
40 if((b <= 0) || !(boost::math::isfinite)(b)) | |
41 { | |
42 *presult = policies::raise_domain_error<RealType>( | |
43 function, | |
44 "The scale parameter \"b\" must be finite and > 0, but was: %1%.", b, pol); | |
45 return false; | |
46 } | |
47 return true; | |
48 } | |
49 | |
50 } // namespace detail | |
51 | |
52 template <class RealType = double, class Policy = policies::policy<> > | |
53 class extreme_value_distribution | |
54 { | |
55 public: | |
56 typedef RealType value_type; | |
57 typedef Policy policy_type; | |
58 | |
59 extreme_value_distribution(RealType a = 0, RealType b = 1) | |
60 : m_a(a), m_b(b) | |
61 { | |
62 RealType err; | |
63 detail::verify_scale_b("boost::math::extreme_value_distribution<%1%>::extreme_value_distribution", b, &err, Policy()); | |
64 detail::check_finite("boost::math::extreme_value_distribution<%1%>::extreme_value_distribution", a, &err, Policy()); | |
65 } // extreme_value_distribution | |
66 | |
67 RealType location()const { return m_a; } | |
68 RealType scale()const { return m_b; } | |
69 | |
70 private: | |
71 RealType m_a, m_b; | |
72 }; | |
73 | |
74 typedef extreme_value_distribution<double> extreme_value; | |
75 | |
76 template <class RealType, class Policy> | |
77 inline const std::pair<RealType, RealType> range(const extreme_value_distribution<RealType, Policy>& /*dist*/) | |
78 { // Range of permissible values for random variable x. | |
79 using boost::math::tools::max_value; | |
80 return std::pair<RealType, RealType>( | |
81 std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(), | |
82 std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>()); | |
83 } | |
84 | |
85 template <class RealType, class Policy> | |
86 inline const std::pair<RealType, RealType> support(const extreme_value_distribution<RealType, Policy>& /*dist*/) | |
87 { // Range of supported values for random variable x. | |
88 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero. | |
89 using boost::math::tools::max_value; | |
90 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); | |
91 } | |
92 | |
93 template <class RealType, class Policy> | |
94 inline RealType pdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x) | |
95 { | |
96 BOOST_MATH_STD_USING // for ADL of std functions | |
97 | |
98 static const char* function = "boost::math::pdf(const extreme_value_distribution<%1%>&, %1%)"; | |
99 | |
100 RealType a = dist.location(); | |
101 RealType b = dist.scale(); | |
102 RealType result = 0; | |
103 if((boost::math::isinf)(x)) | |
104 return 0.0f; | |
105 if(0 == detail::verify_scale_b(function, b, &result, Policy())) | |
106 return result; | |
107 if(0 == detail::check_finite(function, a, &result, Policy())) | |
108 return result; | |
109 if(0 == detail::check_x(function, x, &result, Policy())) | |
110 return result; | |
111 result = exp((a-x)/b) * exp(-exp((a-x)/b)) / b; | |
112 return result; | |
113 } // pdf | |
114 | |
115 template <class RealType, class Policy> | |
116 inline RealType cdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x) | |
117 { | |
118 BOOST_MATH_STD_USING // for ADL of std functions | |
119 | |
120 static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)"; | |
121 | |
122 if((boost::math::isinf)(x)) | |
123 return x < 0 ? 0.0f : 1.0f; | |
124 RealType a = dist.location(); | |
125 RealType b = dist.scale(); | |
126 RealType result = 0; | |
127 if(0 == detail::verify_scale_b(function, b, &result, Policy())) | |
128 return result; | |
129 if(0 == detail::check_finite(function, a, &result, Policy())) | |
130 return result; | |
131 if(0 == detail::check_finite(function, a, &result, Policy())) | |
132 return result; | |
133 if(0 == detail::check_x("boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)", x, &result, Policy())) | |
134 return result; | |
135 | |
136 result = exp(-exp((a-x)/b)); | |
137 | |
138 return result; | |
139 } // cdf | |
140 | |
141 template <class RealType, class Policy> | |
142 RealType quantile(const extreme_value_distribution<RealType, Policy>& dist, const RealType& p) | |
143 { | |
144 BOOST_MATH_STD_USING // for ADL of std functions | |
145 | |
146 static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)"; | |
147 | |
148 RealType a = dist.location(); | |
149 RealType b = dist.scale(); | |
150 RealType result = 0; | |
151 if(0 == detail::verify_scale_b(function, b, &result, Policy())) | |
152 return result; | |
153 if(0 == detail::check_finite(function, a, &result, Policy())) | |
154 return result; | |
155 if(0 == detail::check_probability(function, p, &result, Policy())) | |
156 return result; | |
157 | |
158 if(p == 0) | |
159 return -policies::raise_overflow_error<RealType>(function, 0, Policy()); | |
160 if(p == 1) | |
161 return policies::raise_overflow_error<RealType>(function, 0, Policy()); | |
162 | |
163 result = a - log(-log(p)) * b; | |
164 | |
165 return result; | |
166 } // quantile | |
167 | |
168 template <class RealType, class Policy> | |
169 inline RealType cdf(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c) | |
170 { | |
171 BOOST_MATH_STD_USING // for ADL of std functions | |
172 | |
173 static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)"; | |
174 | |
175 if((boost::math::isinf)(c.param)) | |
176 return c.param < 0 ? 1.0f : 0.0f; | |
177 RealType a = c.dist.location(); | |
178 RealType b = c.dist.scale(); | |
179 RealType result = 0; | |
180 if(0 == detail::verify_scale_b(function, b, &result, Policy())) | |
181 return result; | |
182 if(0 == detail::check_finite(function, a, &result, Policy())) | |
183 return result; | |
184 if(0 == detail::check_x(function, c.param, &result, Policy())) | |
185 return result; | |
186 | |
187 result = -boost::math::expm1(-exp((a-c.param)/b), Policy()); | |
188 | |
189 return result; | |
190 } | |
191 | |
192 template <class RealType, class Policy> | |
193 RealType quantile(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c) | |
194 { | |
195 BOOST_MATH_STD_USING // for ADL of std functions | |
196 | |
197 static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)"; | |
198 | |
199 RealType a = c.dist.location(); | |
200 RealType b = c.dist.scale(); | |
201 RealType q = c.param; | |
202 RealType result = 0; | |
203 if(0 == detail::verify_scale_b(function, b, &result, Policy())) | |
204 return result; | |
205 if(0 == detail::check_finite(function, a, &result, Policy())) | |
206 return result; | |
207 if(0 == detail::check_probability(function, q, &result, Policy())) | |
208 return result; | |
209 | |
210 if(q == 0) | |
211 return policies::raise_overflow_error<RealType>(function, 0, Policy()); | |
212 if(q == 1) | |
213 return -policies::raise_overflow_error<RealType>(function, 0, Policy()); | |
214 | |
215 result = a - log(-boost::math::log1p(-q, Policy())) * b; | |
216 | |
217 return result; | |
218 } | |
219 | |
220 template <class RealType, class Policy> | |
221 inline RealType mean(const extreme_value_distribution<RealType, Policy>& dist) | |
222 { | |
223 RealType a = dist.location(); | |
224 RealType b = dist.scale(); | |
225 RealType result = 0; | |
226 if(0 == detail::verify_scale_b("boost::math::mean(const extreme_value_distribution<%1%>&)", b, &result, Policy())) | |
227 return result; | |
228 if(0 == detail::check_scale("boost::math::mean(const extreme_value_distribution<%1%>&)", a, &result, Policy())) | |
229 return result; | |
230 return a + constants::euler<RealType>() * b; | |
231 } | |
232 | |
233 template <class RealType, class Policy> | |
234 inline RealType standard_deviation(const extreme_value_distribution<RealType, Policy>& dist) | |
235 { | |
236 BOOST_MATH_STD_USING // for ADL of std functions. | |
237 | |
238 RealType b = dist.scale(); | |
239 RealType result = 0; | |
240 if(0 == detail::verify_scale_b("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", b, &result, Policy())) | |
241 return result; | |
242 if(0 == detail::check_scale("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", dist.location(), &result, Policy())) | |
243 return result; | |
244 return constants::pi<RealType>() * b / sqrt(static_cast<RealType>(6)); | |
245 } | |
246 | |
247 template <class RealType, class Policy> | |
248 inline RealType mode(const extreme_value_distribution<RealType, Policy>& dist) | |
249 { | |
250 return dist.location(); | |
251 } | |
252 | |
253 template <class RealType, class Policy> | |
254 inline RealType median(const extreme_value_distribution<RealType, Policy>& dist) | |
255 { | |
256 using constants::ln_ln_two; | |
257 return dist.location() - dist.scale() * ln_ln_two<RealType>(); | |
258 } | |
259 | |
260 template <class RealType, class Policy> | |
261 inline RealType skewness(const extreme_value_distribution<RealType, Policy>& /*dist*/) | |
262 { | |
263 // | |
264 // This is 12 * sqrt(6) * zeta(3) / pi^3: | |
265 // See http://mathworld.wolfram.com/ExtremeValueDistribution.html | |
266 // | |
267 return static_cast<RealType>(1.1395470994046486574927930193898461120875997958366L); | |
268 } | |
269 | |
270 template <class RealType, class Policy> | |
271 inline RealType kurtosis(const extreme_value_distribution<RealType, Policy>& /*dist*/) | |
272 { | |
273 // See http://mathworld.wolfram.com/ExtremeValueDistribution.html | |
274 return RealType(27) / 5; | |
275 } | |
276 | |
277 template <class RealType, class Policy> | |
278 inline RealType kurtosis_excess(const extreme_value_distribution<RealType, Policy>& /*dist*/) | |
279 { | |
280 // See http://mathworld.wolfram.com/ExtremeValueDistribution.html | |
281 return RealType(12) / 5; | |
282 } | |
283 | |
284 | |
285 } // namespace math | |
286 } // namespace boost | |
287 | |
288 #ifdef BOOST_MSVC | |
289 # pragma warning(pop) | |
290 #endif | |
291 | |
292 // This include must be at the end, *after* the accessors | |
293 // for this distribution have been defined, in order to | |
294 // keep compilers that support two-phase lookup happy. | |
295 #include <boost/math/distributions/detail/derived_accessors.hpp> | |
296 | |
297 #endif // BOOST_STATS_EXTREME_VALUE_HPP |