Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JTOOLS::JQuantile Struct Reference

Auxiliary data structure for running average, standard deviation and quantiles. More...

#include <JQuantile.hh>

Inheritance diagram for JTOOLS::JQuantile:
JTOOLS::JStats JMATH::JMath< JFirst_t, JSecond_t > JLANG::JTitle JMATH::JMath< JFirst_t, JSecond_t >

Public Types

enum  Quantile_t { forward_t = +1 , symmetric_t = 0 , backward_t = -1 }
 Options for evaluation of quantile. More...
 

Public Member Functions

 JQuantile (const JTitle &title="")
 Constructor.
 
template<class JElement_t , class JAllocator_t >
 JQuantile (const JTitle &title, const array_type< JElement_t, JAllocator_t > &buffer, const double w=1.0)
 Constructor.
 
void reset ()
 Reset.
 
JQuantileadd (const JQuantile &Q)
 Add quantile.
 
void put (const double x, const double w=1.0)
 Put value.
 
template<class JElement_t , class JAllocator_t >
void put (const array_type< JElement_t, JAllocator_t > &buffer, const double w=1.0)
 Put data.
 
double getQuantile (const double Q, const Quantile_t option=forward_t) const
 Get quantile.
 
JStatsadd (const JStats &Q)
 Add stats.
 
long long int getCount () const
 Get total count.
 
double getTotal () const
 Get total weight.
 
double getXmin () const
 Get minimum value.
 
double getXmax () const
 Get maximum value.
 
double getWmin () const
 Get minimum weight.
 
double getWmax () const
 Get maximum weight.
 
double getMean () const
 Get mean value.
 
double getMean (const double value) const
 Get mean value.
 
double getSTDev () const
 Get standard deviation.
 
double getSTDev (const double value) const
 Get standard deviation.
 
double getDeviation (const bool relative=true) const
 Get maximal deviation from average.
 
bool hasAccuracy (const double precision) const
 Test relative accuracy.
 
std::ostream & print (std::ostream &out, bool lpr=true) const
 Print stats.
 
const std::string & getTitle () const
 Get title.
 
void setTitle (const std::string &title)
 Set title.
 
JFirst_t & mul (const JSecond_t &object)
 Multiply with object.
 
JFirst_t & mul (const JSecond_t &object)
 Multiply with object.
 

Static Protected Member Functions

template<class T >
static double getQuantile (T __begin, T __end, const double W)
 Get quantile.
 

Protected Attributes

std::multimap< double, double > buffer
 
double mean
 
double sigma
 
double total
 
long long int count
 
double xmin
 
double xmax
 
double wmin
 
double wmax
 
std::string title
 

Detailed Description

Auxiliary data structure for running average, standard deviation and quantiles.

Definition at line 38 of file JQuantile.hh.

Member Enumeration Documentation

◆ Quantile_t

Options for evaluation of quantile.

Enumerator
forward_t 

forward

symmetric_t 

symmatric

backward_t 

backward

Definition at line 133 of file JQuantile.hh.

133 {
134 forward_t = +1, //!< forward
135 symmetric_t = 0, //!< symmatric
136 backward_t = -1 //!< backward
137 };
@ backward_t
backward
Definition JQuantile.hh:136
@ symmetric_t
symmatric
Definition JQuantile.hh:135

Constructor & Destructor Documentation

◆ JQuantile() [1/2]

JTOOLS::JQuantile::JQuantile ( const JTitle & title = "")
inline

Constructor.

Parameters
titletitle

Definition at line 47 of file JQuantile.hh.

47 :
49 {
50 reset();
51 }
std::string title
Definition JTitle.hh:73
void reset()
Reset.
Definition JQuantile.hh:76
JStats(const JTitle &title="")
Constructor.
Definition JStats.hh:50

◆ JQuantile() [2/2]

template<class JElement_t , class JAllocator_t >
JTOOLS::JQuantile::JQuantile ( const JTitle & title,
const array_type< JElement_t, JAllocator_t > & buffer,
const double w = 1.0 )
inline

Constructor.

Parameters
titletitle
bufferinput data
wweight

Definition at line 62 of file JQuantile.hh.

64 :
66 {
67 reset();
68
69 put(buffer, w);
70 }
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

Member Function Documentation

◆ reset()

void JTOOLS::JQuantile::reset ( )
inline

Reset.

Definition at line 76 of file JQuantile.hh.

77 {
79
80 buffer.clear();
81 }
void reset()
Reset.
Definition JStats.hh:79

◆ add() [1/2]

JQuantile & JTOOLS::JQuantile::add ( const JQuantile & Q)
inline

Add quantile.

Parameters
Qquantile
Returns
this quantile

Definition at line 90 of file JQuantile.hh.

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 }
JStats & add(const JStats &Q)
Add stats.
Definition JStats.hh:98

◆ put() [1/2]

void JTOOLS::JQuantile::put ( const double x,
const double w = 1.0 )
inline

