Jpp 19.3.0-rc.3
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:
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="", const bool quantiles=false)
 Constructor.
 
template<class JElement_t , class JAllocator_t >
 JQuantile (const JTitle &title, const array_type< JElement_t, JAllocator_t > &buffer, const bool quantiles=false, 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.
 
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.
 
double getQuantile (const double Q, const Quantile_t option=forward_t) const
 Get quantile.
 
std::ostream & print (std::ostream &out, bool lpr=true) const
 Print quantile.
 
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.
 

Static Protected Member Functions

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

Protected Attributes

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

Friends

std::ostream & operator<< (std::ostream &out, const JQuantile &quantile)
 Print quantile.
 

Detailed Description

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

See Knuth TAOCP vol 2, 3rd edition, page 232.
This class acts as a zero-dimensional histogram.
Note that if a weight is used, it should strictly be positive.

Definition at line 43 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 335 of file JQuantile.hh.

335 {
336 forward_t = +1, //!< forward
337 symmetric_t = 0, //!< symmatric
338 backward_t = -1 //!< backward
339 };
@ backward_t
backward
Definition JQuantile.hh:338
@ symmetric_t
symmatric
Definition JQuantile.hh:337

Constructor & Destructor Documentation

◆ JQuantile() [1/2]

JTOOLS::JQuantile::JQuantile ( const JTitle & title = "",
const bool quantiles = false )
inline

Constructor.

Parameters
titletitle
quantilesquantiles

Definition at line 53 of file JQuantile.hh.

54 :
57 {
58 reset();
59 }
std::string title
Definition JTitle.hh:73
JTitle()
Default constructor.
Definition JTitle.hh:24
void reset()
Reset.
Definition JQuantile.hh:87

◆ 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 bool quantiles = false,
const double w = 1.0 )
inline

Constructor.

Parameters
titletitle
bufferinput data
quantilesquantiles
wweight

Definition at line 71 of file JQuantile.hh.

74 :
77 {
78 reset();
79
80 put(buffer, w);
81 }
void put(const double x, const double w=1.0)
Put value.
Definition JQuantile.hh:133
std::multimap< double, double > buffer
Definition JQuantile.hh:452

Member Function Documentation

◆ reset()

void JTOOLS::JQuantile::reset ( )
inline

Reset.

Definition at line 87 of file JQuantile.hh.

88 {
89 mean = 0.0;
90 sigma = 0.0;
91 total = 0.0;
92 count = 0;
93 xmin = std::numeric_limits<double>::max();
94 xmax = std::numeric_limits<double>::lowest();
95 wmin = std::numeric_limits<double>::max();
96 wmax = std::numeric_limits<double>::lowest();
97
98 buffer.clear();
99 }
long long int count
Definition JQuantile.hh:447

◆ add()

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

Add quantile.

Parameters
Qquantile
Returns
this quantile

Definition at line 108 of file JQuantile.hh.

109 {
110 mean += Q.mean;
111 sigma += Q.sigma;
112 total += Q.total;
113 count += Q.count;
114 xmin = std::min(xmin, Q.xmin);
115 xmax = std::max(xmax, Q.xmax);
116 wmin = std::min(wmin, Q.wmin);
117 wmax = std::max(wmax, Q.wmax);
118
119 if (quantiles) {
120 std::copy(Q.buffer.begin(), Q.buffer.end(), std::inserter(buffer, buffer.end()));
121 }
122
123 return *this;
124 }

◆ put() [1/2]

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

Put value.

Parameters
xvalue
wweight

Definition at line 133 of file JQuantile.hh.

134 {
135 total += w;
136 count += 1;
137
138 if (count == 1) {
139
140 mean = x;
141 sigma = 0.0;
142
143 } else {
144
145 const double new_mean = mean + w * (x - mean) / total;
146 const double new_sigma = sigma + w * (x - mean) * (x - new_mean);
147
148 // set up for next iteration
149
150 mean = new_mean;
151 sigma = new_sigma;
152 }
153
154 xmin = std::min(xmin, x);
155 xmax = std::max(xmax, x);
156 wmin = std::min(wmin, w);
157 wmax = std::max(wmax, w);
158
159 if (quantiles) {
160 buffer.insert(std::make_pair(x,w));
161 }
162 }

