37 #ifndef OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED 38 #define OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED 41 #include <openvdb/version.h> 42 #include <openvdb/Exceptions.h> 56 template <
typename ValueType,
typename Less = std::less<ValueType> >
59 using Limits = std::numeric_limits<ValueType>;
65 MinMax() : mMin(Limits::
max()), mMax(Limits::lowest())
67 static_assert(std::numeric_limits<ValueType>::is_specialized,
68 "openvdb::math::MinMax default constructor requires a std::numeric_limits specialization");
72 MinMax(
const ValueType &
min,
const ValueType &
max) : mMin(min), mMax(max)
80 inline void add(
const ValueType &val,
const Less &less = Less())
82 if (less(val, mMin)) mMin = val;
83 if (less(mMax, val)) mMax = val;
87 inline const ValueType&
min()
const {
return mMin; }
90 inline const ValueType&
max()
const {
return mMax; }
93 inline void add(
const MinMax& other,
const Less &less = Less())
95 if (less(other.
mMin, mMin)) mMin = other.
mMin;
96 if (less(mMax, other.
mMax)) mMax = other.
mMax;
100 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 104 std::ostringstream os;
105 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
107 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
108 os <<
" Min=" << mMin <<
", Max=" << mMax << std::endl;
127 , mMin(std::numeric_limits<double>::
max())
136 mMin = std::min<double>(val, mMin);
137 mMax = std::max<double>(val, mMax);
141 void add(
double val, uint64_t n)
144 mMin = std::min<double>(val, mMin);
145 mMax = std::max<double>(val, mMax);
149 inline uint64_t
size()
const {
return mSize; }
152 inline double min()
const {
return mMin; }
155 inline double max()
const {
return mMax; }
158 inline double range()
const {
return mMax - mMin; }
163 if (other.
mSize > 0) this->join(other);
167 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 171 std::ostringstream os;
172 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
174 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
176 os <<
"with " << mSize <<
" samples:\n" 179 <<
", Range="<< this->range() << std::endl;
181 os <<
": no samples were added." << std::endl;
190 assert(other.
mSize > 0);
191 mSize += other.
mSize;
192 mMin = std::min<double>(mMin, other.
mMin);
193 mMax = std::max<double>(mMax, other.
mMax);
223 const double delta = val - mAvg;
224 mAvg += delta/double(mSize);
225 mAux += delta*(val - mAvg);
229 void add(
double val, uint64_t n)
231 const double denom = 1.0/double(mSize + n);
232 const double delta = val - mAvg;
233 mAvg += denom * delta * double(n);
234 mAux += denom * delta * delta * double(mSize) * double(n);
235 Extrema::add(val, n);
241 if (other.
mSize > 0) {
242 const double denom = 1.0/double(mSize + other.
mSize);
243 const double delta = other.
mAvg - mAvg;
244 mAvg += denom * delta * double(other.
mSize);
245 mAux += other.
mAux + denom * delta * delta * double(mSize) * double(other.
mSize);
246 Extrema::join(other);
251 inline double avg()
const {
return mAvg; }
253 inline double mean()
const {
return mAvg; }
260 inline double var()
const {
return mSize<2 ? 0.0 : mAux/double(mSize); }
261 inline double variance()
const {
return this->var(); }
265 inline double std()
const {
return sqrt(this->var()); }
268 inline double stdDev()
const {
return this->std(); }
272 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 276 std::ostringstream os;
277 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
279 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
281 os <<
"with " << mSize <<
" samples:\n" 285 <<
", Std=" << this->stdDev()
286 <<
", Var=" << this->variance() << std::endl;
288 os <<
": no samples were added." << std::endl;
294 using Extrema::mSize;
311 : mSize(0), mMin(min), mMax(max + 1e-10),
312 mDelta(double(numBins)/(max-min)), mBins(numBins)
314 if ( mMax <= mMin ) {
316 }
else if ( numBins == 0 ) {
319 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
325 mSize(0), mMin(s.
min()), mMax(s.
max()+1e-10),
326 mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
328 if ( mMax <= mMin ) {
330 }
else if ( numBins == 0 ) {
333 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
339 inline bool add(
double val, uint64_t n = 1)
341 if (val<mMin || val>mMax)
return false;
342 mBins[size_t(mDelta*(val-mMin))] += n;
352 mBins.size() != other.mBins.size())
return false;
353 for (
size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
354 mSize += other.mSize;
359 inline size_t numBins()
const {
return mBins.size(); }
361 inline double min()
const {
return mMin; }
363 inline double max()
const {
return mMax; }
365 inline double min(
int n)
const {
return mMin+n/mDelta; }
367 inline double max(
int n)
const {
return mMin+(n+1)/mDelta; }
369 inline uint64_t
count(
int n)
const {
return mBins[n]; }
371 inline uint64_t
size()
const {
return mSize; }
374 void print(
const std::string& name =
"", std::ostream& strm = std::cout)
const 378 std::ostringstream os;
379 os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
381 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
383 os <<
"with " << mSize <<
" samples:\n";
384 os <<
"==============================================================\n";
385 os <<
"|| # | Min | Max | Frequency | % ||\n";
386 os <<
"==============================================================\n";
387 for (
int i = 0, e =
int(mBins.size()); i != e; ++i) {
388 os <<
"|| " << std::setw(4) << i <<
" | " << std::setw(14) << this->
min(i) <<
" | " 389 << std::setw(14) << this->
max(i) <<
" | " << std::setw(9) << mBins[i] <<
" | " 390 << std::setw(3) << (100*mBins[i]/mSize) <<
" ||\n";
392 os <<
"==============================================================\n";
394 os <<
": no samples were added." << std::endl;
401 double mMin, mMax, mDelta;
402 std::vector<uint64_t> mBins;
409 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:367
ValueType mMin
Definition: Stats.h:114
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:167
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
uint64_t mSize
Definition: Stats.h:196
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:268
double max() const
Return the maximum value.
Definition: Stats.h:155
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:239
const ValueType & max() const
Return the maximum value.
Definition: Stats.h:90
double mAvg
Definition: Stats.h:297
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:119
void add(double val)
Add a single sample.
Definition: Stats.h:220
double mAux
Definition: Stats.h:297
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:141
double var() const
Return the population variance.
Definition: Stats.h:260
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:149
Extrema()
Constructor.
Definition: Stats.h:125
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:369
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:365
double range() const
Return the range defined as the maximum value minus the minimum value.
Definition: Stats.h:158
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
Templated class to compute the minimum and maximum values.
Definition: Stats.h:57
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:349
Definition: Exceptions.h:92
Stats()
Definition: Stats.h:212
MinMax(const ValueType &min, const ValueType &max)
Constructor.
Definition: Stats.h:72
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:272
Definition: Exceptions.h:40
double variance() const
Return the population variance.
Definition: Stats.h:261
double mMax
Definition: Stats.h:197
double mMin
Definition: Stats.h:197
void add(const MinMax &other, const Less &less=Less())
Add the samples from the other Stats instance.
Definition: Stats.h:93
void add(const ValueType &val, const Less &less=Less())
Add a single sample.
Definition: Stats.h:80
void join(const Extrema &other)
Definition: Stats.h:188
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:324
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:209
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:306
void add(double val)
Add a single sample.
Definition: Stats.h:133
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:100
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
Definition: Stats.h:374
const ValueType & min() const
Return the minimum value.
Definition: Stats.h:87
MinMax()
Empty constructor.
Definition: Stats.h:65
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:359
double max() const
Return the upper bound of this histogram's value range.
Definition: Stats.h:363
double mean() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:253
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:229
void add(const Extrema &other)
Add the samples from the other Stats instance.
Definition: Stats.h:161
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
Definition: Stats.h:310
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:358
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
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:339
ValueType mMax
Definition: Stats.h:114
double min() const
Return the lower bound of this histogram's value range.
Definition: Stats.h:361
double min() const
Return the minimum value.
Definition: Stats.h:152
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:371