Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r63328 - in trunk: boost/random libs/random/doc libs/random/test
From: steven_at_[hidden]
Date: 2010-06-25 18:22:55


Author: steven_watanabe
Date: 2010-06-25 18:22:53 EDT (Fri, 25 Jun 2010)
New Revision: 63328
URL: http://svn.boost.org/trac/boost/changeset/63328

Log:
Add weibull_distribution.
Added:
   trunk/boost/random/weibull_distribution.hpp (contents, props changed)
   trunk/libs/random/test/test_weibull.cpp (contents, props changed)
   trunk/libs/random/test/test_weibull_distribution.cpp (contents, props changed)
Text files modified:
   trunk/libs/random/doc/Jamfile.v2 | 1 +
   trunk/libs/random/doc/distributions.qbk | 1 +
   trunk/libs/random/doc/random.qbk | 1 +
   trunk/libs/random/test/Jamfile.v2 | 2 ++
   4 files changed, 5 insertions(+), 0 deletions(-)

Added: trunk/boost/random/weibull_distribution.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/random/weibull_distribution.hpp 2010-06-25 18:22:53 EDT (Fri, 25 Jun 2010)
@@ -0,0 +1,171 @@
+/* boost random/weibull_distribution.hpp header file
+ *
+ * Copyright Steven Watanabe 2010
+ * 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_WEIBULL_DISTRIBUTION_HPP
+#define BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP
+
+#include <boost/config/no_tr1/cmath.hpp>
+#include <iosfwd>
+#include <istream>
+#include <boost/config.hpp>
+#include <boost/limits.hpp>
+#include <boost/random/detail/operators.hpp>
+#include <boost/random/uniform_01.hpp>
+
+namespace boost {
+namespace random {
+
+/**
+ * The Weibull distribution is a real valued distribution with two
+ * parameters a and b, producing values >= 0.
+ *
+ * It has \f$\displaystyle p(x) = \frac{a}{b}\left(\frac{x}{b}\right)^{a-1}e^{-\left(\frac{x}{b}\right)^a}\f$.
+ */
+template<class RealType = double>
+class weibull_distribution {
+public:
+ typedef RealType result_type;
+ typedef RealType input_type;
+
+ class param_type {
+ public:
+ typedef weibull_distribution distribution_type;
+
+ /**
+ * Constructs a @c param_type from the "a" and "b" parameters
+ * of the distribution.
+ *
+ * Requires: a > 0 && b > 0
+ */
+ explicit param_type(RealType a_arg = 1.0, RealType b_arg = 1.0)
+ : _a(a_arg), _b(b_arg)
+ {}
+
+ /** Returns the "a" parameter of the distribtuion. */
+ RealType a() const { return _a; }
+ /** Returns the "b" parameter of the distribution. */
+ RealType b() const { return _b; }
+
+ /** Writes a @c param_type to a @c std::ostream. */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
+ { os << parm._a << ' ' << parm._b; return os; }
+
+ /** Reads a @c param_type from a @c std::istream. */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
+ { is >> parm._a >> std::ws >> parm._b; return is; }
+
+ /** Returns true if the two sets of parameters are the same. */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
+ { return lhs._a == rhs._a && lhs._b == rhs._b; }
+
+ /** Returns true if the two sets of parameters are the different. */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
+
+ private:
+ RealType _a;
+ RealType _b;
+ };
+
+ /**
+ * Constructs a @c weibull_distribution from its "a" and "b" parameters.
+ *
+ * Requires: a > 0 && b > 0
+ */
+ explicit weibull_distribution(RealType a_arg = 1.0, RealType b_arg = 1.0)
+ : _a(a_arg), _b(b_arg)
+ {}
+ /** Constructs a @c weilbull_distribution from its parameters. */
+ explicit weibull_distribution(const param_type& parm)
+ : _a(parm.a()), _b(parm.b())
+ {}
+
+ /**
+ * Returns a random variate distributed according to the
+ * @c weibull_distribution.
+ */
+ template<class URNG>
+ RealType operator()(URNG& urng) const
+ {
+ using std::pow;
+ using std::log;
+ return _b*pow(-log(1 - uniform_01<RealType>()(urng)), 1/_a);
+ }
+
+ /**
+ * Returns a random variate distributed accordint to the Weibull
+ * distribution with parameters specified by @c parm.
+ */
+ template<class URNG>
+ RealType operator()(URNG& urng, const param_type& parm) const
+ {
+ return weibull_distribution(parm)(urng);
+ }
+
+ /** Returns the "a" parameter of the distribution. */
+ RealType a() const { return _a; }
+ /** Returns the "b" parameter of the distribution. */
+ RealType b() const { return _b; }
+
+ /** Returns the smallest value that the distribution can produce. */
+ RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
+ /** 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(_a, _b); }
+ /** Sets the parameters of the distribution. */
+ void param(const param_type& parm)
+ {
+ _a = parm.a();
+ _b = parm.b();
+ }
+
+ /** Writes a @c weibull_distribution to a @c std::ostream. */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, weibull_distribution, wd)
+ {
+ os << wd.param();
+ return os;
+ }
+
+ /** Reads a @c weibull_distribution from a @c std::istream. */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, weibull_distribution, wd)
+ {
+ param_type parm;
+ if(is >> parm) {
+ wd.param(parm);
+ }
+ return is;
+ }
+
+ /**
+ * Returns true if the two instances of @c weibull_distribution will
+ * return identical sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(weibull_distribution, lhs, rhs)
+ { return lhs._a == rhs._a && lhs._b == rhs._b; }
+
+ /**
+ * Returns true if the two instances of @c weibull_distribution will
+ * return different sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(weibull_distribution)
+
+private:
+ RealType _a;
+ RealType _b;
+};
+
+} // namespace random
+} // namespace boost
+
+#endif // BOOST_RANDOM_WEIBULL_DISTRIBUTION_HPP

Modified: trunk/libs/random/doc/Jamfile.v2
==============================================================================
--- trunk/libs/random/doc/Jamfile.v2 (original)
+++ trunk/libs/random/doc/Jamfile.v2 2010-06-25 18:22:53 EDT (Fri, 25 Jun 2010)
@@ -44,6 +44,7 @@
     uniform_real
     uniform_smallint
     variate_generator
+ weibull_distribution
     xor_combine
 ;
 

Modified: trunk/libs/random/doc/distributions.qbk
==============================================================================
--- trunk/libs/random/doc/distributions.qbk (original)
+++ trunk/libs/random/doc/distributions.qbk 2010-06-25 18:22:53 EDT (Fri, 25 Jun 2010)
@@ -66,6 +66,7 @@
                                simulations)]
                               [measuring the job completion time of an assembly
                                line worker]]
+ [[__weibull_distribution] [weibull distribution] [-]]
   [[__uniform_on_sphere] [uniform distribution on a unit sphere of arbitrary
                           dimension]
                          [choosing a random point on Earth (assumed to be a

Modified: trunk/libs/random/doc/random.qbk
==============================================================================
--- trunk/libs/random/doc/random.qbk (original)
+++ trunk/libs/random/doc/random.qbk 2010-06-25 18:22:53 EDT (Fri, 25 Jun 2010)
@@ -77,6 +77,7 @@
 [def __normal_distribution [classref boost::normal_distribution normal_distribution]]
 [def __lognormal_distribution [classref boost::lognormal_distribution lognormal_distribution]]
 [def __uniform_on_sphere [classref boost::uniform_on_sphere uniform_on_sphere]]
+[def __weibull_distribution [classref boost::random::weibull_distribution weibull_distribution]]
 
 [include performance_data.qbk]
 

Modified: trunk/libs/random/test/Jamfile.v2
==============================================================================
--- trunk/libs/random/test/Jamfile.v2 (original)
+++ trunk/libs/random/test/Jamfile.v2 2010-06-25 18:22:53 EDT (Fri, 25 Jun 2010)
@@ -54,6 +54,8 @@
 run test_discrete_distribution.cpp /boost//unit_test_framework ;
 run test_gamma.cpp ;
 run test_gamma_distribution.cpp /boost//unit_test_framework ;
+run test_weibull.cpp ;
+run test_weibull_distribution.cpp /boost//unit_test_framework ;
 
 # run nondet_random_speed.cpp ;
 # run random_device.cpp ;

Added: trunk/libs/random/test/test_weibull.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_weibull.cpp 2010-06-25 18:22:53 EDT (Fri, 25 Jun 2010)
@@ -0,0 +1,111 @@
+/* test_gamma.cpp
+ *
+ * Copyright Steven Watanabe 2010
+ * 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/weibull_distribution.hpp>
+#include <boost/random/uniform_int.hpp>
+#include <boost/random/uniform_01.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/math/distributions/weibull.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 alpha, double beta, int max) {
+ std::cout << "running gamma(" << alpha << ", " << beta << ")" << " " << max << " times: " << std::flush;
+
+ boost::math::weibull_distribution<> expected(alpha, beta);
+
+ boost::random::weibull_distribution<> dist(alpha, beta);
+ boost::mt19937 gen;
+ kolmogorov_experiment test(max);
+ boost::variate_generator<boost::mt19937&, boost::random::weibull_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_a, double max_b, int trials) {
+ boost::mt19937 gen;
+ boost::uniform_real<> adist(0.00001, max_a);
+ boost::uniform_real<> bdist(0.00001, max_b);
+ int errors = 0;
+ for(int i = 0; i < repeat; ++i) {
+ if(!do_test(adist(gen), bdist(gen), trials)) {
+ ++errors;
+ }
+ }
+ if(errors != 0) {
+ std::cout << "*** " << errors << " errors detected ***" << std::endl;
+ }
+ return errors == 0;
+}
+
+int usage() {
+ std::cerr << "Usage: test_weibull -r <repeat> -a <max a> -b <max b> -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_a = 1000.0;
+ double max_b = 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, 'a', max_a)
+ && !handle_option(argc, argv, 'b', max_b)
+ && !handle_option(argc, argv, 't', trials)) {
+ return usage();
+ }
+ --argc;
+ ++argv;
+ }
+
+ try {
+ if(do_tests(repeat, max_a, max_b, 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_weibull_distribution.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_weibull_distribution.cpp 2010-06-25 18:22:53 EDT (Fri, 25 Jun 2010)
@@ -0,0 +1,132 @@
+/* test_gamma_distribution.cpp
+ *
+ * Copyright Steven Watanabe 2010
+ * 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/weibull_distribution.hpp>
+#include <boost/random/linear_congruential.hpp>
+#include <sstream>
+
+#define BOOST_TEST_MAIN
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE(test_constructors) {
+ boost::random::weibull_distribution<> dist;
+ BOOST_CHECK_EQUAL(dist.a(), 1.0);
+ BOOST_CHECK_EQUAL(dist.b(), 1.0);
+ boost::random::weibull_distribution<> dist_one(7.5);
+ BOOST_CHECK_EQUAL(dist_one.a(), 7.5);
+ BOOST_CHECK_EQUAL(dist_one.b(), 1.0);
+ boost::random::weibull_distribution<> dist_two(7.5, 0.25);
+ BOOST_CHECK_EQUAL(dist_two.a(), 7.5);
+ BOOST_CHECK_EQUAL(dist_two.b(), 0.25);
+ boost::random::weibull_distribution<> copy(dist);
+ BOOST_CHECK_EQUAL(dist, copy);
+ boost::random::weibull_distribution<> copy_one(dist_one);
+ BOOST_CHECK_EQUAL(dist_one, copy_one);
+ boost::random::weibull_distribution<> copy_two(dist_two);
+ BOOST_CHECK_EQUAL(dist_two, copy_two);
+}
+
+BOOST_AUTO_TEST_CASE(test_param) {
+ boost::random::weibull_distribution<> dist(7.5, 0.25);
+ boost::random::weibull_distribution<>::param_type param = dist.param();
+ BOOST_CHECK_EQUAL(param.a(), 7.5);
+ BOOST_CHECK_EQUAL(param.b(), 0.25);
+ boost::random::weibull_distribution<> copy1(param);
+ BOOST_CHECK_EQUAL(dist, copy1);
+ boost::random::weibull_distribution<> copy2;
+ copy2.param(param);
+ BOOST_CHECK_EQUAL(dist, copy2);
+
+ boost::random::weibull_distribution<>::param_type param_copy = param;
+ BOOST_CHECK_EQUAL(param, param_copy);
+ BOOST_CHECK(param == param_copy);
+ BOOST_CHECK(!(param != param_copy));
+ boost::random::weibull_distribution<>::param_type param_default;
+ BOOST_CHECK_EQUAL(param_default.a(), 1.0);
+ BOOST_CHECK_EQUAL(param_default.b(), 1.0);
+ BOOST_CHECK(param != param_default);
+ BOOST_CHECK(!(param == param_default));
+ boost::random::weibull_distribution<>::param_type param_one(7.5);
+ BOOST_CHECK_EQUAL(param_one.a(), 7.5);
+ BOOST_CHECK_EQUAL(param_one.b(), 1.0);
+ BOOST_CHECK(param != param_one);
+ BOOST_CHECK(!(param == param_one));
+ BOOST_CHECK(param_default != param_one);
+ BOOST_CHECK(!(param_default == param_one));
+ boost::random::weibull_distribution<>::param_type param_two(7.5, 0.25);
+ BOOST_CHECK_EQUAL(param_two.a(), 7.5);
+ BOOST_CHECK_EQUAL(param_two.b(), 0.25);
+}
+
+BOOST_AUTO_TEST_CASE(test_min_max) {
+ boost::random::weibull_distribution<> dist;
+ BOOST_CHECK_EQUAL((dist.min)(), 0);
+ BOOST_CHECK_EQUAL((dist.max)(), (std::numeric_limits<double>::infinity)());
+ boost::random::weibull_distribution<> dist_one(7.5);
+ BOOST_CHECK_EQUAL((dist_one.min)(), 0);
+ BOOST_CHECK_EQUAL((dist_one.max)(), (std::numeric_limits<double>::infinity)());
+ boost::random::weibull_distribution<> dist_two(7.5, 0.25);
+ BOOST_CHECK_EQUAL((dist_two.min)(), 0);
+ BOOST_CHECK_EQUAL((dist_two.max)(), (std::numeric_limits<double>::infinity)());
+}
+
+BOOST_AUTO_TEST_CASE(test_comparison) {
+ boost::random::weibull_distribution<> dist;
+ boost::random::weibull_distribution<> dist_copy(dist);
+ boost::random::weibull_distribution<> dist_one(7.5);
+ boost::random::weibull_distribution<> dist_one_copy(dist_one);
+ boost::random::weibull_distribution<> dist_two(7.5, 0.25);
+ boost::random::weibull_distribution<> dist_two_copy(dist_two);
+ BOOST_CHECK(dist == dist_copy);
+ BOOST_CHECK(!(dist != dist_copy));
+ BOOST_CHECK(dist_one == dist_one_copy);
+ BOOST_CHECK(!(dist_one != dist_one_copy));
+ BOOST_CHECK(dist_two == dist_two_copy);
+ BOOST_CHECK(!(dist_two != dist_two_copy));
+ BOOST_CHECK(dist != dist_one);
+ BOOST_CHECK(!(dist == dist_one));
+ BOOST_CHECK(dist != dist_two);
+ BOOST_CHECK(!(dist == dist_two));
+ BOOST_CHECK(dist_one != dist_two);
+ BOOST_CHECK(!(dist_one == dist_two));
+}
+
+BOOST_AUTO_TEST_CASE(test_streaming) {
+ boost::random::weibull_distribution<> dist(7.5, 0.25);
+ std::stringstream stream;
+ stream << dist;
+ boost::random::weibull_distribution<> restored_dist;
+ stream >> restored_dist;
+ BOOST_CHECK_EQUAL(dist, restored_dist);
+}
+
+BOOST_AUTO_TEST_CASE(test_generation) {
+ boost::minstd_rand0 gen;
+ boost::random::weibull_distribution<> dist;
+ boost::random::weibull_distribution<> dist_two(1.0, 1000000.0);
+ for(int i = 0; i < 10; ++i) {
+ // This test is not guaranteed to work, since
+ // a weibull distribution with a large scale parameter
+ // can produce small values and a distribution with
+ // a small scale can produce large values, but the
+ // chances of failure are small.
+ double value = dist(gen);
+ BOOST_CHECK_GE(value, 0.0);
+ BOOST_CHECK_LE(value, 100.0);
+ double value_two = dist_two(gen);
+ BOOST_CHECK_GE(value_two, 100.0);
+ double value_param = dist_two(gen, dist.param());
+ BOOST_CHECK_GE(value_param, 0);
+ BOOST_CHECK_LE(value_param, 100.0);
+ double value_two_param = dist(gen, dist_two.param());
+ BOOST_CHECK_GE(value_two_param, 100.0);
+ }
+}


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