◆ 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 172 of file JQuantile.hh.

174 {
175 for (typename array_type<JElement_t, JAllocator_t>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
176 put(*i, w);
177 }
178 }

◆ getCount()

long long int JTOOLS::JQuantile::getCount ( ) const
inline

Get total count.

Returns
count

Definition at line 186 of file JQuantile.hh.

187 {
188 return count;
189 }

◆ getTotal()

double JTOOLS::JQuantile::getTotal ( ) const
inline

Get total weight.

Returns
weight

Definition at line 197 of file JQuantile.hh.

198 {
199 return total;
200 }

◆ getXmin()

double JTOOLS::JQuantile::getXmin ( ) const
inline

Get minimum value.

Returns
minimum value

Definition at line 208 of file JQuantile.hh.

209 {
210 return xmin;
211 }

◆ getXmax()

double JTOOLS::JQuantile::getXmax ( ) const
inline

Get maximum value.

Returns
maximum value

Definition at line 219 of file JQuantile.hh.

220 {
221 return xmax;
222 }

◆ getWmin()

double JTOOLS::JQuantile::getWmin ( ) const
inline

Get minimum weight.

Returns
minimum weight

Definition at line 230 of file JQuantile.hh.

231 {
232 return wmin;
233 }

◆ getWmax()

double JTOOLS::JQuantile::getWmax ( ) const
inline

Get maximum weight.

Returns
maximum weight

Definition at line 241 of file JQuantile.hh.

242 {
243 return wmax;
244 }

◆ getMean() [1/2]

double JTOOLS::JQuantile::getMean ( ) const
inline

Get mean value.

Returns
mean value

Definition at line 252 of file JQuantile.hh.

253 {
254 if (count != 0.0)
255 return mean;
256 else
257 THROW(JDivisionByZero, "JQuantile::getMean()");
258 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.

◆ getMean() [2/2]

double JTOOLS::JQuantile::getMean ( const double value) const
inline

Get mean value.

Parameters
valuedefault value
Returns
mean value

Definition at line 267 of file JQuantile.hh.

268 {
269 if (count != 0.0)
270 return getMean();
271 else
272 return value;
273 }
double getMean() const
Get mean value.
Definition JQuantile.hh:252

◆ getSTDev() [1/2]

double JTOOLS::JQuantile::getSTDev ( ) const
inline

Get standard deviation.

Returns
standard deviation

Definition at line 281 of file JQuantile.hh.

282 {
283 if (count > 1)
284 return sqrt(count * sigma/(total * (count - 1)));
285 else
286 THROW(JDivisionByZero, "JQuantile::getSTDev()");
287 }

◆ getSTDev() [2/2]

double JTOOLS::JQuantile::getSTDev ( const double value) const
inline

Get standard deviation.

Parameters
valuedefault value
Returns
standard deviation

Definition at line 296 of file JQuantile.hh.

297 {
298 if (count > 1)
299 return getSTDev();
300 else
301 return value;
302 }
double getSTDev() const
Get standard deviation.
Definition JQuantile.hh:281

◆ getDeviation()

double JTOOLS::JQuantile::getDeviation ( const bool relative = true) const
inline

Get maximal deviation from average.

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

Definition at line 311 of file JQuantile.hh.

312 {
313 if (relative)
314 return std::max(getXmax() - getMean(), getMean() - getXmin());
315 else
316 return getXmax() - getXmin();
317 }
double getXmin() const
Get minimum value.
Definition JQuantile.hh:208
double getXmax() const
Get maximum value.
Definition JQuantile.hh:219

◆ hasAccuracy()

bool JTOOLS::JQuantile::hasAccuracy ( const double precision) const
inline

Test relative accuracy.

Parameters
precisionrelative precision
Returns
true if reached accuracy; else false

Definition at line 326 of file JQuantile.hh.

327 {
328 return getCount() > 1 && getSTDev() < precision * getMean();
329 }
long long int getCount() const
Get total count.
Definition JQuantile.hh:186

◆ 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 349 of file JQuantile.hh.

350 {
351 if (quantiles) {
352
353 double W = 0.0;
354
355 for (std::map<double, double>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
356 W += i->second;
357 }
358
359 switch (option) {
360 case forward_t:
361 return (getQuantile(buffer. begin(), buffer. end(), Q*W));
362 case symmetric_t:
363 return (getQuantile(buffer.rbegin(), buffer.rend(), 0.5*(1.0 - Q)*W) -
364 getQuantile(buffer. begin(), buffer. end(), 0.5*(1.0 - Q)*W));
365 case backward_t:
366 return (getQuantile(buffer.rbegin(), buffer.rend(), Q*W));
367 default:
368 THROW(JNoValue, "Invalid option " << option);
369 }
370 }
371
372 THROW(JNoValue, "Option 'quantiles' at JQuantile() incompatible with method getQuantile().");
373 }
double getQuantile(const double Q, const Quantile_t option=forward_t) const
Get quantile.
Definition JQuantile.hh:349

◆ print()

std::ostream & JTOOLS::JQuantile::print ( std::ostream & out,
bool lpr = true ) const
inline

Print quantile.

Parameters
outoutput stream
lprlong print

Definition at line 382 of file JQuantile.hh.

383 {
384 using namespace std;
385
386 const int nc = getTitle().size();
387
388 if (lpr) {
389 out << setw(nc) << left << " " << ' '
390 << setw(12) << left << " mean" << ' '
391 << setw(12) << left << " STD" << ' '
392 << setw(12) << left << " deviation" << endl;
393 }
394
395 out << setw(nc) << left << getTitle() << ' '
396 << SCIENTIFIC(12,5) << getMean() << ' '
397 << SCIENTIFIC(12,5) << getSTDev() << ' '
398 << SCIENTIFIC(12,5) << getDeviation(false) << endl;
399
400 return out;
401 }
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
double getDeviation(const bool relative=true) const
Get maximal deviation from average.
Definition JQuantile.hh:311
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488

◆ 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 426 of file JQuantile.hh.

427 {
428 double w = 0.0;
429
430 for (T i = __begin; i != __end; ++i) {
431
432 w += i->second;
433
434 if (w >= W) {
435 return i->first;
436 }
437 }
438
439 THROW(JNoValue, "Invalid weight " << W);
440 }

◆ 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()

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 }

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
const JQuantile & quantile )
friend

Print quantile.

Parameters
outoutput stream
quantilequantile
Returns
output stream

Definition at line 411 of file JQuantile.hh.

412 {
413 return quantile.print(out, getLongprint(out));
414 }
bool getLongprint(std::ostream &out)
Get long print option.
Definition JManip.hh:121

Member Data Documentation

◆ quantiles

bool JTOOLS::JQuantile::quantiles
protected

Definition at line 443 of file JQuantile.hh.

◆ mean

double JTOOLS::JQuantile::mean
protected

Definition at line 444 of file JQuantile.hh.

◆ sigma

double JTOOLS::JQuantile::sigma
protected

Definition at line 445 of file JQuantile.hh.

◆ total

double JTOOLS::JQuantile::total
protected

Definition at line 446 of file JQuantile.hh.

◆ count

long long int JTOOLS::JQuantile::count
protected

Definition at line 447 of file JQuantile.hh.

◆ xmin

double JTOOLS::JQuantile::xmin
protected

Definition at line 448 of file JQuantile.hh.

◆ xmax

double JTOOLS::JQuantile::xmax
protected

Definition at line 449 of file JQuantile.hh.

◆ wmin

double JTOOLS::JQuantile::wmin
protected

Definition at line 450 of file JQuantile.hh.

◆ wmax

double JTOOLS::JQuantile::wmax
protected

Definition at line 451 of file JQuantile.hh.

◆ buffer

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

Definition at line 452 of file JQuantile.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: