/////////////////////////////////////////////////////////////////////////////// // roc_curve.hpp // // Copyright 2005 Hirotaka Niitsuma. 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_ACCUMULATORS_STATISTICS_ROC_CURVE_HPP_DE_01_01_2006 #define BOOST_ACCUMULATORS_STATISTICS_ROC_CURVE_HPP_DE_01_01_2006 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace accumulators { /////////////////////////////////////////////////////////////////////////////// // num_cells named parameter // BOOST_PARAMETER_NESTED_KEYWORD(tag, roc_curve_num_cells, num_cells) namespace impl { /////////////////////////////////////////////////////////////////////////////// // roc_curve_impl // cumulative_distribution calculation (as histogram) /* */ template struct roc_curve_impl : accumulator_base { typedef typename numeric::functional::average::result_type float_type; typedef std::vector > histogram_type; typedef std::vector< boost::numeric::ublas::c_vector > positive_negative_histogram_type; typedef iterator_range result_type; template roc_curve_impl(Args const &args) : num_cells(args[roc_curve_num_cells]) ,sample_float(0) ,acc_positive( boost::accumulators::p_square_cumulative_distribution_num_cells = num_cells ,boost::accumulators::sample = sample_float) //false_positive_rate , ,acc_negative( boost::accumulators::p_square_cumulative_distribution_num_cells = num_cells ,boost::accumulators::sample = sample_float) //true_negative_rate {} template void operator ()(Args const &args) { float_type positive_or_negative = args[parameter::keyword::get()]; if(positive_or_negative > 0) { this->acc_positive(args);//false_positive_rate } else { this->acc_negative(args);//true_negative_rate } } template result_type result(Args const &args) const { histogram_type histogram_positive = boost::accumulators::p_square_cumulative_distribution(this->acc_positive);//false_positive_rate histogram_type histogram_negative = boost::accumulators::p_square_cumulative_distribution(this->acc_negative);//true_negative_rate histogram_type::iterator hit_pos=histogram_positive.begin(),hit_neg=histogram_negative.begin(); boost::numeric::ublas::c_vector tmp_vec; Value_T vp=0.0, vn=0.0; Value_T h; do { if( hit_pos != histogram_positive.end() && hit_neg != histogram_negative.end() ) { if( hit_pos->first < hit_neg->first ) { h= hit_pos->first; vp= hit_pos->second; hit_pos++; }else if(hit_neg->first < hit_pos->first ) { h= hit_neg->first; vn= hit_neg->second; hit_neg++; } }else{ if( hit_neg != histogram_negative.end() ) { h= hit_neg->first; vn= hit_neg->second; hit_neg++; }else if(hit_pos != histogram_positive.end() ) { h= hit_pos->first; vp= hit_pos->second; hit_pos++; } } tmp_vec[0]=h; tmp_vec[1]=1-vp; //true_positive_rate tmp_vec[2]=1-vn; //false_negative_rate //roc_data.push_back(tmp_vec); this->positive_negative_histogram.push_back(tmp_vec); }while( hit_pos != histogram_positive.end() || hit_neg != histogram_negative.end() ); return make_iterator_range(this->positive_negative_histogram); } private: histogram_type histogram; positive_negative_histogram_type positive_negative_histogram; std::size_t num_cells; // number of cells b float_type sample_float; boost::accumulators::accumulator_set< float_type , boost::accumulators::stats< boost::accumulators::tag::p_square_cumulative_distribution > >acc_positive, acc_negative; }; } // namespace detail /////////////////////////////////////////////////////////////////////////////// // tag::roc_curve // namespace tag { template struct roc_curve : depends_on , roc_curve_num_cells { /// INTERNAL ONLY typedef accumulators::impl::roc_curve_impl impl; }; struct abstract_roc_curve : depends_on<> { }; } /////////////////////////////////////////////////////////////////////////////// // extract::roc_curve // namespace extract { //extractor const roc_curve = {}; extractor const roc_curve = {}; } using extract::roc_curve; template struct feature_of > : feature_of { }; }} // namespace boost::accumulators #endif