Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57322 - in sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling: statistics weights
From: erwann.rogard_at_[hidden]
Date: 2009-11-03 11:40:34


Author: e_r
Date: 2009-11-03 11:40:33 EST (Tue, 03 Nov 2009)
New Revision: 57322
URL: http://svn.boost.org/trac/boost/changeset/57322

Log:
a/m
Added:
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/statistics/
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/statistics/percentage_effective_sample_size.hpp (contents, props changed)
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/statistics/variance_of_mean_normalized.hpp (contents, props changed)
Text files modified:
   sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/weights/prepare_weights.hpp | 128 ++++++++++++---------------------------
   1 files changed, 40 insertions(+), 88 deletions(-)

Added: sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/statistics/percentage_effective_sample_size.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/statistics/percentage_effective_sample_size.hpp 2009-11-03 11:40:33 EST (Tue, 03 Nov 2009)
@@ -0,0 +1,100 @@
+///////////////////////////////////////////////////////////////////////////////
+// accumulator::statistics::percentage_effective_sample_size.hpp //
+// //
+// Copyright 2008 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_STATISTICS_PERCENTAGE_EFFECTIVE_SAMPLE_SIZE_HPP_ER_2009
+#define BOOST_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_STATISTICS_PERCENTAGE_EFFECTIVE_SAMPLE_SIZE_HPP_ER_2009
+#include <boost/parameter/binding.hpp>
+#include <boost/mpl/placeholders.hpp>
+#include <boost/mpl/apply.hpp>
+#include <boost/accumulators/framework/extractor.hpp>
+#include <boost/accumulators/framework/accumulator_base.hpp>
+#include <boost/accumulators/framework/parameters/sample.hpp>
+#include <boost/accumulators/framework/parameters/accumulator.hpp>
+#include <boost/statistics/detail/importance_sampling/statistics/variance_of_mean_normalized.hpp>
+#include <boost/accumulators/framework/depends_on.hpp>
+
+namespace boost {
+namespace statistics{
+namespace detail{
+namespace accumulator{
+namespace impl
+{
+
+ // Var(w/c) = Var(w) / c^2, where c = mean(w)
+ template<typename T>
+ class percentage_effective_sample_size
+ : public boost::accumulators::accumulator_base
+ {
+ typedef boost::accumulators::dont_care dont_care_;
+
+ typedef tag::variance_of_mean_normalized tag_vmn_;
+ typedef boost::accumulators::tag::accumulator tag_acc_;
+
+ public:
+ typedef T result_type;
+ percentage_effective_sample_size(){}
+ percentage_effective_sample_size(dont_care_){}
+ void operator()(dont_care_)const{}
+
+ template<typename Args>
+ result_type result(const Args& args) const
+ {
+
+ typedef
+ typename boost::parameter::binding<Args,tag_acc_>::type cref_;
+ cref_ acc = args[boost::accumulators::accumulator];
+
+ T vmn = accumulators::extract_result<tag_vmn_>(acc);
+ return one / (one+vmn);
+ }
+
+ static const T one;
+ };
+
+ template<typename T>
+ const T percentage_effective_sample_size<T>::one = static_cast<T>(1);
+
+}//impl
+
+///////////////////////////////////////////////////////////////////////////////
+// tag::percentage_effective_sample_size
+namespace tag
+{
+ struct percentage_effective_sample_size
+ : boost::accumulators::depends_on<tag::variance_of_mean_normalized>
+ {
+ typedef statistics::detail::accumulator::impl
+ ::percentage_effective_sample_size<boost::mpl::_1> impl;
+ };
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// extract::percentage_effective_sample_size
+namespace extract
+{
+
+ template<typename AccSet>
+ typename
+ boost::mpl::apply<
+ AccSet,
+ tag::percentage_effective_sample_size
+ >::type::result_type
+ percentage_effective_sample_size(AccSet const& acc){
+ typedef tag::percentage_effective_sample_size the_tag;
+ return boost::accumulators::extract_result<the_tag>(acc);
+ }
+
+}
+
+using extract::percentage_effective_sample_size;
+
+}// accumulator
+}// detail
+}// statistics
+}// boost
+
+#endif
\ No newline at end of file

Added: sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/statistics/variance_of_mean_normalized.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/statistics/variance_of_mean_normalized.hpp 2009-11-03 11:40:33 EST (Tue, 03 Nov 2009)
@@ -0,0 +1,102 @@
+///////////////////////////////////////////////////////////////////////////////
+// accumulator::statistics::variance_of_mean_normalized.hpp //
+// //
+// Copyright 2008 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_STATISTICS_VARIANCE_OF_MEAN_NORMALIZED_HPP_ER_2009
+#define BOOST_STATISTICS_DETAIL_IMPORTANCE_SAMPLING_STATISTICS_VARIANCE_OF_MEAN_NORMALIZED_HPP_ER_2009
+#include <boost/parameter/binding.hpp>
+#include <boost/mpl/placeholders.hpp>
+#include <boost/mpl/apply.hpp>
+#include <boost/accumulators/framework/extractor.hpp>
+#include <boost/accumulators/framework/accumulator_base.hpp>
+#include <boost/accumulators/framework/parameters/sample.hpp>
+#include <boost/accumulators/framework/parameters/accumulator.hpp>
+#include <boost/accumulators/framework/depends_on.hpp>
+#include <boost/accumulators/statistics_fwd.hpp>
+#include <boost/accumulators/statistics/mean.hpp>
+#include <boost/accumulators/statistics/variance.hpp>
+
+namespace boost {
+namespace statistics{
+namespace detail{
+namespace accumulator{
+
+namespace impl
+{
+
+ // Var(w/c) = Var(w) / c^2, where c = mean(w)
+ template<typename T>
+ class variance_of_mean_normalized
+ : public boost::accumulators::accumulator_base{
+ typedef boost::accumulators::dont_care dont_care_;
+
+ typedef boost::accumulators::tag::variance tag_v_;
+ typedef boost::accumulators::tag::mean tag_m_;
+ typedef boost::accumulators::tag::accumulator tag_acc_;
+
+ public:
+ typedef T result_type;
+ variance_of_mean_normalized(){}
+ variance_of_mean_normalized(dont_care_){}
+ void operator()(dont_care_)const{}
+
+ template<typename Args>
+ result_type result(const Args& args) const
+ {
+
+ typedef
+ typename boost::parameter::binding<Args,tag_acc_>::type cref_;
+ cref_ acc = args[boost::accumulators::accumulator];
+
+ T v = accumulators::extract_result<tag_v_>(acc);
+ T c = accumulators::extract_result<tag_m_>(acc);
+ return v / (c*c);
+ }
+ };
+
+}//impl
+
+///////////////////////////////////////////////////////////////////////////////
+// tag::variance_of_mean_normalized
+namespace tag
+{
+ struct variance_of_mean_normalized
+ : boost::accumulators::depends_on<
+ boost::accumulators::tag::mean,
+ boost::accumulators::tag::variance
+ >
+ {
+ typedef impl::variance_of_mean_normalized<boost::mpl::_1> impl;
+
+ };
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// extract::variance_of_mean_normalized
+namespace extract
+{
+
+ template<typename AccSet>
+ typename
+ boost::mpl::apply<
+ AccSet,
+ tag::variance_of_mean_normalized
+ >::type::result_type
+ variance_of_mean_normalized(AccSet const& acc){
+ typedef tag::variance_of_mean_normalized the_tag;
+ return boost::accumulators::extract_result<the_tag>(acc);
+ }
+
+}
+
+using extract::variance_of_mean_normalized;
+
+}// accumulator
+}// detail
+}// statistics
+}// boost
+
+#endif
\ No newline at end of file

Modified: sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/weights/prepare_weights.hpp
==============================================================================
--- sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/weights/prepare_weights.hpp (original)
+++ sandbox/statistics/importance_sampling/boost/statistics/detail/importance_sampling/weights/prepare_weights.hpp 2009-11-03 11:40:33 EST (Tue, 03 Nov 2009)
@@ -11,7 +11,8 @@
 #include <functional>
 #include <boost/format.hpp>
 #include <boost/lambda/lambda.hpp>
-#include <boost/math/tools/precision.hpp>
+#include <boost/iterator/filter_iterator.hpp>
+#include <boost/iterator/iterator_traits.hpp>
 #include <boost/statistics/detail/importance_sampling/weights/apply_exp_offset.hpp>
 #include <boost/statistics/detail/importance_sampling/weights/scale_to_finite_sum.hpp>
 #include <boost/statistics/detail/importance_sampling/weights/effective_sample_size.hpp>
@@ -28,130 +29,81 @@
         typedef T value_type;
         typedef std::size_t size_type;
 
- prepare_weights();
- prepare_weights(value_type max_log);
+ prepare_weights()
+ :max_log_(default_max_log),offset_(zero),scaling_factor_(zero){}
+
+ prepare_weights(value_type max_log)
+ :max_log_(max_log),offset_(zero),scaling_factor_(zero){}
+
         // Default copy/assign
 
         // [ Input ]
         // max_log controls precision hence raising it should decr pc_lt_eps
         // but also incr risk that cum_sum isinf.
- value_type max_log;
+ value_type max_log()const{ return this->max_log_; }
  
         // [ Output ]
- value_type offset; // lw <- lw + offset, max{lw}+offset = max_log
- value_type scaling_factor;// w <- w/c such that sum{w/c}<inf
- value_type pc_ess; // pc effective sample size
- value_type pc_lt_eps; // pc w<eps
+ // lw <- lw + offset, max{lw}+offset = max_log
+ value_type offset()const{ return this->offset_; }
+ // w <- w/c such that sum{w/c}<inf
+ value_type scaling_factor()const{ return this->scaling_factor_; }
         
         // [ Side effect ]
         // 1) w <- exp(lw+offset)
         // 2) if needed, w <- w/c such that sum{w} < inf
         template<typename ItW>
- void operator()(
- ItW b_w, // log( unnormalized weights )
- ItW e_w
- );
+ void operator()(ItW b_w,ItW e_w) // log( unnormalized weights )
+ {
+ this->offset_ = apply_exp_offset(
+ b_w,
+ e_w,
+ this->max_log()
+ );
+
+ // if max_log is small enough (which costs precision), this does not
+ // nothing i.e. scaling_factor = 1
+ this->scaling_factor_ = scale_to_finite_sum(
+ b_w,
+ e_w
+ );
+ }
 
- public:
- static value_type zero;
- static value_type eps;
- static value_type default_max_log;
         static const char* header;
+ private:
+ value_type max_log_;
+ value_type offset_;
+ value_type scaling_factor_;
+ static const value_type zero;
+ static const value_type default_max_log;
 
     };
 
     template<typename T>
- std::ostream& operator<<(std::ostream& out,
- const prepare_weights<T>& that);
-
- // Implementation
-
- template<typename T>
     std::ostream& operator<<(
         std::ostream& out,
         const prepare_weights<T>& that
     ){
         out <<
             (
- format("(%1%,%2%,%3%,%4%)")
- % that.offset
- % that.scaling_factor
- % that.pc_ess
- % that.pc_lt_eps
+ boost::format("(%1%,%2%)")
+ % that.offset()
+ % that.scaling_factor()
             ).str();
         return out;
     }
 
     template<typename T>
     const char* prepare_weights<T>::header
- = "(offset,scaling_factor,pc_ess,pc_lt_eps)";
-
- template<typename T>
- typename prepare_weights<T>::value_type
- prepare_weights<T>::eps = math::tools::epsilon<value_type>();
+ = "(offset,scaling_factor)";
 
     template<typename T>
- typename prepare_weights<T>::value_type
+ const typename prepare_weights<T>::value_type
     prepare_weights<T>::default_max_log = static_cast<value_type>(0);
 
     template<typename T>
- typename prepare_weights<T>::value_type
+ const typename prepare_weights<T>::value_type
     prepare_weights<T>::zero = static_cast<value_type>(0);
 
- template<typename T>
- prepare_weights<T>::prepare_weights()
- :max_log(default_max_log),
- offset(zero),scaling_factor(zero),pc_ess(zero),pc_lt_eps(zero){}
-
- template<typename T>
- prepare_weights<T>::prepare_weights(value_type ml)
- :max_log(ml),
- offset(zero),scaling_factor(zero),pc_ess(zero),pc_lt_eps(zero){}
-
- template<typename T>
- template<typename ItW>
- void
- prepare_weights<T>::operator()(
- ItW b_w,
- ItW e_w
- ){
- offset = apply_exp_offset(
- b_w,
- e_w,
- max_log
- );
-
- // if max_log is small enough (which costs precision), this does not
- // nothing i.e. scaling_factor = 1
- scaling_factor = scale_to_finite_sum(
- b_w,
- e_w
- );
-
- ItW i_lt_eps = std::lower_bound(
- b_w,
- e_w,
- eps,
- ( boost::lambda::_1 >= boost::lambda::_2 )
- );
-
- value_type n_gt_eps
- = static_cast<value_type>( std::distance(b_w,i_lt_eps) );
- value_type n_lt_eps
- = static_cast<value_type>( std::distance(i_lt_eps,e_w) );
-
-
- // Increasing max_log should decrease this number
- pc_lt_eps = n_lt_eps / ( n_lt_eps + n_gt_eps ) ;
-
- // Beware that pc_lt_eps >0 may distort ess
- pc_ess = percentage_effective_sample_size(
- b_w,
- e_w
- );
-
- }
-
 }// importance_weights
 }// detail
 }// statistics


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