Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Protected Member Functions | Protected Attributes | Friends | List of all members
JTOOLS::JQuantile Class Reference

Quantile calculator. More...

#include <JQuantile.hh>

Inheritance diagram for JTOOLS::JQuantile:
JLANG::JTitle

Public Member Functions

 JQuantile (const JTitle &title="", const int option=0)
 Constructor. More...
 
void reset ()
 Reset. More...
 
void put (const double x, const double w=1.0)
 Put value. More...
 
long long int getCount () const
 Get total count. More...
 
double getTotal () const
 Get total weight. More...
 
double getSum () const
 Get weighted sum. More...
 
double getMean () const
 Get mean value. More...
 
double getRMS () const
 Get RMS. More...
 
double getSTDev () 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 bool reverse=false) const
 Get quantile. More...
 
std::ostream & print (std::ostream &out, bool lpr=true) const
 Print quantile. More...
 
const std::string & getTitle () const
 Get title. More...
 
void setTitle (const std::string &title)
 Set title. More...
 

Static Protected Member Functions

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

Protected Attributes

double sum
 
double rms
 
double tot
 
double min
 
double max
 
long long int num
 
bool quantiles
 
std::map< double, double > buffer
 
std::string title
 

Friends

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

Detailed Description

Quantile calculator.

This class acts as a zero-dimensional histogram.

Definition at line 34 of file JQuantile.hh.

Constructor & Destructor Documentation

JTOOLS::JQuantile::JQuantile ( const JTitle title = "",
const int  option = 0 
)
inline

Constructor.

Parameters
titletitle
optionoption

Definition at line 44 of file JQuantile.hh.

45  :
46  JTitle (title),
47  quantiles(false)
48  {
49  reset();
50 
51  quantiles = (option > 0);
52  }
JTitle()
Default constructor.
Definition: JTitle.hh:24
void reset()
Reset.
Definition: JQuantile.hh:58
std::string title
Definition: JTitle.hh:73

Member Function Documentation

void JTOOLS::JQuantile::reset ( )
inline

Reset.

Definition at line 58 of file JQuantile.hh.

59  {
60  sum = 0.0;
61  rms = 0.0;
62  tot = 0.0;
63  num = 0;
64  min = +std::numeric_limits<double>::max();
65  max = -std::numeric_limits<double>::max();
66 
67  buffer.clear();
68  }
long long int num
Definition: JQuantile.hh:268
std::map< double, double > buffer
Definition: JQuantile.hh:271
void JTOOLS::JQuantile::put ( const double  x,
const double  w = 1.0 
)
inline

Put value.

Parameters
xvalue
wweight

Definition at line 77 of file JQuantile.hh.

78  {
79  sum += w*x;
80  rms += w*x*x;
81  tot += w;
82  num += 1;
83  min = std::min(min, x);
84  max = std::max(max, x);
85 
86  if (quantiles) {
87  buffer.insert(std::make_pair(x,w));
88  }
89  }
long long int num
Definition: JQuantile.hh:268
std::map< double, double > buffer
Definition: JQuantile.hh:271
long long int JTOOLS::JQuantile::getCount ( ) const
inline

Get total count.

Returns
count

Definition at line 97 of file JQuantile.hh.

98  {
99  return num;
100  }
long long int num
Definition: JQuantile.hh:268
double JTOOLS::JQuantile::getTotal ( ) const
inline

Get total weight.

Returns
weight

Definition at line 108 of file JQuantile.hh.

109  {
110  return tot;
111  }
double JTOOLS::JQuantile::getSum ( ) const
inline

Get weighted sum.

Returns
weighted sum

Definition at line 120 of file JQuantile.hh.

121  {
122  return sum;
123  }
double JTOOLS::JQuantile::getMean ( ) const
inline

Get mean value.

Returns
mean value

Definition at line 131 of file JQuantile.hh.

132  {
133  if (tot != 0.0)
134  return sum / tot;
135  else
136  throw JDivisionByZero("JQuantile::getMean()");
137  }
double JTOOLS::JQuantile::getRMS ( ) const
inline

Get RMS.

Returns
RMS

Definition at line 145 of file JQuantile.hh.

146  {
147  if (tot != 0.0)
148  return sqrt(rms/tot);
149  else
150  throw JDivisionByZero("JQuantile::getRMS()");
151  }
double JTOOLS::JQuantile::getSTDev ( ) const
inline

Get standard deviation.

Returns
standard deviation

Definition at line 159 of file JQuantile.hh.

160  {
161  if (tot != 0.0)
162  return sqrt(rms*tot - sum*sum)/tot;
163  else
164  throw JDivisionByZero("JQuantile::getSTDev()");
165  }
double JTOOLS::JQuantile::getDeviation ( const bool  relative = true) const
inline

Get maximal deviation from average.

Parameters
relativerelative to average or absolute
Returns
deviation

Definition at line 174 of file JQuantile.hh.

175  {
176  if (relative)
177  return std::max(max - getMean(), getMean() - min);
178  else
179  return std::max(fabs(max), fabs(min));
180  }
double getMean() const
Get mean value.
Definition: JQuantile.hh:131
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 189 of file JQuantile.hh.

190  {
191  return getCount() > 3 && getSTDev() < precision * getMean();
192  }
double getSTDev() const
Get standard deviation.
Definition: JQuantile.hh:159
double getMean() const
Get mean value.
Definition: JQuantile.hh:131
long long int getCount() const
Get total count.
Definition: JQuantile.hh:97
double JTOOLS::JQuantile::getQuantile ( const double  Q,
const bool  reverse = false 
) const
inline

Get quantile.

Parameters
Qquantile
reversereverse
Returns
value

Definition at line 202 of file JQuantile.hh.

203  {
204  if (quantiles) {
205 
206  double W = 0.0;
207 
208  for (std::map<double, double>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
209  W += i->second;
210  }
211 
212  if (reverse)
213  return getQuantile(buffer.rbegin(), buffer.rend(), Q*W);
214  else
215  return getQuantile(buffer. begin(), buffer. end(), Q*W);
216  }
217 
218  throw JNoValue("JQuantile::getQuantile()");
219  }
double getQuantile(const double Q, const bool reverse=false) const
Get quantile.
Definition: JQuantile.hh:202
std::map< double, double > buffer
Definition: JQuantile.hh:271
std::ostream& JTOOLS::JQuantile::print ( std::ostream &  out,
bool  lpr = true 
) const
inline

Print quantile.

Parameters
outoutput stream
lprlong print

Definition at line 228 of file JQuantile.hh.

229  {
230  using namespace std;
231 
232  const int nc = getTitle().size();
233 
234  if (lpr) {
235  out << setw(nc) << left << " " << ' '
236  << setw(10) << left << " mean" << ' '
237  << setw(10) << left << " STD" << ' '
238  << setw(10) << left << " deviation" << endl;
239  }
240 
241  out << setw(nc) << left << getTitle() << ' '
242  << SCIENTIFIC(10,2) << getMean() << ' '
243  << SCIENTIFIC(10,2) << getSTDev() << ' '
244  << SCIENTIFIC(10,2) << getDeviation(false) << endl;
245 
246  return out;
247  }
const std::string & getTitle() const
Get title.
Definition: JTitle.hh:55
double getSTDev() const
Get standard deviation.
Definition: JQuantile.hh:159
double getMean() const
Get mean value.
Definition: JQuantile.hh:131
double getDeviation(const bool relative=true) const
Get maximal deviation from average.
Definition: JQuantile.hh:174
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:498
template<class T >
static double JTOOLS::JQuantile::getQuantile ( __begin,
__end,
const double  W 
)
inlinestaticprotected

Get quantile.

Parameters
__beginbegin of data
__endend of data
Wweight
Returns
value

Definition at line 283 of file JQuantile.hh.

284  {
285  double w = 0.0;
286 
287  for (T i = __begin; i != __end; ++i) {
288 
289  w += i->second;
290 
291  if (w >= W) {
292  return i->first;
293  }
294  }
295 
296  throw JNoValue("JQuantile::getQuantile()");
297  }
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

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

258  {
259  return quantile.print(out, getLongprint(out));
260  }
bool getLongprint(std::ostream &out)
Get long print option.
Definition: JPrint.hh:138
std::ostream & print(std::ostream &out, bool lpr=true) const
Print quantile.
Definition: JQuantile.hh:228

Member Data Documentation

double JTOOLS::JQuantile::sum
protected

Definition at line 263 of file JQuantile.hh.

double JTOOLS::JQuantile::rms
protected

Definition at line 264 of file JQuantile.hh.

double JTOOLS::JQuantile::tot
protected

Definition at line 265 of file JQuantile.hh.

double JTOOLS::JQuantile::min
protected

Definition at line 266 of file JQuantile.hh.

double JTOOLS::JQuantile::max
protected

Definition at line 267 of file JQuantile.hh.

long long int JTOOLS::JQuantile::num
protected

Definition at line 268 of file JQuantile.hh.

bool JTOOLS::JQuantile::quantiles
protected

Definition at line 270 of file JQuantile.hh.

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

Definition at line 271 of file JQuantile.hh.

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

Definition at line 73 of file JTitle.hh.


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