Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r56589 - sandbox/statistics/kernel/boost/statistics/kernel/functional
From: erwann.rogard_at_[hidden]
Date: 2009-10-04 18:57:02


Author: e_r
Date: 2009-10-04 18:57:01 EDT (Sun, 04 Oct 2009)
New Revision: 56589
URL: http://svn.boost.org/trac/boost/changeset/56589

Log:
m
Text files modified:
   sandbox/statistics/kernel/boost/statistics/kernel/functional/estimator.hpp | 249 ++++++++++++---------------------------
   sandbox/statistics/kernel/boost/statistics/kernel/functional/nw_visitor.hpp | 12 +
   sandbox/statistics/kernel/boost/statistics/kernel/functional/rp_visitor.hpp | 11 +
   3 files changed, 90 insertions(+), 182 deletions(-)

Modified: sandbox/statistics/kernel/boost/statistics/kernel/functional/estimator.hpp
==============================================================================
--- sandbox/statistics/kernel/boost/statistics/kernel/functional/estimator.hpp (original)
+++ sandbox/statistics/kernel/boost/statistics/kernel/functional/estimator.hpp 2009-10-04 18:57:01 EDT (Sun, 04 Oct 2009)
@@ -8,29 +8,33 @@
 #ifndef BOOST_STATISTICS_KERNEL_FUNCTIONAL_DENSITY_VISITOR_HPP_ER_2009
 #define BOOST_STATISTICS_KERNEL_FUNCTIONAL_DENSITY_VISITOR_HPP_ER_2009
 #include <algorithm>
+#include <boost/concept/assert.hpp>
+#include <boost/concept_check.hpp>
 #include <boost/call_traits.hpp>
 #include <boost/call_traits.hpp>
 #include <boost/type_traits/is_reference.hpp>
+#include <boost/type_traits/is_convertible.hpp>
 #include <boost/mpl/nested_type.hpp>
-#include <boost/statistics/estimator_concept/meta/result_of_estimate.hpp>
+#include <boost/mpl/assert.hpp>
 #include <boost/statistics/kernel/functional/detail/mean_accumulator.hpp>
