Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r67669 - in trunk: boost boost/random libs/random/test
From: steven_at_[hidden]
Date: 2011-01-05 00:17:39


Author: steven_watanabe
Date: 2011-01-05 00:17:37 EST (Wed, 05 Jan 2011)
New Revision: 67669
URL: http://svn.boost.org/trac/boost/changeset/67669

Log:
Implement chi_squared_distribution.
Added:
   trunk/boost/random/chi_squared_distribution.hpp (contents, props changed)
   trunk/libs/random/test/test_chi_squared.cpp (contents, props changed)
   trunk/libs/random/test/test_chi_squared_distribution.cpp (contents, props changed)
Text files modified:
   trunk/boost/random.hpp | 1 +
   trunk/libs/random/test/Jamfile.v2 | 2 ++
   2 files changed, 3 insertions(+), 0 deletions(-)

Modified: trunk/boost/random.hpp
==============================================================================
--- trunk/boost/random.hpp (original)
+++ trunk/boost/random.hpp 2011-01-05 00:17:37 EST (Wed, 05 Jan 2011)
@@ -80,6 +80,7 @@
 #include <boost/random/gamma_distribution.hpp>
 #include <boost/random/binomial_distribution.hpp>
 #include <boost/random/negative_binomial_distribution.hpp>
+#include <boost/random/chi_squared_distribution.hpp>
 #include <boost/random/uniform_on_sphere.hpp>
 
 #endif // BOOST_RANDOM_HPP

Added: trunk/boost/random/chi_squared_distribution.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/random/chi_squared_distribution.hpp 2011-01-05 00:17:37 EST (Wed, 05 Jan 2011)
@@ -0,0 +1,209 @@
+/* boost random/chi_squared_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_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
+#define BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
+
+#include <iosfwd>
+#include <boost/limits.hpp>
+
+#include <boost/random/detail/config.hpp>
+#include <boost/random/gamma_distribution.hpp>
+
+namespace boost {
+namespace random {
+
+/**
+ * The chi squared distribution is a real valued distribution with
+ * one parameter, @c n. The distribution produces values > 0.
+ *
+ * The distribution function is
+ * \f$\displaystyle P(x) = \frac{x^{(n/2)-1}e^{-x/2}}{\Gamma(n/2)2^{n/2}}\f$.
+ */
+template<class RealType = double>
+class chi_squared_distribution {
+public:
+ typedef RealType result_type;
+ typedef RealType input_type;
+
+ class param_type {
+ public:
+ typedef chi_squared_distribution distribution_type;
+ /**
+ * Construct a param_type object. @c n
+ * is the parameter of the distribution.
+ *
+ * Requires: t >=0 && 0 <= p <= 1
+ */
+ explicit param_type(RealType n_arg = RealType(1))
+ : _n(n_arg)
+ {}
+ /** Returns the @c n parameter of the distribution. */
+ RealType n() const { return _n; }
+#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
+ /** Writes the parameters of the distribution to a @c std::ostream. */
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os,
+ const param_type& parm)
+ {
+ os << parm._n;
+ return os;
+ }
+
+ /** Reads the parameters of the distribution from a @c std::istream. */
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is, param_type& parm)
+ {
+ is >> parm._n;
+ return is;
+ }
+#endif
+ /** Returns true if the parameters have the same values. */
+ friend bool operator==(const param_type& lhs, const param_type& rhs)
+ {
+ return lhs._n == rhs._n;
+ }
+ /** Returns true if the parameters have different values. */
+ friend bool operator!=(const param_type& lhs, const param_type& rhs)
+ {
+ return !(lhs == rhs);
+ }
+ private:
+ RealType _n;
+ };
+
+ /**
+ * Construct a @c chi_squared_distribution object. @c n
+ * is the parameter of the distribution.
+ *
+ * Requires: t >=0 && 0 <= p <= 1
+ */
+ explicit chi_squared_distribution(RealType n_arg = RealType(1))
+ : _impl(n_arg / 2)
+ {
+ }
+
+ /**
+ * Construct an @c chi_squared_distribution object from the
+ * parameters.
+ */
+ explicit chi_squared_distribution(const param_type& parm)
+ : _impl(parm.n() / 2)
+ {
+ }
+
+ /**
+ * Returns a random variate distributed according to the
+ * chi squared distribution.
+ */
+ template<class URNG>
+ RealType operator()(URNG& urng)
+ {
+ return 2 * _impl(urng);
+ }
+
+ /**
+ * Returns a random variate distributed according to the
+ * chi squared distribution with parameters specified by @c param.
+ */
+ template<class URNG>
+ RealType operator()(URNG& urng, const param_type& parm) const
+ {
+ return chi_squared_distribution(parm)(urng);
+ }
+
+ /** Returns the @c n parameter of the distribution. */
+ RealType n() const { return 2 * _impl.alpha(); }
+
+ /** Returns the smallest value that the distribution can produce. */
+ RealType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 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(n()); }
+ /** Sets parameters of the distribution. */
+ void param(const param_type& parm)
+ {
+ typedef gamma_distribution<RealType> impl_type;
+ typename impl_type::param_type impl_parm(parm.n() / 2);
+ _impl.param(impl_parm);
+ }
+
+ /**
+ * Effects: Subsequent uses of the distribution do not depend
+ * on values produced by any engine prior to invoking reset.
+ */
+ void reset() { _impl.reset(); }
+
+#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
+ /** Writes the parameters of the distribution to a @c std::ostream. */
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os,
+ const chi_squared_distribution& c2d)
+ {
+ os << c2d.param();
+ return os;
+ }
+
+ /** Reads the parameters of the distribution from a @c std::istream. */
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is,
+ chi_squared_distribution& c2d)
+ {
+ c2d.read(is);
+ return is;
+ }
+#endif
+
+ /** Returns true if the two distributions will produce the same
+ sequence of values, given equal generators. */
+ friend bool operator==(const chi_squared_distribution& lhs,
+ const chi_squared_distribution& rhs)
+ {
+ return lhs._impl == rhs._impl;
+ }
+ /** Returns true if the two distributions could produce different
+ sequences of values, given equal generators. */
+ friend bool operator!=(const chi_squared_distribution& lhs,
+ const chi_squared_distribution& rhs)
+ {
+ return !(lhs == rhs);
+ }
+
+private:
+
+ /// @cond
+
+ template<class CharT, class Traits>
+ void read(std::basic_istream<CharT, Traits>& is) {
+ param_type parm;
+ if(is >> parm) {
+ param(parm);
+ }
+ }
+
+ gamma_distribution<RealType> _impl;
+
+ /// @endcond
+};
+
+}
+
+}
+
+#endif

