Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r56568 - in sandbox/statistics/cross_validation: boost/statistics/detail/cross_validation/error boost/statistics/detail/cross_validation/estimator/adaptor boost/statistics/detail/cross_validation/estimator/concept boost/statistics/detail/cross_validation/k_fold libs/statistics/detail/cross_validation/example
From: erwann.rogard_at_[hidden]
Date: 2009-10-04 01:28:43


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

Log:
m/a
Added:
   sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/cross_validate.hpp (contents, props changed)
Text files modified:
   sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/error/sqrt_mse.hpp | 2
   sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/adaptor/nullary_predictor.hpp | 1
   sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/concept/predictor.hpp | 20 ++++++++-------
   sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/concept/trainer.hpp | 18 ++++++++------
   sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/include.hpp | 1
   sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/train_predict.hpp | 21 ++++++++++------
   sandbox/statistics/cross_validation/libs/statistics/detail/cross_validation/example/k_fold.cpp | 49 ++++++++++++++++++++++++++-------------
   7 files changed, 69 insertions(+), 43 deletions(-)

Modified: sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/error/sqrt_mse.hpp
==============================================================================
--- sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/error/sqrt_mse.hpp (original)
+++ sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/error/sqrt_mse.hpp 2009-10-04 01:28:41 EDT (Sun, 04 Oct 2009)
@@ -48,7 +48,7 @@
         res = sqrt( res );
         return res;
     };
-
+
 }// error
 }// cross_validation
 }// detail

