32 #ifndef SHARK_STATISTICS_H 33 #define SHARK_STATISTICS_H 41 #include <boost/math/special_functions/fpclassify.hpp> 44 #include <boost/range/algorithm/nth_element.hpp> 51 #include <boost/serialization/string.hpp> 52 #include <boost/serialization/map.hpp> 53 #include <boost/serialization/vector.hpp> 59 return std::numeric_limits<double>::quiet_NaN();
63 return boost::math::isnan(value);
69 virtual std::string
name()
const=0;
71 virtual RealVector
statistics(std::vector<RealVector>
const& points)
const=0;
80 RealVector
statistics(std::vector<RealVector>
const& points)
const{
81 std::size_t N = points.size();
82 RealVector missing(points[0].size(),0.0);
83 for(std::size_t i = 0; i != N;++i){
84 for(std::size_t j = 0; j != missing.size(); ++j){
100 RealVector
statistics(std::vector<RealVector>
const& points)
const{
101 std::size_t N = points.size();
102 RealVector sum(points[0].size(),0.0);
103 UIntVector numSamples(points[0].size(),0);
104 for(std::size_t i = 0; i != N;++i){
105 for(std::size_t j = 0; j != sum.size(); ++j){
107 sum(j) += points[i](j);
122 RealVector
statistics(std::vector<RealVector>
const& points)
const{
123 std::size_t N = points.size();
126 RealVector
variance(mean.size(),0.0);
127 UIntVector numSamples(points[0].size(),0);
128 for(std::size_t i = 0; i != N;++i){
129 for(std::size_t j = 0; j != mean.size(); ++j){
145 return boost::lexical_cast<std::string>(m_quantile)+
"-Quantile";
148 RealVector
statistics(std::vector<RealVector>
const& points)
const{
149 std::size_t N = points.size();
151 for(std::size_t j = 0; j != quantiles.size(); ++j){
153 std::vector<double> values;
154 for(std::size_t i = 0; i != N;++i){
156 values.push_back(points[i](j));
158 if(values.size() == 0)
continue;
161 std::size_t element = std::size_t(values.size()*m_quantile);
162 std::vector<double>::iterator pos= values.begin()+element;
163 boost::nth_element(values,pos);
201 template<
class Parameter>
206 ResultTable(std::size_t numDimensions, std::string
const& parameterName=
"unnamed")
207 :m_dimensionNames(numDimensions,
"unnamed"),m_parameterName(parameterName){}
210 return m_parameterName;
214 m_dimensionNames[i]=
name;
218 return m_dimensionNames[i];
222 return m_dimensionNames.size();
225 void update(Parameter
const& parameter, RealVector
const& point){
227 m_results[parameter].push_back(point);
230 void update(Parameter
const& parameter,
double value){
231 RealVector point(1,value);
232 update(parameter, point);
235 void update(Parameter
const& parameter,
double value1,
double value2){
239 update(parameter, point);
242 void update(Parameter
const& parameter,
double value1,
double value2,
double value3){
247 update(parameter, point);
250 std::vector<RealVector>
const&
operator[](Parameter
const& param)
const{
251 return m_results.find(param)->second;
255 return m_results.begin();
257 const_iterator
end()
const{
258 return m_results.end();
262 return m_results.size();
266 const_iterator pos = begin();
272 template<
class Archive>
273 void serialize(Archive &ar,
const unsigned int file_version) {
274 ar & m_dimensionNames;
275 ar & m_parameterName;
281 std::vector<std::string> m_dimensionNames;
282 std::string m_parameterName;
283 std::map<Parameter, std::vector<RealVector> > m_results;
290 template<
class Parameter>
298 iterator end = m_resultsTable->end();
299 for(iterator pos=m_resultsTable->begin(); pos != end; ++pos){
300 m_statistics[pos->first][statisticName] =
object.statistics(pos->second);
302 m_statisticNames.push_back(statisticName);
306 addStatistic(
object.
name(),
object);
309 std::map<std::string,RealVector>
const&
operator[](Parameter
const& parameter)
const{
310 return m_statistics.find(parameter)->second;
314 return m_statistics.begin();
316 const_iterator
end()
const{
317 return m_statistics.end();
322 return m_resultsTable->parameterName();
326 return m_resultsTable->numParams();
330 return m_resultsTable->parameterValue(i);
335 return m_resultsTable->numDimensions();
339 return m_resultsTable->dimensionName(i);
344 return m_statisticNames.size();
347 return m_statisticNames[i];
350 std::vector<std::string> m_statisticNames;
352 std::map<Parameter, std::map<std::string,RealVector> > m_statistics;
355 template<
class Parameter>
359 for(std::size_t i = 0; i != statistics.
numStatistics(); ++i){
360 for(std::size_t j = 0; j != statistics.
numDimensions(); ++j){
367 for(std::size_t k = 0; k != statistics.
numParams(); ++k){
369 std::map<std::string,RealVector> paramResults=statistics[param];
371 for(std::size_t i = 0; i != statistics.
numStatistics(); ++i){
372 for(std::size_t j = 0; j != statistics.
numDimensions(); ++j){
373 std::cout<<
" "<<paramResults[statistics.
statisticName(i)](j);
381 #endif // SHARK_STATISTICS_H