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

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

#include <JStats.hh>

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

Public Member Functions

 JStats (const JTitle &title="")
 Constructor.
 
template<class JElement_t , class JAllocator_t >
 JStats (const JTitle &title, const array_type< JElement_t, JAllocator_t > &buffer, const double w=1.0)
 Constructor.
 
void reset ()
 Reset.
 
JStatsadd (const JStats &Q)
 Add stats.
 
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.
 
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.
 

Protected Attributes

double mean
 
double sigma
 
double total
 
long long int count
 
double xmin
 
double xmax
 
double wmin
 
double wmax
 
std::string title
 

Friends

std::ostream & operator<< (std::ostream &out, const JStats &stats)
 Print stats.
 

Detailed Description

Auxiliary data structure for running average and standard deviation.

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 41 of file JStats.hh.

Constructor & Destructor Documentation

◆ JStats() [1/2]

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

Constructor.

Parameters
titletitle

Definition at line 50 of file JStats.hh.

50 :
52 {
53 reset();
54 }
std::string title
Definition JTitle.hh:73
JTitle()
Default constructor.
Definition JTitle.hh:24
void reset()
Reset.
Definition JStats.hh:79

◆ JStats() [2/2]

template<class JElement_t , class JAllocator_t >
JTOOLS::JStats::JStats ( 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 65 of file JStats.hh.

67 :
69 {
70 reset();
71
72 put(buffer, w);
73 }
void put(const double x, const double w=1.0)
Put value.
Definition JStats.hh:119

Member Function Documentation

◆ reset()

void JTOOLS::JStats::reset ( )
inline

Reset.

Definition at line 79 of file JStats.hh.

80 {
81 mean = 0.0;
82 sigma = 0.0;
83 total = 0.0;
84 count = 0;
85 xmin = std::numeric_limits<double>::max();
86 xmax = std::numeric_limits<double>::lowest();
87 wmin = std::numeric_limits<double>::max();
88 wmax = std::numeric_limits<double>::lowest();
89 }
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

◆ add()

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

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 }

◆ put() [1/2]

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

Put value.

Parameters
xvalue
wweight

Definition at line 119 of file JStats.hh.

120 {
121 total += w;
122 count += 1;
123
124 if (count == 1) {
125
126 mean = x;
127 sigma = 0.0;
128
129 } else {
130
131 const double new_mean = mean + w * (x - mean) / total;
132 const double new_sigma = sigma + w * (x - mean) * (x - new_mean);
133
134 // set up for next iteration
135
136 mean = new_mean;
137 sigma = new_sigma;
138 }
139
140 xmin = std::min(xmin, x);
141 xmax = std::max(xmax, x);
142 wmin = std::min(wmin, w);
143 wmax = std::max(wmax, w);
144 }

◆ put() [2/2]

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

Put data.

Parameters
bufferinput data
wweight

Definition at line 154 of file JStats.hh.

156 {
157 for (typename array_type<JElement_t, JAllocator_t>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
158 put(*i, w);
159 }
160 }

◆ getCount()

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

Get total count.

Returns
count

Definition at line 168 of file JStats.hh.

169 {
170 return count;
171 }

◆ getTotal()

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

Get total weight.

Returns
weight

Definition at line 179 of file JStats.hh.

180 {
181 return total;
182 }

◆ getXmin()

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

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
inline

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
inline

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
inline

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
inline

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 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.

◆ getMean() [2/2]

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

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
inline

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
inline

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
inline

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
inline

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
inline

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

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 JStats & stats )
friend

Print stats.

Parameters
outoutput stream
statsstats
Returns
output stream

Definition at line 349 of file JStats.hh.

350 {
351 return stats.print(out, getLongprint(out));
352 }
bool getLongprint(std::ostream &out)
Get long print option.
Definition JManip.hh:121

Member Data Documentation

◆ mean

double JTOOLS::JStats::mean
protected

Definition at line 355 of file JStats.hh.

◆ sigma

double JTOOLS::JStats::sigma
protected

Definition at line 356 of file JStats.hh.

◆ total

double JTOOLS::JStats::total
protected

Definition at line 357 of file JStats.hh.

◆ count

long long int JTOOLS::JStats::count
protected

Definition at line 358 of file JStats.hh.

◆ xmin

double JTOOLS::JStats::xmin
protected

Definition at line 359 of file JStats.hh.

◆ xmax

double JTOOLS::JStats::xmax
protected

Definition at line 360 of file JStats.hh.

◆ wmin

double JTOOLS::JStats::wmin
protected

Definition at line 361 of file JStats.hh.

◆ wmax

double JTOOLS::JStats::wmax
protected

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: