Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JQuantile.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JQUANTILE__
2#define __JTOOLS__JQUANTILE__
3
4#include <ostream>
5#include <iomanip>
6#include <cmath>
7#include <limits>
8#include <algorithm>
9#include <map>
10
11#include "JLang/JException.hh"
12#include "JLang/JTitle.hh"
13#include "JLang/JVectorize.hh"
14#include "JLang/JManip.hh"
15
16#include "JMath/JMath.hh"
17
18#include "JTools/JStats.hh"
19
20
21/**
22 * \author mdejong
23 */
24
25namespace JTOOLS {}
26namespace JPP { using namespace JTOOLS; }
27
28namespace JTOOLS {
29
30 using JLANG::JNoValue;
33
34
35 /**
36 * Auxiliary data structure for running average, standard deviation and quantiles.
37 */
38 struct JQuantile :
39 public JStats,
40 public JMath<JQuantile>
41 {
42 /**
43 * Constructor.
44 *
45 * \param title title
46 */
47 JQuantile(const JTitle& title = "") :
49 {
50 reset();
51 }
52
53
54 /**
55 * Constructor.
56 *
57 * \param title title
58 * \param buffer input data
59 * \param w weight
60 */
61 template<class JElement_t, class JAllocator_t>
64 const double w = 1.0) :
66 {
67 reset();
68
69 put(buffer, w);
70 }
71
72
73 /**
74 * Reset.
75 */
76 void reset()
77 {
79
80 buffer.clear();
81 }
82
83
84 /**
85 * Add quantile.
86 *
87 * \param Q quantile
88 * \return this quantile
89 */
91 {
92 JStats::add(Q);
93
94 std::copy(Q.buffer.begin(), Q.buffer.end(), std::inserter(buffer, buffer.end()));
95
96 return *this;
97 }
98
99
100 /**
101 * Put value.
102 *
103 * \param x value
104 * \param w weight
105 */
106 void put(const double x, const double w = 1.0)
107 {
108 JStats::put(x, w);
109
110 buffer.insert(std::make_pair(x,w));
111 }
112
113
114 /**
115 * Put data.
116 *
117 * \param buffer input data
118 * \param w weight
119 */
120 template<class JElement_t, class JAllocator_t>
122 const double w = 1.0)
123 {
124 for (typename array_type<JElement_t, JAllocator_t>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
125 put(*i, w);
126 }
127 }
128
129
130 /**
131 * Options for evaluation of quantile.
132 */
134 forward_t = +1, //!< forward
135 symmetric_t = 0, //!< symmatric
136 backward_t = -1 //!< backward
137 };
138
139
140 /**
141 * Get quantile.
142 *
143 * \param Q quantile
144 * \param option option
145 * \return value
146 */
147 double getQuantile(const double Q, const Quantile_t option = forward_t) const
148 {
149 double W = 0.0;
150
151 for (std::map<double, double>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
152 W += i->second;
153 }
154
155 switch (option) {
156 case forward_t:
157 return (getQuantile(buffer. begin(), buffer. end(), Q*W));
158 case symmetric_t:
159 return (getQuantile(buffer.rbegin(), buffer.rend(), 0.5*(1.0 - Q)*W) -
160 getQuantile(buffer. begin(), buffer. end(), 0.5*(1.0 - Q)*W));
161 case backward_t:
162 return (getQuantile(buffer.rbegin(), buffer.rend(), Q*W));
163 default:
164 THROW(JNoValue, "Invalid option " << option);
165 }
166 }
167
168 protected:
169 /**
170 * Get quantile.
171 *
172 * \param __begin begin of data
173 * \param __end end of data
174 * \param W weight
175 * \return value
176 */
177 template<class T>
178 static double getQuantile(T __begin, T __end, const double W)
179 {
180 double w = 0.0;
181
182 for (T i = __begin; i != __end; ++i) {
183
184 w += i->second;
185
186 if (w >= W) {
187 return i->first;
188 }
189 }
190
191 THROW(JNoValue, "Invalid weight " << W);
192 }
193
195 };
196}
197
198#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
I/O manipulators.
Base class for data structures with artithmetic capabilities.
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
Exception for missing value.
Auxiliary class for title.
Definition JTitle.hh:19
std::string title
Definition JTitle.hh:73
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition JVectorize.hh:54
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Auxiliary data structure for return type of make methods.
Definition JVectorize.hh:28
Auxiliary base class for aritmetic operations of derived class types.
Definition JMath.hh:347
Auxiliary data structure for running average, standard deviation and quantiles.
Definition JQuantile.hh:41
JQuantile(const JTitle &title="")
Constructor.
Definition JQuantile.hh:47
double getQuantile(const double Q, const Quantile_t option=forward_t) const
Get quantile.
Definition JQuantile.hh:147
void put(const array_type< JElement_t, JAllocator_t > &buffer, const double w=1.0)
Put data.
Definition JQuantile.hh:121
Quantile_t
Options for evaluation of quantile.
Definition JQuantile.hh:133
@ backward_t
backward
Definition JQuantile.hh:136
@ symmetric_t
symmatric
Definition JQuantile.hh:135
JQuantile(const JTitle &title, const array_type< JElement_t, JAllocator_t > &buffer, const double w=1.0)
Constructor.
Definition JQuantile.hh:62
JQuantile & add(const JQuantile &Q)
Add quantile.
Definition JQuantile.hh:90
static double getQuantile(T __begin, T __end, const double W)
Get quantile.
Definition JQuantile.hh:178
void reset()
Reset.
Definition JQuantile.hh:76
void put(const double x, const double w=1.0)
Put value.
Definition JQuantile.hh:106
std::multimap< double, double > buffer
Definition JQuantile.hh:194
Auxiliary data structure for running average and standard deviation.
Definition JStats.hh:44
void put(const double x, const double w=1.0)
Put value.
Definition JStats.hh:119
void reset()
Reset.
Definition JStats.hh:79
JStats & add(const JStats &Q)
Add stats.
Definition JStats.hh:98