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> >
58 using Limits = std::numeric_limits<ValueType>;
64 MinMax() : mMin(Limits::
max()), mMax(Limits::lowest())
66 static_assert(std::numeric_limits<ValueType>::is_specialized,
67 "openvdb::math::MinMax default constructor requires a std::numeric_limits specialization");
79 inline void add(
const ValueType &val,
const Less &less = Less())
81 if (less(val, mMin)) mMin = val;
82 if (less(mMax, val)) mMax = val;
86 inline const ValueType&
min()
const {
return mMin; }
89 inline const ValueType&
max()
const {
return mMax; }
92 inline void add(
const MinMax& other,
const Less &less = Less())
94 if (less(other.
mMin, mMin)) mMin = other.
mMin;
95 if (less(mMax, other.
mMax)) mMax = other.
mMax;
99 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 103 std::ostringstream os;
104 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
106 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
107 os <<
" Min=" << mMin <<
", Max=" << mMax << std::endl;
126 , mMin(std::numeric_limits<double>::
max())
135 mMin = std::min<double>(val, mMin);
136 mMax = std::max<double>(val, mMax);
140 void add(
double val, uint64_t n)
143 mMin = std::min<double>(val, mMin);
144 mMax = std::max<double>(val, mMax);
148 inline uint64_t
size()
const {
return mSize; }
151 inline double min()
const {
return mMin; }
154 inline double max()
const {
return mMax; }
157 inline double range()
const {
return mMax - mMin; }
162 if (other.
mSize > 0) this->join(other);
166 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 170 std::ostringstream os;
171 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
173 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
175 os <<
"with " << mSize <<
" samples:\n" 178 <<
", Range="<< this->range() << std::endl;
180 os <<
": no samples were added." << std::endl;
189 assert(other.
mSize > 0);
190 mSize += other.
mSize;
191 mMin = std::min<double>(mMin, other.
mMin);
192 mMax = std::max<double>(mMax, other.
mMax);
222 const double delta = val - mAvg;
223 mAvg += delta/double(mSize);
224 mAux += delta*(val - mAvg);
228 void add(
double val, uint64_t n)
230 const double denom = 1.0/double(mSize + n);
231 const double delta = val - mAvg;
232 mAvg += denom * delta * double(n);
233 mAux += denom * delta * delta * double(mSize) * double(n);
234 Extrema::add(val, n);
240 if (other.
mSize > 0) {
241 const double denom = 1.0/double(mSize + other.
mSize);
242 const double delta = other.
mAvg - mAvg;
243 mAvg += denom * delta * double(other.
mSize);
244 mAux += other.
mAux + denom * delta * delta * double(mSize) * double(other.
mSize);
245 Extrema::join(other);
250 inline double avg()
const {
return mAvg; }
252 inline double mean()
const {
return mAvg; }
259 inline double var()
const {
return mSize<2 ? 0.0 : mAux/double(mSize); }
260 inline double variance()
const {
return this->var(); }
264 inline double std()
const {
return sqrt(this->var()); }
267 inline double stdDev()
const {
return this->std(); }
271 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 275 std::ostringstream os;
276 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
278 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
280 os <<
"with " << mSize <<
" samples:\n" 284 <<
", Std=" << this->stdDev()
285 <<
", Var=" << this->variance() << std::endl;
287 os <<
": no samples were added." << std::endl;
293 using Extrema::mSize;
310 : mSize(0), mMin(
min), mMax(
max + 1e-10),
311 mDelta(double(numBins)/(
max-
min)), mBins(numBins)
313 if ( mMax <= mMin ) {
315 }
else if ( numBins == 0 ) {
318 for (
size_t i=0;
i<numBins; ++
i) mBins[
i]=0;
324 mSize(0), mMin(s.
min()), mMax(s.
max()+1e-10),
325 mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
327 if ( mMax <= mMin ) {
329 }
else if ( numBins == 0 ) {
332 for (
size_t i=0;
i<numBins; ++
i) mBins[
i]=0;
338 inline bool add(
double val, uint64_t n = 1)
340 if (val<mMin || val>mMax)
return false;
341 mBins[size_t(mDelta*(val-mMin))] += n;
351 mBins.size() != other.mBins.size())
return false;
352 for (
size_t i=0, e=mBins.size();
i!=e; ++
i) mBins[
i] += other.mBins[
i];
353 mSize += other.mSize;
358 inline size_t numBins()
const {
return mBins.size(); }
360 inline double min()
const {
return mMin; }
362 inline double max()
const {
return mMax; }
364 inline double min(
int n)
const {
return mMin+n/mDelta; }
366 inline double max(
int n)
const {
return mMin+(n+1)/mDelta; }
368 inline uint64_t
count(
int n)
const {
return mBins[n]; }
370 inline uint64_t
size()
const {
return mSize; }
373 void print(
const std::string& name =
"", std::ostream& strm = std::cout)
const 377 std::ostringstream os;
378 os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
380 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
382 os <<
"with " << mSize <<
" samples:\n";
383 os <<
"==============================================================\n";
384 os <<
"|| # | Min | Max | Frequency | % ||\n";
385 os <<
"==============================================================\n";
386 for (
int i = 0, e =
int(mBins.size());
i != e; ++
i) {
387 os <<
"|| " << std::setw(4) <<
i <<
" | " << std::setw(14) << this->
min(
i) <<
" | " 388 << std::setw(14) << this->
max(
i) <<
" | " << std::setw(9) << mBins[
i] <<
" | " 389 << std::setw(3) << (100*mBins[
i]/mSize) <<
" ||\n";
391 os <<
"==============================================================\n";
393 os <<
": no samples were added." << std::endl;
400 double mMin, mMax, mDelta;
401 std::vector<uint64_t> mBins;
408 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED 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:271
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:267
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:208
void add(double val)
Add a single sample.
Definition: Stats.h:219
MinMax()
Empty constructor.
Definition: Stats.h:64
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
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:348
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
void add(double val)
Add a single sample.
Definition: Stats.h:132
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:323
double mMin
Definition: Stats.h:196
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
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:354
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:338
double max() const
Return the upper bound of this histogram's value range.
Definition: Stats.h:362
double min() const
Return the minimum value.
Definition: Stats.h:151
double var() const
Return the population variance.
Definition: Stats.h:259
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:166
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
Definition: Stats.h:309
double mAvg
Definition: Stats.h:296
Templated class to compute the minimum and maximum values.
Definition: Stats.h:56
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:99
double mMax
Definition: Stats.h:196
double mAux
Definition: Stats.h:296
Extrema()
Constructor.
Definition: Stats.h:124
void join(const Extrema &other)
Definition: Stats.h:187
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:358
#define OPENVDB_VERSION_NAME
Definition: version.h:43
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
Definition: Stats.h:373
uint64_t mSize
Definition: Stats.h:195
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:368
MinMax(const ValueType &min, const ValueType &max)
Constructor.
Definition: Stats.h:71
double max() const
Return the maximum value.
Definition: Stats.h:154
Definition: Exceptions.h:39
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:140
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:364
void add(const ValueType &val, const Less &less=Less())
Add a single sample.
Definition: Stats.h:79
ValueType mMin
Definition: Stats.h:113
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:305
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:238
Stats()
Definition: Stats.h:211
Definition: Exceptions.h:92
ValueType mMax
Definition: Stats.h:113
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:148
uint64_t mSize
Definition: Stats.h:195
double min() const
Return the lower bound of this histogram's value range.
Definition: Stats.h:360
double variance() const
Return the population variance.
Definition: Stats.h:260
double mean() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:252
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:118
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:366
const ValueType & min() const
Return the minimum value.
Definition: Stats.h:86
void add(const Extrema &other)
Add the samples from the other Stats instance.
Definition: Stats.h:160
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:370
const ValueType & max() const
Return the maximum value.
Definition: Stats.h:89
void add(const MinMax &other, const Less &less=Less())
Add the samples from the other Stats instance.
Definition: Stats.h:92
double range() const
Return the range defined as the maximum value minus the minimum value.
Definition: Stats.h:157
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:228