Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r56900 - in sandbox/statistics/importance_sampling/boost/statistics: . detail detail/importance_sampling detail/importance_sampling/random detail/importance_sampling/weights
From: erwann.rogard_at_[hidden]
Date: 2009-10-16 01:11:07


Author: e_r
Date: 2009-10-16 01:11:06 EDT (Fri, 16 Oct 2009)
New Revision: 56900
URL: http://svn.boost.org/trac/boost/changeset/56900

Log:
a
Added:
   sandbox/statistics/importance_sampling/boost/statistics/
   sandbox/statistics/importance_sampling/boost/statistics/detail/
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/generate.hpp (contents, props changed)
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/grid.hpp (contents, props changed)
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/include.hpp (contents, props changed)
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/sampler.hpp (contents, props changed)
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/weights/
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/weights/apply_exp_offset.hpp (contents, props changed)

Added: sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/generate.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/generate.hpp 2009-10-16 01:11:06 EDT (Fri, 16 Oct 2009)
@@ -0,0 +1,69 @@
+///////////////////////////////////////////////////////////////////////////////
+// importance_sampling::generate.hpp //
+// //
+// Copyright 2009 Erwann Rogard. 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_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_GENERATE_HPP_ER_2009
+#define BOOST_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_GENERATE_HPP_ER_2009
+#include <iterator>
+#include <functional>
+#include <boost/iterator/iterator_traits.hpp>
+#include <boost/math/tools/precision.hpp>
+#include <boost/random/variate_generator.hpp>
+#include <boost/utility.hpp>
+#include <boost/statistics/detail/importance_sampling/random/sampler.hpp>
+#include <boost/random/ref_distribution.hpp>
+
+namespace boost{
+namespace statistics{
+namespace detail{
+namespace importance_sampling{
+
+ template<typename ItT,typename N,typename ItW,typename ItP,typename U>
+ ItT generate(
+ ItT b_t, // target values (output)
+ N n, // sample size
+ ItW b_w, // unnormalized weights
+ ItW e_w, // unnormalized weights
+ ItP b_p, // proposal values
+ U& urng
+ )
+ {
+ typedef boost::iterator_range<ItW> range_w_;
+ typedef boost::iterator_range<ItP> range_p_;
+ typedef typename boost::iterator_value<ItW>::type w_;
+ typedef sampler<range_p_,w_> iss_;
+ typedef boost::random::ref_distribution<iss_&> ref_iss_;
+ typedef boost::variate_generator<U&,ref_iss_> gen_iss_;
+
+ range_w_ range_w(b_w,e_w);
+ range_p_ range_p(
+ b_p,
+ boost::next(
+ b_p,
+ std::distance(
+ b_w,
+ e_w
+ )
+ )
+ );
+
+ iss_ iss( range_w, range_p );
+ ref_iss_ ref_iss( iss );
+ gen_iss_ gen_iss( urng, iss );
+
+ return std::generate_n(
+ b_t,
+ n,
+ gen_iss
+ );
+ }
+
+}// importance_sampling
+}// statistics
+}// detail
+}// boost
+
+#endif
\ No newline at end of file

Added: sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/grid.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/grid.hpp 2009-10-16 01:11:06 EDT (Fri, 16 Oct 2009)
@@ -0,0 +1,55 @@
+///////////////////////////////////////////////////////////////////////////////
+// importance_sampling::grid.hpp //
+// //
+// Copyright 2009 Erwann Rogard. 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_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_GRID_HPP_ER_2009
+#define BOOST_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_GRID_HPP_ER_2009
+#include <boost/numeric/conversion/converter.hpp>
+#include <boost/limits.hpp>
+
+namespace boost{
+namespace statistics{
+namespace detail{
+namespace importance_sampling{
+
+// Equally spaced univariate grid.
+template<typename T>
+class grid{
+ public:
+ typedef std::size_t size_type;
+ private:
+ typedef boost::numeric::converter<T,size_type> conv_t;
+ public:
+ typedef T result_type;
+ grid(T min, T max, size_type n);
+ T operator()();
+
+ private:
+ size_type n_;
+ size_type i_;
+ T d_;
+ T x_;
+};
+
+template<typename T>
+grid<T>::grid(T min, T max, size_type n)
+: n_(n), i_(0), d_((max-min)/conv_t::convert(n-1)),x_(min) {}
+
+template<typename T>
+T grid<T>::operator()(){
+ BOOST_ASSERT(i_<n_);
+ T result = x_;
+ x_ += d_;
+ ++i_;
+ return result;
+}
+
+}// importance_sampling
+}// detail
+}// statistics
+}// boost
+
+#endif
\ No newline at end of file

Added: sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/include.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/include.hpp 2009-10-16 01:11:06 EDT (Fri, 16 Oct 2009)
@@ -0,0 +1,15 @@
+///////////////////////////////////////////////////////////////////////////////
+// importance_sampling::random::include.hpp //
+// //
+// Copyright 2009 Erwann Rogard. 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_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_RANDOM_INCLUDE_HPP_ER_2009
+#define BOOST_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_RANDOM_INCLUDE_HPP_ER_2009
+
+#include <boost/statistics/detail/importance_sampling/random/generate.hpp>
+#include <boost/statistics/detail/importance_sampling/random/grid.hpp>
+#include <boost/statistics/detail/importance_sampling/random/sampler.hpp>
+
+#endif
\ No newline at end of file

