Revision 161:4797bbf470e7

View differences:

any/include/boost/math/distributions.hpp
1
//  Copyright John Maddock 2006, 2007.
2
//  Copyright Paul A. Bristow 2006, 2007, 2009, 2010.
3

  
4
//  Use, modification and distribution are subject to the
5
//  Boost Software License, Version 1.0. (See accompanying file
6
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7

  
8
// This file includes *all* the distributions.
9
// this *may* be convenient if many are used
10
// - to avoid including each distribution individually.
11

  
12
#ifndef BOOST_MATH_DISTRIBUTIONS_HPP
13
#define BOOST_MATH_DISTRIBUTIONS_HPP
14

  
15
#include <boost/math/distributions/arcsine.hpp>
16
#include <boost/math/distributions/bernoulli.hpp>
17
#include <boost/math/distributions/beta.hpp>
18
#include <boost/math/distributions/binomial.hpp>
19
#include <boost/math/distributions/cauchy.hpp>
20
#include <boost/math/distributions/chi_squared.hpp>
21
#include <boost/math/distributions/complement.hpp>
22
#include <boost/math/distributions/exponential.hpp>
23
#include <boost/math/distributions/extreme_value.hpp>
24
#include <boost/math/distributions/fisher_f.hpp>
25
#include <boost/math/distributions/gamma.hpp>
26
#include <boost/math/distributions/geometric.hpp>
27
#include <boost/math/distributions/hyperexponential.hpp>
28
#include <boost/math/distributions/hypergeometric.hpp>
29
#include <boost/math/distributions/inverse_chi_squared.hpp>
30
#include <boost/math/distributions/inverse_gamma.hpp>
31
#include <boost/math/distributions/inverse_gaussian.hpp>
32
#include <boost/math/distributions/laplace.hpp>
33
#include <boost/math/distributions/logistic.hpp>
34
#include <boost/math/distributions/lognormal.hpp>
35
#include <boost/math/distributions/negative_binomial.hpp>
36
#include <boost/math/distributions/non_central_chi_squared.hpp>
37
#include <boost/math/distributions/non_central_beta.hpp>
38
#include <boost/math/distributions/non_central_f.hpp>
39
#include <boost/math/distributions/non_central_t.hpp>
40
#include <boost/math/distributions/normal.hpp>
41
#include <boost/math/distributions/pareto.hpp>
42
#include <boost/math/distributions/poisson.hpp>
43
#include <boost/math/distributions/rayleigh.hpp>
44
#include <boost/math/distributions/skew_normal.hpp>
45
#include <boost/math/distributions/students_t.hpp>
46
#include <boost/math/distributions/triangular.hpp>
47
#include <boost/math/distributions/uniform.hpp>
48
#include <boost/math/distributions/weibull.hpp>
49
#include <boost/math/distributions/find_scale.hpp>
50
#include <boost/math/distributions/find_location.hpp>
51

  
52
#endif // BOOST_MATH_DISTRIBUTIONS_HPP
53

  
any/include/boost/math/distributions/arcsine.hpp
1
// boost/math/distributions/arcsine.hpp
2

  
3
// Copyright John Maddock 2014.
4
// Copyright Paul A. Bristow 2014.
5

  
6
// Use, modification and distribution are subject to the
7
// Boost Software License, Version 1.0.
8
// (See accompanying file LICENSE_1_0.txt
9
// or copy at http://www.boost.org/LICENSE_1_0.txt)
10

  
11
// http://en.wikipedia.org/wiki/arcsine_distribution
12

  
13
// The arcsine Distribution is a continuous probability distribution.
14
// http://en.wikipedia.org/wiki/Arcsine_distribution
15
// http://www.wolframalpha.com/input/?i=ArcSinDistribution
16

  
17
// Standard arcsine distribution is a special case of beta distribution with both a & b = one half,
18
// and 0 <= x <= 1.
19

  
20
// It is generalized to include any bounded support a <= x <= b from 0 <= x <= 1
21
// by Wolfram and Wikipedia,
22
// but using location and scale parameters by
23
// Virtual Laboratories in Probability and Statistics http://www.math.uah.edu/stat/index.html
24
// http://www.math.uah.edu/stat/special/Arcsine.html
25
// The end-point version is simpler and more obvious, so we implement that.
26
// TODO Perhaps provide location and scale functions?
27

  
28

  
29
#ifndef BOOST_MATH_DIST_ARCSINE_HPP
30
#define BOOST_MATH_DIST_ARCSINE_HPP
31

  
32
#include <boost/math/distributions/fwd.hpp>
33
#include <boost/math/distributions/complement.hpp> // complements.
34
#include <boost/math/distributions/detail/common_error_handling.hpp> // error checks.
35
#include <boost/math/constants/constants.hpp>
36

  
37
#include <boost/math/special_functions/fpclassify.hpp> // isnan.
38

  
39
#if defined (BOOST_MSVC)
40
#  pragma warning(push)
41
#  pragma warning(disable: 4702) // Unreachable code,
42
// in domain_error_imp in error_handling.
43
#endif
44

  
45
#include <utility>
46
#include <exception>  // For std::domain_error.
47

  
48
namespace boost
49
{
50
  namespace math
51
  {
52
    namespace arcsine_detail
53
    {
54
      // Common error checking routines for arcsine distribution functions:
55
      // Duplicating for x_min and x_max provides specific error messages.
56
      template <class RealType, class Policy>
57
      inline bool check_x_min(const char* function, const RealType& x, RealType* result, const Policy& pol)
58
      {
59
        if (!(boost::math::isfinite)(x))
60
        {
61
          *result = policies::raise_domain_error<RealType>(
62
            function,
63
            "x_min argument is %1%, but must be finite !", x, pol);
64
          return false;
65
        }
66
        return true;
67
      } // bool check_x_min
68

  
69
      template <class RealType, class Policy>
70
      inline bool check_x_max(const char* function, const RealType& x, RealType* result, const Policy& pol)
71
      {
72
        if (!(boost::math::isfinite)(x))
73
        {
74
          *result = policies::raise_domain_error<RealType>(
75
            function,
76
            "x_max argument is %1%, but must be finite !", x, pol);
77
          return false;
78
        }
79
        return true;
80
      } // bool check_x_max
81

  
82

  
83
      template <class RealType, class Policy>
84
      inline bool check_x_minmax(const char* function, const RealType& x_min, const RealType& x_max, RealType* result, const Policy& pol)
85
      { // Check x_min < x_max
86
        if (x_min >= x_max)
87
        {
88
          std::string msg = "x_max argument is %1%, but must be > x_min = " + lexical_cast<std::string>(x_min) + "!";
89
          *result = policies::raise_domain_error<RealType>(
90
            function,
91
            msg.c_str(), x_max, pol);
92
           // "x_max argument is %1%, but must be > x_min !", x_max, pol);
93
            //  "x_max argument is %1%, but must be > x_min %2!", x_max, x_min, pol); would be better. 
94
          // But would require replication of all helpers functions in /policies/error_handling.hpp for two values,
95
          // as well as two value versions of raise_error, raise_domain_error and do_format ...
96
          // so use slightly hacky lexical_cast to string instead.
97
          return false;
98
        }
99
        return true;
100
      } // bool check_x_minmax
101

  
102
      template <class RealType, class Policy>
103
      inline bool check_prob(const char* function, const RealType& p, RealType* result, const Policy& pol)
104
      {
105
        if ((p < 0) || (p > 1) || !(boost::math::isfinite)(p))
106
        {
107
          *result = policies::raise_domain_error<RealType>(
108
            function,
109
            "Probability argument is %1%, but must be >= 0 and <= 1 !", p, pol);
110
          return false;
111
        }
112
        return true;
113
      } // bool check_prob
114

  
115
      template <class RealType, class Policy>
116
      inline bool check_x(const char* function, const RealType& x_min, const RealType& x_max, const RealType& x, RealType* result, const Policy& pol)
117
      { // Check x finite and x_min < x < x_max.
118
        if (!(boost::math::isfinite)(x))
119
        {
120
          *result = policies::raise_domain_error<RealType>(
121
            function,
122
            "x argument is %1%, but must be finite !", x, pol);
123
          return false;
124
        }
125
        if ((x < x_min) || (x > x_max))
126
        {
127
          // std::cout << x_min << ' ' << x << x_max << std::endl;
128
          *result = policies::raise_domain_error<RealType>(
129
            function,
130
            "x argument is %1%, but must be x_min < x < x_max !", x, pol);
131
          // For example:
132
          // Error in function boost::math::pdf(arcsine_distribution<double> const&, double) : x argument is -1.01, but must be x_min < x < x_max !
133
          // TODO Perhaps show values of x_min and x_max?
134
          return false;
135
        }
136
        return true;
137
      } // bool check_x
138

  
139
      template <class RealType, class Policy>
140
      inline bool check_dist(const char* function, const RealType& x_min, const RealType& x_max, RealType* result, const Policy& pol)
141
      { // Check both x_min and x_max finite, and x_min  < x_max.
142
        return check_x_min(function, x_min, result, pol)
143
            && check_x_max(function, x_max, result, pol)
144
            && check_x_minmax(function, x_min, x_max, result, pol);
145
      } // bool check_dist
146

  
147
      template <class RealType, class Policy>
148
      inline bool check_dist_and_x(const char* function, const RealType& x_min, const RealType& x_max, RealType x, RealType* result, const Policy& pol)
149
      {
150
        return check_dist(function, x_min, x_max, result, pol)
151
          && arcsine_detail::check_x(function, x_min, x_max, x, result, pol);
152
      } // bool check_dist_and_x
153

  
154
      template <class RealType, class Policy>
155
      inline bool check_dist_and_prob(const char* function, const RealType& x_min, const RealType& x_max, RealType p, RealType* result, const Policy& pol)
156
      {
157
        return check_dist(function, x_min, x_max, result, pol)
158
          && check_prob(function, p, result, pol);
159
      } // bool check_dist_and_prob
160

  
161
    } // namespace arcsine_detail
162

  
163
    template <class RealType = double, class Policy = policies::policy<> >
164
    class arcsine_distribution
165
    {
166
    public:
167
      typedef RealType value_type;
168
      typedef Policy policy_type;
169

  
170
      arcsine_distribution(RealType x_min = 0, RealType x_max = 1) : m_x_min(x_min), m_x_max(x_max)
171
      { // Default beta (alpha = beta = 0.5) is standard arcsine with x_min = 0, x_max = 1.
172
        // Generalized to allow x_min and x_max to be specified.
173
        RealType result;
174
        arcsine_detail::check_dist(
175
          "boost::math::arcsine_distribution<%1%>::arcsine_distribution",
176
          m_x_min,
177
          m_x_max,
178
          &result, Policy());
179
      } // arcsine_distribution constructor.
180
      // Accessor functions:
181
      RealType x_min() const
182
      {
183
        return m_x_min;
184
      }
185
      RealType x_max() const
186
      {
187
        return m_x_max;
188
      }
189

  
190
    private:
191
      RealType m_x_min; // Two x min and x max parameters of the arcsine distribution.
192
      RealType m_x_max;
193
    }; // template <class RealType, class Policy> class arcsine_distribution
194

  
195
    // Convenient typedef to construct double version.
196
    typedef arcsine_distribution<double> arcsine;
197

  
198

  
199
    template <class RealType, class Policy>
200
    inline const std::pair<RealType, RealType> range(const arcsine_distribution<RealType, Policy>&  dist)
201
    { // Range of permissible values for random variable x.
202
      using boost::math::tools::max_value;
203
      return std::pair<RealType, RealType>(static_cast<RealType>(dist.x_min()), static_cast<RealType>(dist.x_max()));
204
    }
205

  
206
    template <class RealType, class Policy>
207
    inline const std::pair<RealType, RealType> support(const arcsine_distribution<RealType, Policy>&  dist)
208
    { // Range of supported values for random variable x.
209
      // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
210
      return std::pair<RealType, RealType>(static_cast<RealType>(dist.x_min()), static_cast<RealType>(dist.x_max()));
211
    }
212

  
213
    template <class RealType, class Policy>
214
    inline RealType mean(const arcsine_distribution<RealType, Policy>& dist)
215
    { // Mean of arcsine distribution .
216
      RealType result;
217
      RealType x_min = dist.x_min();
218
      RealType x_max = dist.x_max();
219

  
220
      if (false == arcsine_detail::check_dist(
221
        "boost::math::mean(arcsine_distribution<%1%> const&, %1% )",
222
        x_min,
223
        x_max,
224
        &result, Policy())
225
        )
226
      {
227
        return result;
228
      }
229
      return  (x_min + x_max) / 2;
230
    } // mean
231

  
232
    template <class RealType, class Policy>
233
    inline RealType variance(const arcsine_distribution<RealType, Policy>& dist)
234
    { // Variance of standard arcsine distribution = (1-0)/8 = 0.125.
235
      RealType result;
236
      RealType x_min = dist.x_min();
237
      RealType x_max = dist.x_max();
238
      if (false == arcsine_detail::check_dist(
239
        "boost::math::variance(arcsine_distribution<%1%> const&, %1% )",
240
        x_min,
241
        x_max,
242
        &result, Policy())
243
        )
244
      {
245
        return result;
246
      }
247
      return  (x_max - x_min) * (x_max - x_min) / 8;
248
    } // variance
249

  
250
    template <class RealType, class Policy>
251
    inline RealType mode(const arcsine_distribution<RealType, Policy>& /* dist */)
252
    { //There are always [*two] values for the mode, at ['x_min] and at ['x_max], default 0 and 1,
253
      // so instead we raise the exception domain_error.
254
      return policies::raise_domain_error<RealType>(
255
        "boost::math::mode(arcsine_distribution<%1%>&)",
256
        "The arcsine distribution has two modes at x_min and x_max: "
257
        "so the return value is %1%.",
258
        std::numeric_limits<RealType>::quiet_NaN(), Policy());
259
    } // mode
260

  
261
    template <class RealType, class Policy>
262
    inline RealType median(const arcsine_distribution<RealType, Policy>& dist)
263
    { // Median of arcsine distribution (a + b) / 2 == mean.
264
      RealType x_min = dist.x_min();
265
      RealType x_max = dist.x_max();
266
      RealType result;
267
      if (false == arcsine_detail::check_dist(
268
        "boost::math::median(arcsine_distribution<%1%> const&, %1% )",
269
        x_min,
270
        x_max,
271
        &result, Policy())
272
        )
273
      {
274
        return result;
275
      }
276
      return  (x_min + x_max) / 2;
277
    }
278

  
279
    template <class RealType, class Policy>
280
    inline RealType skewness(const arcsine_distribution<RealType, Policy>& dist)
281
    {
282
      RealType result;
283
      RealType x_min = dist.x_min();
284
      RealType x_max = dist.x_max();
285

  
286
      if (false == arcsine_detail::check_dist(
287
        "boost::math::skewness(arcsine_distribution<%1%> const&, %1% )",
288
        x_min,
289
        x_max,
290
        &result, Policy())
291
        )
292
      {
293
        return result;
294
      }
295
      return 0;
296
    } // skewness
297

  
298
    template <class RealType, class Policy>
299
    inline RealType kurtosis_excess(const arcsine_distribution<RealType, Policy>& dist)
300
    {
301
      RealType result;
302
      RealType x_min = dist.x_min();
303
      RealType x_max = dist.x_max();
304

  
305
      if (false == arcsine_detail::check_dist(
306
        "boost::math::kurtosis_excess(arcsine_distribution<%1%> const&, %1% )",
307
        x_min,
308
        x_max,
309
        &result, Policy())
310
        )
311
      {
312
        return result;
313
      }
314
      result = -3;
315
      return  result / 2;
316
    } // kurtosis_excess
317

  
318
    template <class RealType, class Policy>
319
    inline RealType kurtosis(const arcsine_distribution<RealType, Policy>& dist)
320
    {
321
      RealType result;
322
      RealType x_min = dist.x_min();
323
      RealType x_max = dist.x_max();
324

  
325
      if (false == arcsine_detail::check_dist(
326
        "boost::math::kurtosis(arcsine_distribution<%1%> const&, %1% )",
327
        x_min,
328
        x_max,
329
        &result, Policy())
330
        )
331
      {
332
        return result;
333
      }
334

  
335
      return 3 + kurtosis_excess(dist);
336
    } // kurtosis
337

  
338
    template <class RealType, class Policy>
339
    inline RealType pdf(const arcsine_distribution<RealType, Policy>& dist, const RealType& xx)
340
    { // Probability Density/Mass Function arcsine.
341
      BOOST_FPU_EXCEPTION_GUARD
342
      BOOST_MATH_STD_USING // For ADL of std functions.
343

  
344
      static const char* function = "boost::math::pdf(arcsine_distribution<%1%> const&, %1%)";
345

  
346
      RealType lo = dist.x_min();
347
      RealType hi = dist.x_max();
348
      RealType x = xx;
349

  
350
      // Argument checks:
351
      RealType result = 0; 
352
      if (false == arcsine_detail::check_dist_and_x(
353
        function,
354
        lo, hi, x,
355
        &result, Policy()))
356
      {
357
        return result;
358
      }
359
      using boost::math::constants::pi;
360
      result = static_cast<RealType>(1) / (pi<RealType>() * sqrt((x - lo) * (hi - x)));
361
      return result;
362
    } // pdf
363

  
364
    template <class RealType, class Policy>
365
    inline RealType cdf(const arcsine_distribution<RealType, Policy>& dist, const RealType& x)
366
    { // Cumulative Distribution Function arcsine.
367
      BOOST_MATH_STD_USING // For ADL of std functions.
368

  
369
      static const char* function = "boost::math::cdf(arcsine_distribution<%1%> const&, %1%)";
370

  
371
      RealType x_min = dist.x_min();
372
      RealType x_max = dist.x_max();
373

  
374
      // Argument checks:
375
      RealType result = 0;
376
      if (false == arcsine_detail::check_dist_and_x(
377
        function,
378
        x_min, x_max, x,
379
        &result, Policy()))
380
      {
381
        return result;
382
      }
383
      // Special cases:
384
      if (x == x_min)
385
      {
386
        return 0;
387
      }
388
      else if (x == x_max)
389
      {
390
        return 1;
391
      }
392
      using boost::math::constants::pi;
393
      result = static_cast<RealType>(2) * asin(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
394
      return result;
395
    } // arcsine cdf
396

  
397
    template <class RealType, class Policy>
398
    inline RealType cdf(const complemented2_type<arcsine_distribution<RealType, Policy>, RealType>& c)
399
    { // Complemented Cumulative Distribution Function arcsine.
400
      BOOST_MATH_STD_USING // For ADL of std functions.
401
      static const char* function = "boost::math::cdf(arcsine_distribution<%1%> const&, %1%)";
402

  
403
      RealType x = c.param;
404
      arcsine_distribution<RealType, Policy> const& dist = c.dist;
405
      RealType x_min = dist.x_min();
406
      RealType x_max = dist.x_max();
407

  
408
      // Argument checks:
409
      RealType result = 0;
410
      if (false == arcsine_detail::check_dist_and_x(
411
        function,
412
        x_min, x_max, x,
413
        &result, Policy()))
414
      {
415
        return result;
416
      }
417
      if (x == x_min)
418
      {
419
        return 0;
420
      }
421
      else if (x == x_max)
422
      {
423
        return 1;
424
      }
425
      using boost::math::constants::pi;
426
      // Naive version x = 1 - x;
427
      // result = static_cast<RealType>(2) * asin(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
428
      // is less accurate, so use acos instead of asin for complement.
429
      result = static_cast<RealType>(2) * acos(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
430
      return result;
431
    } // arcine ccdf
432

  
433
    template <class RealType, class Policy>
434
    inline RealType quantile(const arcsine_distribution<RealType, Policy>& dist, const RealType& p)
435
    { 
436
      // Quantile or Percent Point arcsine function or
437
      // Inverse Cumulative probability distribution function CDF.
438
      // Return x (0 <= x <= 1),
439
      // for a given probability p (0 <= p <= 1).
440
      // These functions take a probability as an argument
441
      // and return a value such that the probability that a random variable x
442
      // will be less than or equal to that value
443
      // is whatever probability you supplied as an argument.
444
      BOOST_MATH_STD_USING // For ADL of std functions.
445

  
446
      using boost::math::constants::half_pi;
447

  
448
      static const char* function = "boost::math::quantile(arcsine_distribution<%1%> const&, %1%)";
449

  
450
      RealType result = 0; // of argument checks:
451
      RealType x_min = dist.x_min();
452
      RealType x_max = dist.x_max();
453
      if (false == arcsine_detail::check_dist_and_prob(
454
        function,
455
        x_min, x_max, p,
456
        &result, Policy()))
457
      {
458
        return result;
459
      }
460
      // Special cases:
461
      if (p == 0)
462
      {
463
        return 0;
464
      }
465
      if (p == 1)
466
      {
467
        return 1;
468
      }
469

  
470
      RealType sin2hpip = sin(half_pi<RealType>() * p);
471
      RealType sin2hpip2 = sin2hpip * sin2hpip;
472
      result = -x_min * sin2hpip2 + x_min + x_max * sin2hpip2;
473

  
474
      return result;
475
    } // quantile
476

  
477
    template <class RealType, class Policy>
478
    inline RealType quantile(const complemented2_type<arcsine_distribution<RealType, Policy>, RealType>& c)
479
    { 
480
      // Complement Quantile or Percent Point arcsine function.
481
      // Return the number of expected x for a given
482
      // complement of the probability q.
483
      BOOST_MATH_STD_USING // For ADL of std functions.
484

  
485
      using boost::math::constants::half_pi;
486
      static const char* function = "boost::math::quantile(arcsine_distribution<%1%> const&, %1%)";
487

  
488
      // Error checks:
489
      RealType q = c.param;
490
      const arcsine_distribution<RealType, Policy>& dist = c.dist;
491
      RealType result = 0;
492
      RealType x_min = dist.x_min();
493
      RealType x_max = dist.x_max();
494
      if (false == arcsine_detail::check_dist_and_prob(
495
        function,
496
        x_min,
497
        x_max,
498
        q,
499
        &result, Policy()))
500
      {
501
        return result;
502
      }
503
      // Special cases:
504
      if (q == 1)
505
      {
506
        return 0;
507
      }
508
      if (q == 0)
509
      {
510
        return 1;
511
      }
512
      // Naive RealType p = 1 - q; result = sin(half_pi<RealType>() * p); loses accuracy, so use a cos alternative instead.
513
      //result = cos(half_pi<RealType>() * q); // for arcsine(0,1)
514
      //result = result * result;
515
      // For generalized arcsine:
516
      RealType cos2hpip = cos(half_pi<RealType>() * q);
517
      RealType cos2hpip2 = cos2hpip * cos2hpip;
518
      result = -x_min * cos2hpip2 + x_min + x_max * cos2hpip2;
519

  
520
      return result;
521
    } // Quantile Complement
522

  
523
  } // namespace math
524
} // namespace boost
525

  
526
// This include must be at the end, *after* the accessors
527
// for this distribution have been defined, in order to
528
// keep compilers that support two-phase lookup happy.
529
#include <boost/math/distributions/detail/derived_accessors.hpp>
530

  
531
#if defined (BOOST_MSVC)
532
# pragma warning(pop)
533
#endif
534

  
535
#endif // BOOST_MATH_DIST_ARCSINE_HPP
any/include/boost/math/distributions/bernoulli.hpp
1
// boost\math\distributions\bernoulli.hpp
2

  
3
// Copyright John Maddock 2006.
4
// Copyright Paul A. Bristow 2007.
5

  
6
// Use, modification and distribution are subject to the
7
// Boost Software License, Version 1.0.
8
// (See accompanying file LICENSE_1_0.txt
9
// or copy at http://www.boost.org/LICENSE_1_0.txt)
10

  
11
// http://en.wikipedia.org/wiki/bernoulli_distribution
12
// http://mathworld.wolfram.com/BernoulliDistribution.html
13

  
14
// bernoulli distribution is the discrete probability distribution of
15
// the number (k) of successes, in a single Bernoulli trials.
16
// It is a version of the binomial distribution when n = 1.
17

  
18
// But note that the bernoulli distribution
19
// (like others including the poisson, binomial & negative binomial)
20
// is strictly defined as a discrete function: only integral values of k are envisaged.
21
// However because of the method of calculation using a continuous gamma function,
22
// it is convenient to treat it as if a continous function,
23
// and permit non-integral values of k.
24
// To enforce the strict mathematical model, users should use floor or ceil functions
25
// on k outside this function to ensure that k is integral.
26

  
27
#ifndef BOOST_MATH_SPECIAL_BERNOULLI_HPP
28
#define BOOST_MATH_SPECIAL_BERNOULLI_HPP
29

  
30
#include <boost/math/distributions/fwd.hpp>
31
#include <boost/math/tools/config.hpp>
32
#include <boost/math/distributions/complement.hpp> // complements
33
#include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
34
#include <boost/math/special_functions/fpclassify.hpp> // isnan.
35

  
36
#include <utility>
37

  
38
namespace boost
39
{
40
  namespace math
41
  {
42
    namespace bernoulli_detail
43
    {
44
      // Common error checking routines for bernoulli distribution functions:
45
      template <class RealType, class Policy>
46
      inline bool check_success_fraction(const char* function, const RealType& p, RealType* result, const Policy& /* pol */)
47
      {
48
        if(!(boost::math::isfinite)(p) || (p < 0) || (p > 1))
49
        {
50
          *result = policies::raise_domain_error<RealType>(
51
            function,
52
            "Success fraction argument is %1%, but must be >= 0 and <= 1 !", p, Policy());
53
          return false;
54
        }
55
        return true;
56
      }
57
      template <class RealType, class Policy>
58
      inline bool check_dist(const char* function, const RealType& p, RealType* result, const Policy& /* pol */, const mpl::true_&)
59
      {
60
        return check_success_fraction(function, p, result, Policy());
61
      }
62
      template <class RealType, class Policy>
63
      inline bool check_dist(const char* , const RealType& , RealType* , const Policy& /* pol */, const mpl::false_&)
64
      {
65
         return true;
66
      }
67
      template <class RealType, class Policy>
68
      inline bool check_dist(const char* function, const RealType& p, RealType* result, const Policy& /* pol */)
69
      {
70
         return check_dist(function, p, result, Policy(), typename policies::constructor_error_check<Policy>::type());
71
      }
72

  
73
      template <class RealType, class Policy>
74
      inline bool check_dist_and_k(const char* function, const RealType& p, RealType k, RealType* result, const Policy& pol)
75
      {
76
        if(check_dist(function, p, result, Policy(), typename policies::method_error_check<Policy>::type()) == false)
77
        {
78
          return false;
79
        }
80
        if(!(boost::math::isfinite)(k) || !((k == 0) || (k == 1)))
81
        {
82
          *result = policies::raise_domain_error<RealType>(
83
            function,
84
            "Number of successes argument is %1%, but must be 0 or 1 !", k, pol);
85
          return false;
86
        }
87
       return true;
88
      }
89
      template <class RealType, class Policy>
90
      inline bool check_dist_and_prob(const char* function, RealType p, RealType prob, RealType* result, const Policy& /* pol */)
91
      {
92
        if((check_dist(function, p, result, Policy(), typename policies::method_error_check<Policy>::type()) && detail::check_probability(function, prob, result, Policy())) == false)
93
        {
94
          return false;
95
        }
96
        return true;
97
      }
98
    } // namespace bernoulli_detail
99

  
100

  
101
    template <class RealType = double, class Policy = policies::policy<> >
102
    class bernoulli_distribution
103
    {
104
    public:
105
      typedef RealType value_type;
106
      typedef Policy policy_type;
107

  
108
      bernoulli_distribution(RealType p = 0.5) : m_p(p)
109
      { // Default probability = half suits 'fair' coin tossing
110
        // where probability of heads == probability of tails.
111
        RealType result; // of checks.
112
        bernoulli_detail::check_dist(
113
           "boost::math::bernoulli_distribution<%1%>::bernoulli_distribution",
114
          m_p,
115
          &result, Policy());
116
      } // bernoulli_distribution constructor.
117

  
118
      RealType success_fraction() const
119
      { // Probability.
120
        return m_p;
121
      }
122

  
123
    private:
124
      RealType m_p; // success_fraction
125
    }; // template <class RealType> class bernoulli_distribution
126

  
127
    typedef bernoulli_distribution<double> bernoulli;
128

  
129
    template <class RealType, class Policy>
130
    inline const std::pair<RealType, RealType> range(const bernoulli_distribution<RealType, Policy>& /* dist */)
131
    { // Range of permissible values for random variable k = {0, 1}.
132
      using boost::math::tools::max_value;
133
      return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
134
    }
135

  
136
    template <class RealType, class Policy>
137
    inline const std::pair<RealType, RealType> support(const bernoulli_distribution<RealType, Policy>& /* dist */)
138
    { // Range of supported values for random variable k = {0, 1}.
139
      // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
140
      return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
141
    }
142

  
143
    template <class RealType, class Policy>
144
    inline RealType mean(const bernoulli_distribution<RealType, Policy>& dist)
145
    { // Mean of bernoulli distribution = p (n = 1).
146
      return dist.success_fraction();
147
    } // mean
148

  
149
    // Rely on dereived_accessors quantile(half)
150
    //template <class RealType>
151
    //inline RealType median(const bernoulli_distribution<RealType, Policy>& dist)
152
    //{ // Median of bernoulli distribution is not defined.
153
    //  return tools::domain_error<RealType>(BOOST_CURRENT_FUNCTION, "Median is not implemented, result is %1%!", std::numeric_limits<RealType>::quiet_NaN());
154
    //} // median
155

  
156
    template <class RealType, class Policy>
157
    inline RealType variance(const bernoulli_distribution<RealType, Policy>& dist)
158
    { // Variance of bernoulli distribution =p * q.
159
      return  dist.success_fraction() * (1 - dist.success_fraction());
160
    } // variance
161

  
162
    template <class RealType, class Policy>
163
    RealType pdf(const bernoulli_distribution<RealType, Policy>& dist, const RealType& k)
164
    { // Probability Density/Mass Function.
165
      BOOST_FPU_EXCEPTION_GUARD
166
      // Error check:
167
      RealType result = 0; // of checks.
168
      if(false == bernoulli_detail::check_dist_and_k(
169
        "boost::math::pdf(bernoulli_distribution<%1%>, %1%)",
170
        dist.success_fraction(), // 0 to 1
171
        k, // 0 or 1
172
        &result, Policy()))
173
      {
174
        return result;
175
      }
176
      // Assume k is integral.
177
      if (k == 0)
178
      {
179
        return 1 - dist.success_fraction(); // 1 - p
180
      }
181
      else  // k == 1
182
      {
183
        return dist.success_fraction(); // p
184
      }
185
    } // pdf
186

  
187
    template <class RealType, class Policy>
188
    inline RealType cdf(const bernoulli_distribution<RealType, Policy>& dist, const RealType& k)
189
    { // Cumulative Distribution Function Bernoulli.
190
      RealType p = dist.success_fraction();
191
      // Error check:
192
      RealType result = 0;
193
      if(false == bernoulli_detail::check_dist_and_k(
194
        "boost::math::cdf(bernoulli_distribution<%1%>, %1%)",
195
        p,
196
        k,
197
        &result, Policy()))
198
      {
199
        return result;
200
      }
201
      if (k == 0)
202
      {
203
        return 1 - p;
204
      }
205
      else
206
      { // k == 1
207
        return 1;
208
      }
209
    } // bernoulli cdf
210

  
211
    template <class RealType, class Policy>
212
    inline RealType cdf(const complemented2_type<bernoulli_distribution<RealType, Policy>, RealType>& c)
213
    { // Complemented Cumulative Distribution Function bernoulli.
214
      RealType const& k = c.param;
215
      bernoulli_distribution<RealType, Policy> const& dist = c.dist;
216
      RealType p = dist.success_fraction();
217
      // Error checks:
218
      RealType result = 0;
219
      if(false == bernoulli_detail::check_dist_and_k(
220
        "boost::math::cdf(bernoulli_distribution<%1%>, %1%)",
221
        p,
222
        k,
223
        &result, Policy()))
224
      {
225
        return result;
226
      }
227
      if (k == 0)
228
      {
229
        return p;
230
      }
231
      else
232
      { // k == 1
233
        return 0;
234
      }
235
    } // bernoulli cdf complement
236

  
237
    template <class RealType, class Policy>
238
    inline RealType quantile(const bernoulli_distribution<RealType, Policy>& dist, const RealType& p)
239
    { // Quantile or Percent Point Bernoulli function.
240
      // Return the number of expected successes k either 0 or 1.
241
      // for a given probability p.
242

  
243
      RealType result = 0; // of error checks:
244
      if(false == bernoulli_detail::check_dist_and_prob(
245
        "boost::math::quantile(bernoulli_distribution<%1%>, %1%)",
246
        dist.success_fraction(),
247
        p,
248
        &result, Policy()))
249
      {
250
        return result;
251
      }
252
      if (p <= (1 - dist.success_fraction()))
253
      { // p <= pdf(dist, 0) == cdf(dist, 0)
254
        return 0;
255
      }
256
      else
257
      {
258
        return 1;
259
      }
260
    } // quantile
261

  
262
    template <class RealType, class Policy>
263
    inline RealType quantile(const complemented2_type<bernoulli_distribution<RealType, Policy>, RealType>& c)
264
    { // Quantile or Percent Point bernoulli function.
265
      // Return the number of expected successes k for a given
266
      // complement of the probability q.
267
      //
268
      // Error checks:
269
      RealType q = c.param;
270
      const bernoulli_distribution<RealType, Policy>& dist = c.dist;
271
      RealType result = 0;
272
      if(false == bernoulli_detail::check_dist_and_prob(
273
        "boost::math::quantile(bernoulli_distribution<%1%>, %1%)",
274
        dist.success_fraction(),
275
        q,
276
        &result, Policy()))
277
      {
278
        return result;
279
      }
280

  
281
      if (q <= 1 - dist.success_fraction())
282
      { // // q <= cdf(complement(dist, 0)) == pdf(dist, 0)
283
        return 1;
284
      }
285
      else
286
      {
287
        return 0;
288
      }
289
    } // quantile complemented.
290

  
291
    template <class RealType, class Policy>
292
    inline RealType mode(const bernoulli_distribution<RealType, Policy>& dist)
293
    {
294
      return static_cast<RealType>((dist.success_fraction() <= 0.5) ? 0 : 1); // p = 0.5 can be 0 or 1
295
    }
296

  
297
    template <class RealType, class Policy>
298
    inline RealType skewness(const bernoulli_distribution<RealType, Policy>& dist)
299
    {
300
      BOOST_MATH_STD_USING; // Aid ADL for sqrt.
301
      RealType p = dist.success_fraction();
302
      return (1 - 2 * p) / sqrt(p * (1 - p));
303
    }
304

  
305
    template <class RealType, class Policy>
306
    inline RealType kurtosis_excess(const bernoulli_distribution<RealType, Policy>& dist)
307
    {
308
      RealType p = dist.success_fraction();
309
      // Note Wolfram says this is kurtosis in text, but gamma2 is the kurtosis excess,
310
      // and Wikipedia also says this is the kurtosis excess formula.
311
      // return (6 * p * p - 6 * p + 1) / (p * (1 - p));
312
      // But Wolfram kurtosis article gives this simpler formula for kurtosis excess:
313
      return 1 / (1 - p) + 1/p -6;
314
    }
315

  
316
    template <class RealType, class Policy>
317
    inline RealType kurtosis(const bernoulli_distribution<RealType, Policy>& dist)
318
    {
319
      RealType p = dist.success_fraction();
320
      return 1 / (1 - p) + 1/p -6 + 3;
321
      // Simpler than:
322
      // return (6 * p * p - 6 * p + 1) / (p * (1 - p)) + 3;
323
    }
324

  
325
  } // namespace math
326
} // namespace boost
327

  
328
// This include must be at the end, *after* the accessors
329
// for this distribution have been defined, in order to
330
// keep compilers that support two-phase lookup happy.
331
#include <boost/math/distributions/detail/derived_accessors.hpp>
332

  
333
#endif // BOOST_MATH_SPECIAL_BERNOULLI_HPP
334

  
335

  
336

  
any/include/boost/math/distributions/beta.hpp
1
// boost\math\distributions\beta.hpp
2

  
3
// Copyright John Maddock 2006.
4
// Copyright Paul A. Bristow 2006.
5

  
6
// Use, modification and distribution are subject to the
7
// Boost Software License, Version 1.0.
8
// (See accompanying file LICENSE_1_0.txt
9
// or copy at http://www.boost.org/LICENSE_1_0.txt)
10

  
11
// http://en.wikipedia.org/wiki/Beta_distribution
12
// http://www.itl.nist.gov/div898/handbook/eda/section3/eda366h.htm
13
// http://mathworld.wolfram.com/BetaDistribution.html
14

  
15
// The Beta Distribution is a continuous probability distribution.
16
// The beta distribution is used to model events which are constrained to take place
17
// within an interval defined by maxima and minima,
18
// so is used extensively in PERT and other project management systems
19
// to describe the time to completion.
20
// The cdf of the beta distribution is used as a convenient way
21
// of obtaining the sum over a set of binomial outcomes.
22
// The beta distribution is also used in Bayesian statistics.
23

  
24
#ifndef BOOST_MATH_DIST_BETA_HPP
25
#define BOOST_MATH_DIST_BETA_HPP
26

  
27
#include <boost/math/distributions/fwd.hpp>
28
#include <boost/math/special_functions/beta.hpp> // for beta.
29
#include <boost/math/distributions/complement.hpp> // complements.
30
#include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
31
#include <boost/math/special_functions/fpclassify.hpp> // isnan.
32
#include <boost/math/tools/roots.hpp> // for root finding.
33

  
34
#if defined (BOOST_MSVC)
35
#  pragma warning(push)
36
#  pragma warning(disable: 4702) // unreachable code
37
// in domain_error_imp in error_handling
38
#endif
39

  
40
#include <utility>
41

  
42
namespace boost
43
{
44
  namespace math
45
  {
46
    namespace beta_detail
47
    {
48
      // Common error checking routines for beta distribution functions:
49
      template <class RealType, class Policy>
50
      inline bool check_alpha(const char* function, const RealType& alpha, RealType* result, const Policy& pol)
51
      {
52
        if(!(boost::math::isfinite)(alpha) || (alpha <= 0))
53
        {
54
          *result = policies::raise_domain_error<RealType>(
55
            function,
56
            "Alpha argument is %1%, but must be > 0 !", alpha, pol);
57
          return false;
58
        }
59
        return true;
60
      } // bool check_alpha
61

  
62
      template <class RealType, class Policy>
63
      inline bool check_beta(const char* function, const RealType& beta, RealType* result, const Policy& pol)
64
      {
65
        if(!(boost::math::isfinite)(beta) || (beta <= 0))
66
        {
67
          *result = policies::raise_domain_error<RealType>(
68
            function,
69
            "Beta argument is %1%, but must be > 0 !", beta, pol);
70
          return false;
71
        }
72
        return true;
73
      } // bool check_beta
74

  
75
      template <class RealType, class Policy>
76
      inline bool check_prob(const char* function, const RealType& p, RealType* result, const Policy& pol)
77
      {
78
        if((p < 0) || (p > 1) || !(boost::math::isfinite)(p))
79
        {
80
          *result = policies::raise_domain_error<RealType>(
81
            function,
82
            "Probability argument is %1%, but must be >= 0 and <= 1 !", p, pol);
83
          return false;
84
        }
85
        return true;
86
      } // bool check_prob
87

  
88
      template <class RealType, class Policy>
89
      inline bool check_x(const char* function, const RealType& x, RealType* result, const Policy& pol)
90
      {
91
        if(!(boost::math::isfinite)(x) || (x < 0) || (x > 1))
92
        {
93
          *result = policies::raise_domain_error<RealType>(
94
            function,
95
            "x argument is %1%, but must be >= 0 and <= 1 !", x, pol);
96
          return false;
97
        }
98
        return true;
99
      } // bool check_x
100

  
101
      template <class RealType, class Policy>
102
      inline bool check_dist(const char* function, const RealType& alpha, const RealType& beta, RealType* result, const Policy& pol)
103
      { // Check both alpha and beta.
104
        return check_alpha(function, alpha, result, pol)
105
          && check_beta(function, beta, result, pol);
106
      } // bool check_dist
107

  
108
      template <class RealType, class Policy>
109
      inline bool check_dist_and_x(const char* function, const RealType& alpha, const RealType& beta, RealType x, RealType* result, const Policy& pol)
110
      {
111
        return check_dist(function, alpha, beta, result, pol)
112
          && beta_detail::check_x(function, x, result, pol);
113
      } // bool check_dist_and_x
114

  
115
      template <class RealType, class Policy>
116
      inline bool check_dist_and_prob(const char* function, const RealType& alpha, const RealType& beta, RealType p, RealType* result, const Policy& pol)
117
      {
118
        return check_dist(function, alpha, beta, result, pol)
119
          && check_prob(function, p, result, pol);
120
      } // bool check_dist_and_prob
121

  
122
      template <class RealType, class Policy>
123
      inline bool check_mean(const char* function, const RealType& mean, RealType* result, const Policy& pol)
124
      {
125
        if(!(boost::math::isfinite)(mean) || (mean <= 0))
126
        {
127
          *result = policies::raise_domain_error<RealType>(
128
            function,
129
            "mean argument is %1%, but must be > 0 !", mean, pol);
130
          return false;
131
        }
132
        return true;
133
      } // bool check_mean
134
      template <class RealType, class Policy>
135
      inline bool check_variance(const char* function, const RealType& variance, RealType* result, const Policy& pol)
136
      {
137
        if(!(boost::math::isfinite)(variance) || (variance <= 0))
138
        {
139
          *result = policies::raise_domain_error<RealType>(
140
            function,
141
            "variance argument is %1%, but must be > 0 !", variance, pol);
142
          return false;
143
        }
144
        return true;
145
      } // bool check_variance
146
    } // namespace beta_detail
147

  
148
    // typedef beta_distribution<double> beta;
149
    // is deliberately NOT included to avoid a name clash with the beta function.
150
    // Use beta_distribution<> mybeta(...) to construct type double.
151

  
152
    template <class RealType = double, class Policy = policies::policy<> >
153
    class beta_distribution
154
    {
155
    public:
156
      typedef RealType value_type;
157
      typedef Policy policy_type;
158

  
159
      beta_distribution(RealType l_alpha = 1, RealType l_beta = 1) : m_alpha(l_alpha), m_beta(l_beta)
160
      {
161
        RealType result;
162
        beta_detail::check_dist(
163
           "boost::math::beta_distribution<%1%>::beta_distribution",
164
          m_alpha,
165
          m_beta,
166
          &result, Policy());
167
      } // beta_distribution constructor.
168
      // Accessor functions:
169
      RealType alpha() const
170
      {
171
        return m_alpha;
172
      }
173
      RealType beta() const
174
      { // .
175
        return m_beta;
176
      }
177

  
178
      // Estimation of the alpha & beta parameters.
179
      // http://en.wikipedia.org/wiki/Beta_distribution
180
      // gives formulae in section on parameter estimation.
181
      // Also NIST EDA page 3 & 4 give the same.
182
      // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366h.htm
183
      // http://www.epi.ucdavis.edu/diagnostictests/betabuster.html
184

  
185
      static RealType find_alpha(
186
        RealType mean, // Expected value of mean.
187
        RealType variance) // Expected value of variance.
188
      {
189
        static const char* function = "boost::math::beta_distribution<%1%>::find_alpha";
190
        RealType result = 0; // of error checks.
191
        if(false ==
192
            (
193
              beta_detail::check_mean(function, mean, &result, Policy())
194
              && beta_detail::check_variance(function, variance, &result, Policy())
195
            )
196
          )
197
        {
198
          return result;
199
        }
200
        return mean * (( (mean * (1 - mean)) / variance)- 1);
201
      } // RealType find_alpha
202

  
203
      static RealType find_beta(
204
        RealType mean, // Expected value of mean.
205
        RealType variance) // Expected value of variance.
206
      {
207
        static const char* function = "boost::math::beta_distribution<%1%>::find_beta";
208
        RealType result = 0; // of error checks.
209
        if(false ==
210
            (
211
              beta_detail::check_mean(function, mean, &result, Policy())
212
              &&
213
              beta_detail::check_variance(function, variance, &result, Policy())
214
            )
215
          )
216
        {
217
          return result;
218
        }
219
        return (1 - mean) * (((mean * (1 - mean)) /variance)-1);
220
      } //  RealType find_beta
221

  
222
      // Estimate alpha & beta from either alpha or beta, and x and probability.
223
      // Uses for these parameter estimators are unclear.
224

  
225
      static RealType find_alpha(
226
        RealType beta, // from beta.
227
        RealType x, //  x.
228
        RealType probability) // cdf
229
      {
230
        static const char* function = "boost::math::beta_distribution<%1%>::find_alpha";
231
        RealType result = 0; // of error checks.
232
        if(false ==
233
            (
234
             beta_detail::check_prob(function, probability, &result, Policy())
235
             &&
236
             beta_detail::check_beta(function, beta, &result, Policy())
237
             &&
238
             beta_detail::check_x(function, x, &result, Policy())
239
            )
240
          )
241
        {
242
          return result;
243
        }
244
        return ibeta_inva(beta, x, probability, Policy());
245
      } // RealType find_alpha(beta, a, probability)
246

  
247
      static RealType find_beta(
248
        // ibeta_invb(T b, T x, T p); (alpha, x, cdf,)
249
        RealType alpha, // alpha.
250
        RealType x, // probability x.
251
        RealType probability) // probability cdf.
252
      {
253
        static const char* function = "boost::math::beta_distribution<%1%>::find_beta";
254
        RealType result = 0; // of error checks.
255
        if(false ==
256
            (
257
              beta_detail::check_prob(function, probability, &result, Policy())
258
              &&
259
              beta_detail::check_alpha(function, alpha, &result, Policy())
260
              &&
261
              beta_detail::check_x(function, x, &result, Policy())
262
            )
263
          )
264
        {
265
          return result;
266
        }
267
        return ibeta_invb(alpha, x, probability, Policy());
268
      } //  RealType find_beta(alpha, x, probability)
269

  
270
    private:
271
      RealType m_alpha; // Two parameters of the beta distribution.
272
      RealType m_beta;
273
    }; // template <class RealType, class Policy> class beta_distribution
274

  
275
    template <class RealType, class Policy>
276
    inline const std::pair<RealType, RealType> range(const beta_distribution<RealType, Policy>& /* dist */)
277
    { // Range of permissible values for random variable x.
278
      using boost::math::tools::max_value;
279
      return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
280
    }
281

  
282
    template <class RealType, class Policy>
283
    inline const std::pair<RealType, RealType> support(const beta_distribution<RealType, Policy>&  /* dist */)
284
    { // Range of supported values for random variable x.
285
      // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
286
      return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
287
    }
288

  
289
    template <class RealType, class Policy>
290
    inline RealType mean(const beta_distribution<RealType, Policy>& dist)
291
    { // Mean of beta distribution = np.
292
      return  dist.alpha() / (dist.alpha() + dist.beta());
293
    } // mean
294

  
295
    template <class RealType, class Policy>
296
    inline RealType variance(const beta_distribution<RealType, Policy>& dist)
297
    { // Variance of beta distribution = np(1-p).
298
      RealType a = dist.alpha();
299
      RealType b = dist.beta();
300
      return  (a * b) / ((a + b ) * (a + b) * (a + b + 1));
301
    } // variance
302

  
303
    template <class RealType, class Policy>
304
    inline RealType mode(const beta_distribution<RealType, Policy>& dist)
305
    {
306
      static const char* function = "boost::math::mode(beta_distribution<%1%> const&)";
307

  
308
      RealType result;
309
      if ((dist.alpha() <= 1))
310
      {
311
        result = policies::raise_domain_error<RealType>(
312
          function,
313
          "mode undefined for alpha = %1%, must be > 1!", dist.alpha(), Policy());
314
        return result;
315
      }
316

  
317
      if ((dist.beta() <= 1))
318
      {
319
        result = policies::raise_domain_error<RealType>(
320
          function,
321
          "mode undefined for beta = %1%, must be > 1!", dist.beta(), Policy());
322
        return result;
323
      }
324
      RealType a = dist.alpha();
325
      RealType b = dist.beta();
326
      return (a-1) / (a + b - 2);
327
    } // mode
328

  
329
    //template <class RealType, class Policy>
330
    //inline RealType median(const beta_distribution<RealType, Policy>& dist)
331
    //{ // Median of beta distribution is not defined.
332
    //  return tools::domain_error<RealType>(function, "Median is not implemented, result is %1%!", std::numeric_limits<RealType>::quiet_NaN());
333
    //} // median
334

  
335
    //But WILL be provided by the derived accessor as quantile(0.5).
336

  
337
    template <class RealType, class Policy>
338
    inline RealType skewness(const beta_distribution<RealType, Policy>& dist)
339
    {
340
      BOOST_MATH_STD_USING // ADL of std functions.
341
      RealType a = dist.alpha();
342
      RealType b = dist.beta();
343
      return (2 * (b-a) * sqrt(a + b + 1)) / ((a + b + 2) * sqrt(a * b));
344
    } // skewness
345

  
346
    template <class RealType, class Policy>
347
    inline RealType kurtosis_excess(const beta_distribution<RealType, Policy>& dist)
348
    {
349
      RealType a = dist.alpha();
350
      RealType b = dist.beta();
351
      RealType a_2 = a * a;
352
      RealType n = 6 * (a_2 * a - a_2 * (2 * b - 1) + b * b * (b + 1) - 2 * a * b * (b + 2));
353
      RealType d = a * b * (a + b + 2) * (a + b + 3);
354
      return  n / d;
355
    } // kurtosis_excess
356

  
357
    template <class RealType, class Policy>
358
    inline RealType kurtosis(const beta_distribution<RealType, Policy>& dist)
359
    {
360
      return 3 + kurtosis_excess(dist);
361
    } // kurtosis
362

  
363
    template <class RealType, class Policy>
364
    inline RealType pdf(const beta_distribution<RealType, Policy>& dist, const RealType& x)
365
    { // Probability Density/Mass Function.
366
      BOOST_FPU_EXCEPTION_GUARD
367

  
368
      static const char* function = "boost::math::pdf(beta_distribution<%1%> const&, %1%)";
369

  
370
      BOOST_MATH_STD_USING // for ADL of std functions
371

  
372
      RealType a = dist.alpha();
373
      RealType b = dist.beta();
374

  
375
      // Argument checks:
376
      RealType result = 0;
377
      if(false == beta_detail::check_dist_and_x(
378
        function,
379
        a, b, x,
380
        &result, Policy()))
381
      {
382
        return result;
383
      }
384
      using boost::math::beta;
385
      return ibeta_derivative(a, b, x, Policy());
386
    } // pdf
387

  
388
    template <class RealType, class Policy>
389
    inline RealType cdf(const beta_distribution<RealType, Policy>& dist, const RealType& x)
390
    { // Cumulative Distribution Function beta.
391
      BOOST_MATH_STD_USING // for ADL of std functions
392

  
393
      static const char* function = "boost::math::cdf(beta_distribution<%1%> const&, %1%)";
394

  
395
      RealType a = dist.alpha();
396
      RealType b = dist.beta();
397

  
398
      // Argument checks:
399
      RealType result = 0;
400
      if(false == beta_detail::check_dist_and_x(
401
        function,
402
        a, b, x,
403
        &result, Policy()))
404
      {
405
        return result;
406
      }
407
      // Special cases:
408
      if (x == 0)
409
      {
410
        return 0;
411
      }
412
      else if (x == 1)
413
      {
414
        return 1;
415
      }
416
      return ibeta(a, b, x, Policy());
417
    } // beta cdf
418

  
419
    template <class RealType, class Policy>
420
    inline RealType cdf(const complemented2_type<beta_distribution<RealType, Policy>, RealType>& c)
421
    { // Complemented Cumulative Distribution Function beta.
422

  
423
      BOOST_MATH_STD_USING // for ADL of std functions
424

  
425
      static const char* function = "boost::math::cdf(beta_distribution<%1%> const&, %1%)";
426

  
427
      RealType const& x = c.param;
428
      beta_distribution<RealType, Policy> const& dist = c.dist;
429
      RealType a = dist.alpha();
430
      RealType b = dist.beta();
431

  
432
      // Argument checks:
433
      RealType result = 0;
434
      if(false == beta_detail::check_dist_and_x(
435
        function,
436
        a, b, x,
437
        &result, Policy()))
438
      {
439
        return result;
440
      }
441
      if (x == 0)
442
      {
443
        return 1;
444
      }
445
      else if (x == 1)
446
      {
447
        return 0;
448
      }
449
      // Calculate cdf beta using the incomplete beta function.
450
      // Use of ibeta here prevents cancellation errors in calculating
451
      // 1 - x if x is very small, perhaps smaller than machine epsilon.
452
      return ibetac(a, b, x, Policy());
453
    } // beta cdf
454

  
455
    template <class RealType, class Policy>
456
    inline RealType quantile(const beta_distribution<RealType, Policy>& dist, const RealType& p)
457
    { // Quantile or Percent Point beta function or
458
      // Inverse Cumulative probability distribution function CDF.
459
      // Return x (0 <= x <= 1),
460
      // for a given probability p (0 <= p <= 1).
461
      // These functions take a probability as an argument
462
      // and return a value such that the probability that a random variable x
463
      // will be less than or equal to that value
464
      // is whatever probability you supplied as an argument.
465

  
466
      static const char* function = "boost::math::quantile(beta_distribution<%1%> const&, %1%)";
467

  
468
      RealType result = 0; // of argument checks:
469
      RealType a = dist.alpha();
470
      RealType b = dist.beta();
471
      if(false == beta_detail::check_dist_and_prob(
472
        function,
473
        a, b, p,
474
        &result, Policy()))
475
      {
476
        return result;
477
      }
478
      // Special cases:
479
      if (p == 0)
480
      {
481
        return 0;
482
      }
483
      if (p == 1)
484
      {
485
        return 1;
486
      }
487
      return ibeta_inv(a, b, p, static_cast<RealType*>(0), Policy());
488
    } // quantile
489

  
490
    template <class RealType, class Policy>
491
    inline RealType quantile(const complemented2_type<beta_distribution<RealType, Policy>, RealType>& c)
492
    { // Complement Quantile or Percent Point beta function .
493
      // Return the number of expected x for a given
494
      // complement of the probability q.
495

  
496
      static const char* function = "boost::math::quantile(beta_distribution<%1%> const&, %1%)";
497

  
498
      //
499
      // Error checks:
500
      RealType q = c.param;
501
      const beta_distribution<RealType, Policy>& dist = c.dist;
502
      RealType result = 0;
503
      RealType a = dist.alpha();
504
      RealType b = dist.beta();
505
      if(false == beta_detail::check_dist_and_prob(
506
        function,
507
        a,
508
        b,
509
        q,
510
        &result, Policy()))
511
      {
512
        return result;
513
      }
514
      // Special cases:
515
      if(q == 1)
516
      {
517
        return 0;
518
      }
519
      if(q == 0)
520
      {
521
        return 1;
522
      }
523

  
524
      return ibetac_inv(a, b, q, static_cast<RealType*>(0), Policy());
525
    } // Quantile Complement
526

  
527
  } // namespace math
528
} // namespace boost
529

  
530
// This include must be at the end, *after* the accessors
531
// for this distribution have been defined, in order to
532
// keep compilers that support two-phase lookup happy.
533
#include <boost/math/distributions/detail/derived_accessors.hpp>
534

  
535
#if defined (BOOST_MSVC)
536
# pragma warning(pop)
537
#endif
538

  
539
#endif // BOOST_MATH_DIST_BETA_HPP
540

  
541

  
any/include/boost/math/distributions/binomial.hpp
1
// boost\math\distributions\binomial.hpp
2

  
3
// Copyright John Maddock 2006.
4
// Copyright Paul A. Bristow 2007.
5

  
6
// Use, modification and distribution are subject to the
7
// Boost Software License, Version 1.0.
8
// (See accompanying file LICENSE_1_0.txt
9
// or copy at http://www.boost.org/LICENSE_1_0.txt)
10

  
11
// http://en.wikipedia.org/wiki/binomial_distribution
12

  
13
// Binomial distribution is the discrete probability distribution of
14
// the number (k) of successes, in a sequence of
15
// n independent (yes or no, success or failure) Bernoulli trials.
16

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff