OpenVDB: Stats.h Source File

OpenVDB 3.1.0

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

Stats.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2015 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 //
36
37 #ifndef OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
38 #define OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
39
40 #include <iosfwd> // for ostringstream
41 #include <openvdb/version.h>
42 #include <iostream>
43 #include <iomanip>
44 #include <sstream>
45 #include <vector>
46 #include "Math.h"
47
48 namespace openvdb {
49 OPENVDB_USE_VERSION_NAMESPACE
50 namespace OPENVDB_VERSION_NAME {
51 namespace math {
52
55 class Extrema
56 {
57 public:
58
61 Extrema()
62 : mSize(0)
63 , mMin(std::numeric_limits<double>::max())
64 , mMax(-mMin)
65 {
66 }
67
69 void add(double val)
70 {
71 ++mSize;
72 mMin = std::min<double>(val, mMin);
73 mMax = std::max<double>(val, mMax);
74 }
75
77 void add(double val, uint64_t n)
78 {
79 mSize += n;
80 mMin = std::min<double>(val, mMin);
81 mMax = std::max<double>(val, mMax);
82 }
83
85 inline uint64_t size() const { return mSize; }
86
88 inline double min() const { return mMin; }
89
91 inline double max() const { return mMax; }
92
94 inline double range() const { return mMax - mMin; }
95
97 void add(const Extrema& other)
98 {
99 if (other.mSize > 0) this->join(other);
100 }
101
103 void print(const std::string &name= "", std::ostream &strm=std::cout, int precision=3) const
104 {
105 // Write to a temporary string stream so as not to affect the state
106 // (precision, field width, etc.) of the output stream.
107 std::ostringstream os;
108 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
109 os << "Extrema ";
110 if (!name.empty()) os << "for \"" << name << "\" ";
111 if (mSize>0) {
112 os << "with " << mSize << " samples:\n"
113 << " Min=" << mMin
114 << ", Max=" << mMax
115 << ", Range="<< this->range() << std::endl;
116 } else {
117 os << ": no samples were added." << std::endl;
118 }
119 strm << os.str();
120 }
121
122 protected:
123
124 inline void join(const Extrema& other)
125 {
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);
130 }
131
132 uint64_t mSize;
133 double mMin, mMax;
134 };//end Extrema
135
136
145 class Stats : public Extrema
146 {
147 public:
148 Stats()
149 : Extrema()
150 , mAvg(0.0)
151 , mAux(0.0)
152 {
153 }
154
156 void add(double val)
157 {
158 Extrema::add(val);
159 const double delta = val - mAvg;
160 mAvg += delta/double(mSize);
161 mAux += delta*(val - mAvg);
162 }
163
165 void add(double val, uint64_t n)
166 {
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);
172 }
173
175 void add(const Stats& other)
176 {
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);
183 }
184 }
185
187 inline double avg() const { return mAvg; }
189 inline double mean() const { return mAvg; }
191
193 //num/(num-1)
196 inline double var() const { return mSize<2 ? 0.0 : mAux/double(mSize); }
197 inline double variance() const { return this->var(); }
199
201 inline double std() const { return sqrt(this->var()); }
204 inline double stdDev() const { return this->std(); }
206
208 void print(const std::string &name= "", std::ostream &strm=std::cout, int precision=3) const
209 {
210 // Write to a temporary string stream so as not to affect the state
211 // (precision, field width, etc.) of the output stream.
212 std::ostringstream os;
213 os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
214 os << "Statistics ";
215 if (!name.empty()) os << "for \"" << name << "\" ";
216 if (mSize>0) {
217 os << "with " << mSize << " samples:\n"
218 << " Min=" << mMin
219 << ", Max=" << mMax
220 << ", Ave=" << mAvg
221 << ", Std=" << this->stdDev()
222 << ", Var=" << this->variance() << std::endl;
223 } else {
224 os << ": no samples were added." << std::endl;
225 }
226 strm << os.str();
227 }
228
229 protected:
230 using Extrema::mSize;
231 using Extrema::mMin;
232 using Extrema::mMax;
233 double mAvg, mAux;
234 }; // end Stats
235
236
238
239
242 class Histogram
243 {
244 public:
246 Histogram(double min, double max, size_t numBins = 10)
247 : mSize(0), mMin(min), mMax(max+1e-10),
248 mDelta(double(numBins)/(max-min)), mBins(numBins)
249 {
250 assert(numBins > 1);
251 assert(mMax-mMin > 1e-10);
252 for (size_t i=0; i<numBins; ++i) mBins[i]=0;
253 }
254
257 Histogram(const Stats& s, size_t numBins = 10):
258 mSize(0), mMin(s.min()), mMax(s.max()+1e-10),
259 mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
260 {
261 assert(numBins > 1);
262 assert(mMax-mMin > 1e-10);
263 for (size_t i=0; i<numBins; ++i) mBins[i]=0;
264 }
265
269 inline bool add(double val, uint64_t n = 1)
270 {
271 if (val<mMin || val>mMax) return false;
272 mBins[size_t(mDelta*(val-mMin))] += n;
273 mSize += n;
274 return true;
275 }
276
279 bool add(const Histogram& other)
280 {
281 if (!isApproxEqual(mMin, other.mMin) || !isApproxEqual(mMax, other.mMax) ||
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;
285 return true;
286 }
287
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; }
302
304 void print(const std::string& name = "", std::ostream& strm = std::cout) const
305 {
306 // Write to a temporary string stream so as not to affect the state
307 // (precision, field width, etc.) of the output stream.
308 std::ostringstream os;
309 os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
310 os << "Histogram ";
311 if (!name.empty()) os << "for \"" << name << "\" ";
312 if (mSize > 0) {
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";
321 }
322 os << "==============================================================\n";
323 } else {
324 os << ": no samples were added." << std::endl;
325 }
326 strm << os.str();
327 }
328
329 private:
330 uint64_t mSize;
331 double mMin, mMax, mDelta;
332 std::vector<uint64_t> mBins;
333 };
334
335 } // namespace math
336 } // namespace OPENVDB_VERSION_NAME
337 } // namespace openvdb
338
339 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
340
341 // Copyright (c) 2012-2015 DreamWorks Animation LLC
342 // All rights reserved. This software is distributed under the
343 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v3_1_0::math::Stats::add
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:175
openvdb::v3_1_0::math::Extrema::add
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:77
openvdb::v3_1_0::math::Extrema::min
double min() const
Return the minimum value.
Definition: Stats.h:88
openvdb::v3_1_0::math::Extrema::print
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
openvdb::v3_1_0::math::Extrema
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:55
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v3_1_0::math::Stats::Stats
Stats()
Definition: Stats.h:148
openvdb::v3_1_0::math::Stats::mAvg
double mAvg
Definition: Stats.h:233
openvdb::v3_1_0::math::Histogram::add
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
openvdb::v3_1_0::math::Extrema::size
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:85
openvdb::v3_1_0::math::Stats::mAux
double mAux
Definition: Stats.h:233
openvdb::v3_1_0::math::Histogram::Histogram
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
openvdb::v3_1_0::math::Extrema::mSize
uint64_t mSize
Definition: Stats.h:132
openvdb::v3_1_0::math::Histogram::max
double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:297
openvdb::v3_1_0::math::isApproxEqual
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
openvdb::v3_1_0::tools::composite::min
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:105
openvdb::v3_1_0::math::Stats::mean
double mean() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:189
openvdb::v3_1_0::math::Extrema::add
void add(const Extrema &other)
Add the samples from the other Stats instance.
Definition: Stats.h:97
openvdb::v3_1_0::math::Histogram::size
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:301
openvdb::v3_1_0::math::Histogram::numBins
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:289
openvdb::v3_1_0::math::Histogram::add
bool add(double val, uint64_t n=1)
Add n samples with constant value val, provided that the val falls within this histogram&#39;s value rang...
Definition: Stats.h:269
openvdb::v3_1_0::math::Histogram::print
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
Definition: Stats.h:304
openvdb::v3_1_0::math::Histogram::Histogram
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
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v3_1_0::math::Stats::var
double var() const
Return the population variance.
Definition: Stats.h:196
openvdb
Definition: Exceptions.h:39
openvdb::v3_1_0::math::Stats
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:145
openvdb::v3_1_0::math::Histogram::max
double max() const
Return the upper bound of this histogram&#39;s value range.
Definition: Stats.h:293
openvdb::v3_1_0::math::Histogram::min
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:295
openvdb::v3_1_0::math::Extrema::add
void add(double val)
Add a single sample.
Definition: Stats.h:69
openvdb::v3_1_0::math::Extrema::range
double range() const
Return the range defined as the maximum value minus the minimum value.
Definition: Stats.h:94
version.h
openvdb::v3_1_0::math::Stats::variance
double variance() const
Return the population variance.
Definition: Stats.h:197
openvdb::v3_1_0::math::Histogram::count
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:299
openvdb::v3_1_0::tools::composite::max
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:109
openvdb::v3_1_0::math::Extrema::mMin
double mMin
Definition: Stats.h:133
openvdb::v3_1_0::math::Histogram::min
double min() const
Return the lower bound of this histogram&#39;s value range.
Definition: Stats.h:291
openvdb::v3_1_0::math::Histogram
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:242
openvdb::v3_1_0::math::Extrema::mMax
double mMax
Definition: Stats.h:133
openvdb::v3_1_0::math::Stats::print
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
openvdb::v3_1_0::math::Extrema::join
void join(const Extrema &other)
Definition: Stats.h:124
openvdb::v3_1_0::math::Stats::add
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:165
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_1_0::math::Stats::stdDev
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:204
openvdb::v3_1_0::math::Stats::add
void add(double val)
Add a single sample.
Definition: Stats.h:156
openvdb::v3_1_0::math::Extrema::Extrema
Extrema()
Constructor.
Definition: Stats.h:61
openvdb::v3_1_0::math::Extrema::max
double max() const
Return the maximum value.
Definition: Stats.h:91