10 #ifndef OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED 11 #define OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED 14 #include <openvdb/version.h> 29 template <
typename ValueType,
typename Less = std::less<ValueType> >
32 using Limits = std::numeric_limits<ValueType>;
38 MinMax() : mMin(Limits::
max()), mMax(Limits::lowest())
40 static_assert(std::numeric_limits<ValueType>::is_specialized,
41 "openvdb::math::MinMax default constructor requires a std::numeric_limits specialization");
46 : mMin(min), mMax(max) {}
49 inline void add(
const ValueType &val,
const Less &less = Less())
51 if (less(val, mMin)) mMin = val;
52 if (less(mMax, val)) mMax = val;
56 inline const ValueType&
min()
const {
return mMin; }
59 inline const ValueType&
max()
const {
return mMax; }
62 inline void add(
const MinMax& other,
const Less &less = Less())
64 if (less(other.
mMin, mMin)) mMin = other.
mMin;
65 if (less(mMax, other.
mMax)) mMax = other.
mMax;
69 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 73 std::ostringstream os;
74 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
76 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
77 os <<
" Min=" << mMin <<
", Max=" << mMax << std::endl;
96 , mMin(
std::numeric_limits<double>::
max())
105 mMin = std::min<double>(val, mMin);
106 mMax = std::max<double>(val, mMax);
110 void add(
double val, uint64_t n)
113 mMin = std::min<double>(val, mMin);
114 mMax = std::max<double>(val, mMax);
118 inline uint64_t
size()
const {
return mSize; }
121 inline double min()
const {
return mMin; }
124 inline double max()
const {
return mMax; }
127 inline double range()
const {
return mMax - mMin; }
132 if (other.
mSize > 0) this->join(other);
136 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 140 std::ostringstream os;
141 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
143 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
145 os <<
"with " << mSize <<
" samples:\n" 148 <<
", Range="<< this->range() << std::endl;
150 os <<
": no samples were added." << std::endl;
159 assert(other.
mSize > 0);
160 mSize += other.
mSize;
161 mMin = std::min<double>(mMin, other.
mMin);
162 mMax = std::max<double>(mMax, other.
mMax);
192 const double delta = val - mAvg;
193 mAvg += delta/double(mSize);
194 mAux += delta*(val - mAvg);
198 void add(
double val, uint64_t n)
200 const double denom = 1.0/double(mSize + n);
201 const double delta = val - mAvg;
202 mAvg += denom * delta * double(n);
203 mAux += denom * delta * delta * double(mSize) * double(n);
204 Extrema::add(val, n);
210 if (other.
mSize > 0) {
211 const double denom = 1.0/double(mSize + other.
mSize);
212 const double delta = other.
mAvg - mAvg;
213 mAvg += denom * delta * double(other.
mSize);
214 mAux += other.
mAux + denom * delta * delta * double(mSize) * double(other.
mSize);
215 Extrema::join(other);
221 inline double avg()
const {
return mAvg; }
222 inline double mean()
const {
return mAvg; }
229 inline double var()
const {
return mSize<2 ? 0.0 : mAux/double(mSize); }
230 inline double variance()
const {
return this->var(); }
236 inline double std()
const {
return sqrt(this->var()); }
241 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 245 std::ostringstream os;
246 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
248 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
250 os <<
"with " << mSize <<
" samples:\n" 254 <<
", Std=" << this->stdDev()
255 <<
", Var=" << this->variance() << std::endl;
257 os <<
": no samples were added." << std::endl;
263 using Extrema::mSize;
280 : mSize(0), mMin(min), mMax(max + 1e-10),
281 mDelta(double(numBins)/(max-min)), mBins(numBins)
283 if ( mMax <= mMin ) {
285 }
else if ( numBins == 0 ) {
288 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
294 mSize(0), mMin(s.
min()), mMax(s.
max()+1e-10),
295 mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
297 if ( mMax <= mMin ) {
299 }
else if ( numBins == 0 ) {
302 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
308 inline bool add(
double val, uint64_t n = 1)
310 if (val<mMin || val>mMax)
return false;
311 mBins[size_t(mDelta*(val-mMin))] += n;
321 mBins.size() != other.mBins.size())
return false;
322 for (
size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
323 mSize += other.mSize;
328 inline size_t numBins()
const {
return mBins.size(); }
330 inline double min()
const {
return mMin; }
332 inline double max()
const {
return mMax; }
334 inline double min(
int n)
const {
return mMin+n/mDelta; }
336 inline double max(
int n)
const {
return mMin+(n+1)/mDelta; }
338 inline uint64_t
count(
int n)
const {
return mBins[n]; }
340 inline uint64_t
size()
const {
return mSize; }
343 void print(
const std::string& name =
"", std::ostream& strm = std::cout)
const 347 std::ostringstream os;
348 os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
350 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
352 os <<
"with " << mSize <<
" samples:\n";
353 os <<
"==============================================================\n";
354 os <<
"|| # | Min | Max | Frequency | % ||\n";
355 os <<
"==============================================================\n";
356 for (
int i = 0, e =
int(mBins.size()); i != e; ++i) {
357 os <<
"|| " << std::setw(4) << i <<
" | " << std::setw(14) << this->
min(i) <<
" | " 358 << std::setw(14) << this->
max(i) <<
" | " << std::setw(9) << mBins[i] <<
" | " 359 << std::setw(3) << (100*mBins[i]/mSize) <<
" ||\n";
361 os <<
"==============================================================\n";
363 os <<
": no samples were added." << std::endl;
370 double mMin, mMax, mDelta;
371 std::vector<uint64_t> mBins;
378 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED void join(const Extrema &other)
Definition: Stats.h:157
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:308
Definition: Exceptions.h:65
MinMax()
Empty constructor.
Definition: Stats.h:38
double mean() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:222
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:338
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
void add(double val)
Add a single sample.
Definition: Stats.h:102
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
const ValueType & min() const
Return the minimum value.
Definition: Stats.h:56
Templated class to compute the minimum and maximum values.
Definition: Stats.h:30
void add(const Extrema &other)
Add the samples from the other Stats instance.
Definition: Stats.h:130
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:237
double std() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:236
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:328
void add(double val)
Add a single sample.
Definition: Stats.h:189
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:88
double max() const
Return the upper bound of this histogram's value range.
Definition: Stats.h:332
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:110
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:275
Stats()
Definition: Stats.h:181
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:69
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:136
double avg() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:221
void add(const MinMax &other, const Less &less=Less())
Add the samples from the other Stats instance.
Definition: Stats.h:62
double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:336
double min() const
Return the minimum value.
Definition: Stats.h:121
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:334
double mAux
Definition: Stats.h:266
double max() const
Return the maximum value.
Definition: Stats.h:124
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Definition: Math.h:406
double mAvg
Definition: Stats.h:266
Definition: Exceptions.h:13
double variance() const
Return the population variance.
Definition: Stats.h:230
void add(const ValueType &val, const Less &less=Less())
Add a single sample.
Definition: Stats.h:49
double min() const
Return the lower bound of this histogram's value range.
Definition: Stats.h:330
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:340
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:241
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:208
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:293
Extrema()
Constructor.
Definition: Stats.h:94
const ValueType & max() const
Return the maximum value.
Definition: Stats.h:59
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
Definition: Stats.h:279
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:318
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:178
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:118
double range() const
Return the range defined as the maximum value minus the minimum value.
Definition: Stats.h:127
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:198
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
Definition: Stats.h:343
ValueType mMin
Definition: Stats.h:83
ValueType mMax
Definition: Stats.h:83
double mMax
Definition: Stats.h:166
uint64_t mSize
Definition: Stats.h:165
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
double mMin
Definition: Stats.h:166
MinMax(const ValueType &min, const ValueType &max)
Constructor.
Definition: Stats.h:45
double var() const
Return the population variance.
Definition: Stats.h:229
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212