Jpp  18.0.0-rc.4
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Static Protected Member Functions | Protected Attributes | Friends | List of all members
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. More...
 
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. More...
 
void reset ()
 Reset. More...
 
JQuantileadd (const JQuantile &Q)
 Add quantile. More...
 
void put (const double x, const double w=1.0)
 Put value. More...
 
template<class JElement_t , class JAllocator_t >
void put (const array_type< JElement_t, JAllocator_t > &buffer, const double w=1.0)
 Put data. More...
 
long long int getCount () const
 Get total count. More...
 
double getTotal () const
 Get total weight. More...
 
double getXmin () const
 Get minimum value. More...
 
double getXmax () const
 Get maximum value. More...
 
double getWmin () const
 Get minimum weight. More...
 
double getWmax () const
 Get maximum weight. More...
 
double getMean () const
 Get mean value. More...
 
double getMean (const double value) const
 Get mean value. More...
 
double getSTDev () const
 Get standard deviation. More...
 
double getSTDev (const double value) const
 Get standard deviation. More...
 
double getDeviation (const bool relative=true) const
 Get maximal deviation from average. More...
 
bool hasAccuracy (const double precision) const
 Test relative accuracy. More...
 
double getQuantile (const double Q, const Quantile_t option=forward_t) const
 Get quantile. More...
 
std::ostream & print (std::ostream &out, bool lpr=true) const
 Print quantile. More...
 
const std::stringgetTitle () const
 Get title. More...
 
void setTitle (const std::string &title)
 Set title. More...
 
JFirst_t & mul (const JSecond_t &object)
 Multiply with object. More...
 

Static Protected Member Functions

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

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. More...
 

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

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  };

Constructor & Destructor Documentation

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

Constructor.

Parameters
titletitle
quantilesquantiles

Definition at line 53 of file JQuantile.hh.

54  :
55  JTitle(title),
57  {
58  reset();
59  }
void reset()
Reset.
Definition: JQuantile.hh:87
JTitle()
Default constructor.
Definition: JTitle.hh:24
std::string title
Definition: JTitle.hh:73
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  :
75  JTitle(title),
77  {
78  reset();
79 
80  put(buffer, w);
81  }
data_type w[N+1][M+1]
Definition: JPolint.hh:778
void reset()
Reset.
Definition: JQuantile.hh:87
std::multimap< double, double > buffer
Definition: JQuantile.hh:452
JTitle()
Default constructor.
Definition: JTitle.hh:24
void put(const double x, const double w=1.0)
Put value.
Definition: JQuantile.hh:133
std::string title
Definition: JTitle.hh:73

Member Function Documentation

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
std::multimap< double, double > buffer
Definition: JQuantile.hh:452
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  }
long long int count
Definition: JQuantile.hh:447
std::multimap< double, double > buffer
Definition: JQuantile.hh:452
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
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  }
data_type w[N+1][M+1]
Definition: JPolint.hh:778
long long int count
Definition: JQuantile.hh:447
std::multimap< double, double > buffer
Definition: JQuantile.hh:452
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  }
data_type w[N+1][M+1]
Definition: JPolint.hh:778
std::multimap< double, double > buffer
Definition: JQuantile.hh:452
void put(const double x, const double w=1.0)
Put value.
Definition: JQuantile.hh:133
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  }
long long int count
Definition: JQuantile.hh:447
double JTOOLS::JQuantile::getTotal ( ) const
inline

Get total weight.

Returns
weight

Definition at line 197 of file JQuantile.hh.

198  {
199  return total;
200  }
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  }
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  }
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  }
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  }
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.
Definition: JException.hh:696
long long int count
Definition: JQuantile.hh:447
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  }
long long int count
Definition: JQuantile.hh:447
double getMean() const
Get mean value.
Definition: JQuantile.hh:252
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  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
long long int count
Definition: JQuantile.hh:447
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
long long int count
Definition: JQuantile.hh:447
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 getMean() const
Get mean value.
Definition: JQuantile.hh:252
double getXmax() const
Get maximum value.
Definition: JQuantile.hh:219
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  }
double getSTDev() const
Get standard deviation.
Definition: JQuantile.hh:281
double getMean() const
Get mean value.
Definition: JQuantile.hh:252
long long int getCount() const
Get total count.
Definition: JQuantile.hh:186
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  }
Q(UTCMax_s-UTCMin_s)-livetime_s
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
std::multimap< double, double > buffer
Definition: JQuantile.hh:452
double getQuantile(const double Q, const Quantile_t option=forward_t) const
Get quantile.
Definition: JQuantile.hh:349
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  }
double getSTDev() const
Get standard deviation.
Definition: JQuantile.hh:281
const std::string & getTitle() const
Get title.
Definition: JTitle.hh:55
double getMean() const
Get mean value.
Definition: JQuantile.hh:252
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:484
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  }
data_type w[N+1][M+1]
Definition: JPolint.hh:778
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
do set_variable OUTPUT_DIRECTORY $WORKDIR T
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  }
std::string title
Definition: JTitle.hh:73
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  }
std::string title
Definition: JTitle.hh:73
template<class JFirst_t, class JSecond_t = JNullType>
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 Function Documentation

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  }
std::ostream & print(std::ostream &out, bool lpr=true) const
Print quantile.
Definition: JQuantile.hh:382
bool getLongprint(std::ostream &out)
Get long print option.
Definition: JManip.hh:121

Member Data Documentation

bool JTOOLS::JQuantile::quantiles
protected

Definition at line 443 of file JQuantile.hh.

double JTOOLS::JQuantile::mean
protected

Definition at line 444 of file JQuantile.hh.

double JTOOLS::JQuantile::sigma
protected

Definition at line 445 of file JQuantile.hh.

double JTOOLS::JQuantile::total
protected

Definition at line 446 of file JQuantile.hh.

long long int JTOOLS::JQuantile::count
protected

Definition at line 447 of file JQuantile.hh.

double JTOOLS::JQuantile::xmin
protected

Definition at line 448 of file JQuantile.hh.

double JTOOLS::JQuantile::xmax
protected

Definition at line 449 of file JQuantile.hh.

double JTOOLS::JQuantile::wmin
protected

Definition at line 450 of file JQuantile.hh.

double JTOOLS::JQuantile::wmax
protected

Definition at line 451 of file JQuantile.hh.

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

Definition at line 452 of file JQuantile.hh.

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: