|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r67710 - in trunk: boost boost/random libs/random/test
From: steven_at_[hidden]
Date: 2011-01-05 21:53:02
Author: steven_watanabe
Date: 2011-01-05 21:53:00 EST (Wed, 05 Jan 2011)
New Revision: 67710
URL: http://svn.boost.org/trac/boost/changeset/67710
Log:
Update normal_distribution to match C++0x. Implement student_t_distribution.
Added:
trunk/boost/random/student_t_distribution.hpp (contents, props changed)
trunk/libs/random/test/test_normal.cpp (contents, props changed)
trunk/libs/random/test/test_normal_distribution.cpp (contents, props changed)
trunk/libs/random/test/test_student_t.cpp (contents, props changed)
trunk/libs/random/test/test_student_t_distribution.cpp (contents, props changed)
Text files modified:
trunk/boost/random.hpp | 1
trunk/boost/random/normal_distribution.hpp | 275 ++++++++++++++++++++++++++-------------
trunk/libs/random/test/Jamfile.v2 | 4
3 files changed, 190 insertions(+), 90 deletions(-)
Modified: trunk/boost/random.hpp
==============================================================================
--- trunk/boost/random.hpp (original)
+++ trunk/boost/random.hpp 2011-01-05 21:53:00 EST (Wed, 05 Jan 2011)
@@ -82,6 +82,7 @@
#include <boost/random/negative_binomial_distribution.hpp>
#include <boost/random/chi_squared_distribution.hpp>
#include <boost/random/fisher_f_distribution.hpp>
+#include <boost/random/student_t_distribution.hpp>
#include <boost/random/uniform_on_sphere.hpp>
#endif // BOOST_RANDOM_HPP
Modified: trunk/boost/random/normal_distribution.hpp
==============================================================================
--- trunk/boost/random/normal_distribution.hpp (original)
+++ trunk/boost/random/normal_distribution.hpp 2011-01-05 21:53:00 EST (Wed, 05 Jan 2011)
@@ -1,6 +1,7 @@
/* boost random/normal_distribution.hpp header file
*
* Copyright Jens Maurer 2000-2001
+ * Copyright Steven Watanabe 2010-2011
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
@@ -18,116 +19,210 @@
#include <boost/config/no_tr1/cmath.hpp>
#include <cassert>
-#include <iostream>
+#include <istream>
+#include <iosfwd>
#include <boost/limits.hpp>
#include <boost/static_assert.hpp>
#include <boost/random/detail/config.hpp>
+#include <boost/random/detail/operators.hpp>
+#include <boost/random/uniform_01.hpp>
namespace boost {
+namespace random {
+
+// deterministic Box-Muller method, uses trigonometric functions
/**
* Instantiations of class template normal_distribution model a
* \random_distribution. Such a distribution produces random numbers
* @c x distributed with probability density function
- * \f$\displaystyle p(x) = \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}\f$,
+ * \f$\displaystyle p(x) =
+ * \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}
+ * \f$,
* where mean and sigma are the parameters of the distribution.
*/
-// deterministic Box-Muller method, uses trigonometric functions
template<class RealType = double>
class normal_distribution
{
public:
- typedef RealType input_type;
- typedef RealType result_type;
+ typedef RealType input_type;
+ typedef RealType result_type;
+
+ class param_type {
+ public:
+ typedef normal_distribution distribution_type;
+
+ /**
+ * Constructs a @c param_type with a given mean and
+ * standard deviation.
+ *
+ * Requires: sigma >= 0
+ */
+ explicit param_type(RealType mean_arg = RealType(0.0),
+ RealType sigma_arg = RealType(1.0))
+ : _mean(mean_arg),
+ _sigma(sigma_arg)
+ {}
+
+ /** Returns the mean of the distribution. */
+ RealType mean() const { return _mean; }
+
+ /** Returns the standand deviation of the distribution. */
+ RealType sigma() const { return _sigma; }
+
+ /** Writes a @c param_type to a @c std::ostream. */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
+ { os << parm._mean << " " << parm._sigma ; return os; }
+
+ /** Reads a @c param_type from a @c std::istream. */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
+ { is >> parm._mean >> std::ws >> parm._sigma; return is; }
+
+ /** Returns true if the two sets of parameters are the same. */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
+ { return lhs._mean == rhs._mean && lhs._sigma == rhs._sigma; }
+
+ /** Returns true if the two sets of parameters are the different. */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
+
+ private:
+ RealType _mean;
+ RealType _sigma;
+ };
+
+ /**
+ * Constructs a @c normal_distribution object. @c mean and @c sigma are
+ * the parameters for the distribution.
+ *
+ * Requires: sigma >= 0
+ */
+ explicit normal_distribution(const RealType& mean_arg = RealType(0.0),
+ const RealType& sigma_arg = RealType(1.0))
+ : _mean(mean_arg), _sigma(sigma_arg),
+ _r1(0), _r2(0), _cached_rho(0), _valid(false)
+ {
+ assert(_sigma >= RealType(0));
+ }
+
+ /**
+ * Constructs a @c normal_distribution object from its parameters.
+ */
+ explicit normal_distribution(const param_type& parm)
+ : _mean(parm.mean()), _sigma(parm.sigma()),
+ _r1(0), _r2(0), _cached_rho(0), _valid(false)
+ {}
+
+ /** Returns the mean of the distribution. */
+ RealType mean() const { return _mean; }
+ /** Returns the standard deviation of the distribution. */
+ RealType sigma() const { return _sigma; }
+
+ /** Returns the smallest value that the distribution can produce. */
+ RealType min BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return -std::numeric_limits<RealType>::infinity(); }
+ /** Returns the largest value that the distribution can produce. */
+ RealType max BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return std::numeric_limits<RealType>::infinity(); }
+
+ /** Returns the parameters of the distribution. */
+ param_type param() const { return param_type(_mean, _sigma); }
+ /** Sets the parameters of the distribution. */
+ void param(const param_type& parm)
+ {
+ _mean = parm.mean();
+ _sigma = parm.sigma();
+ _valid = false;
+ }
+
+ /**
+ * Effects: Subsequent uses of the distribution do not depend
+ * on values produced by any engine prior to invoking reset.
+ */
+ void reset() { _valid = false; }
+
+ /** Returns a normal variate. */
+ template<class Engine>
+ result_type operator()(Engine& eng)
+ {
+ using std::sqrt;
+ using std::log;
+ using std::sin;
+ using std::cos;
+
+ if(!_valid) {
+ _r1 = boost::uniform_01<RealType>()(eng);
+ _r2 = boost::uniform_01<RealType>()(eng);
+ _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
+ _valid = true;
+ } else {
+ _valid = false;
+ }
+ // Can we have a boost::mathconst please?
+ const result_type pi = result_type(3.14159265358979323846);
+
+ return _cached_rho * (_valid ?
+ cos(result_type(2)*pi*_r1) :
+ sin(result_type(2)*pi*_r1))
+ * _sigma + _mean;
+ }
+
+ /** Returns a normal variate with parameters specified by @c param. */
+ template<class URNG>
+ result_type operator()(URNG& urng, const param_type& parm)
+ {
+ return normal_distribution(parm)(urng);
+ }
+
+ /** Writes a @c normal_distribution to a @c std::ostream. */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, normal_distribution, nd)
+ {
+ os << nd._mean << " " << nd._sigma << " "
+ << nd._valid << " " << nd._cached_rho << " " << nd._r1;
+ return os;
+ }
+
+ /** Reads a @c normal_distribution from a @c std::istream. */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, normal_distribution, nd)
+ {
+ is >> std::ws >> nd._mean >> std::ws >> nd._sigma
+ >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
+ >> std::ws >> nd._r1;
+ return is;
+ }
+
+ /**
+ * Returns true if the two instances of @c normal_distribution will
+ * return identical sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(normal_distribution, lhs, rhs)
+ {
+ return lhs._mean == rhs._mean && lhs._sigma == rhs._sigma
+ && lhs._valid == rhs._valid
+ && (!lhs._valid || (lhs._r1 == rhs._r1 && lhs._r2 == rhs._r2));
+ }
+
+ /**
+ * Returns true if the two instances of @c normal_distribution will
+ * return different sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(normal_distribution)
-#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
- BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
-#endif
-
- /**
- * Constructs a normal_distribution object. @c mean and @c sigma are
- * the parameters for the distribution.
- *
- * Requires: sigma > 0
- */
- explicit normal_distribution(const result_type& mean_arg = result_type(0.0),
- const result_type& sigma_arg = result_type(1.0))
- : _mean(mean_arg), _sigma(sigma_arg),
- _r1(0), _r2(0), _cached_rho(0), _valid(false)
- {
- assert(_sigma >= result_type(0));
- }
-
- // compiler-generated copy constructor is NOT fine, need to purge cache
- normal_distribution(const normal_distribution& other)
- : _mean(other._mean), _sigma(other._sigma),
- _r1(0), _r2(0), _cached_rho(0), _valid(false)
- {
- }
-
- // compiler-generated copy ctor and assignment operator are fine
-
- /**
- * Returns: The "mean" parameter of the distribution.
- */
- RealType mean() const { return _mean; }
- /**
- * Returns: The "sigma" parameter of the distribution.
- */
- RealType sigma() const { return _sigma; }
-
- void reset() { _valid = false; }
-
- template<class Engine>
- result_type operator()(Engine& eng)
- {
-#ifndef BOOST_NO_STDC_NAMESPACE
- // allow for Koenig lookup
- using std::sqrt; using std::log; using std::sin; using std::cos;
-#endif
- if(!_valid) {
- _r1 = eng();
- _r2 = eng();
- _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
- _valid = true;
- } else {
- _valid = false;
- }
- // Can we have a boost::mathconst please?
- const result_type pi = result_type(3.14159265358979323846);
-
- return _cached_rho * (_valid ?
- cos(result_type(2)*pi*_r1) :
- sin(result_type(2)*pi*_r1))
- * _sigma + _mean;
- }
-
-#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os, const normal_distribution& nd)
- {
- os << nd._mean << " " << nd._sigma << " "
- << nd._valid << " " << nd._cached_rho << " " << nd._r1;
- return os;
- }
-
- template<class CharT, class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is, normal_distribution& nd)
- {
- is >> std::ws >> nd._mean >> std::ws >> nd._sigma
- >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
- >> std::ws >> nd._r1;
- return is;
- }
-#endif
private:
- result_type _mean, _sigma;
- result_type _r1, _r2, _cached_rho;
- bool _valid;
+
+ /// @cond
+
+ RealType _mean, _sigma;
+ RealType _r1, _r2, _cached_rho;
+ bool _valid;
+
+ /// @endcond
+
};
+} // namespace random
+
+using random::normal_distribution;
+
} // namespace boost
#endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
Added: trunk/boost/random/student_t_distribution.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/random/student_t_distribution.hpp 2011-01-05 21:53:00 EST (Wed, 05 Jan 2011)
@@ -0,0 +1,180 @@
+/* boost random/student_t_distribution.hpp header file
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id$
+ */
+
+#ifndef BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP
+#define BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP
+
+#include <boost/config/no_tr1/cmath.hpp>
+#include <iosfwd>
+#include <boost/config.hpp>
+#include <boost/limits.hpp>
+#include <boost/random/detail/operators.hpp>
+#include <boost/random/chi_squared_distribution.hpp>
+#include <boost/random/normal_distribution.hpp>
+
+namespace boost {
+namespace random {
+
+/**
+ * The Student t distribution is a real valued distribution with one
+ * parameter n, the number of degrees of freedom.
+ *
+ * It has \f$\displaystyle p(x) =
+ * \frac{1}{\sqrt{n\pi}}
+ * \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
+ * \left(1}\frac{x^2}{n}\right)^{-(n+1)/2}
+ * \f$.
+ */
+template<class RealType = double>
+class student_t_distribution {
+public:
+ typedef RealType result_type;
+ typedef RealType input_type;
+
+ class param_type {
+ public:
+ typedef student_t_distribution distribution_type;
+
+ /**
+ * Constructs a @c param_type with "n" degrees of freedom.
+ *
+ * Requires: n > 0
+ */
+ explicit param_type(RealType n_arg = RealType(1.0))
+ : _n(n_arg)
+ {}
+
+ /** Returns the number of degrees of freedom of the distribution. */
+ RealType n() const { return _n; }
+
+ /** Writes a @c param_type to a @c std::ostream. */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
+ { os << parm._n; return os; }
+
+ /** Reads a @c param_type from a @c std::istream. */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
+ { is >> parm._n; return is; }
+
+ /** Returns true if the two sets of parameters are the same. */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
+ { return lhs._n == rhs._n; }
+
+ /** Returns true if the two sets of parameters are the different. */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
+
+ private:
+ RealType _n;
+ };
+
+ /**
+ * Constructs an @c student_t_distribution with "n" degrees of freedom.
+ *
+ * Requires: n > 0
+ */
+ explicit student_t_distribution(RealType n_arg = RealType(1.0))
+ : _normal(), _chi_squared(n_arg)
+ {}
+ /** Constructs an @c student_t_distribution from its parameters. */
+ explicit student_t_distribution(const param_type& parm)
+ : _normal(), _chi_squared(parm.n())
+ {}
+
+ /**
+ * Returns a random variate distributed according to the
+ * Student t distribution.
+ */
+ template<class URNG>
+ RealType operator()(URNG& urng)
+ {
+ using std::sqrt;
+ return _normal(urng) / sqrt(_chi_squared(urng) / n());
+ }
+
+ /**
+ * Returns a random variate distributed accordint to the Student
+ * t distribution with parameters specified by @c param.
+ */
+ template<class URNG>
+ RealType operator()(URNG& urng, const param_type& parm) const
+ {
+ return student_t_distribution(parm)(urng);
+ }
+
+ /** Returns the number of degrees of freedom. */
+ RealType n() const { return _chi_squared.n(); }
+
+ /** Returns the smallest value that the distribution can produce. */
+ RealType min BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return -std::numeric_limits<RealType>::infinity(); }
+ /** Returns the largest value that the distribution can produce. */
+ RealType max BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return std::numeric_limits<RealType>::infinity(); }
+
+ /** Returns the parameters of the distribution. */
+ param_type param() const { return param_type(n()); }
+ /** Sets the parameters of the distribution. */
+ void param(const param_type& parm)
+ {
+ typedef chi_squared_distribution<RealType> chi_squared_type;
+ typename chi_squared_type::param_type chi_squared_param(parm.n());
+ _chi_squared.param(chi_squared_param);
+ }
+
+ /**
+ * Effects: Subsequent uses of the distribution do not depend
+ * on values produced by any engine prior to invoking reset.
+ */
+ void reset()
+ {
+ _normal.reset();
+ _chi_squared.reset();
+ }
+
+ /** Writes a @c student_t_distribution to a @c std::ostream. */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, student_t_distribution, td)
+ {
+ os << td.param();
+ return os;
+ }
+
+ /** Reads a @c student_t_distribution from a @c std::istream. */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, student_t_distribution, td)
+ {
+ param_type parm;
+ if(is >> parm) {
+ td.param(parm);
+ }
+ return is;
+ }
+
+ /**
+ * Returns true if the two instances of @c student_t_distribution will
+ * return identical sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(student_t_distribution, lhs, rhs)
+ { return lhs._normal == rhs._normal && lhs._chi_squared == rhs._chi_squared; }
+
+ /**
+ * Returns true if the two instances of @c student_t_distribution will
+ * return different sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(student_t_distribution)
+
+private:
+ normal_distribution<RealType> _normal;
+ chi_squared_distribution<RealType> _chi_squared;
+};
+
+} // namespace random
+} // namespace boost
+
+#endif // BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP
Modified: trunk/libs/random/test/Jamfile.v2
==============================================================================
--- trunk/libs/random/test/Jamfile.v2 (original)
+++ trunk/libs/random/test/Jamfile.v2 2011-01-05 21:53:00 EST (Wed, 05 Jan 2011)
@@ -64,6 +64,10 @@
run test_chi_squared_distribution.cpp /boost//unit_test_framework ;
run test_fisher_f.cpp ;
run test_fisher_f_distribution.cpp /boost//unit_test_framework ;
+run test_student_t.cpp ;
+run test_student_t_distribution.cpp /boost//unit_test_framework ;
+run test_normal.cpp ;
+run test_normal_distribution.cpp /boost//unit_test_framework ;
# run nondet_random_speed.cpp ;
# run random_device.cpp ;
Added: trunk/libs/random/test/test_normal.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_normal.cpp 2011-01-05 21:53:00 EST (Wed, 05 Jan 2011)
@@ -0,0 +1,111 @@
+/* test_normal.cpp
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * $Id$
+ *
+ */
+
+#include <boost/random/normal_distribution.hpp>
+#include <boost/random/uniform_int.hpp>
+#include <boost/random/uniform_01.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/math/distributions/normal.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/exception/diagnostic_information.hpp>
+#include <vector>
+#include <iostream>
+#include <numeric>
+
+#include "statistic_tests.hpp"
+
+bool do_test(double m, double s, int max) {
+ std::cout << "running normal(" << m << ", " << s << ")" << " " << max << " times: " << std::flush;
+
+ boost::math::normal expected(m, s);
+
+ boost::random::normal_distribution<> dist(m, s);
+ boost::mt19937 gen;
+ kolmogorov_experiment test(max);
+ boost::variate_generator<boost::mt19937&, boost::random::normal_distribution<> > vgen(gen, dist);
+
+ double prob = test.probability(test.run(vgen, expected));
+
+ bool result = prob < 0.99;
+ const char* err = result? "" : "*";
+ std::cout << std::setprecision(17) << prob << err << std::endl;
+
+ std::cout << std::setprecision(6);
+
+ return result;
+}
+
+bool do_tests(int repeat, double max_m, double max_s, int trials) {
+ boost::mt19937 gen;
+ boost::uniform_real<> mdist(-max_m, max_m);
+ boost::uniform_real<> sdist(0.00001, max_s);
+ int errors = 0;
+ for(int i = 0; i < repeat; ++i) {
+ if(!do_test(mdist(gen), sdist(gen), trials)) {
+ ++errors;
+ }
+ }
+ if(errors != 0) {
+ std::cout << "*** " << errors << " errors detected ***" << std::endl;
+ }
+ return errors == 0;
+}
+
+int usage() {
+ std::cerr << "Usage: test_normal -r <repeat> -m <max mean> -s <max sigma> -t <trials>" << std::endl;
+ return 2;
+}
+
+template<class T>
+bool handle_option(int& argc, char**& argv, char opt, T& value) {
+ if(argv[0][1] == opt && argc > 1) {
+ --argc;
+ ++argv;
+ value = boost::lexical_cast<T>(argv[0]);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+int main(int argc, char** argv) {
+ int repeat = 10;
+ double max_m = 1000.0;
+ double max_s = 1000.0;
+ int trials = 1000000;
+
+ if(argc > 0) {
+ --argc;
+ ++argv;
+ }
+ while(argc > 0) {
+ if(argv[0][0] != '-') return usage();
+ else if(!handle_option(argc, argv, 'r', repeat)
+ && !handle_option(argc, argv, 'm', max_m)
+ && !handle_option(argc, argv, 's', max_s)
+ && !handle_option(argc, argv, 't', trials)) {
+ return usage();
+ }
+ --argc;
+ ++argv;
+ }
+
+ try {
+ if(do_tests(repeat, max_m, max_s, trials)) {
+ return 0;
+ } else {
+ return EXIT_FAILURE;
+ }
+ } catch(...) {
+ std::cerr << boost::current_exception_diagnostic_information() << std::endl;
+ return EXIT_FAILURE;
+ }
+}
Added: trunk/libs/random/test/test_normal_distribution.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_normal_distribution.cpp 2011-01-05 21:53:00 EST (Wed, 05 Jan 2011)
@@ -0,0 +1,36 @@
+/* test_normal_distribution.cpp
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * $Id$
+ *
+ */
+
+#include <boost/random/normal_distribution.hpp>
+#include <limits>
+
+#define BOOST_RANDOM_DISTRIBUTION boost::random::normal_distribution<>
+#define BOOST_RANDOM_ARG1 mean
+#define BOOST_RANDOM_ARG2 sigma
+#define BOOST_RANDOM_ARG1_DEFAULT 0.0
+#define BOOST_RANDOM_ARG2_DEFAULT 1.0
+#define BOOST_RANDOM_ARG1_VALUE 7.5
+#define BOOST_RANDOM_ARG2_VALUE 0.25
+
+#define BOOST_RANDOM_DIST0_MIN -(std::numeric_limits<double>::infinity)()
+#define BOOST_RANDOM_DIST0_MAX (std::numeric_limits<double>::infinity)()
+#define BOOST_RANDOM_DIST1_MIN -(std::numeric_limits<double>::infinity)()
+#define BOOST_RANDOM_DIST1_MAX (std::numeric_limits<double>::infinity)()
+#define BOOST_RANDOM_DIST2_MIN -(std::numeric_limits<double>::infinity)()
+#define BOOST_RANDOM_DIST2_MAX (std::numeric_limits<double>::infinity)()
+
+#define BOOST_RANDOM_TEST1_PARAMS (-100.0)
+#define BOOST_RANDOM_TEST1_MAX 0
+
+#define BOOST_RANDOM_TEST2_PARAMS (100.0)
+#define BOOST_RANDOM_TEST2_MIN 0
+
+#include "test_distribution.ipp"
Added: trunk/libs/random/test/test_student_t.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_student_t.cpp 2011-01-05 21:53:00 EST (Wed, 05 Jan 2011)
@@ -0,0 +1,108 @@
+/* test_student_t.cpp
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * $Id$
+ *
+ */
+
+#include <boost/random/student_t_distribution.hpp>
+#include <boost/random/uniform_int.hpp>
+#include <boost/random/uniform_real.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/math/distributions/students_t.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/exception/diagnostic_information.hpp>
+#include <vector>
+#include <iostream>
+#include <numeric>
+
+#include "statistic_tests.hpp"
+
+bool do_test(double n, int max) {
+ std::cout << "running student_t(" << n << ")" << " " << max << " times: " << std::flush;
+
+ boost::math::students_t expected(n);
+
+ boost::random::student_t_distribution<> dist(n);
+ boost::mt19937 gen;
+ kolmogorov_experiment test(max);
+ boost::variate_generator<boost::mt19937&, boost::random::student_t_distribution<> > vgen(gen, dist);
+
+ double prob = test.probability(test.run(vgen, expected));
+
+ bool result = prob < 0.99;
+ const char* err = result? "" : "*";
+ std::cout << std::setprecision(17) << prob << err << std::endl;
+
+ std::cout << std::setprecision(6);
+
+ return result;
+}
+
+bool do_tests(int repeat, double max_n, int trials) {
+ boost::mt19937 gen;
+ boost::uniform_real<> ndist(0.00001, max_n);
+ int errors = 0;
+ for(int i = 0; i < repeat; ++i) {
+ if(!do_test(ndist(gen), trials)) {
+ ++errors;
+ }
+ }
+ if(errors != 0) {
+ std::cout << "*** " << errors << " errors detected ***" << std::endl;
+ }
+ return errors == 0;
+}
+
+int usage() {
+ std::cerr << "Usage: test_student_t -r <repeat> -n <max n> -t <trials>" << std::endl;
+ return 2;
+}
+
+template<class T>
+bool handle_option(int& argc, char**& argv, char opt, T& value) {
+ if(argv[0][1] == opt && argc > 1) {
+ --argc;
+ ++argv;
+ value = boost::lexical_cast<T>(argv[0]);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+int main(int argc, char** argv) {
+ int repeat = 10;
+ double max_n = 1000.0;
+ int trials = 1000000;
+
+ if(argc > 0) {
+ --argc;
+ ++argv;
+ }
+ while(argc > 0) {
+ if(argv[0][0] != '-') return usage();
+ else if(!handle_option(argc, argv, 'r', repeat)
+ && !handle_option(argc, argv, 'n', max_n)
+ && !handle_option(argc, argv, 't', trials)) {
+ return usage();
+ }
+ --argc;
+ ++argv;
+ }
+
+ try {
+ if(do_tests(repeat, max_n, trials)) {
+ return 0;
+ } else {
+ return EXIT_FAILURE;
+ }
+ } catch(...) {
+ std::cerr << boost::current_exception_diagnostic_information() << std::endl;
+ return EXIT_FAILURE;
+ }
+}
Added: trunk/libs/random/test/test_student_t_distribution.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_student_t_distribution.cpp 2011-01-05 21:53:00 EST (Wed, 05 Jan 2011)
@@ -0,0 +1,29 @@
+/* test_student_t_distribution.cpp
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * $Id$
+ *
+ */
+
+#include <boost/random/student_t_distribution.hpp>
+#include <limits>
+
+#define BOOST_RANDOM_DISTRIBUTION boost::random::student_t_distribution<>
+#define BOOST_RANDOM_ARG1 n
+#define BOOST_RANDOM_ARG1_DEFAULT 1.0
+#define BOOST_RANDOM_ARG1_VALUE 7.5
+
+#define BOOST_RANDOM_DIST0_MIN -(std::numeric_limits<double>::infinity)()
+#define BOOST_RANDOM_DIST0_MAX (std::numeric_limits<double>::infinity)()
+#define BOOST_RANDOM_DIST1_MIN -(std::numeric_limits<double>::infinity)()
+#define BOOST_RANDOM_DIST1_MAX (std::numeric_limits<double>::infinity)()
+
+#define BOOST_RANDOM_TEST1_PARAMS
+
+#define BOOST_RANDOM_TEST2_PARAMS (100.0)
+
+#include "test_distribution.ipp"
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk