35 #ifndef OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
36 #define OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
39 #include <openvdb/version.h>
62 Stats(): mSize(0), mAvg(0.0), mAux(0.0),
63 mMin(std::numeric_limits<double>::
max()), mMax(-mMin) {}
69 mMin = std::min<double>(val, mMin);
70 mMax = std::max<double>(val, mMax);
71 const double delta = val - mAvg;
72 mAvg += delta/double(mSize);
73 mAux += delta*(val - mAvg);
77 void add(
double val, uint64_t n)
79 mMin = std::min<double>(val, mMin);
80 mMax = std::max<double>(val, mMax);
81 const double denom = 1.0/double(mSize + n);
82 const double delta = val - mAvg;
83 mAvg += denom*delta*n;
84 mAux += denom*delta*delta*mSize*n;
91 if (other.mSize > 0) {
92 mMin = std::min<double>(mMin, other.mMin);
93 mMax = std::max<double>(mMax, other.mMax);
94 const double denom = 1.0/double(mSize + other.mSize);
95 const double delta = other.mAvg - mAvg;
96 mAvg += denom*delta*other.mSize;
97 mAux += other.mAux + denom*delta*delta*mSize*other.mSize;
103 uint64_t
size()
const {
return mSize; }
105 double min()
const {
return mMin; }
107 double max()
const {
return mMax; }
109 double mean()
const {
return mAvg; }
112 double variance()
const {
return mSize<2 ? 0.0 : mAux/double(mSize); }
115 double stdDev()
const {
return sqrt(this->variance()); }
118 void print(
const std::string &name=
"", std::ostream &strm=std::cout,
int precision=3)
const
122 std::ostringstream os;
123 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
125 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
127 os <<
"with " << mSize <<
" samples:\n"
131 <<
", Std=" << this->stdDev()
132 <<
", Var=" << this->variance() << std::endl;
134 os <<
": no samples were added." << std::endl;
141 double mAvg, mAux, mMin, mMax;
155 : mSize(0), mMin(min), mMax(max+1e-10),
156 mDelta(double(numBins)/(max-min)), mBins(numBins)
159 assert(mMax-mMin > 1e-10);
160 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
166 mSize(0), mMin(s.
min()), mMax(s.
max()+1e-10),
167 mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
170 assert(mMax-mMin > 1e-10);
171 for (
size_t i=0; i<numBins; ++i) mBins[i]=0;
177 bool add(
double val, uint64_t n = 1)
179 if (val<mMin || val>mMax)
return false;
180 mBins[size_t(mDelta*(val-mMin))] += n;
190 mBins.size() != other.mBins.size())
return false;
191 for (
size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
192 mSize += other.mSize;
197 size_t numBins()
const {
return mBins.size(); }
199 double min()
const {
return mMin; }
201 double max()
const {
return mMax; }
203 double min(
int n)
const {
return mMin+n/mDelta; }
205 double max(
int n)
const {
return mMin+(n+1)/mDelta; }
207 uint64_t
count(
int n)
const {
return mBins[n]; }
209 uint64_t
size()
const {
return mSize; }
212 void print(
const std::string& name =
"", std::ostream& strm = std::cout)
const
216 std::ostringstream os;
217 os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
219 if (!name.empty()) os <<
"for \"" << name <<
"\" ";
221 os <<
"with " << mSize <<
" samples:\n";
222 os <<
"==============================================================\n";
223 os <<
"|| # | Min | Max | Frequency | % ||\n";
224 os <<
"==============================================================\n";
225 for (
size_t i=0, e=mBins.size(); i!=e; ++i) {
226 os <<
"|| " << std::setw(4) << i <<
" | " << std::setw(14) << this->
min(i) <<
" | "
227 << std::setw(14) << this->
max(i) <<
" | " << std::setw(9) << mBins[i] <<
" | "
228 << std::setw(3) << (100*mBins[i]/mSize) <<
" ||\n";
230 os <<
"==============================================================\n";
232 os <<
": no samples were added." << std::endl;
239 double mMin, mMax, mDelta;
240 std::vector<uint64_t> mBins;
247 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
double mean() const
Return the mean value.
Definition: Stats.h:109
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
double min() const
Return the minimum value.
Definition: Stats.h:105
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:187
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:203
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:115
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:165
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:209
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:59
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:177
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:89
Stats()
Definition: Stats.h:62
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
Definition: Stats.h:154
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:77
std::string str() const
String representation.
#define OPENVDB_VERSION_NAME
Definition: version.h:45
double min() const
Return the lower bound of this histogram's value range.
Definition: Stats.h:199
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:103
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:118
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
double max() const
Return the upper bound of this histogram's value range.
Definition: Stats.h:201
double max() const
Return the maximum value.
Definition: Stats.h:107
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:197
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:207
double variance() const
Return the population variance.
Definition: Stats.h:112
void add(double val)
Add a single sample.
Definition: Stats.h:66
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
Definition: Stats.h:212
double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:205
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:56
bool isApproxEqual(const Hermite &lhs, const Hermite &rhs)
Definition: Hermite.h:470
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:150