-
 namespace boost{
 namespace statistics{
 namespace kernel{
 
- // R: range of training_data
- // V: visitor
+ // A type that models Trainer and Predictor
+ // See https://svn.boost.org/svn/boost/sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/concept/
+ //
+ // R: range of a data range
+ // V: visitor
     // K: kernel
     // A: accumulator
     //
- // V R
- // rp_visitor {x[i]:i=1,...,n}
- // nw_visitor_tuple {(x,y)[i]:i=1,...,n}
+ // V -> data range of type R
+ // rp_visitor -> {x[i]:i=1,...,n}
+ // meta_nw_visitor_unary<F1,F2>::apply -> {(x,y)[i]:i=1,...,n}
     //
- // See statistics/estimator_concept
- // Models TrainableEstimator
- // If V == nw_visitor_tuple, models TrainableRegressionEstimator
+ // It is recommended that R derives from iterator_range because it is cheap
+ // to copy.
     template<
         typename R,
         template<typename,typename,typename> class V,
@@ -41,187 +45,84 @@
     class estimator{
     
         public:
- typedef R training_data_type;
+ typedef R training_dataset_type;
         typedef K kernel_type;
         typedef A accumulator_type;
 
         template<typename X>
- struct result{
+ struct visitor
+ {
             typedef V<K,const X&,A> arg_;
             typedef typename mpl::nested_type<arg_>::type type;
         };
+
+ template<typename X>
+ struct result_of_predict
+ {
+ typedef typename visitor<X>::type v_;
+ typedef typename v_::result_type type;
+ };
     
         // Constructor
- estimator();
- estimator(K k);
- estimator(K k,const A&);
- estimator(const estimator&);
- estimator& operator=(const estimator&);
+ estimator(){}
+ estimator(K k)
+ :k_(k){}
+ estimator(K k,const A& a)
+ :k_(k),a_(a){}
+ estimator(const estimator& that)
+ :td_(that.td_),k_(that.k_),a_(that.a_){}
+ estimator& operator=(const estimator& that){
+ if(&that = this){
+ this->td_ = that.td_;
+ this->k_ = that.k_;
+ this->a_ = that.a_;
+ }
+ return *this;
+ }
     
- // Evaluate
- template<typename X>
- typename result<X>::type operator()(const X& x)const;
-
         // Access
- const R& train_data()const;
- const K& kernel()const;
- const A& accumulator()const;
-
- void set_train_data(const R& train_data);
+ const R& training_dataset()const{
+ return this->td_;
+ }
+ const K& kernel()const{
+ return this->k_;
+ }
+ const A& accumulator()const{
+ return this->a_;
+ }
+
+ void train(const R& training_dataset){
+ BOOST_CONCEPT_ASSERT((
+ boost::Assignable<R>
+ ));
+ this->td_ = training_dataset;
+ }
+
+ template<typename X>
+ typename visitor<X>::type
+ visit(const X& x)const{
+ typedef estimator this_type;
+ typedef typename visitor<X>::type v_;
+
+ return std::for_each(
+ boost::begin( this->training_dataset() ),
+ boost::end( this->training_dataset() ),
+ v_( this->kernel(), x, this->a_)
+ );
+ }
+
+ template<typename X>
+ typename result_of_predict<X>::type
+ predict(const X& x)const{
+ return visit(x).estimate();
+ }
 
     private:
- R train_data_;
+ R td_;
         K k_;
         A a_; // Initialized accumulator
     };
 
-}//kernel
-
- template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
- >
- void train(kernel::estimator<R,V,K,A>& e, const R& train_data){
- e.set_train_data(train_data);
- }
-
- template<
- typename R,
- template<typename,typename,typename> class V,
- typename K, typename A, typename X
- >
- struct result_of_estimate< kernel::estimator<R,V,K,A>,X > {
- typedef kernel::estimator<R,V,K,A> est_;
- typedef typename est_::template result<X> meta_vis_;
- typedef typename meta_vis_::type vis_;
- typedef typename vis_::result_type type;
- };
-
- template<
- typename R,
- template<typename,typename,typename> class V,
- typename K, typename A, typename X
- >
- typename result_of_estimate< kernel::estimator<R,V,K,A>, X >::type
- estimate(const kernel::estimator<R,V,K,A>& e, const X& x){
- return e(x).estimate();
- }
-
-// Implementation //
-namespace kernel{
-
-// Constructor
-
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
-estimator<R,V,K,A>::estimator():train_data_(),k_(),a_(){}
-
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
-estimator<R,V,K,A>::estimator(K k):k_(k),a_(){}
-
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
-estimator<R,V,K,A>::estimator(K k,const A& a)
-:train_data_(),k_(k),a_(a){}
-
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
-estimator<R,V,K,A>::estimator(const estimator& that)
-:train_data_(that.train_data_),k_(that.k_),a_(that.a_){}
-
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
-estimator<R,V,K,A>&
-estimator<R,V,K,A>::operator=(const estimator& that){
- if(&that = this){
- this->train_data_ = that.train_data_;
- this->k_ = that.k_;
- this->a_ = that.a_;
- }
- return *this;
-}
-
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
-void estimator<R,V,K,A>::set_train_data(const R& train_data){
- this->train_data_ = train_data;
-}
-
-// Evaluate
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
- template<typename X>
-typename estimator<R,V,K,A>::template result<X>::type
-estimator<R,V,K,A>::operator()(const X& x)const{
- typedef typename estimator<R,V,K,A>::template result<X>::type result_;
- return std::for_each(
- boost::begin( this->train_data() ),
- boost::end( this->train_data() ),
- result_( this->kernel(), x, this->a_)
- );
-}
-
-// Access
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
-const R&
-estimator<R,V,K,A>::train_data()const{ return this->train_data_; }
-
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
-const K&
-estimator<R,V,K,A>::kernel()const{ return this->k_; }
-
-template<
- typename R,
- template<typename,typename,typename> class V,
- typename K,
- typename A
->
-const A&
-estimator<R,V,K,A>::accumulator()const{
- return this->a_;
-}
-
 }// kernel
 }// statistics
 }// boost

Modified: sandbox/statistics/kernel/boost/statistics/kernel/functional/nw_visitor.hpp
==============================================================================
--- sandbox/statistics/kernel/boost/statistics/kernel/functional/nw_visitor.hpp (original)
+++ sandbox/statistics/kernel/boost/statistics/kernel/functional/nw_visitor.hpp 2009-10-04 18:57:01 EDT (Sun, 04 Oct 2009)
@@ -17,10 +17,10 @@
 namespace statistics{
 namespace kernel{
    
-// Let E[Y|X=x] denote the conditional mean of Y given X = x
+// This visitor, f, updates a Nadaraya-Watson estimate of E[Y|X=x0] each
+// time f(x,y) is called.
 //
-// This class is to be used to visit the training data, to obtain an estimator,
-// by the Nadaraya-Watson method, of E[Y|X=x]
+// K,X,A : See rp_visitor
 template<
     typename K,
     typename X,
@@ -50,14 +50,17 @@
     nw_visitor& operator=(const nw_visitor&);
         
     // Update
+ protected:
     template<typename X1,typename Y1> // Training data point
     result_type operator()(const X1& x1,const Y1& y1);
-
+
+ public:
     // Access
     result_type unnormalized_estimate()const;
     result_type normalizing_constant()const;
     result_type estimate()const;
 
+
     const A& accumulator()const;
     const rp_visitor_type& rp_visitor()const;
         
@@ -134,7 +137,6 @@
     return (this->rp_visitor_);
 }
     
-
 }// kernel
 }// statistics
 }// boost

Modified: sandbox/statistics/kernel/boost/statistics/kernel/functional/rp_visitor.hpp
==============================================================================
--- sandbox/statistics/kernel/boost/statistics/kernel/functional/rp_visitor.hpp (original)
+++ sandbox/statistics/kernel/boost/statistics/kernel/functional/rp_visitor.hpp 2009-10-04 18:57:01 EDT (Sun, 04 Oct 2009)
@@ -18,10 +18,15 @@
 namespace statistics{
 namespace kernel{
 
-// Let p(x) denote the density of X = x
+// This visitor, f, keeps a data point, x0, and each time f(x) is called, it
+// updates an estimate of the density at x0, p(x0), by the Rosenblatt-Parzen
+// method, using a given kernel k.
 //
-// This class is to be used to visit the training data, to obtain an estimator,
-// by the Rosenblatt-Parzen method, of p(x)
+// The estimate is the average of the kernel evaluations over the traversed
+// dataset. That average is implemented by an accumulator of type A.
+//
+// X can be a reference which is only recommended if the x object is expensive
+// to copy
 template<
     typename K,
     typename X,


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