OpenVDB  2.1.0
Stats.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 //
34 
35 #ifndef OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
36 #define OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
37 
38 #include <iosfwd> // for ostringstream
39 #include <openvdb/version.h>
40 #include <iostream>
41 #include <iomanip>
42 #include <sstream>
43 #include <vector>
44 #include "Math.h"
45 
46 namespace openvdb {
48 namespace OPENVDB_VERSION_NAME {
49 namespace math {
50 
59 class Stats
60 {
61 public:
62  Stats(): mSize(0), mAvg(0.0), mAux(0.0),
63  mMin(std::numeric_limits<double>::max()), mMax(-mMin) {}
64 
66  void add(double val)
67  {
68  mSize++;
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);
74  }
75 
77  void add(double val, uint64_t n)
78  {
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;
85  mSize += n;
86  }
87 
89  void add(const Stats& other)
90  {
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;
98  mSize += other.mSize;
99  }
100  }
101 
103  inline uint64_t size() const { return mSize; }
104 
106  inline double min() const { return mMin; }
107 
109  inline double max() const { return mMax; }
110 
112  inline double avg() const { return mAvg; }
114  inline double mean() const { return mAvg; }
116 
118  //num/(num-1)
121  inline double var() const { return mSize<2 ? 0.0 : mAux/double(mSize); }
122  inline double variance() const { return this->var(); }
124 
126  inline double std() const { return sqrt(this->var()); }
129  inline double stdDev() const { return this->std(); }
131 
133  void print(const std::string &name= "", std::ostream &strm=std::cout, int precision=3) const
134  {
135  // Write to a temporary string stream so as not to affect the state
136  // (precision, field width, etc.) of the output stream.
137  std::ostringstream os;
138  os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
139  os << "Statistics ";
140  if (!name.empty()) os << "for \"" << name << "\" ";
141  if (mSize>0) {
142  os << "with " << mSize << " samples:\n"
143  << " Min=" << mMin
144  << ", Max=" << mMax
145  << ", Ave=" << mAvg
146  << ", Std=" << this->stdDev()
147  << ", Var=" << this->variance() << std::endl;
148  } else {
149  os << ": no samples were added." << std::endl;
150  }
151  strm << os.str();
152  }
153 
154 private:
155  uint64_t mSize;
156  double mAvg, mAux, mMin, mMax;
157 }; // end Stats
158 
159 
161 
162 
166 {
167 public:
169  Histogram(double min, double max, size_t numBins = 10)
170  : mSize(0), mMin(min), mMax(max+1e-10),
171  mDelta(double(numBins)/(max-min)), mBins(numBins)
172  {
173  assert(numBins > 1);
174  assert(mMax-mMin > 1e-10);
175  for (size_t i=0; i<numBins; ++i) mBins[i]=0;
176  }
177 
180  Histogram(const Stats& s, size_t numBins = 10):
181  mSize(0), mMin(s.min()), mMax(s.max()+1e-10),
182  mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
183  {
184  assert(numBins > 1);
185  assert(mMax-mMin > 1e-10);
186  for (size_t i=0; i<numBins; ++i) mBins[i]=0;
187  }
188 
192  inline bool add(double val, uint64_t n = 1)
193  {
194  if (val<mMin || val>mMax) return false;
195  mBins[size_t(mDelta*(val-mMin))] += n;
196  mSize += n;
197  return true;
198  }
199 
202  bool add(const Histogram& other)
203  {
204  if (!isApproxEqual(mMin, other.mMin) || !isApproxEqual(mMax, other.mMax) ||
205  mBins.size() != other.mBins.size()) return false;
206  for (size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
207  mSize += other.mSize;
208  return true;
209  }
210 
212  inline size_t numBins() const { return mBins.size(); }
214  inline double min() const { return mMin; }
216  inline double max() const { return mMax; }
218  inline double min(int n) const { return mMin+n/mDelta; }
220  inline double max(int n) const { return mMin+(n+1)/mDelta; }
222  inline uint64_t count(int n) const { return mBins[n]; }
224  inline uint64_t size() const { return mSize; }
225 
227  void print(const std::string& name = "", std::ostream& strm = std::cout) const
228  {
229  // Write to a temporary string stream so as not to affect the state
230  // (precision, field width, etc.) of the output stream.
231  std::ostringstream os;
232  os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
233  os << "Histogram ";
234  if (!name.empty()) os << "for \"" << name << "\" ";
235  if (mSize > 0) {
236  os << "with " << mSize << " samples:\n";
237  os << "==============================================================\n";
238  os << "|| # | Min | Max | Frequency | % ||\n";
239  os << "==============================================================\n";
240  for (size_t i=0, e=mBins.size(); i!=e; ++i) {
241  os << "|| " << std::setw(4) << i << " | " << std::setw(14) << this->min(i) << " | "
242  << std::setw(14) << this->max(i) << " | " << std::setw(9) << mBins[i] << " | "
243  << std::setw(3) << (100*mBins[i]/mSize) << " ||\n";
244  }
245  os << "==============================================================\n";
246  } else {
247  os << ": no samples were added." << std::endl;
248  }
249  strm << os.str();
250  }
251 
252 private:
253  uint64_t mSize;
254  double mMin, mMax, mDelta;
255  std::vector<uint64_t> mBins;
256 };
257 
258 } // namespace math
259 } // namespace OPENVDB_VERSION_NAME
260 } // namespace openvdb
261 
262 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
263 
264 // Copyright (c) 2012-2013 DreamWorks Animation LLC
265 // All rights reserved. This software is distributed under the
266 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
Definition: Stats.h:227
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:165
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
double min() const
Return the lower bound of this histogram&#39;s value range.
Definition: Stats.h:214
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:77
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:59
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:202
double max() const
Return the maximum value.
Definition: Stats.h:109
double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:220
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:192
double variance() const
Return the population variance.
Definition: Stats.h:122
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:133
double max() const
Return the upper bound of this histogram&#39;s value range.
Definition: Stats.h:216
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:129
std::string str() const
String representation.
void add(double val)
Add a single sample.
Definition: Stats.h:66
#define OPENVDB_VERSION_NAME
Definition: version.h:45
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:222
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:89
Stats()
Definition: Stats.h:62
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:224
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
Definition: Stats.h:169
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:212
double min() const
Return the minimum value.
Definition: Stats.h:106
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:218
bool isApproxEqual(const Hermite &lhs, const Hermite &rhs)
Definition: Hermite.h:470
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:180
double mean() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:114
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:103
double var() const
Return the population variance.
Definition: Stats.h:121