2
0
mirror of https://github.com/boostorg/random.git synced 2026-01-19 04:22:17 +00:00

Update exponential_distribution to C++0x.

[SVN r68372]
This commit is contained in:
Steven Watanabe
2011-01-22 21:44:25 +00:00
parent 9b72a50702
commit 28d9e4da2b
5 changed files with 347 additions and 44 deletions

View File

@@ -1,6 +1,7 @@
/* boost random/exponential_distribution.hpp header file
*
* Copyright Jens Maurer 2000-2001
* 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)
@@ -18,16 +19,18 @@
#include <boost/config/no_tr1/cmath.hpp>
#include <cassert>
#include <iostream>
#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 {
/**
* The exponential distribution has a single parameter lambda.
* The exponential distribution is a model of \random_distribution with
* a single parameter lambda.
*
* It has \f$\displaystyle p(x) = \lambda e^{-\lambda x}\f$
*/
@@ -35,60 +38,147 @@ template<class RealType = double>
class exponential_distribution
{
public:
typedef RealType input_type;
typedef RealType result_type;
typedef RealType input_type;
typedef RealType result_type;
#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
#endif
class param_type
{
public:
explicit exponential_distribution(result_type lambda_arg = result_type(1.0))
: _lambda(lambda_arg) { assert(_lambda > result_type(0)); }
typedef exponential_distribution distribution_type;
// compiler-generated copy ctor and assignment operator are fine
/**
* Constructs parameters with a given lambda.
*
* Requires: lambda > 0
*/
param_type(RealType lambda_arg = RealType(1.0))
: _lambda(lambda_arg) { assert(_lambda > RealType(0)); }
result_type lambda() const { return _lambda; }
/** Returns the lambda parameter of the distribution. */
RealType lambda() const { return _lambda; }
void reset() { }
/** Writes the parameters to a @c std::ostream. */
BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
{
os << parm._lambda;
return os;
}
/** Reads the parameters from a @c std::istream. */
BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
{
is >> parm._lambda;
return is;
}
template<class Engine>
result_type operator()(Engine& eng)
{
#ifndef BOOST_NO_STDC_NAMESPACE
using std::log;
#endif
return -result_type(1) /
_lambda * log(result_type(1)-uniform_01<RealType>()(eng));
}
/** Returns true if the two sets of parameters are equal. */
BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
{ return lhs._lambda == rhs._lambda; }
#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 exponential_distribution& ed)
{
os << ed._lambda;
return os;
}
/** Returns true if the two sets of parameters are different. */
BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
template<class CharT, class Traits>
friend std::basic_istream<CharT,Traits>&
operator>>(std::basic_istream<CharT,Traits>& is, exponential_distribution& ed)
{
is >> std::ws >> ed._lambda;
return is;
}
#endif
private:
RealType _lambda;
};
friend bool operator==(const exponential_distribution& lhs,
const exponential_distribution& rhs)
{
return lhs._lambda == rhs._lambda;
}
/**
* Constructs an exponential_distribution with a given lambda.
*
* Requires: lambda > 0
*/
explicit exponential_distribution(RealType lambda_arg = RealType(1.0))
: _lambda(lambda_arg) { assert(_lambda > RealType(0)); }
/**
* Constructs an exponential_distribution from its parameters
*/
explicit exponential_distribution(const param_type& parm)
: _lambda(parm.lambda()) {}
// compiler-generated copy ctor and assignment operator are fine
/** Returns the lambda parameter of the distribution. */
RealType lambda() const { return _lambda; }
/** Returns the smallest value that the distribution can produce. */
RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
{ return RealType(0); }
/** Returns the largest value that the distribution can produce. */
RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
{ return (std::numeric_limits<RealType>::infinity)(); }
/** Returns the parameters of the distribution. */
param_type param() const { return param_type(_lambda); }
/** Sets the parameters of the distribution. */
void param(const param_type& parm) { _lambda = parm.lambda(); }
/**
* Effects: Subsequent uses of the distribution do not depend
* on values produced by any engine prior to invoking reset.
*/
void reset() { }
/**
* Returns a random variate distributed according to the
* exponential distribution.
*/
template<class Engine>
result_type operator()(Engine& eng) const
{
using std::log;
return -result_type(1) /
_lambda * log(result_type(1)-uniform_01<RealType>()(eng));
}
/**
* Returns a random variate distributed according to the exponential
* distribution with parameters specified by param.
*/
template<class Engine>
result_type operator()(Engine& eng, const param_type& parm) const
{
return exponential_distribution(parm)(eng);
}
/** Writes the distribution to a std::ostream. */
BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, exponential_distribution, ed)
{
os << ed._lambda;
return os;
}
/** Reads the distribution from a std::istream. */
BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, exponential_distribution, ed)
{
is >> ed._lambda;
return is;
}
/**
* Returns true iff the two distributions will produce identical
* sequences of values given equal generators.
*/
BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(exponential_distribution, lhs, rhs)
{ return lhs._lambda == rhs._lambda; }
/**
* Returns true iff the two distributions will produce different
* sequences of values given equal generators.
*/
BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(exponential_distribution)
private:
result_type _lambda;
/// \cond
result_type _lambda;
/// \endcond
};
} // namespace random
using random::exponential_distribution;
} // namespace boost
#endif // BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP

