Jpp test-rotations-old-533-g2bdbdb559
the software that should make you happy
Loading...
Searching...
No Matches
JAstronomyToolkit.hh
Go to the documentation of this file.
1#ifndef __JASTRONOMY__JASTRONOMYTOOLKIT__
2#define __JASTRONOMY__JASTRONOMYTOOLKIT__
3
4#include <vector>
5#include <map>
6#include <cmath>
7#include <numeric>
8#include <limits>
9#include <algorithm>
10
11#include "JLang/JException.hh"
12
13
14/**
15 * \author mdejong
16 */
17
18namespace JASTRONOMY {}
19namespace JPP { using namespace JASTRONOMY; }
20
21namespace JASTRONOMY {
22
25
26
27 /**
28 * Auxiliary container for statistical analysis of a large number of values.
29 *
30 * The data are organised in groups according to a numerical key
31 * so that the computation involving quantiles can be speed up.\n
32 * The numerical key is obtained by scaling the value with a given factor and then taking the floor of the result.\n
33 * The quantiles correspond to a distribution with ascending values.
34 */
35 template<class T>
36 struct JQuantile :
37 public std::map<int, std::vector<T> >
38 {
40 typedef typename map_type::value_type value_type;
41 typedef typename map_type::const_iterator const_iterator;
42 typedef typename map_type::const_reverse_iterator const_reverse_iterator;
43
44
45 /**
46 * Constructor.
47 *
48 * \param factor factor
49 */
50 JQuantile(const double factor) :
52 {}
53
54
55 /**
56 * Get total number of values.
57 *
58 * \return number of values
59 */
60 size_t getN() const
61 {
62 return std::accumulate(this->begin(), this->end(), 0, [](const size_t n, const value_type& i) { return n + i.second.size(); });
63 }
64
65
66 /**
67 * Get minimal value.
68 *
69 * \return value
70 */
71 T getMin() const
72 {
73 for (const_iterator i = this->begin(); i != this->end(); ++i) {
74 if (!i->second.empty()) {
75 return *std::min_element(i->second.begin(), i->second.end());
76 }
77 }
78
79 THROW(JEmptyCollection, "Missing data.");
80 }
81
82
83 /**
84 * Get maximal value.
85 *
86 * \return value
87 */
88 T getMax() const
89 {
90 for (const_reverse_iterator i = this->rbegin(); i != this->rend(); ++i) {
91 if (!i->second.empty()) {
92 return *std::max_element(i->second.begin(), i->second.end());
93 }
94 }
95
96 THROW(JEmptyCollection, "Missing data.");
97 }
98
99
100 /**
101 * Get key of given value.
102 *
103 * \param value value
104 * \return key
105 */
106 int getKey(const T value) const
107 {
108 return floor(value * factor);
109 }
110
111
112 /**
113 * Add value to container.
114 *
115 * \param value value
116 */
117 void put(const T value)
118 {
119 (*this)[getKey(value)].push_back(value);
120 }
121
122
123 /**
124 * Get minimal value corresponding given maximal probability.
125 *
126 * \param P probability [0,1>
127 * \return value
128 */
129 T getValue(const double P) const
130 {
131 using namespace std;
132
133 if (P < 0.0 || P > 1.0) {
134 THROW(JValueOutOfRange, "Invalid probability " << P);
135 }
136
137 const size_t N = this->getN();
138 const size_t M = N - (size_t) (P * N);
139
140 if (N == 0) {
141 THROW(JEmptyCollection, "Missing data.");
142 }
143
144 if (M == N) { return this->getMax(); }
145 if (M == 0) { return this->getMin(); }
146
147 size_t n = N;
148
149 for (const_reverse_iterator p = this->rbegin(); p != this->rend(); ++p) {
150
151 n -= p->second.size();
152
153 if (n <= M) {
154
155 vector<T> buffer(p->second.begin(), p->second.end());
156
157 sort(buffer.begin(), buffer.end());
158
159 return buffer[M - n];
160 }
161 }
162
163 THROW(JValueOutOfRange, "Invalid index " << M << ' ' << N);
164 }
165
166
167 /**
168 * Get maximal probability corresponding given minimal value.
169 *
170 * \param value value
171 * \return probability
172 */
173 double getProbability(const double value) const
174 {
175 const int M = getKey(value);
176 size_t n = 0;
177
178 const_reverse_iterator i = this->rbegin();
179
180 for ( ; i != this->rend() && i->first > M; ++i) {
181 n += i->second.size();
182 }
183
184 if (i != this->rend()) {
185 for (T x : i->second) {
186 if (x >= value) {
187 n += 1;
188 }
189 }
190 }
191
192 return (double) n / (double) this->getN();
193 }
194
195
196 const T factor; //!< scaling factor for rounding value
197 };
198}
199
200#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Exception for an empty collection.
Exception for accessing a value in a collection that is outside of its range.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary container for statistical analysis of a large number of values.
double getProbability(const double value) const
Get maximal probability corresponding given minimal value.
void put(const T value)
Add value to container.
map_type::value_type value_type
T getMax() const
Get maximal value.
T getMin() const
Get minimal value.
const T factor
scaling factor for rounding value
std::map< int, std::vector< T > > map_type
JQuantile(const double factor)
Constructor.
T getValue(const double P) const
Get minimal value corresponding given maximal probability.
int getKey(const T value) const
Get key of given value.
map_type::const_reverse_iterator const_reverse_iterator
map_type::const_iterator const_iterator
size_t getN() const
Get total number of values.