Alexandria  2.16
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Scott.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
25 #ifndef ALEXANDRIA_HISTOGRAM_BINNING_SCOTT_H
26 #define ALEXANDRIA_HISTOGRAM_BINNING_SCOTT_H
27 
28 #include <cmath>
29 #include <algorithm>
30 #include <vector>
31 #include <boost/accumulators/accumulators.hpp>
32 #include <boost/accumulators/statistics/stats.hpp>
33 #include <boost/accumulators/statistics/max.hpp>
34 #include <boost/accumulators/statistics/min.hpp>
35 #include <boost/accumulators/statistics/variance.hpp>
36 
37 #include "Histogram/Histogram.h"
38 
39 namespace Euclid {
40 namespace Histogram {
41 namespace Binning {
42 
53 template<typename VarType>
54 class Scott: public BinStrategy<VarType> {
55 public:
56 
57  template<typename Iterator>
58  void computeBins(Iterator begin, Iterator end) {
59  using namespace boost::accumulators;
60 
61  accumulator_set<VarType, stats<tag::variance, tag::max, tag::min>> acc;
62  std::for_each(begin, end, std::bind<void>(std::ref(acc), std::placeholders::_1));
63 
64  size_t n = end - begin;
65 
66  VarType sigma = std::sqrt(variance(acc));
67  VarType h = 3.5 * sigma / std::pow(n, 1. / 3.);
68  VarType vmin = min(acc);
69  VarType vmax = max(acc);
70 
71  if (sigma == 0) {
72  vmax += 0.5;
73  vmin -= 0.5;
74  h = 1;
75  }
76 
77  VarType range = vmax - vmin;
78 
79  m_nbins = std::ceil(range / h);
80 
81  m_step = range / m_nbins;
82  m_start = vmin;
83  m_end = vmax;
84  }
85 
86  ssize_t getBinIndex(VarType value) const final {
87  if (value == m_end)
88  return m_nbins - 1;
89  return (value - m_start) / m_step;
90  }
91 
92  std::pair<VarType, VarType> getBinEdges(size_t i) const final {
93  return std::make_pair(i * m_step + m_start, (i + 1) * m_step + m_start);
94  }
95 
96  VarType getEdge(size_t i) const final {
97  return i * m_step + m_start;
98  }
99 
100 private:
102  VarType m_start, m_step, m_end;
103 };
104 
105 } // end of namespace Binning
106 } // end of namespace Histogram
107 } // end of namespace Euclid
108 
109 #endif // ALEXANDRIA_HISTOGRAM_BINNING_SCOTT_H
T ceil(T...args)
ssize_t getBinIndex(VarType value) const final
Definition: Scott.h:86
VarType getEdge(size_t i) const final
Definition: Scott.h:96
std::pair< VarType, VarType > getBinEdges(size_t i) const final
Definition: Scott.h:92
T make_pair(T...args)
T ref(T...args)
void computeBins(Iterator begin, Iterator end)
Definition: Scott.h:58
T pow(T...args)
T sqrt(T...args)
T for_each(T...args)