Jpp 20.0.0-rc.8
the software that should make you happy
Loading...
Searching...
No Matches
JGarage.hh
Go to the documentation of this file.
1#ifndef __JASTRONOMY__JGARAGE__
2#define __JASTRONOMY__JGARAGE__
3
4#include <vector>
5#include <numeric>
6#include <algorithm>
7#include <stdexcept>
8
9
10/**
11 * \author mdejong
12 */
13
14namespace JASTRONOMY {}
15namespace JPP { using namespace JASTRONOMY; }
16
17namespace JASTRONOMY {
18
19 /**
20 * Auxiliary container for statistical analysis of a large number of positive (or zero) values.
21 *
22 * The data are organised in groups according to a numerical hash key
23 * so that the computation involving quantiles can be made faster.\n
24 * The numerical hash key is obtained by multiplying the input value with the given factor and then taking the integer value of the result.\n
25 * The computed quantiles correspond to a distribution with ascending values.
26 */
27 template<class T, const size_t factor = 0>
28 struct JGarage :
29 public std::vector< std::vector<T> >
30 {
32 typedef typename map_type::value_type value_type;
33 typedef typename map_type::const_iterator const_iterator;
34 typedef typename map_type::const_reverse_iterator const_reverse_iterator;
35
36
37 /**
38 * Get total number of values.
39 *
40 * \return number of values
41 */
42 size_t getN() const
43 {
44 using namespace std;
45
46 return accumulate(this->begin(), this->end(), 0, [](const size_t n, const value_type& buffer) { return n + buffer.size(); });
47 }
48
49
50 /**
51 * Get minimal value.
52 *
53 * \return value
54 */
55 T getMin() const
56 {
57 using namespace std;
58
59 for (const_iterator i = this->begin(); i != this->end(); ++i) {
60 if (!i->empty()) {
61 return *min_element(i->begin(), i->end());
62 }
63 }
64
65 throw length_error("No data.");
66 }
67
68
69 /**
70 * Get maximal value.
71 *
72 * \return value
73 */
74 T getMax() const
75 {
76 using namespace std;
77
78 for (const_reverse_iterator i = this->rbegin(); i != this->rend(); ++i) {
79 if (!i->empty()) {
80 return *max_element(i->begin(), i->end());
81 }
82 }
83
84 throw length_error("No data.");
85 }
86
87
88 /**
89 * Get hash key corresponding to given value.
90 *
91 * \param value value
92 * \return key
93 */
94 static size_t getKey(const T value)
95 {
96 return value * factor;
97 }
98
99
100 /**
101 * Add value to container.
102 *
103 * \param value value
104 */
105 void put(const T value)
106 {
107 const size_t index = getKey(value);
108
109 if (index >= this->size()) {
110 this->resize(index + 1);
111 }
112
113 (*this)[index].push_back(value);
114 }
115
116
117 /**
118 * Add values to container.
119 *
120 * \param N number of values
121 * \param object function object returning value
122 */
123 template<class JFunction_t>
124 void put(const size_t N, const JFunction_t& object)
125 {
126 for (size_t i = 0; i != N; ++i) {
127 this->put(object());
128 }
129 }
130
131
132 /**
133 * For-each method.
134 *
135 * \param object function object
136 */
137 template<class JFunction_t>
138 inline void operator()(const JFunction_t& object) const
139 {
140 for (const_iterator i = this->begin(); i != this->end(); ++i) {
141 for (const T x : *i) {
142 object(x);
143 }
144 }
145 }
146
147
148 /**
149 * Get minimal value corresponding given maximal probability.
150 *
151 * \param P probability [0,1>
152 * \return value
153 */
154 T getValue(const double P) const
155 {
156 using namespace std;
157
158 if (P < 0.0 || P > 1.0) {
159 throw out_of_range("Invalid probability.");
160 }
161
162 const size_t N = this->getN();
163 const size_t M = N - (P * N);
164
165 if (N == 0) {
166 throw length_error("No data.");
167 }
168
169 if (M == 0) { return getMin(); }
170 if (M == N) { return getMax(); }
171
172 size_t n = N;
173
174 const_reverse_iterator q = this->rbegin();
175
176 while ((n -= q->size()) > M) {
177 ++q;
178 }
179
180 value_type buffer(q->begin(), q->end());
181
182 typename value_type::iterator p = next(buffer.begin(), M - n);
183
184 nth_element(buffer.begin(), p, buffer.end());
185
186 return *p;
187 }
188
189
190 /**
191 * Get maximal probability corresponding given minimal value.
192 *
193 * \param value value
194 * \return probability
195 */
196 double getProbability(const T value) const
197 {
198 using namespace std;
199
200 const size_t N = this->getN();
201 const int M = getKey(value);
202
203 if (N == 0) {
204 throw length_error("No data.");
205 }
206
207 size_t n = 0;
208 int i = this->size() - 1;
209
210 for ( ; i != -1 && i > M; --i) {
211 n += (*this)[i].size();
212 }
213
214 if (i != -1) {
215 for (const T x : (*this)[i]) {
216 if (x >= value) {
217 n += 1;
218 }
219 }
220 }
221
222 return (double) n / (double) N;
223 }
224 };
225}
226
227#endif
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary container for statistical analysis of a large number of positive (or zero) values.
Definition JGarage.hh:30
std::vector< std::vector< T > > map_type
Definition JGarage.hh:31
void put(const size_t N, const JFunction_t &object)
Add values to container.
Definition JGarage.hh:124
T getMax() const
Get maximal value.
Definition JGarage.hh:74
static size_t getKey(const T value)
Get hash key corresponding to given value.
Definition JGarage.hh:94
double getProbability(const T value) const
Get maximal probability corresponding given minimal value.
Definition JGarage.hh:196
map_type::const_iterator const_iterator
Definition JGarage.hh:33
T getValue(const double P) const
Get minimal value corresponding given maximal probability.
Definition JGarage.hh:154
map_type::value_type value_type
Definition JGarage.hh:32
size_t getN() const
Get total number of values.
Definition JGarage.hh:42
map_type::const_reverse_iterator const_reverse_iterator
Definition JGarage.hh:34
void operator()(const JFunction_t &object) const
For-each method.
Definition JGarage.hh:138
T getMin() const
Get minimal value.
Definition JGarage.hh:55
void put(const T value)
Add value to container.
Definition JGarage.hh:105