Modified: trunk/libs/random/test/Jamfile.v2
==============================================================================
--- trunk/libs/random/test/Jamfile.v2 (original)
+++ trunk/libs/random/test/Jamfile.v2 2011-01-05 00:17:37 EST (Wed, 05 Jan 2011)
@@ -60,6 +60,8 @@
 run test_extreme_value_distribution.cpp /boost//unit_test_framework ;
 run test_negative_binomial.cpp ;
 run test_negative_binomial_distribution.cpp /boost//unit_test_framework ;
+run test_chi_squared.cpp ;
+run test_chi_squared_distribution.cpp /boost//unit_test_framework ;
 
 # run nondet_random_speed.cpp ;
 # run random_device.cpp ;

Added: trunk/libs/random/test/test_chi_squared.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_chi_squared.cpp 2011-01-05 00:17:37 EST (Wed, 05 Jan 2011)
@@ -0,0 +1,108 @@
+/* test_chi_squared.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/chi_squared_distribution.hpp>
+#include <boost/random/uniform_int.hpp>
+#include <boost/random/uniform_01.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/math/distributions/chi_squared.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 chi_squared(" << n << ")" << " " << max << " times: " << std::flush;
+
+ boost::math::chi_squared expected(n);
+
+ boost::random::chi_squared_distribution<> dist(n);
+ boost::mt19937 gen;
+ kolmogorov_experiment test(max);
+ boost::variate_generator<boost::mt19937&, boost::random::chi_squared_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_chi_squared -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_chi_squared_distribution.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/test/test_chi_squared_distribution.cpp 2011-01-05 00:17:37 EST (Wed, 05 Jan 2011)
@@ -0,0 +1,34 @@
+/* test_chi_squared_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/chi_squared_distribution.hpp>
+#include <limits>
+
+#define BOOST_RANDOM_DISTRIBUTION boost::random::chi_squared_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 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_DIST2_MIN 0
+#define BOOST_RANDOM_DIST2_MAX (std::numeric_limits<double>::infinity)()
+
+#define BOOST_RANDOM_TEST1_PARAMS
+#define BOOST_RANDOM_TEST1_MIN 0.0
+#define BOOST_RANDOM_TEST1_MAX 100.0
+
+#define BOOST_RANDOM_TEST2_PARAMS (10000.0)
+#define BOOST_RANDOM_TEST2_MIN 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