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)
6 // distributions.hpp provides definitions of the concept of a distribution
7 // and non-member accessor functions that must be implemented by all distributions.
8 // This is used to verify that
9 // all the features of a distributions have been fully implemented.
11 #ifndef BOOST_MATH_DISTRIBUTION_CONCEPT_HPP
12 #define BOOST_MATH_DISTRIBUTION_CONCEPT_HPP
14 #include <boost/math/distributions/complement.hpp>
15 #include <boost/math/distributions/fwd.hpp>
18 #pragma warning(disable: 4100)
19 #pragma warning(disable: 4510)
20 #pragma warning(disable: 4610)
21 #pragma warning(disable: 4189) // local variable is initialized but not referenced.
23 #include <boost/concept_check.hpp>
34 // Begin by defining a concept archetype
35 // for a distribution class:
37 template <class RealType>
38 class distribution_archetype
41 typedef RealType value_type;
43 distribution_archetype(const distribution_archetype&); // Copy constructible.
44 distribution_archetype& operator=(const distribution_archetype&); // Assignable.
46 // There is no default constructor,
47 // but we need a way to instantiate the archetype:
48 static distribution_archetype& get_object()
50 // will never get caled:
51 return *reinterpret_cast<distribution_archetype*>(0);
53 }; // template <class RealType>class distribution_archetype
55 // Non-member accessor functions:
56 // (This list defines the functions that must be implemented by all distributions).
58 template <class RealType>
59 RealType pdf(const distribution_archetype<RealType>& dist, const RealType& x);
61 template <class RealType>
62 RealType cdf(const distribution_archetype<RealType>& dist, const RealType& x);
64 template <class RealType>
65 RealType quantile(const distribution_archetype<RealType>& dist, const RealType& p);
67 template <class RealType>
68 RealType cdf(const complemented2_type<distribution_archetype<RealType>, RealType>& c);
70 template <class RealType>
71 RealType quantile(const complemented2_type<distribution_archetype<RealType>, RealType>& c);
73 template <class RealType>
74 RealType mean(const distribution_archetype<RealType>& dist);
76 template <class RealType>
77 RealType standard_deviation(const distribution_archetype<RealType>& dist);
79 template <class RealType>
80 RealType variance(const distribution_archetype<RealType>& dist);
82 template <class RealType>
83 RealType hazard(const distribution_archetype<RealType>& dist);
85 template <class RealType>
86 RealType chf(const distribution_archetype<RealType>& dist);
87 // http://en.wikipedia.org/wiki/Characteristic_function_%28probability_theory%29
89 template <class RealType>
90 RealType coefficient_of_variation(const distribution_archetype<RealType>& dist);
92 template <class RealType>
93 RealType mode(const distribution_archetype<RealType>& dist);
95 template <class RealType>
96 RealType skewness(const distribution_archetype<RealType>& dist);
98 template <class RealType>
99 RealType kurtosis_excess(const distribution_archetype<RealType>& dist);
101 template <class RealType>
102 RealType kurtosis(const distribution_archetype<RealType>& dist);
104 template <class RealType>
105 RealType median(const distribution_archetype<RealType>& dist);
107 template <class RealType>
108 std::pair<RealType, RealType> range(const distribution_archetype<RealType>& dist);
110 template <class RealType>
111 std::pair<RealType, RealType> support(const distribution_archetype<RealType>& dist);
114 // Next comes the concept checks for verifying that a class
115 // fullfils the requirements of a Distribution:
117 template <class Distribution>
118 struct DistributionConcept
120 typedef typename Distribution::value_type value_type;
124 function_requires<CopyConstructibleConcept<Distribution> >();
125 function_requires<AssignableConcept<Distribution> >();
127 const Distribution& dist = DistributionConcept<Distribution>::get_object();
130 // The result values are ignored in all these checks.
131 value_type v = cdf(dist, x);
132 v = cdf(complement(dist, x));
133 suppress_unused_variable_warning(v);
135 suppress_unused_variable_warning(v);
136 v = quantile(dist, x);
137 suppress_unused_variable_warning(v);
138 v = quantile(complement(dist, x));
139 suppress_unused_variable_warning(v);
141 suppress_unused_variable_warning(v);
143 suppress_unused_variable_warning(v);
144 v = standard_deviation(dist);
145 suppress_unused_variable_warning(v);
147 suppress_unused_variable_warning(v);
149 suppress_unused_variable_warning(v);
151 suppress_unused_variable_warning(v);
152 v = coefficient_of_variation(dist);
153 suppress_unused_variable_warning(v);
155 suppress_unused_variable_warning(v);
157 suppress_unused_variable_warning(v);
158 v = kurtosis_excess(dist);
159 suppress_unused_variable_warning(v);
161 suppress_unused_variable_warning(v);
162 std::pair<value_type, value_type> pv;
164 suppress_unused_variable_warning(pv);
166 suppress_unused_variable_warning(pv);
170 suppress_unused_variable_warning(v);
171 v = cdf(complement(dist, f));
172 suppress_unused_variable_warning(v);
174 suppress_unused_variable_warning(v);
175 v = quantile(dist, f);
176 suppress_unused_variable_warning(v);
177 v = quantile(complement(dist, f));
178 suppress_unused_variable_warning(v);
180 suppress_unused_variable_warning(v);
182 suppress_unused_variable_warning(v);
185 suppress_unused_variable_warning(v);
186 v = cdf(complement(dist, d));
187 suppress_unused_variable_warning(v);
189 suppress_unused_variable_warning(v);
190 v = quantile(dist, d);
191 suppress_unused_variable_warning(v);
192 v = quantile(complement(dist, d));
193 suppress_unused_variable_warning(v);
195 suppress_unused_variable_warning(v);
197 suppress_unused_variable_warning(v);
201 suppress_unused_variable_warning(v);
202 v = cdf(complement(dist, ld));
203 suppress_unused_variable_warning(v);
205 suppress_unused_variable_warning(v);
206 v = quantile(dist, ld);
207 suppress_unused_variable_warning(v);
208 v = quantile(complement(dist, ld));
209 suppress_unused_variable_warning(v);
210 v = hazard(dist, ld);
211 suppress_unused_variable_warning(v);
213 suppress_unused_variable_warning(v);
217 suppress_unused_variable_warning(v);
218 v = cdf(complement(dist, i));
219 suppress_unused_variable_warning(v);
221 suppress_unused_variable_warning(v);
222 v = quantile(dist, i);
223 suppress_unused_variable_warning(v);
224 v = quantile(complement(dist, i));
225 suppress_unused_variable_warning(v);
227 suppress_unused_variable_warning(v);
229 suppress_unused_variable_warning(v);
230 unsigned long li = 1;
232 suppress_unused_variable_warning(v);
233 v = cdf(complement(dist, li));
234 suppress_unused_variable_warning(v);
236 suppress_unused_variable_warning(v);
237 v = quantile(dist, li);
238 suppress_unused_variable_warning(v);
239 v = quantile(complement(dist, li));
240 suppress_unused_variable_warning(v);
241 v = hazard(dist, li);
242 suppress_unused_variable_warning(v);
244 suppress_unused_variable_warning(v);
245 test_extra_members(dist);
248 static void test_extra_members(const D&)
250 template <class R, class P>
251 static void test_extra_members(const boost::math::bernoulli_distribution<R, P>& d)
253 value_type r = d.success_fraction();
254 (void)r; // warning suppression
256 template <class R, class P>
257 static void test_extra_members(const boost::math::beta_distribution<R, P>& d)
259 value_type r1 = d.alpha();
260 value_type r2 = d.beta();
261 r1 = boost::math::beta_distribution<R, P>::find_alpha(r1, r2);
262 suppress_unused_variable_warning(r1);
263 r1 = boost::math::beta_distribution<R, P>::find_beta(r1, r2);
264 suppress_unused_variable_warning(r1);
265 r1 = boost::math::beta_distribution<R, P>::find_alpha(r1, r2, r1);
266 suppress_unused_variable_warning(r1);
267 r1 = boost::math::beta_distribution<R, P>::find_beta(r1, r2, r1);
268 suppress_unused_variable_warning(r1);
270 template <class R, class P>
271 static void test_extra_members(const boost::math::binomial_distribution<R, P>& d)
273 value_type r = d.success_fraction();
275 r = Distribution::find_lower_bound_on_p(r, r, r);
276 r = Distribution::find_lower_bound_on_p(r, r, r, Distribution::clopper_pearson_exact_interval);
277 r = Distribution::find_lower_bound_on_p(r, r, r, Distribution::jeffreys_prior_interval);
278 r = Distribution::find_upper_bound_on_p(r, r, r);
279 r = Distribution::find_upper_bound_on_p(r, r, r, Distribution::clopper_pearson_exact_interval);
280 r = Distribution::find_upper_bound_on_p(r, r, r, Distribution::jeffreys_prior_interval);
281 r = Distribution::find_minimum_number_of_trials(r, r, r);
282 r = Distribution::find_maximum_number_of_trials(r, r, r);
283 suppress_unused_variable_warning(r);
285 template <class R, class P>
286 static void test_extra_members(const boost::math::cauchy_distribution<R, P>& d)
288 value_type r = d.location();
290 suppress_unused_variable_warning(r);
292 template <class R, class P>
293 static void test_extra_members(const boost::math::chi_squared_distribution<R, P>& d)
295 value_type r = d.degrees_of_freedom();
296 r = Distribution::find_degrees_of_freedom(r, r, r, r);
297 r = Distribution::find_degrees_of_freedom(r, r, r, r, r);
298 suppress_unused_variable_warning(r);
300 template <class R, class P>
301 static void test_extra_members(const boost::math::exponential_distribution<R, P>& d)
303 value_type r = d.lambda();
304 suppress_unused_variable_warning(r);
306 template <class R, class P>
307 static void test_extra_members(const boost::math::extreme_value_distribution<R, P>& d)
309 value_type r = d.scale();
311 suppress_unused_variable_warning(r);
313 template <class R, class P>
314 static void test_extra_members(const boost::math::fisher_f_distribution<R, P>& d)
316 value_type r = d.degrees_of_freedom1();
317 r = d.degrees_of_freedom2();
318 suppress_unused_variable_warning(r);
320 template <class R, class P>
321 static void test_extra_members(const boost::math::gamma_distribution<R, P>& d)
323 value_type r = d.scale();
325 suppress_unused_variable_warning(r);
327 template <class R, class P>
328 static void test_extra_members(const boost::math::inverse_chi_squared_distribution<R, P>& d)
330 value_type r = d.scale();
331 r = d.degrees_of_freedom();
332 suppress_unused_variable_warning(r);
334 template <class R, class P>
335 static void test_extra_members(const boost::math::inverse_gamma_distribution<R, P>& d)
337 value_type r = d.scale();
339 suppress_unused_variable_warning(r);
341 template <class R, class P>
342 static void test_extra_members(const boost::math::hypergeometric_distribution<R, P>& d)
344 unsigned u = d.defective();
345 u = d.sample_count();
347 suppress_unused_variable_warning(u);
349 template <class R, class P>
350 static void test_extra_members(const boost::math::laplace_distribution<R, P>& d)
352 value_type r = d.scale();
354 suppress_unused_variable_warning(r);
356 template <class R, class P>
357 static void test_extra_members(const boost::math::logistic_distribution<R, P>& d)
359 value_type r = d.scale();
361 suppress_unused_variable_warning(r);
363 template <class R, class P>
364 static void test_extra_members(const boost::math::lognormal_distribution<R, P>& d)
366 value_type r = d.scale();
368 suppress_unused_variable_warning(r);
370 template <class R, class P>
371 static void test_extra_members(const boost::math::negative_binomial_distribution<R, P>& d)
373 value_type r = d.success_fraction();
375 r = Distribution::find_lower_bound_on_p(r, r, r);
376 r = Distribution::find_upper_bound_on_p(r, r, r);
377 r = Distribution::find_minimum_number_of_trials(r, r, r);
378 r = Distribution::find_maximum_number_of_trials(r, r, r);
379 suppress_unused_variable_warning(r);
381 template <class R, class P>
382 static void test_extra_members(const boost::math::non_central_beta_distribution<R, P>& d)
384 value_type r1 = d.alpha();
385 value_type r2 = d.beta();
386 r1 = d.non_centrality();
387 (void)r1; // warning suppression
388 (void)r2; // warning suppression
390 template <class R, class P>
391 static void test_extra_members(const boost::math::non_central_chi_squared_distribution<R, P>& d)
393 value_type r = d.degrees_of_freedom();
394 r = d.non_centrality();
395 r = Distribution::find_degrees_of_freedom(r, r, r);
396 r = Distribution::find_degrees_of_freedom(boost::math::complement(r, r, r));
397 r = Distribution::find_non_centrality(r, r, r);
398 r = Distribution::find_non_centrality(boost::math::complement(r, r, r));
399 (void)r; // warning suppression
401 template <class R, class P>
402 static void test_extra_members(const boost::math::non_central_f_distribution<R, P>& d)
404 value_type r = d.degrees_of_freedom1();
405 r = d.degrees_of_freedom2();
406 r = d.non_centrality();
407 (void)r; // warning suppression
409 template <class R, class P>
410 static void test_extra_members(const boost::math::non_central_t_distribution<R, P>& d)
412 value_type r = d.degrees_of_freedom();
413 r = d.non_centrality();
414 (void)r; // warning suppression
416 template <class R, class P>
417 static void test_extra_members(const boost::math::normal_distribution<R, P>& d)
419 value_type r = d.scale();
422 r = d.standard_deviation();
423 (void)r; // warning suppression
425 template <class R, class P>
426 static void test_extra_members(const boost::math::pareto_distribution<R, P>& d)
428 value_type r = d.scale();
430 (void)r; // warning suppression
432 template <class R, class P>
433 static void test_extra_members(const boost::math::poisson_distribution<R, P>& d)
435 value_type r = d.mean();
436 (void)r; // warning suppression
438 template <class R, class P>
439 static void test_extra_members(const boost::math::rayleigh_distribution<R, P>& d)
441 value_type r = d.sigma();
442 (void)r; // warning suppression
444 template <class R, class P>
445 static void test_extra_members(const boost::math::students_t_distribution<R, P>& d)
447 value_type r = d.degrees_of_freedom();
448 r = d.find_degrees_of_freedom(r, r, r, r);
449 r = d.find_degrees_of_freedom(r, r, r, r, r);
450 (void)r; // warning suppression
452 template <class R, class P>
453 static void test_extra_members(const boost::math::triangular_distribution<R, P>& d)
455 value_type r = d.lower();
458 (void)r; // warning suppression
460 template <class R, class P>
461 static void test_extra_members(const boost::math::weibull_distribution<R, P>& d)
463 value_type r = d.scale();
465 (void)r; // warning suppression
467 template <class R, class P>
468 static void test_extra_members(const boost::math::uniform_distribution<R, P>& d)
470 value_type r = d.lower();
472 (void)r; // warning suppression
475 static Distribution* pd;
476 static Distribution& get_object()
478 // In reality this will never get called:
481 }; // struct DistributionConcept
483 template <class Distribution>
484 Distribution* DistributionConcept<Distribution>::pd = 0;
486 } // namespace concepts
490 #endif // BOOST_MATH_DISTRIBUTION_CONCEPT_HPP