Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
Scott.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2021 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 <algorithm>
29#include <boost/accumulators/accumulators.hpp>
30#include <boost/accumulators/statistics/max.hpp>
31#include <boost/accumulators/statistics/min.hpp>
32#include <boost/accumulators/statistics/stats.hpp>
33#include <boost/accumulators/statistics/variance.hpp>
34#include <cmath>
35#include <vector>
36
37#include "Histogram/Histogram.h"
38
39namespace Euclid {
40namespace Histogram {
41namespace Binning {
42
53template <typename VarType>
54class Scott : public BinStrategy<VarType> {
55public:
56 template <typename Iterator>
57 void computeBins(Iterator begin, Iterator end) {
58 using namespace boost::accumulators;
59
60 accumulator_set<VarType, stats<tag::variance, tag::max, tag::min>> acc;
61 std::for_each(begin, end, std::bind<void>(std::ref(acc), std::placeholders::_1));
62
63 size_t n = end - begin;
64
65 VarType sigma = std::sqrt(variance(acc));
66 VarType h = 3.5 * sigma / std::pow(n, 1. / 3.);
67 VarType vmin = min(acc);
68 VarType vmax = max(acc);
69
70 if (sigma == 0) {
71 vmax += 0.5;
72 vmin -= 0.5;
73 h = 1;
74 }
75
76 VarType range = vmax - vmin;
77
78 m_nbins = std::ceil(range / h);
79
80 m_step = range / m_nbins;
81 m_start = vmin;
82 m_end = vmax;
83 }
84
85 ssize_t getBinIndex(VarType value) const final {
86 if (value == m_end)
87 return m_nbins - 1;
88 return (value - m_start) / m_step;
89 }
90
92 return std::make_pair(i * m_step + m_start, (i + 1) * m_step + m_start);
93 }
94
95 VarType getEdge(size_t i) const final {
96 return i * m_step + m_start;
97 }
98
99private:
100 using BinStrategy<VarType>::m_nbins;
102};
103
104} // end of namespace Binning
105} // end of namespace Histogram
106} // end of namespace Euclid
107
108#endif // ALEXANDRIA_HISTOGRAM_BINNING_SCOTT_H
T ceil(T... args)
VarType getEdge(size_t i) const final
Definition Scott.h:95
std::pair< VarType, VarType > getBinEdges(size_t i) const final
Definition Scott.h:91
void computeBins(Iterator begin, Iterator end)
Definition Scott.h:57
ssize_t getBinIndex(VarType value) const final
Definition Scott.h:85
T for_each(T... args)
T make_pair(T... args)
T pow(T... args)
T ref(T... args)
T sqrt(T... args)