Modified: sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/adaptor/nullary_predictor.hpp
==============================================================================
--- sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/adaptor/nullary_predictor.hpp (original)
+++ sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/adaptor/nullary_predictor.hpp 2009-10-04 01:28:41 EDT (Sun, 04 Oct 2009)
@@ -21,6 +21,7 @@
 namespace adaptor{
 
     // Adapts an accumulator associated with a tag to model Predictor
+ //
     // See http://boost-sandbox.sourceforge.net/libs/accumulators/doc/html/accumulators/user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.concepts
     // B->const_reference() must return an accumulator
     template<

Modified: sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/concept/predictor.hpp
==============================================================================
--- sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/concept/predictor.hpp (original)
+++ sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/concept/predictor.hpp 2009-10-04 01:28:41 EDT (Sun, 04 Oct 2009)
@@ -9,7 +9,8 @@
 #ifndef BOOST_STATISTICS_DETAIL_CROSS_VALIDATION_ESTIMATOR_CONCEPT_PREDICTOR_HPP_ER_2009
 #define BOOST_STATISTICS_DETAIL_CROSS_VALIDATION_ESTIMATOR_CONCEPT_PREDICTOR_HPP_ER_2009
 #include <boost/concept_check.hpp>
-#include <boost/mpl/empty_base.hpp>
+#include <boost/statistics/detail/cross_validation/estimator/meta/estimator.hpp>
+#include <boost/statistics/detail/cross_validation/estimator/meta/input_data.hpp>
 
 namespace boost{
 namespace statistics{
@@ -18,22 +19,23 @@
 namespace estimator{
 namespace concept{
 
- // Type Object
- // E estimator
- // T test point
- template <typename E,typename T, typename B = mpl::empty_base>
- class Predictor : B
+ template <typename B>
+ class Predictor : public B
     {
+ typedef typename B::estimator_type e_;
+ typedef typename B::input_data_type i_;
+
         public:
         
         BOOST_CONCEPT_USAGE(Predictor)
         {
- e.predict( t );
+ e.predict( i );
         }
     
     private:
- E e;
- T t;
+ e_ e;
+ i_ i;
+
     };
 
 }// concept

Modified: sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/concept/trainer.hpp
==============================================================================
--- sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/concept/trainer.hpp (original)
+++ sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/estimator/concept/trainer.hpp 2009-10-04 01:28:41 EDT (Sun, 04 Oct 2009)
@@ -10,6 +10,8 @@
 #define BOOST_STATISTICS_DETAIL_CROSS_VALIDATION_ESTIMATOR_CONCEPT_TRAINER_HPP_ER_2009
 #include <boost/concept_check.hpp>
 #include <boost/mpl/empty_base.hpp>
+#include <boost/statistics/detail/cross_validation/estimator/meta/estimator.hpp>
+#include <boost/statistics/detail/cross_validation/estimator/meta/training_dataset.hpp>
 
 namespace boost{
 namespace statistics{
@@ -18,21 +20,21 @@
 namespace estimator{
 namespace concept{
 
- // Type Object
- // E estimator type
- // R range of training points
- template <typename E,typename R,typename B = mpl::empty_base>
- class Trainer : B
+ template <typename B = mpl::empty_base>
+ class Trainer : public B
     {
+ typedef typename B::estimator_type e_;
+ typedef typename B::training_dataset_type d_;
+
         public:
         BOOST_CONCEPT_USAGE(Trainer)
         {
- e.train( r );
+ e.train( d );
         }
     
         private:
- E e;
- R r;
+ e_ e;
+ d_ d;
     };
 
 }// concept

Added: sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/cross_validate.hpp
==============================================================================
--- (empty file)
+++ sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/cross_validate.hpp 2009-10-04 01:28:41 EDT (Sun, 04 Oct 2009)
@@ -0,0 +1,63 @@
+//////////////////////////////////////////////////////////////////////////////
+// cross_validation::k_fold::initialize.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 BOOST_STATISTICS_DETAIL_CROSS_VALIDATION_K_FOLD_INITILIAZE_HPP_ER_2009
+#define BOOST_STATISTICS_DETAIL_CROSS_VALIDATION_K_FOLD_INITILIAZE_HPP_ER_2009
+#include <algorithm>
+#include <boost/range.hpp>
+#include <boost/typeof/typeof.hpp>
+#include <boost/statistics/detail/cross_validation/k_fold/train_predict.hpp>
+
+namespace boost{
+namespace statistics{
+namespace detail{
+namespace cross_validation{
+namespace k_fold{
+
+ template<
+ typename U,typename Ft,typename Fi,typename Fo,
+ typename E,typename It,typename It1
+ >
+ It cross_validate(
+ partition<U,Ft,Fi,Fo>& kfp,
+ E estimator,
+ It i_p, // predicted values
+ It1 i_o // output values
+ ){
+ kfp.initialize();
+ while(kfp.j()<kfp.k()){
+ i_p = train_predict(
+ kfp,
+ estimator,
+ i_p
+ );
+
+ BOOST_AUTO(
+ r,
+ kfp.output_range()
+ );
+
+ i_o = std::copy(
+ boost::begin(r),
+ boost::end(r),
+ i_o
+ );
+
+ kfp.increment();
+ }
+ return i_p;
+ }
+
+}// k_fold
+}// cross_validation
+}// detail
+}// statistics
+}// boost
+
+#endif
+

Modified: sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/include.hpp
==============================================================================
--- sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/include.hpp (original)
+++ sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/include.hpp 2009-10-04 01:28:41 EDT (Sun, 04 Oct 2009)
@@ -11,6 +11,7 @@
 
 #include <boost/statistics/detail/cross_validation/k_fold/partition.hpp>
 #include <boost/statistics/detail/cross_validation/k_fold/train_predict.hpp>
+#include <boost/statistics/detail/cross_validation/k_fold/cross_validate.hpp>
 
 #endif
 

Modified: sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/train_predict.hpp
==============================================================================
--- sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/train_predict.hpp (original)
+++ sandbox/statistics/cross_validation/boost/statistics/detail/cross_validation/k_fold/train_predict.hpp 2009-10-04 01:28:41 EDT (Sun, 04 Oct 2009)
@@ -16,6 +16,9 @@
 #include <boost/foreach.hpp>
 #include <boost/iterator/iterator_traits.hpp>
 #include <boost/iterator/iterator_concepts.hpp>
+#include <boost/statistics/detail/cross_validation/estimator/meta/input_data.hpp>
+#include <boost/statistics/detail/cross_validation/estimator/meta/estimator.hpp>
+#include <boost/statistics/detail/cross_validation/estimator/meta/training_dataset.hpp>
 #include <boost/statistics/detail/cross_validation/estimator/concept/predictor.hpp>
 #include <boost/statistics/detail/cross_validation/estimator/concept/trainer.hpp>
 #include <boost/statistics/detail/cross_validation/k_fold/partition.hpp>
@@ -32,7 +35,7 @@
     It train_predict(
         partition<U,Ft,Fi,Fo>& kfp,
         E estimator,
- It out_iter
+ It i_p // predicted values
     )
     {
         typedef partition<U,Ft,Fi,Fo> part_;
@@ -40,10 +43,12 @@
         typedef typename part_::meta_input_range::type ir_;
         typedef typename range_value<ir_>::type i_;
 
- BOOST_CONCEPT_ASSERT(( estimator::concept::Trainer<E,tr_> ));
- BOOST_CONCEPT_ASSERT((
- cross_validation::estimator::concept::Predictor<E,i_>
- ));
+ typedef estimator::meta::estimator<E> m1_;
+ typedef estimator::meta::training_dataset<tr_,m1_> m2_;
+ typedef estimator::meta::input_data<i_, m2_> m3_;
+
+ BOOST_CONCEPT_ASSERT(( estimator::concept::Trainer<m3_> ));
+ BOOST_CONCEPT_ASSERT(( estimator::concept::Predictor<m3_> ));
 
         // std::back_inserter cannot work
         BOOST_CONCEPT_ASSERT((boost_concepts::IncrementableIterator<It>));
@@ -57,11 +62,11 @@
         BOOST_FOREACH(ref_i_ i, kfp.input_range())
         {
     
- *out_iter = estimator.predict(i);
- ++out_iter;
+ *i_p = estimator.predict(i);
+ ++i_p;
         }
 
- return out_iter;
+ return i_p;
     }
 
 }// k_fold

Modified: sandbox/statistics/cross_validation/libs/statistics/detail/cross_validation/example/k_fold.cpp
==============================================================================
--- sandbox/statistics/cross_validation/libs/statistics/detail/cross_validation/example/k_fold.cpp (original)
+++ sandbox/statistics/cross_validation/libs/statistics/detail/cross_validation/example/k_fold.cpp 2009-10-04 01:28:41 EDT (Sun, 04 Oct 2009)
@@ -121,25 +121,26 @@
             kf_p.increment();
         }
     }
- kf_p.initialize();
     os << std::endl;
 
- vals_ est_vec_x(n);
- vals_it_ est_vec_b = boost::begin(est_vec_x);
- vals_it_ est_vec_e;
+ // Cross-Validation #1
+ vals_ preds(n);
+ vals_it_ preds_b = boost::begin(preds);
+ vals_it_ preds_e;
     BOOST_ASSERT(n / k > 1);
+ kf_p.initialize();
     while(kf_p.j()<kf_p.k()){
         acc_ acc;
         joined_ joined(acc);
- est_vec_e = train_predict(
+ preds_e = train_predict(
             kf_p,
             joined,
- est_vec_b
+ preds_b
         );
- val_ tmp = *est_vec_b;
+ val_ tmp = *preds_b;
         BOOST_AUTO(
             r,
- make_iterator_range(boost::next(est_vec_b),est_vec_e)
+ make_iterator_range(boost::next(preds_b),preds_e)
         );
         BOOST_FOREACH(const val_& v, r)
         {
@@ -147,24 +148,38 @@
             // a.k.a. marginal estimator in statistical terminology.
             BOOST_ASSERT(tmp == v);
         }
+
+ preds_b = preds_e;
+ kf_p.increment();
+ }
+
+ // Cross-Validation #2
+ {
+ vals_ output(n);
+ acc_ acc;
+ joined_ joined(acc);
+ cross_validate(
+ kf_p,
+ joined,
+ boost::begin(preds),
+ boost::begin(output)
+ );
 
         val_ sqrt_mse = cv::error::sqrt_mse(
- est_vec_b,
- est_vec_e,
- boost::begin(kf_p.output_range())
+ boost::begin(preds),
+ boost::end(preds),
+ boost::begin(output)
         );
 
         val_ mae = cv::error::mean_abs_error(
- est_vec_b,
- est_vec_e,
- boost::begin(kf_p.output_range())
+ boost::begin(preds),
+ boost::end(preds),
+ boost::begin(output)
         );
         
         os << "sqrt_mse = " << sqrt_mse << std::endl;
         os << "mae = " << mae << std::endl;
-
- est_vec_b = est_vec_e;
- kf_p.increment();
+
     }
 
     os << "<-" << std::endl;


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