Put value.

Parameters
xvalue
wweight

Definition at line 106 of file JQuantile.hh.

107 {
108 JStats::put(x, w);
109
110 buffer.insert(std::make_pair(x,w));
111 }
void put(const double x, const double w=1.0)
Put value.
Definition JStats.hh:119

◆ put() [2/2]

template<class JElement_t , class JAllocator_t >
void JTOOLS::JQuantile::put ( const array_type< JElement_t, JAllocator_t > & buffer,
const double w = 1.0 )
inline

Put data.

Parameters
bufferinput data
wweight

Definition at line 121 of file JQuantile.hh.

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 }

◆ getQuantile() [1/2]

double JTOOLS::JQuantile::getQuantile ( const double Q,
const Quantile_t option = forward_t ) const
inline

Get quantile.

Parameters
Qquantile
optionoption
Returns
value

Definition at line 147 of file JQuantile.hh.

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 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
double getQuantile(const double Q, const Quantile_t option=forward_t) const
Get quantile.
Definition JQuantile.hh:147

◆ getQuantile() [2/2]

template<class T >
static double JTOOLS::JQuantile::getQuantile ( T __begin,
T __end,
const double W )
inlinestaticprotected

Get quantile.

Parameters
__beginbegin of data
__endend of data
Wweight
Returns
value

Definition at line 178 of file JQuantile.hh.

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 }

◆ add() [2/2]

JStats & JTOOLS::JStats::add ( const JStats & Q)
inlineinherited

Add stats.

Parameters
Qstats
Returns
this stats

Definition at line 98 of file JStats.hh.

99 {
100 mean += Q.mean;
101 sigma += Q.sigma;
102 total += Q.total;
103 count += Q.count;
104 xmin = std::min(xmin, Q.xmin);
105 xmax = std::max(xmax, Q.xmax);
106 wmin = std::min(wmin, Q.wmin);
107 wmax = std::max(wmax, Q.wmax);
108
109 return *this;
110 }
double sigma
Definition JStats.hh:356
double mean
Definition JStats.hh:355
double wmin
Definition JStats.hh:361
double wmax
Definition JStats.hh:362
long long int count
Definition JStats.hh:358
double xmax
Definition JStats.hh:360
double total
Definition JStats.hh:357
double xmin
Definition JStats.hh:359

◆ getCount()

long long int JTOOLS::JStats::getCount ( ) const
inlineinherited

Get total count.

Returns
count

Definition at line 168 of file JStats.hh.

169 {
170 return count;
171 }

◆ getTotal()

double JTOOLS::JStats::getTotal ( ) const
inlineinherited

Get total weight.

Returns
weight

Definition at line 179 of file JStats.hh.

180 {
181 return total;
182 }

◆ getXmin()

double JTOOLS::JStats::getXmin ( ) const
inlineinherited

Get minimum value.

Returns
minimum value

Definition at line 190 of file JStats.hh.

191 {
192 return xmin;
193 }

◆ getXmax()

double JTOOLS::JStats::getXmax ( ) const
inlineinherited

Get maximum value.

Returns
maximum value

Definition at line 201 of file JStats.hh.

202 {
203 return xmax;
204 }

◆ getWmin()

double JTOOLS::JStats::getWmin ( ) const
inlineinherited

Get minimum weight.

Returns
minimum weight

Definition at line 212 of file JStats.hh.

213 {
214 return wmin;
215 }

◆ getWmax()

double JTOOLS::JStats::getWmax ( ) const
inlineinherited

Get maximum weight.

Returns
maximum weight

Definition at line 223 of file JStats.hh.

224 {
225 return wmax;
226 }

◆ getMean() [1/2]

double JTOOLS::JStats::getMean ( ) const
inlineinherited

Get mean value.

Returns
mean value

Definition at line 234 of file JStats.hh.

235 {
236 if (count != 0.0)
237 return mean;
238 else
239 THROW(JDivisionByZero, "JStats::getMean() count " << count);
240 }

◆ getMean() [2/2]

double JTOOLS::JStats::getMean ( const double value) const
inlineinherited

Get mean value.

Parameters
valuedefault value
Returns
mean value

Definition at line 249 of file JStats.hh.

250 {
251 if (count != 0.0)
252 return getMean();
253 else
254 return value;
255 }
double getMean() const
Get mean value.
Definition JStats.hh:234

◆ getSTDev() [1/2]

double JTOOLS::JStats::getSTDev ( ) const
inlineinherited

Get standard deviation.

Returns
standard deviation

Definition at line 263 of file JStats.hh.

264 {
265 if (count > 1)
266 return sqrt(count * sigma/(total * (count - 1)));
267 else
268 THROW(JDivisionByZero, "JStats::getSTDev() count " << count);
269 }

◆ getSTDev() [2/2]

double JTOOLS::JStats::getSTDev ( const double value) const
inlineinherited

Get standard deviation.

