37 #ifndef OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
38 #define OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
41 #include <openvdb/version.h>
63 , mMin(std::numeric_limits<double>::
max())
72 mMin = std::min<double>(val, mMin);
73 mMax = std::max<double>(val, mMax);
77 void add(
double val, uint64_t n)
80 mMin = std::min<double>(val, mMin);
81 mMax = std::max<double>(val, mMax);
85 inline uint64_t
size()
const {
return mSize; }
88 inline double min()
const {
return mMin; }
91 inline double max()
const {
return mMax; }
94 inline double range()
const {
return mMax - mMin; }
99 if (other.
mSize > 0) this->join(other);
103 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const
107 std::ostringstream os;
108 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
110 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
112 os <<
"with " << mSize <<
" samples:\n"
115 <<
", Range="<< this->range() << std::endl;
117 os <<
": no samples were added." << std::endl;
126 assert(other.
mSize > 0);
127 mSize += other.
mSize;
128 mMin = std::min<double>(mMin, other.
mMin);
129 mMax = std::max<double>(mMax, other.
mMax);
159 const double delta = val - mAvg;
160 mAvg += delta/double(mSize);
161 mAux += delta*(val - mAvg);
165 void add(
double val, uint64_t n)
167 const double denom = 1.0/double(mSize + n);
168 const double delta = val - mAvg;
169 mAvg += denom * delta * double(n);
170 mAux += denom * delta * delta * double(mSize) * double(n);
171 Extrema::add(val, n);
177 if (other.
mSize > 0) {
178 const double denom = 1.0/double(mSize + other.
mSize);
179 const double delta = other.
mAvg - mAvg;
180 mAvg += denom * delta * double(other.
mSize);
181 mAux += other.
mAux + denom * delta * delta * double(mSize) * double(other.
mSize);
182 Extrema::join(other);
187 inline double avg()
const {
return mAvg; }
189 inline double mean()
const {
return mAvg; }
196 inline double var()
const {
return mSize<2 ? 0.0 : mAux/double(mSize); }
197 inline double variance()
const {
return this->var(); }
201 inline double std()
const {
return sqrt(this->var()); }
204 inline double stdDev()
const {
return this->std(); }
208 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const
212 std::ostringstream os;
213 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
215 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
217 os <<
"with " << mSize <<
" samples:\n"
221 <<
", Std=" << this->stdDev()
222 <<
", Var=" << this->variance() << std::endl;
224 os <<
": no samples were added." << std::endl;
230 using Extrema::mSize;
247 : mSize(0), mMin(min), mMax(max+1e-10),
248 mDelta(double(numBins)/(max-min)), mBins(numBins)
251 assert(mMax-mMin > 1e-10);
252 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
258 mSize(0), mMin(s.
min()), mMax(s.
max()+1e-10),
259 mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
262 assert(mMax-mMin > 1e-10);
263 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
269 inline bool add(
double val, uint64_t n = 1)
271 if (val<mMin || val>mMax)
return false;
272 mBins[size_t(mDelta*(val-mMin))] += n;
282 mBins.size() != other.mBins.size())
return false;
283 for (
size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
284 mSize += other.mSize;
289 inline size_t numBins()
const {
return mBins.size(); }
291 inline double min()
const {
return mMin; }
293 inline double max()
const {
return mMax; }
295 inline double min(
int n)
const {
return mMin+n/mDelta; }
297 inline double max(
int n)
const {
return mMin+(n+1)/mDelta; }
299 inline uint64_t
count(
int n)
const {
return mBins[n]; }
301 inline uint64_t
size()
const {
return mSize; }
304 void print(
const std::string& name =
"", std::ostream& strm = std::cout)
const
308 std::ostringstream os;
309 os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
311 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
313 os <<
"with " << mSize <<
" samples:\n";
314 os <<
"==============================================================\n";
315 os <<
"|| # | Min | Max | Frequency | % ||\n";
316 os <<
"==============================================================\n";
317 for (
int i = 0, e =
int(mBins.size()); i != e; ++i) {
318 os <<
"|| " << std::setw(4) << i <<
" | " << std::setw(14) << this->
min(i) <<
" | "
319 << std::setw(14) << this->
max(i) <<
" | " << std::setw(9) << mBins[i] <<
" | "
320 << std::setw(3) << (100*mBins[i]/mSize) <<
" ||\n";
322 os <<
"==============================================================\n";
324 os <<
": no samples were added." << std::endl;
331 double mMin, mMax, mDelta;
332 std::vector<uint64_t> mBins;
339 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:175
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:77
double min() const
Return the minimum value.
Definition: Stats.h:88
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:103
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:55
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Stats()
Definition: Stats.h:148
double mAvg
Definition: Stats.h:233
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:279
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:85
double mAux
Definition: Stats.h:233
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:257
uint64_t mSize
Definition: Stats.h:132
double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:297
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
double mean() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:189
void add(const Extrema &other)
Add the samples from the other Stats instance.
Definition: Stats.h:97
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:301
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:289
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:269
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
Definition: Stats.h:304
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
Definition: Stats.h:246
#define OPENVDB_VERSION_NAME
Definition: version.h:43
double var() const
Return the population variance.
Definition: Stats.h:196
Definition: Exceptions.h:39
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:145
double max() const
Return the upper bound of this histogram's value range.
Definition: Stats.h:293
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:295
void add(double val)
Add a single sample.
Definition: Stats.h:69
double range() const
Return the range defined as the maximum value minus the minimum value.
Definition: Stats.h:94
double variance() const
Return the population variance.
Definition: Stats.h:197
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:299
double mMin
Definition: Stats.h:133
double min() const
Return the lower bound of this histogram's value range.
Definition: Stats.h:291
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:242
double mMax
Definition: Stats.h:133
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:208
void join(const Extrema &other)
Definition: Stats.h:124
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:165
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:204
void add(double val)
Add a single sample.
Definition: Stats.h:156
Extrema()
Constructor.
Definition: Stats.h:61
double max() const
Return the maximum value.
Definition: Stats.h:91