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");
45 MinMax(
const ValueType &
min,
const ValueType &
max) : mMin(min), mMax(max)
53 inline void add(
const ValueType &val,
const Less &less = Less())
55 if (less(val, mMin)) mMin = val;
56 if (less(mMax, val)) mMax = val;
60 inline const ValueType&
min()
const {
return mMin; }
63 inline const ValueType&
max()
const {
return mMax; }
66 inline void add(
const MinMax& other,
const Less &less = Less())
68 if (less(other.
mMin, mMin)) mMin = other.
mMin;
69 if (less(mMax, other.
mMax)) mMax = other.
mMax;
73 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 77 std::ostringstream os;
78 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
80 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
81 os <<
" Min=" << mMin <<
", Max=" << mMax << std::endl;
100 , mMin(
std::numeric_limits<double>::
max())
109 mMin = std::min<double>(val, mMin);
110 mMax = std::max<double>(val, mMax);
114 void add(
double val, uint64_t n)
117 mMin = std::min<double>(val, mMin);
118 mMax = std::max<double>(val, mMax);
122 inline uint64_t
size()
const {
return mSize; }
125 inline double min()
const {
return mMin; }
128 inline double max()
const {
return mMax; }
131 inline double range()
const {
return mMax - mMin; }
136 if (other.
mSize > 0) this->join(other);
140 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 144 std::ostringstream os;
145 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
147 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
149 os <<
"with " << mSize <<
" samples:\n" 152 <<
", Range="<< this->range() << std::endl;
154 os <<
": no samples were added." << std::endl;
163 assert(other.
mSize > 0);
164 mSize += other.
mSize;
165 mMin = std::min<double>(mMin, other.
mMin);
166 mMax = std::max<double>(mMax, other.
mMax);
196 const double delta = val - mAvg;
197 mAvg += delta/double(mSize);
198 mAux += delta*(val - mAvg);
202 void add(
double val, uint64_t n)
204 const double denom = 1.0/double(mSize + n);
205 const double delta = val - mAvg;
206 mAvg += denom * delta * double(n);
207 mAux += denom * delta * delta * double(mSize) * double(n);
208 Extrema::add(val, n);
214 if (other.
mSize > 0) {
215 const double denom = 1.0/double(mSize + other.
mSize);
216 const double delta = other.
mAvg - mAvg;
217 mAvg += denom * delta * double(other.
mSize);
218 mAux += other.
mAux + denom * delta * delta * double(mSize) * double(other.
mSize);
219 Extrema::join(other);
225 inline double avg()
const {
return mAvg; }
226 inline double mean()
const {
return mAvg; }
233 inline double var()
const {
return mSize<2 ? 0.0 : mAux/double(mSize); }
234 inline double variance()
const {
return this->var(); }
240 inline double std()
const {
return sqrt(this->var()); }
245 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const 249 std::ostringstream os;
250 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
252 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
254 os <<
"with " << mSize <<
" samples:\n" 258 <<
", Std=" << this->stdDev()
259 <<
", Var=" << this->variance() << std::endl;
261 os <<
": no samples were added." << std::endl;
267 using Extrema::mSize;
284 : mSize(0), mMin(min), mMax(max + 1e-10),
285 mDelta(double(numBins)/(max-min)), mBins(numBins)
287 if ( mMax <= mMin ) {
289 }
else if ( numBins == 0 ) {
292 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
298 mSize(0), mMin(s.
min()), mMax(s.
max()+1e-10),
299 mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
301 if ( mMax <= mMin ) {
303 }
else if ( numBins == 0 ) {
306 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
312 inline bool add(
double val, uint64_t n = 1)
314 if (val<mMin || val>mMax)
return false;
315 mBins[size_t(mDelta*(val-mMin))] += n;
325 mBins.size() != other.mBins.size())
return false;
326 for (
size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
327 mSize += other.mSize;
332 inline size_t numBins()
const {
return mBins.size(); }
334 inline double min()
const {
return mMin; }
336 inline double max()
const {
return mMax; }
338 inline double min(
int n)
const {
return mMin+n/mDelta; }
340 inline double max(
int n)
const {
return mMin+(n+1)/mDelta; }
342 inline uint64_t
count(
int n)
const {
return mBins[n]; }
344 inline uint64_t
size()
const {
return mSize; }
347 void print(
const std::string& name =
"", std::ostream& strm = std::cout)
const 351 std::ostringstream os;
352 os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
354 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
356 os <<
"with " << mSize <<
" samples:\n";
357 os <<
"==============================================================\n";
358 os <<
"|| # | Min | Max | Frequency | % ||\n";
359 os <<
"==============================================================\n";
360 for (
int i = 0, e =
int(mBins.size()); i != e; ++i) {
361 os <<
"|| " << std::setw(4) << i <<
" | " << std::setw(14) << this->
min(i) <<
" | " 362 << std::setw(14) << this->
max(i) <<
" | " << std::setw(9) << mBins[i] <<
" | " 363 << std::setw(3) << (100*mBins[i]/mSize) <<
" ||\n";
365 os <<
"==============================================================\n";
367 os <<
": no samples were added." << std::endl;
374 double mMin, mMax, mDelta;
375 std::vector<uint64_t> mBins;
382 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED double max() const
Return the upper bound of this histogram's value range.
Definition: Stats.h:336
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:344
ValueType mMin
Definition: Stats.h:87
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
Definition: Stats.h:283
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:212
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
const ValueType & max() const
Return the maximum value.
Definition: Stats.h:63
Templated class to compute the minimum and maximum values.
Definition: Stats.h:30
double avg() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:225
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:297
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:140
uint64_t mSize
Definition: Stats.h:169
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:342
double range() const
Return the range defined as the maximum value minus the minimum value.
Definition: Stats.h:131
double max() const
Return the maximum value.
Definition: Stats.h:128
double min() const
Return the lower bound of this histogram's value range.
Definition: Stats.h:334
ValueType mMax
Definition: Stats.h:87
Definition: Exceptions.h:65
Extrema()
Constructor.
Definition: Stats.h:98
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:322
double mAvg
Definition: Stats.h:270
double std() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:240
const ValueType & min() const
Return the minimum value.
Definition: Stats.h:60
void add(double val)
Add a single sample.
Definition: Stats.h:193
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:114
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:245
double mMax
Definition: Stats.h:170
double mMin
Definition: Stats.h:170
double var() const
Return the population variance.
Definition: Stats.h:233
double mAux
Definition: Stats.h:270
MinMax(const ValueType &min, const ValueType &max)
Constructor.
Definition: Stats.h:45
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:92
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:182
Definition: Exceptions.h:13
void add(double val)
Add a single sample.
Definition: Stats.h:106
Stats()
Definition: Stats.h:185
void add(const ValueType &val, const Less &less=Less())
Add a single sample.
Definition: Stats.h:53
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:202
void add(const MinMax &other, const Less &less=Less())
Add the samples from the other Stats instance.
Definition: Stats.h:66
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:122
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:407
double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:340
double variance() const
Return the population variance.
Definition: Stats.h:234
MinMax()
Empty constructor.
Definition: Stats.h:38
void join(const Extrema &other)
Definition: Stats.h:161
double min() const
Return the minimum value.
Definition: Stats.h:125
double mean() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:226
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:279
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:338
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:332
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
void add(const Extrema &other)
Add the samples from the other Stats instance.
Definition: Stats.h:134
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:73
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
Definition: Stats.h:347
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:312
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:241