Parameters
valuedefault value
Returns
standard deviation

Definition at line 278 of file JStats.hh.

279 {
280 if (count > 1)
281 return getSTDev();
282 else
283 return value;
284 }
double getSTDev() const
Get standard deviation.
Definition JStats.hh:263

◆ getDeviation()

double JTOOLS::JStats::getDeviation ( const bool relative = true) const
inlineinherited

Get maximal deviation from average.

Parameters
relativeif true, relative to average, else absolute
Returns
deviation

Definition at line 293 of file JStats.hh.

294 {
295 if (relative)
296 return std::max(getXmax() - getMean(), getMean() - getXmin());
297 else
298 return getXmax() - getXmin();
299 }
double getXmax() const
Get maximum value.
Definition JStats.hh:201
double getXmin() const
Get minimum value.
Definition JStats.hh:190

◆ hasAccuracy()

bool JTOOLS::JStats::hasAccuracy ( const double precision) const
inlineinherited

Test relative accuracy.

Parameters
precisionrelative precision
Returns
true if reached accuracy; else false

Definition at line 308 of file JStats.hh.

309 {
310 return getCount() > 1 && getSTDev() < precision * getMean();
311 }
long long int getCount() const
Get total count.
Definition JStats.hh:168

◆ print()

std::ostream & JTOOLS::JStats::print ( std::ostream & out,
bool lpr = true ) const
inlineinherited

Print stats.

Parameters
outoutput stream
lprlong print

Definition at line 320 of file JStats.hh.

321 {
322 using namespace std;
323
324 const int nc = getTitle().size();
325
326 if (lpr) {
327 out << setw(nc) << left << " " << ' '
328 << setw(12) << left << " mean" << ' '
329 << setw(12) << left << " STD" << ' '
330 << setw(12) << left << " deviation" << endl;
331 }
332
333 out << setw(nc) << left << getTitle() << ' '
334 << SCIENTIFIC(12,5) << getMean() << ' '
335 << SCIENTIFIC(12,5) << getSTDev() << ' '
336 << SCIENTIFIC(12,5) << getDeviation(false) << endl;
337
338 return out;
339 }
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
double getDeviation(const bool relative=true) const
Get maximal deviation from average.
Definition JStats.hh:293
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488

◆ getTitle()

const std::string & JLANG::JTitle::getTitle ( ) const
inlineinherited

Get title.

Returns
title

Definition at line 55 of file JTitle.hh.

56 {
57 return this->title;
58 }

◆ setTitle()

void JLANG::JTitle::setTitle ( const std::string & title)
inlineinherited

Set title.

Parameters
titletitle

Definition at line 66 of file JTitle.hh.

67 {
68 this->title = title;
69 }

◆ mul() [1/2]

template<class JFirst_t , class JSecond_t >
JFirst_t & JMATH::JMath< JFirst_t, JSecond_t >::mul ( const JSecond_t & object)
inlineinherited

Multiply with object.

Parameters
objectobject
Returns
result object

Definition at line 354 of file JMath.hh.

355 {
356 return static_cast<JFirst_t&>(*this) = JFirst_t().mul(static_cast<const JFirst_t&>(*this), object);
357 }

◆ mul() [2/2]

template<class JFirst_t , class JSecond_t >
JFirst_t & JMATH::JMath< JFirst_t, JSecond_t >::mul ( const JSecond_t & object)
inlineinherited

Multiply with object.

Parameters
objectobject
Returns
result object

Definition at line 354 of file JMath.hh.

355 {
356 return static_cast<JFirst_t&>(*this) = JFirst_t().mul(static_cast<const JFirst_t&>(*this), object);
357 }

Member Data Documentation

◆ buffer

std::multimap<double, double> JTOOLS::JQuantile::buffer
protected

Definition at line 194 of file JQuantile.hh.

◆ mean

double JTOOLS::JStats::mean
protectedinherited

Definition at line 355 of file JStats.hh.

◆ sigma

double JTOOLS::JStats::sigma
protectedinherited

Definition at line 356 of file JStats.hh.

◆ total

double JTOOLS::JStats::total
protectedinherited

Definition at line 357 of file JStats.hh.

◆ count

long long int JTOOLS::JStats::count
protectedinherited

Definition at line 358 of file JStats.hh.

◆ xmin

double JTOOLS::JStats::xmin
protectedinherited

Definition at line 359 of file JStats.hh.

◆ xmax

double JTOOLS::JStats::xmax
protectedinherited

Definition at line 360 of file JStats.hh.

◆ wmin

double JTOOLS::JStats::wmin
protectedinherited

Definition at line 361 of file JStats.hh.

◆ wmax

double JTOOLS::JStats::wmax
protectedinherited

Definition at line 362 of file JStats.hh.

◆ title

std::string JLANG::JTitle::title
protectedinherited

Definition at line 73 of file JTitle.hh.


The documentation for this struct was generated from the following file: