37 #ifndef OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED 38 #define OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED 55 template <
typename ValueType,
typename Less = std::less<ValueType> >
61 MinMax(
const ValueType &
min,
const ValueType &
max) : mMin(min), mMax(max)
66 inline void add(
const ValueType &val,
const Less &less = Less())
68 if (less(val, mMin)) mMin = val;
69 if (less(mMax, val)) mMax = val;
73 inline const ValueType&
min()
const {
return mMin; }
76 inline const ValueType&
max()
const {
return mMax; }
79 inline void add(
const MinMax& other,
const Less &less = Less())
81 if (less(other.
mMin, mMin)) mMin = other.
mMin;
82 if (less(mMax, other.
mMax)) mMax = other.
mMax;
86 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 90 std::ostringstream os;
91 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
93 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
94 os <<
" Min=" << mMin <<
", Max=" << mMax << std::endl;
113 , mMin(std::numeric_limits<double>::
max())
122 mMin = std::min<double>(val, mMin);
123 mMax = std::max<double>(val, mMax);
127 void add(
double val, uint64_t n)
130 mMin = std::min<double>(val, mMin);
131 mMax = std::max<double>(val, mMax);
135 inline uint64_t
size()
const {
return mSize; }
138 inline double min()
const {
return mMin; }
141 inline double max()
const {
return mMax; }
144 inline double range()
const {
return mMax - mMin; }
149 if (other.
mSize > 0) this->join(other);
153 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 157 std::ostringstream os;
158 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
160 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
162 os <<
"with " << mSize <<
" samples:\n" 165 <<
", Range="<< this->range() << std::endl;
167 os <<
": no samples were added." << std::endl;
176 assert(other.
mSize > 0);
177 mSize += other.
mSize;
178 mMin = std::min<double>(mMin, other.
mMin);
179 mMax = std::max<double>(mMax, other.
mMax);
209 const double delta = val - mAvg;
210 mAvg += delta/double(mSize);
211 mAux += delta*(val - mAvg);
215 void add(
double val, uint64_t n)
217 const double denom = 1.0/double(mSize + n);
218 const double delta = val - mAvg;
219 mAvg += denom * delta * double(n);
220 mAux += denom * delta * delta * double(mSize) * double(n);
221 Extrema::add(val, n);
227 if (other.
mSize > 0) {
228 const double denom = 1.0/double(mSize + other.
mSize);
229 const double delta = other.
mAvg - mAvg;
230 mAvg += denom * delta * double(other.
mSize);
231 mAux += other.
mAux + denom * delta * delta * double(mSize) * double(other.
mSize);
232 Extrema::join(other);
237 inline double avg()
const {
return mAvg; }
239 inline double mean()
const {
return mAvg; }
246 inline double var()
const {
return mSize<2 ? 0.0 : mAux/double(mSize); }
247 inline double variance()
const {
return this->var(); }
251 inline double std()
const {
return sqrt(this->var()); }
254 inline double stdDev()
const {
return this->std(); }
258 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 262 std::ostringstream os;
263 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
265 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
267 os <<
"with " << mSize <<
" samples:\n" 271 <<
", Std=" << this->stdDev()
272 <<
", Var=" << this->variance() << std::endl;
274 os <<
": no samples were added." << std::endl;
280 using Extrema::mSize;
297 : mSize(0), mMin(min), mMax(max + 1e-10),
298 mDelta(double(numBins)/(max-min)), mBins(numBins)
300 if ( mMax <= mMin ) {
302 }
else if ( numBins == 0 ) {
305 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
311 mSize(0), mMin(s.
min()), mMax(s.
max()+1e-10),
312 mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
314 if ( mMax <= mMin ) {
316 }
else if ( numBins == 0 ) {
319 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
325 inline bool add(
double val, uint64_t n = 1)
327 if (val<mMin || val>mMax)
return false;
328 mBins[size_t(mDelta*(val-mMin))] += n;
338 mBins.size() != other.mBins.size())
return false;
339 for (
size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
340 mSize += other.mSize;
345 inline size_t numBins()
const {
return mBins.size(); }
347 inline double min()
const {
return mMin; }
349 inline double max()
const {
return mMax; }
351 inline double min(
int n)
const {
return mMin+n/mDelta; }
353 inline double max(
int n)
const {
return mMin+(n+1)/mDelta; }
355 inline uint64_t
count(
int n)
const {
return mBins[n]; }
357 inline uint64_t
size()
const {
return mSize; }
360 void print(
const std::string& name =
"", std::ostream& strm = std::cout)
const 364 std::ostringstream os;
365 os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
367 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
369 os <<
"with " << mSize <<
" samples:\n";
370 os <<
"==============================================================\n";
371 os <<
"|| # | Min | Max | Frequency | % ||\n";
372 os <<
"==============================================================\n";
373 for (
int i = 0, e =
int(mBins.size()); i != e; ++i) {
374 os <<
"|| " << std::setw(4) << i <<
" | " << std::setw(14) << this->
min(i) <<
" | " 375 << std::setw(14) << this->
max(i) <<
" | " << std::setw(9) << mBins[i] <<
" | " 376 << std::setw(3) << (100*mBins[i]/mSize) <<
" ||\n";
378 os <<
"==============================================================\n";
380 os <<
": no samples were added." << std::endl;
387 double mMin, mMax, mDelta;
388 std::vector<uint64_t> mBins;
395 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED double max() const
Return the maximum value.
Definition: Stats.h:141
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:292
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:127
void add(const ValueType &val, const Less &less=Less())
Add a single sample.
Definition: Stats.h:66
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
ValueType mMin
Definition: Stats.h:100
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
uint64_t mSize
Definition: Stats.h:182
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:225
void add(const MinMax &other, const Less &less=Less())
Add the samples from the other Stats instance.
Definition: Stats.h:79
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:357
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:135
MinMax(const ValueType &min, const ValueType &max)
Constructor.
Definition: Stats.h:61
Stats()
Definition: Stats.h:198
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:351
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:370
void add(const Extrema &other)
Add the samples from the other Stats instance.
Definition: Stats.h:147
ValueType mMax
Definition: Stats.h:100
double variance() const
Return the population variance.
Definition: Stats.h:247
double range() const
Return the range defined as the maximum value minus the minimum value.
Definition: Stats.h:144
double mean() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:239
#define OPENVDB_VERSION_NAME
Definition: version.h:43
double min() const
Return the lower bound of this histogram's value range.
Definition: Stats.h:347
void print(const std::string &name="", std::ostream &strm=std::cout, int precision=3) const
Print statistics to the specified output stream.
Definition: Stats.h:258
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:254
bool add(const Histogram &other)
Add all the contributions from the other histogram, provided that it has the same configuration as th...
Definition: Stats.h:335
Definition: Exceptions.h:39
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:195
void add(double val)
Add a single sample.
Definition: Stats.h:119
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:215
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
Definition: Stats.h:360
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:105
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:355
void add(double val)
Add a single sample.
Definition: Stats.h:206
double min() const
Return the minimum value.
Definition: Stats.h:138
const ValueType & max() const
Return the maximum value.
Definition: Stats.h:76
void print(const std::string &name="", std::ostream &strm=std::cout, int precision=3) const
Print extrema to the specified output stream.
Definition: Stats.h:153
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:345
Histogram(const Stats &s, size_t numBins=10)
Construct with the given bin count and with minimum and maximum values taken from a Stats object...
Definition: Stats.h:310
double mMin
Definition: Stats.h:183
Definition: Exceptions.h:88
double mMax
Definition: Stats.h:183
bool add(double val, uint64_t n=1)
Add n samples with constant value val, provided that the val falls within this histogram's value rang...
Definition: Stats.h:325
double var() const
Return the population variance.
Definition: Stats.h:246
double max() const
Return the upper bound of this histogram's value range.
Definition: Stats.h:349
Templated class to compute the minimum and maximum values.
Definition: Stats.h:56
void join(const Extrema &other)
Definition: Stats.h:174
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
Definition: Stats.h:296
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
double mAvg
Definition: Stats.h:283
double mAux
Definition: Stats.h:283
const ValueType & min() const
Return the minimum value.
Definition: Stats.h:73
Extrema()
Constructor.
Definition: Stats.h:111
double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:353
void print(const std::string &name="", std::ostream &strm=std::cout, int precision=3) const
Print MinMax to the specified output stream.
Definition: Stats.h:86