~ubuntu-branches/ubuntu/saucy/deal.ii/saucy

« back to all changes in this revision

Viewing changes to contrib/boost/include/boost/math/distributions/students_t.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV
  • Date: 2009-05-08 23:13:50 UTC
  • Revision ID: james.westby@ubuntu.com-20090508231350-rrh1ltgi0tifabwc
Tags: upstream-6.2.0
ImportĀ upstreamĀ versionĀ 6.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  Copyright John Maddock 2006.
 
2
//  Copyright Paul A. Bristow 2006.
 
3
//  Use, modification and distribution are subject to the
 
4
//  Boost Software License, Version 1.0. (See accompanying file
 
5
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
6
 
 
7
#ifndef BOOST_STATS_STUDENTS_T_HPP
 
8
#define BOOST_STATS_STUDENTS_T_HPP
 
9
 
 
10
// http://en.wikipedia.org/wiki/Student%27s_t_distribution
 
11
// http://www.itl.nist.gov/div898/handbook/eda/section3/eda3664.htm
 
12
 
 
13
#include <boost/math/distributions/fwd.hpp>
 
14
#include <boost/math/special_functions/beta.hpp> // for ibeta(a, b, x).
 
15
#include <boost/math/distributions/complement.hpp>
 
16
#include <boost/math/distributions/detail/common_error_handling.hpp>
 
17
 
 
18
#include <utility>
 
19
 
 
20
#ifdef BOOST_MSVC
 
21
# pragma warning(push)
 
22
# pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
 
23
#endif
 
24
 
 
25
namespace boost{ namespace math{
 
26
 
 
27
template <class RealType = double, class Policy = policies::policy<> >
 
28
class students_t_distribution
 
29
{
 
30
public:
 
31
   typedef RealType value_type;
 
32
   typedef Policy policy_type;
 
33
 
 
34
   students_t_distribution(RealType i) : m_df(i)
 
35
   { // Constructor.
 
36
      RealType result;
 
37
      detail::check_df(
 
38
         "boost::math::students_t_distribution<%1%>::students_t_distribution", m_df, &result, Policy());
 
39
   } // students_t_distribution
 
40
 
 
41
   RealType degrees_of_freedom()const
 
42
   {
 
43
      return m_df;
 
44
   }
 
45
 
 
46
   // Parameter estimation:
 
47
   static RealType find_degrees_of_freedom(
 
48
      RealType difference_from_mean,
 
49
      RealType alpha,
 
50
      RealType beta,
 
51
      RealType sd,
 
52
      RealType hint = 100);
 
53
 
 
54
private:
 
55
   //
 
56
   // Data members:
 
57
   //
 
58
   RealType m_df;  // degrees of freedom are a real number.
 
59
};
 
60
 
 
61
typedef students_t_distribution<double> students_t;
 
62
 
 
63
template <class RealType, class Policy>
 
64
inline const std::pair<RealType, RealType> range(const students_t_distribution<RealType, Policy>& /*dist*/)
 
65
{ // Range of permissible values for random variable x.
 
66
   using boost::math::tools::max_value;
 
67
   return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
 
68
}
 
69
 
 
70
template <class RealType, class Policy>
 
71
inline const std::pair<RealType, RealType> support(const students_t_distribution<RealType, Policy>& /*dist*/)
 
72
{ // Range of supported values for random variable x.
 
73
   // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
 
74
   using boost::math::tools::max_value;
 
75
   return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
 
76
}
 
77
 
 
78
template <class RealType, class Policy>
 
79
inline RealType pdf(const students_t_distribution<RealType, Policy>& dist, const RealType& t)
 
80
{
 
81
   BOOST_FPU_EXCEPTION_GUARD
 
82
   BOOST_MATH_STD_USING  // for ADL of std functions
 
83
 
 
84
   RealType degrees_of_freedom = dist.degrees_of_freedom();
 
85
   // Error check:
 
86
   RealType error_result;
 
87
   if(false == detail::check_df(
 
88
      "boost::math::pdf(const students_t_distribution<%1%>&, %1%)", degrees_of_freedom, &error_result, Policy()))
 
89
      return error_result;
 
90
   // Might conceivably permit df = +infinity and use normal distribution.
 
91
   RealType result;
 
92
   RealType basem1 = t * t / degrees_of_freedom;
 
93
   if(basem1 < 0.125)
 
94
   {
 
95
      result = exp(-boost::math::log1p(basem1, Policy()) * (1+degrees_of_freedom) / 2);
 
96
   }
 
97
   else
 
98
   {
 
99
      result = pow(1 / (1 + basem1), (degrees_of_freedom + 1) / 2);
 
100
   }
 
101
   result /= sqrt(degrees_of_freedom) * boost::math::beta(degrees_of_freedom / 2, RealType(0.5f), Policy());
 
102
   return result;
 
103
} // pdf
 
104
 
 
105
template <class RealType, class Policy>
 
106
inline RealType cdf(const students_t_distribution<RealType, Policy>& dist, const RealType& t)
 
107
{
 
108
   RealType degrees_of_freedom = dist.degrees_of_freedom();
 
109
   // Error check:
 
110
   RealType error_result;
 
111
   if(false == detail::check_df(
 
112
      "boost::math::cdf(const students_t_distribution<%1%>&, %1%)", degrees_of_freedom, &error_result, Policy()))
 
113
      return error_result;
 
114
 
 
115
   if (t == 0)
 
116
   {
 
117
     return 0.5;
 
118
   }
 
119
   //
 
120
   // Calculate probability of Student's t using the incomplete beta function.
 
121
   // probability = ibeta(degrees_of_freedom / 2, 1/2, degrees_of_freedom / (degrees_of_freedom + t*t))
 
122
   //
 
123
   // However when t is small compared to the degrees of freedom, that formula
 
124
   // suffers from rounding error, use the identity formula to work around
 
125
   // the problem:
 
126
   //
 
127
   // I[x](a,b) = 1 - I[1-x](b,a)
 
128
   //
 
129
   // and:
 
130
   //
 
131
   //     x = df / (df + t^2)
 
132
   //
 
133
   // so:
 
134
   //
 
135
   // 1 - x = t^2 / (df + t^2)
 
136
   //
 
137
   RealType t2 = t * t;
 
138
   RealType probability;
 
139
   if(degrees_of_freedom > 2 * t2)
 
140
   {
 
141
      RealType z = t2 / (degrees_of_freedom + t2);
 
142
      probability = ibetac(static_cast<RealType>(0.5), degrees_of_freedom / 2, z, Policy()) / 2;
 
143
   }
 
144
   else
 
145
   {
 
146
      RealType z = degrees_of_freedom / (degrees_of_freedom + t2);
 
147
      probability = ibeta(degrees_of_freedom / 2, static_cast<RealType>(0.5), z, Policy()) / 2;
 
148
   }
 
149
   return (t > 0 ? 1   - probability : probability);
 
150
} // cdf
 
151
 
 
152
template <class RealType, class Policy>
 
153
inline RealType quantile(const students_t_distribution<RealType, Policy>& dist, const RealType& p)
 
154
{
 
155
   BOOST_MATH_STD_USING // for ADL of std functions
 
156
   //
 
157
   // Obtain parameters:
 
158
   //
 
159
   RealType degrees_of_freedom = dist.degrees_of_freedom();
 
160
   RealType probability = p;
 
161
   //
 
162
   // Check for domain errors:
 
163
   //
 
164
   static const char* function = "boost::math::quantile(const students_t_distribution<%1%>&, %1%)";
 
165
   RealType error_result;
 
166
   if(false == detail::check_df(
 
167
      function, degrees_of_freedom, &error_result, Policy())
 
168
         && detail::check_probability(function, probability, &error_result, Policy()))
 
169
      return error_result;
 
170
 
 
171
   // Special cases, regardless of degrees_of_freedom.
 
172
   if (probability == 0)
 
173
      return -policies::raise_overflow_error<RealType>(function, 0, Policy());
 
174
   if (probability == 1)
 
175
     return policies::raise_overflow_error<RealType>(function, 0, Policy());
 
176
   if (probability == static_cast<RealType>(0.5))
 
177
     return 0;
 
178
   //
 
179
   // This next block is disabled in favour of a faster method than
 
180
   // incomplete beta inverse, code retained for future reference:
 
181
   //
 
182
#if 0
 
183
   //
 
184
   // Calculate quantile of Student's t using the incomplete beta function inverse:
 
185
   //
 
186
   probability = (probability > 0.5) ? 1 - probability : probability;
 
187
   RealType t, x, y;
 
188
   x = ibeta_inv(degrees_of_freedom / 2, RealType(0.5), 2 * probability, &y);
 
189
   if(degrees_of_freedom * y > tools::max_value<RealType>() * x)
 
190
      t = tools::overflow_error<RealType>(function);
 
191
   else
 
192
      t = sqrt(degrees_of_freedom * y / x);
 
193
   //
 
194
   // Figure out sign based on the size of p:
 
195
   //
 
196
   if(p < 0.5)
 
197
      t = -t;
 
198
 
 
199
   return t;
 
200
#endif
 
201
   //
 
202
   // Depending on how many digits RealType has, this may forward
 
203
   // to the incomplete beta inverse as above.  Otherwise uses a
 
204
   // faster method that is accurate to ~15 digits everywhere
 
205
   // and a couple of epsilon at double precision and in the central 
 
206
   // region where most use cases will occur...
 
207
   //
 
208
   return boost::math::detail::fast_students_t_quantile(degrees_of_freedom, probability, Policy());
 
209
} // quantile
 
210
 
 
211
template <class RealType, class Policy>
 
212
inline RealType cdf(const complemented2_type<students_t_distribution<RealType, Policy>, RealType>& c)
 
213
{
 
214
   return cdf(c.dist, -c.param);
 
215
}
 
216
 
 
217
template <class RealType, class Policy>
 
218
inline RealType quantile(const complemented2_type<students_t_distribution<RealType, Policy>, RealType>& c)
 
219
{
 
220
   return -quantile(c.dist, c.param);
 
221
}
 
222
 
 
223
//
 
224
// Parameter estimation follows:
 
225
//
 
226
namespace detail{
 
227
//
 
228
// Functors for finding degrees of freedom:
 
229
//
 
230
template <class RealType, class Policy>
 
231
struct sample_size_func
 
232
{
 
233
   sample_size_func(RealType a, RealType b, RealType s, RealType d)
 
234
      : alpha(a), beta(b), ratio(s*s/(d*d)) {}
 
235
 
 
236
   RealType operator()(const RealType& df)
 
237
   {
 
238
      if(df <= tools::min_value<RealType>())
 
239
         return 1;
 
240
      students_t_distribution<RealType, Policy> t(df);
 
241
      RealType qa = quantile(complement(t, alpha));
 
242
      RealType qb = quantile(complement(t, beta));
 
243
      qa += qb;
 
244
      qa *= qa;
 
245
      qa *= ratio;
 
246
      qa -= (df + 1);
 
247
      return qa;
 
248
   }
 
249
   RealType alpha, beta, ratio;
 
250
};
 
251
 
 
252
}  // namespace detail
 
253
 
 
254
template <class RealType, class Policy>
 
255
RealType students_t_distribution<RealType, Policy>::find_degrees_of_freedom(
 
256
      RealType difference_from_mean,
 
257
      RealType alpha,
 
258
      RealType beta,
 
259
      RealType sd,
 
260
      RealType hint)
 
261
{
 
262
   static const char* function = "boost::math::students_t_distribution<%1%>::find_degrees_of_freedom";
 
263
   //
 
264
   // Check for domain errors:
 
265
   //
 
266
   RealType error_result;
 
267
   if(false == detail::check_probability(
 
268
      function, alpha, &error_result, Policy())
 
269
         && detail::check_probability(function, beta, &error_result, Policy()))
 
270
      return error_result;
 
271
 
 
272
   if(hint <= 0)
 
273
      hint = 1;
 
274
 
 
275
   detail::sample_size_func<RealType, Policy> f(alpha, beta, sd, difference_from_mean);
 
276
   tools::eps_tolerance<RealType> tol(policies::digits<RealType, Policy>());
 
277
   boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
 
278
   std::pair<RealType, RealType> r = tools::bracket_and_solve_root(f, hint, RealType(2), false, tol, max_iter, Policy());
 
279
   RealType result = r.first + (r.second - r.first) / 2;
 
280
   if(max_iter >= policies::get_max_root_iterations<Policy>())
 
281
   {
 
282
      policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
 
283
         " either there is no answer to how many degrees of freedom are required"
 
284
         " or the answer is infinite.  Current best guess is %1%", result, Policy());
 
285
   }
 
286
   return result;
 
287
}
 
288
 
 
289
template <class RealType, class Policy>
 
290
inline RealType mean(const students_t_distribution<RealType, Policy>& )
 
291
{
 
292
   return 0;
 
293
}
 
294
 
 
295
template <class RealType, class Policy>
 
296
inline RealType variance(const students_t_distribution<RealType, Policy>& dist)
 
297
{
 
298
   // Error check:
 
299
   RealType error_result;
 
300
   if(false == detail::check_df(
 
301
      "boost::math::variance(students_t_distribution<%1%> const&, %1%)", dist.degrees_of_freedom(), &error_result, Policy()))
 
302
      return error_result;
 
303
 
 
304
   RealType v = dist.degrees_of_freedom();
 
305
   return v / (v - 2);
 
306
}
 
307
 
 
308
template <class RealType, class Policy>
 
309
inline RealType mode(const students_t_distribution<RealType, Policy>& /*dist*/)
 
310
{
 
311
   return 0;
 
312
}
 
313
 
 
314
template <class RealType, class Policy>
 
315
inline RealType median(const students_t_distribution<RealType, Policy>& /*dist*/)
 
316
{
 
317
   return 0;
 
318
}
 
319
 
 
320
template <class RealType, class Policy>
 
321
inline RealType skewness(const students_t_distribution<RealType, Policy>& dist)
 
322
{
 
323
   if(dist.degrees_of_freedom() <= 3)
 
324
   {
 
325
      policies::raise_domain_error<RealType>(
 
326
         "boost::math::skewness(students_t_distribution<%1%> const&, %1%)",
 
327
         "Skewness is undefined for degrees of freedom <= 3, but got %1%.",
 
328
         dist.degrees_of_freedom(), Policy());
 
329
   }
 
330
   return 0;
 
331
}
 
332
 
 
333
template <class RealType, class Policy>
 
334
inline RealType kurtosis(const students_t_distribution<RealType, Policy>& dist)
 
335
{
 
336
   RealType df = dist.degrees_of_freedom();
 
337
   if(df <= 3)
 
338
   {
 
339
      policies::raise_domain_error<RealType>(
 
340
         "boost::math::kurtosis(students_t_distribution<%1%> const&, %1%)",
 
341
         "Skewness is undefined for degrees of freedom <= 3, but got %1%.",
 
342
         df, Policy());
 
343
   }
 
344
   return 3 * (df - 2) / (df - 4);
 
345
}
 
346
 
 
347
template <class RealType, class Policy>
 
348
inline RealType kurtosis_excess(const students_t_distribution<RealType, Policy>& dist)
 
349
{
 
350
   // see http://mathworld.wolfram.com/Kurtosis.html
 
351
   RealType df = dist.degrees_of_freedom();
 
352
   if(df <= 3)
 
353
   {
 
354
      policies::raise_domain_error<RealType>(
 
355
         "boost::math::kurtosis_excess(students_t_distribution<%1%> const&, %1%)",
 
356
         "Skewness is undefined for degrees of freedom <= 3, but got %1%.",
 
357
         df, Policy());
 
358
   }
 
359
   return 6 / (df - 4);
 
360
}
 
361
 
 
362
} // namespace math
 
363
} // namespace boost
 
364
 
 
365
#ifdef BOOST_MSVC
 
366
# pragma warning(pop)
 
367
#endif
 
368
 
 
369
// This include must be at the end, *after* the accessors
 
370
// for this distribution have been defined, in order to
 
371
// keep compilers that support two-phase lookup happy.
 
372
#include <boost/math/distributions/detail/derived_accessors.hpp>
 
373
 
 
374
#endif // BOOST_STATS_STUDENTS_T_HPP