Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r55645 - in sandbox/statistics/random: boost/random libs/random/example libs/random/src
From: erwann.rogard_at_[hidden]
Date: 2009-08-17 22:27:39


Author: e_r
Date: 2009-08-17 22:27:38 EDT (Mon, 17 Aug 2009)
New Revision: 55645
URL: http://svn.boost.org/trac/boost/changeset/55645

Log:
minor changes to /statistics
Added:
   sandbox/statistics/random/boost/random/discrete_distribution_sw_2009.hpp (contents, props changed)
   sandbox/statistics/random/libs/random/example/
   sandbox/statistics/random/libs/random/example/categorical.cpp (contents, props changed)
   sandbox/statistics/random/libs/random/example/categorical.h (contents, props changed)
Text files modified:
   sandbox/statistics/random/boost/random/categorical_distribution.hpp | 2 ++
   sandbox/statistics/random/boost/random/students_t.hpp | 5 +++--
   sandbox/statistics/random/libs/random/src/main.cpp | 8 +++++---
   3 files changed, 10 insertions(+), 5 deletions(-)

Modified: sandbox/statistics/random/boost/random/categorical_distribution.hpp
==============================================================================
--- sandbox/statistics/random/boost/random/categorical_distribution.hpp (original)
+++ sandbox/statistics/random/boost/random/categorical_distribution.hpp 2009-08-17 22:27:38 EDT (Mon, 17 Aug 2009)
@@ -20,6 +20,8 @@
 namespace boost{
 namespace random{
 
+// See alternate implementation : discrete_distribution
+//
 // Usage:
 // typedef categorical_distribution<> rmult_;
 // typedef rmult_::value_type value_type;

Added: sandbox/statistics/random/boost/random/discrete_distribution_sw_2009.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/random/boost/random/discrete_distribution_sw_2009.hpp 2009-08-17 22:27:38 EDT (Mon, 17 Aug 2009)
@@ -0,0 +1,98 @@
+// discrete_distribution.hpp
+//
+// Copyright (c) 2009
+// Steven Watanabe
+//
+// 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)
+
+#ifndef BOOST_RANDOM_DISCRETE_DISTRIBUTION_HPP_INCLUDED
+#define BOOST_RANDOM_DISCRETE_DISTRIBUTION_HPP_INCLUDED
+
+#include <vector>
+#include <cassert>
+#include <limits>
+#include <numeric>
+#include <utility>
+
+#include <boost/range.hpp>
+#include <boost/random/uniform_int.hpp>
+#include <boost/random/uniform_01.hpp>
+#include <boost/random/variate_generator.hpp>
+
+namespace boost {
+namespace random {
+
+template<class IntType, class WeightType>
+class discrete_distribution {
+public:
+ typedef WeightType input_type;
+ typedef IntType result_type;
+
+ template<class Iter>
+ discrete_distribution(Iter begin, Iter end) : weights(begin, end), data(weights.size()) {
+ std::size_t size = weights.size();
+ //assert(size <= (std::numeric_limits<IntType>::max)());
+ std::vector<std::pair<WeightType, IntType> > below_average;
+ std::vector<std::pair<WeightType, IntType> > above_average;
+ WeightType weight_sum = std::accumulate(weights.begin(), weights.end(), static_cast<WeightType>(0));
+ WeightType weight_average = weight_sum / size;
+ for(std::size_t i = 0; i < size; ++i) {
+ if(weights[i] < weight_average) {
+ below_average.push_back(std::make_pair(weights[i] / weight_average, static_cast<IntType>(i)));
+ } else {
+ above_average.push_back(std::make_pair(weights[i] / weight_average, static_cast<IntType>(i)));
+ }
+ }
+ typedef typename range_iterator<
+ std::vector<
+ std::pair<WeightType, IntType>
+ >
+ >::type iter_;
+ iter_ b_iter = below_average.begin();
+ iter_ b_end = below_average.end();
+ iter_ a_iter = above_average.begin();
+ iter_ a_end = above_average.end();
+ while(b_iter != b_end && a_iter != a_end) {
+ data[b_iter->second] = std::make_pair(b_iter->first, a_iter->second);
+ a_iter->first -= (1 - b_iter->first);
+ if(a_iter->first < 1) {
+ *b_iter = *a_iter++;
+ } else {
+ ++b_iter;
+ }
+ }
+ for(; b_iter != b_end; ++b_iter) {
+ data[b_iter->second].first = 1;
+ }
+ for(; a_iter != a_end; ++a_iter) {
+ data[a_iter->second].first = 1;
+ }
+ }
+ template<class Engine>
+ IntType operator()(Engine& eng) const {
+ assert(!data.empty());
+ boost::variate_generator<Engine&, boost::uniform_01<WeightType> > real_gen(eng, boost::uniform_01<WeightType>());
+ WeightType test = real_gen() * data.size();
+ IntType result = static_cast<IntType>(test);
+ if(test - result < data[result].first) {
+ return result;
+ } else {
+ return(data[result].second);
+ }
+ }
+
+ result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
+ result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return static_cast<result_type>(weights.size() - 1); }
+private:
+ std::vector<WeightType> weights;
+ std::vector<std::pair<WeightType, IntType> > data;
+};
+
+}
+}
+
+#endif
+
+

Modified: sandbox/statistics/random/boost/random/students_t.hpp
==============================================================================
--- sandbox/statistics/random/boost/random/students_t.hpp (original)
+++ sandbox/statistics/random/boost/random/students_t.hpp 2009-08-17 22:27:38 EDT (Mon, 17 Aug 2009)
@@ -1,5 +1,5 @@
 //////////////////////////////////////////////////////////////////////////////
-// random::students_t_distribution.hpp //
+// random::students_t_distribution.hpp //
 // //
 // //
 // (C) Copyright 2009 Erwann Rogard //
@@ -16,10 +16,11 @@
 #include <boost/random/variate_generator.hpp>
 #include <boost/random/normal_distribution.hpp>
 #include <boost/random/chi_squared.hpp>
+
 namespace boost{
 namespace random{
 
- // Samples from a location-scale distribution
+ // Samples from a students_t distribution
     template<typename T>
     class students_t_distribution{
             typedef boost::normal_distribution<T> nd_;

Added: sandbox/statistics/random/libs/random/example/categorical.cpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/random/libs/random/example/categorical.cpp 2009-08-17 22:27:38 EDT (Mon, 17 Aug 2009)
@@ -0,0 +1,81 @@
+#include <algorithm>
+#include <boost/timer.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/chi_squared.hpp>
+#include <boost/random/variate_generator.hpp>
+#include <boost/random/categorical_distribution.hpp>
+#include <boost/random/discrete_distribution_sw_2009.hpp>
+#include <boost/binary_op/algorithm/sort_on_head.hpp>
+
+void example_categorical(std::ostream& out){
+
+ using namespace boost;
+ typedef double val_;
+ typedef std::vector<val_> vals_;
+
+ typedef mt19937 urng_;
+ typedef boost::random::chi_squared_distribution<val_> rdist_;
+ typedef boost::variate_generator<urng_&,rdist_> vg_;
+ typedef boost::random::categorical_distribution<> cat_dist_;
+ typedef range_size<vals_>::type size_;
+ typedef boost::random::discrete_distribution<
+ cat_dist_::result_type,
+ val_
+ > discr_dist_;
+
+ const val_ df = 4.0;
+ const size_ n = 1e4;
+ const size_ m = 1e5;
+
+ vals_ weights; weights.reserve(n);
+ vals_ values(n);
+ urng_ urng;
+ rdist_ rdist( df );
+ vg_ vg( urng, rdist);
+ std::generate_n(
+ std::back_inserter(weights),
+ n,
+ vg
+ );
+ vals_ sorted_weights = weights;
+
+ timer t;
+
+ out << ( format("--initialize, n = %1%")%n ).str() << std::endl;
+ out << "categorical : ";
+ t.restart();
+ binary_op::sort_on_head_greater(
+ boost::begin(weights),
+ boost::end(weights),
+ boost::begin(values)
+ );
+ cat_dist_ cat_dist(sorted_weights);
+ out << (format("t = %1%")%t.elapsed()).str();
+
+ t.restart();
+ out << std::endl << "discrete : ";
+ discr_dist_ discr(
+ boost::begin(weights),
+ boost::end(weights)
+ );
+ out << (format("t = %1%")%t.elapsed()).str();
+
+ out << std::endl << ( format("--sample, m = %1%")%m ).str() << std::endl;
+ out << "categorical : ";
+ t.restart();
+ for(unsigned i = 0; i<m; i++){
+ cat_dist(urng);
+ }
+ out << (format("t = %1%")%t.elapsed()).str();
+
+ out << std::endl << "discrete : ";
+ t.restart();
+ for(unsigned i = 0; i<m; i++){
+ cat_dist(urng);
+ }
+ out << (format("t = %1%")%t.elapsed()).str();
+
+
+}
+
+

Added: sandbox/statistics/random/libs/random/example/categorical.h
==============================================================================
--- (empty file)
+++ sandbox/statistics/random/libs/random/example/categorical.h 2009-08-17 22:27:38 EDT (Mon, 17 Aug 2009)
@@ -0,0 +1,17 @@
+//////////////////////////////////////////////////////////////////////////////
+// random::students_t_distribution.hpp //
+// //
+// //
+// (C) Copyright 2009 Erwann Rogard //
+// Use, modification and distribution are subject to 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) //
+//////////////////////////////////////////////////////////////////////////////
+#ifndef LIBS_RANDOM_EXAMPLE_CATEGORICAL_HPP_ER_2009
+#define LIBS_RANDOM_EXAMPLE_CATEGORICAL_HPP_ER_2009
+#include <iostream>
+
+void example_categorical(std::ostream& out);
+
+#endif
+

Modified: sandbox/statistics/random/libs/random/src/main.cpp
==============================================================================
--- sandbox/statistics/random/libs/random/src/main.cpp (original)
+++ sandbox/statistics/random/libs/random/src/main.cpp 2009-08-17 22:27:38 EDT (Mon, 17 Aug 2009)
@@ -1,10 +1,12 @@
-
-#include <boost/random/include.hpp>
+#include <iostream>
+#include <libs/random/example/categorical.h>
 
 int main(){
 
- // Examples can be found in
+ // Other examples are in
     // sandbox/statistics/dist_random/libs/dist_random/example
+
+ example_categorical(std::cout);
     
     return 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