Added: sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/sampler.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/random/sampler.hpp 2009-10-16 01:11:06 EDT (Fri, 16 Oct 2009)
@@ -0,0 +1,126 @@
+///////////////////////////////////////////////////////////////////////////////
+// importance_sampling::sampler.hpp //
+// //
+// Copyright 2009 Erwann Rogard. 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_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_SAMPLER_HPP_ER_2009
+#define BOOST_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_SAMPLER_HPP_ER_2009
+#include <vector>
+#include <boost/mpl/not.hpp>
+#include <boost/mpl/assert.hpp>
+#include <boost/call_traits.hpp>
+#include <boost/type_traits.hpp>
+#include <boost/utility.hpp>
+#include <boost/range.hpp>
+#include <boost/random/variate_generator.hpp>
+#include <boost/random/uniform_real.hpp>
+// TODO #include <boost/random/discrete_distributionhpp> when becomes avail
+#include <boost/random/discrete_distribution_sw_2009.hpp>
+
+namespace boost{
+namespace statistics{
+namespace detail{
+namespace importance_sampling{
+
+// Samples by SIR given a set of proposal values and their unnormalized weights
+//
+// Models RandomDistribution
+//
+// R1: type of a range values
+// W: type of each weight
+template<typename R1,typename W>
+class sampler{
+ typedef typename remove_reference<R1>::type const_values_;
+ typedef typename remove_cv<const_values_>::type values_t;
+ typedef typename range_size<values_t>::type size_;
+ typedef is_reference<R1> is_ref_;
+ public:
+ typedef typename range_value<const_values_>::type result_type;
+ private:
+ typedef random::discrete_distribution<size_,W> discr_dist_t;
+ public:
+ typedef typename discr_dist_t::input_type input_type;
+
+ sampler(){}
+ template<typename R0>
+ sampler(
+ const R0& unnormalized_weights,
+ typename call_traits<R1>::param_type values
+ ):discr_dist_(
+ boost::begin(unnormalized_weights),
+ boost::end(unnormalized_weights)
+ ),values_(values){
+ BOOST_ASSERT(
+ boost::size(unnormalized_weights) == boost::size(this->values())
+ );
+ }
+
+ sampler(const sampler& that)
+ :discr_dist_(that.discr_dist_),values_(that.values_){}
+
+ sampler& operator=(const sampler& that)
+ {
+ if(&that!=this){
+ discr_dist_ = that.discr_dist_;
+ values_ = that.values_;
+ }
+ return (*this);
+ }
+
+ template<typename U>
+ result_type operator()(U& urng)const
+ {
+ typedef typename discr_dist_t::result_type k_t;
+ k_t k = discr_dist_(urng);
+ BOOST_ASSERT( k < boost::size(this->values()) );
+ return (*boost::next(boost::begin(this->values()),k));
+ }
+ const discr_dist_t& discrete_distribution()const
+ {
+ return this->discr_dist_;
+ }
+
+ // TODO os/is
+
+ typename call_traits<R1>::const_reference values()const{
+ return this->values_;
+ }
+
+ private:
+ discr_dist_t discr_dist_;
+ typename call_traits<R1>::value_type values_;
+};
+
+ template<typename R0,typename R1>
+ sampler<
+ R0,
+ typename remove_cv<
+ typename remove_reference<
+ typename range_value<R0>::type
+ >::type
+ >::type
+ >
+ make_sampler(
+ const R0& unnormalized_weights,
+ typename call_traits<R1>::param_type values
+ )
+ {
+ typedef sampler<
+ R0,
+ typename remove_cv<
+ typename remove_reference<
+ typename range_value<R0>::type
+ >::type
+ >::type
+ > result_;
+ return result_(unnormalized_weights,values);
+ }
+
+}// importance_sampling
+}// detail
+}// statistics
+}// boost
+
+#endif
\ No newline at end of file

Added: sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/weights/apply_exp_offset.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/weights/apply_exp_offset.hpp 2009-10-16 01:11:06 EDT (Fri, 16 Oct 2009)
@@ -0,0 +1,64 @@
+///////////////////////////////////////////////////////////////////////////////
+// importance_sampling::apply_exp_offset //
+// //
+// Copyright 2009 Erwann Rogard. 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_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_WEIGHTS_APPLY_EXP_OFFSET_HPP_ER_2009
+#define BOOST_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_WEIGHTS_APPLY_EXP_OFFSET_HPP_ER_2009
+#include <cmath>
+#include <algorithm>
+#include <boost/lambda/lambda.hpp>
+#include <boost/lambda/bind.hpp>
+#include <boost/math/tools/precision.hpp>
+
+namespace boost{
+namespace statistics{
+namespace detail{
+namespace importance_sampling{
+
+ // Returns The smallest value, c, s/t *i + c <= t, i in [b_w,e_w)
+ // Side effect: *i <- exp(*i+c)
+ //
+ // The greater c, the higher the precision but also risk of isinf.
+ // Textbook often show t = 0 so that exp(w+c) <= 1.
+ template<typename It>
+ typename iterator_value<It>::type
+ apply_exp_offset(
+ It b,
+ It e,
+ typename iterator_value<It>::type t
+ ){
+ typedef typename iterator_value<It>::type val_;
+ val_ max = *std::max_element(b,e);
+ val_ offset = (t - max);
+ std::transform(b,e,b,
+ lambda::bind<val_>(exp,lambda::_1 + offset)
+ );
+ return offset;
+ }
+
+ // Same as above, but t set such that exp(t+epsilon) = inf, exp(t)<inf
+ template<typename It>
+ typename iterator_value<It>::type
+ apply_exp_offset(
+ It b,
+ It e
+ ){
+ typedef typename iterator_value<It>::type val_;
+ const val_ log_max = boost::math::tools::log_max_value<val_>();
+
+ return apply_exp_offset(
+ b,
+ e,
+ log_max
+ );
+ }
+
+}// importance_weights
+}// detail
+}// statistics
+}// boost
+
+#endif
\ No newline at end of file


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