View File

@@ -73,6 +73,8 @@ run test_piecewise_constant.cpp ;
run test_piecewise_constant_distribution.cpp /boost//unit_test_framework ;
run test_piecewise_linear.cpp ;
run test_piecewise_linear_distribution.cpp /boost//unit_test_framework ;
run test_exponential.cpp ;
run test_exponential_distribution.cpp /boost//unit_test_framework ;
# run nondet_random_speed.cpp ;
# run random_device.cpp ;

24
test/test_exponential.cpp Normal file
View File

@@ -0,0 +1,24 @@
/* test_exponential.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/exponential_distribution.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/math/distributions/exponential.hpp>
#define BOOST_RANDOM_DISTRIBUTION boost::random::exponential_distribution<>
#define BOOST_RANDOM_DISTRIBUTION_NAME exponential
#define BOOST_MATH_DISTRIBUTION boost::math::exponential
#define BOOST_RANDOM_ARG1_TYPE double
#define BOOST_RANDOM_ARG1_NAME lambda
#define BOOST_RANDOM_ARG1_DEFAULT 1000.0
#define BOOST_RANDOM_ARG1_DISTRIBUTION(n) boost::uniform_real<>(0.0001, n)
#include "test_real_distribution.ipp"

View File

@@ -0,0 +1,32 @@
/* test_exponential_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/exponential_distribution.hpp>
#include <limits>
#define BOOST_RANDOM_DISTRIBUTION boost::random::exponential_distribution<>
#define BOOST_RANDOM_ARG1 lambda
#define BOOST_RANDOM_ARG1_DEFAULT 1.0
#define BOOST_RANDOM_ARG1_VALUE 7.5
#define BOOST_RANDOM_DIST0_MIN 0
#define BOOST_RANDOM_DIST0_MAX (std::numeric_limits<double>::infinity)()
#define BOOST_RANDOM_DIST1_MIN 0
#define BOOST_RANDOM_DIST1_MAX (std::numeric_limits<double>::infinity)()
#define BOOST_RANDOM_TEST1_PARAMS
#define BOOST_RANDOM_TEST1_MIN 0.0
#define BOOST_RANDOM_TEST2_PARAMS (1000.0)
#define BOOST_RANDOM_TEST2_MIN 0.0
#include "test_distribution.ipp"

View File

@@ -0,0 +1,155 @@
/* test_real_distribution.ipp
*
* 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$
*
*/
#ifndef BOOST_MATH_DISTRIBUTION_INIT
#ifdef BOOST_RANDOM_ARG2_TYPE
#define BOOST_MATH_DISTRIBUTION_INIT (BOOST_RANDOM_ARG1_NAME, BOOST_RANDOM_ARG2_NAME)
#else
#define BOOST_MATH_DISTRIBUTION_INIT (BOOST_RANDOM_ARG1_NAME)
#endif
#endif
#include <boost/random/mersenne_twister.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <iostream>
#include "statistic_tests.hpp"
bool do_test(BOOST_RANDOM_ARG1_TYPE BOOST_RANDOM_ARG1_NAME,
#ifdef BOOST_RANDOM_ARG2_TYPE
BOOST_RANDOM_ARG2_TYPE BOOST_RANDOM_ARG2_NAME,
#endif
int max) {
std::cout << "running " BOOST_PP_STRINGIZE(BOOST_RANDOM_DISTRIBUTION_NAME) "("
<< BOOST_RANDOM_ARG1_NAME;
#ifdef BOOST_RANDOM_ARG2_NAME
std::cout << ", " << BOOST_RANDOM_ARG2_NAME;
#endif
std::cout << ")" << " " << max << " times: " << std::flush;
BOOST_MATH_DISTRIBUTION expected BOOST_MATH_DISTRIBUTION_INIT;
BOOST_RANDOM_DISTRIBUTION dist(BOOST_RANDOM_ARG1_NAME
#ifdef BOOST_RANDOM_ARG2_NAME
, BOOST_RANDOM_ARG2_NAME
#endif
);
boost::mt19937 gen;
kolmogorov_experiment test(max);
boost::variate_generator<boost::mt19937&, BOOST_RANDOM_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;
}
template<class Dist1
#ifdef BOOST_RANDOM_ARG2_NAME
, class Dist2
#endif
>
bool do_tests(int repeat, Dist1 d1,
#ifdef BOOST_RANDOM_ARG2_NAME
Dist2 d2,
#endif
int trials) {
boost::mt19937 gen;
int errors = 0;
for(int i = 0; i < repeat; ++i) {
if(!do_test(d1(gen),
#ifdef BOOST_RANDOM_ARG2_NAME
d2(gen),
#endif
trials)) {
++errors;
}
}
if(errors != 0) {
std::cout << "*** " << errors << " errors detected ***" << std::endl;
}
return errors == 0;
}
int usage() {
std::cerr << "Usage: test_" BOOST_PP_STRINGIZE(BOOST_RANDOM_DISTRIBUTION_NAME)
" -r <repeat>"
" -" BOOST_PP_STRINGIZE(BOOST_RANDOM_ARG1_NAME)
" <max " BOOST_PP_STRINGIZE(BOOST_RANDOM_ARG1_NAME) ">"
#ifdef BOOST_RANDOM_ARG2_NAME
" -" BOOST_PP_STRINGIZE(BOOST_RANDOM_ARG2_NAME)
" <max " BOOST_PP_STRINGIZE(BOOST_RANDOM_ARG2_NAME) ">"
#endif
" -t <trials>" << std::endl;
return 2;
}
template<class T>
bool handle_option(int& argc, char**& argv, const char* opt, T& value) {
if(std::strcmp(argv[0], opt) == 0 && 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;
BOOST_RANDOM_ARG1_TYPE max_arg1 = BOOST_RANDOM_ARG1_DEFAULT;
#ifdef BOOST_RANDOM_ARG2_TYPE
BOOST_RANDOM_ARG2_TYPE max_arg2 = BOOST_RANDOM_ARG2_DEFAULT;
#endif
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, "-" BOOST_PP_STRINGIZE(BOOST_RANDOM_ARG1_NAME), max_arg1)
#ifdef BOOST_RANDOM_ARG2_TYPE
&& !handle_option(argc, argv, "-" BOOST_PP_STRINGIZE(BOOST_RANDOM_ARG2_NAME), max_arg2)
#endif
&& !handle_option(argc, argv, "-t", trials)) {
return usage();
}
--argc;
++argv;
}
try {
if(do_tests(repeat,
BOOST_RANDOM_ARG1_DISTRIBUTION(max_arg1),
#ifdef BOOST_RANDOM_ARG2_TYPE
BOOST_RANDOM_ARG2_DISTRIBUTION(max_arg2),
#endif
trials)) {
return 0;
} else {
return EXIT_FAILURE;
}
} catch(...) {
std::cerr << boost::current_exception_diagnostic_information() << std::endl;
return EXIT_FAILURE;
}
}