OpenVDB  3.0.0
Stats.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2014 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 
53 class Extrema
54 {
55 public:
56 
60  : mSize(0)
61  , mMin(std::numeric_limits<double>::max())
62  , mMax(-mMin)
63  {
64  }
65 
67  void add(double val)
68  {
69  ++mSize;
70  mMin = std::min<double>(val, mMin);
71  mMax = std::max<double>(val, mMax);
72  }
73 
75  void add(double val, uint64_t n)
76  {
77  mSize += n;
78  mMin = std::min<double>(val, mMin);
79  mMax = std::max<double>(val, mMax);
80  }
81 
83  inline uint64_t size() const { return mSize; }
84 
86  inline double min() const { return mMin; }
87 
89  inline double max() const { return mMax; }
90 
92  void add(const Extrema& other)
93  {
94  if (other.mSize > 0) this->join(other);
95  }
96 
98  void print(const std::string &name= "", std::ostream &strm=std::cout, int precision=3) const
99  {
100  // Write to a temporary string stream so as not to affect the state
101  // (precision, field width, etc.) of the output stream.
102  std::ostringstream os;
103  os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
104  os << "Extrema ";
105  if (!name.empty()) os << "for \"" << name << "\" ";
106  if (mSize>0) {
107  os << "with " << mSize << " samples:\n"
108  << " Min=" << mMin
109  << ", Max=" << mMax << std::endl;
110  } else {
111  os << ": no samples were added." << std::endl;
112  }
113  strm << os.str();
114  }
115 
116 protected:
117 
118  inline void join(const Extrema& other)
119  {
120  assert(other.mSize > 0);
121  mSize += other.mSize;
122  mMin = std::min<double>(mMin, other.mMin);
123  mMax = std::max<double>(mMax, other.mMax);
124  }
125 
126  uint64_t mSize;
127  double mMin, mMax;
128 };//end Extrema
129 
130 
139 class Stats : public Extrema
140 {
141 public:
143  : Extrema()
144  , mAvg(0.0)
145  , mAux(0.0)
146  {
147  }
148 
150  void add(double val)
151  {
152  Extrema::add(val);
153  const double delta = val - mAvg;
154  mAvg += delta/double(mSize);
155  mAux += delta*(val - mAvg);
156  }
157 
159  void add(double val, uint64_t n)
160  {
161  const double denom = 1.0/double(mSize + n);
162  const double delta = val - mAvg;
163  mAvg += denom * delta * double(n);
164  mAux += denom * delta * delta * double(mSize) * double(n);
165  Extrema::add(val, n);
166  }
167 
169  void add(const Stats& other)
170  {
171  if (other.mSize > 0) {
172  const double denom = 1.0/double(mSize + other.mSize);
173  const double delta = other.mAvg - mAvg;
174  mAvg += denom * delta * double(other.mSize);
175  mAux += other.mAux + denom * delta * delta * double(mSize) * double(other.mSize);
176  Extrema::join(other);
177  }
178  }
179 
181  inline double avg() const { return mAvg; }
183  inline double mean() const { return mAvg; }
185 
187  //num/(num-1)
190  inline double var() const { return mSize<2 ? 0.0 : mAux/double(mSize); }
191  inline double variance() const { return this->var(); }
193 
195  inline double std() const { return sqrt(this->var()); }
198  inline double stdDev() const { return this->std(); }
200 
202  void print(const std::string &name= "", std::ostream &strm=std::cout, int precision=3) const
203  {
204  // Write to a temporary string stream so as not to affect the state
205  // (precision, field width, etc.) of the output stream.
206  std::ostringstream os;
207  os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
208  os << "Statistics ";
209  if (!name.empty()) os << "for \"" << name << "\" ";
210  if (mSize>0) {
211  os << "with " << mSize << " samples:\n"
212  << " Min=" << mMin
213  << ", Max=" << mMax
214  << ", Ave=" << mAvg
215  << ", Std=" << this->stdDev()
216  << ", Var=" << this->variance() << std::endl;
217  } else {
218  os << ": no samples were added." << std::endl;
219  }
220  strm << os.str();
221  }
222 
223 protected:
224  using Extrema::mSize;
225  using Extrema::mMin;
226  using Extrema::mMax;
227  double mAvg, mAux;
228 }; // end Stats
229 
230 
232 
233 
237 {
238 public:
240  Histogram(double min, double max, size_t numBins = 10)
241  : mSize(0), mMin(min), mMax(max+1e-10),
242  mDelta(double(numBins)/(max-min)), mBins(numBins)
243  {
244  assert(numBins > 1);
245  assert(mMax-mMin > 1e-10);
246  for (size_t i=0; i<numBins; ++i) mBins[i]=0;
247  }
248 
251  Histogram(const Stats& s, size_t numBins = 10):
252  mSize(0), mMin(s.min()), mMax(s.max()+1e-10),
253  mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
254  {
255  assert(numBins > 1);
256  assert(mMax-mMin > 1e-10);
257  for (size_t i=0; i<numBins; ++i) mBins[i]=0;
258  }
259 
263  inline bool add(double val, uint64_t n = 1)
264  {
265  if (val<mMin || val>mMax) return false;
266  mBins[size_t(mDelta*(val-mMin))] += n;
267  mSize += n;
268  return true;
269  }
270 
273  bool add(const Histogram& other)
274  {
275  if (!isApproxEqual(mMin, other.mMin) || !isApproxEqual(mMax, other.mMax) ||
276  mBins.size() != other.mBins.size()) return false;
277  for (size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
278  mSize += other.mSize;
279  return true;
280  }
281 
283  inline size_t numBins() const { return mBins.size(); }
285  inline double min() const { return mMin; }
287  inline double max() const { return mMax; }
289  inline double min(int n) const { return mMin+n/mDelta; }
291  inline double max(int n) const { return mMin+(n+1)/mDelta; }
293  inline uint64_t count(int n) const { return mBins[n]; }
295  inline uint64_t size() const { return mSize; }
296 
298  void print(const std::string& name = "", std::ostream& strm = std::cout) const
299  {
300  // Write to a temporary string stream so as not to affect the state
301  // (precision, field width, etc.) of the output stream.
302  std::ostringstream os;
303  os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
304  os << "Histogram ";
305  if (!name.empty()) os << "for \"" << name << "\" ";
306  if (mSize > 0) {
307  os << "with " << mSize << " samples:\n";
308  os << "==============================================================\n";
309  os << "|| # | Min | Max | Frequency | % ||\n";
310  os << "==============================================================\n";
311  for (int i = 0, e = int(mBins.size()); i != e; ++i) {
312  os << "|| " << std::setw(4) << i << " | " << std::setw(14) << this->min(i) << " | "
313  << std::setw(14) << this->max(i) << " | " << std::setw(9) << mBins[i] << " | "
314  << std::setw(3) << (100*mBins[i]/mSize) << " ||\n";
315  }
316  os << "==============================================================\n";
317  } else {
318  os << ": no samples were added." << std::endl;
319  }
320  strm << os.str();
321  }
322 
323 private:
324  uint64_t mSize;
325  double mMin, mMax, mDelta;
326  std::vector<uint64_t> mBins;
327 };
328 
329 } // namespace math
330 } // namespace OPENVDB_VERSION_NAME
331 } // namespace openvdb
332 
333 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
334 
335 // Copyright (c) 2012-2014 DreamWorks Animation LLC
336 // All rights reserved. This software is distributed under the
337 // 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:298
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:75
double min() const
Return the minimum value.
Definition: Stats.h:86
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:202
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
Definition: Stats.h:240
double min(int n) const
Return the minimum value in the nth bin.
Definition: Stats.h:289
void add(double val, uint64_t n)
Add n samples with constant value val.
Definition: Stats.h:159
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:98
double variance() const
Return the population variance.
Definition: Stats.h:191
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
double var() const
Return the population variance.
Definition: Stats.h:190
void add(double val)
Add a single sample.
Definition: Stats.h:150
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Definition: Stats.h:83
double min() const
Return the lower bound of this histogram's value range.
Definition: Stats.h:285
uint64_t mSize
Definition: Stats.h:126
size_t numBins() const
Return the number of bins in this histogram.
Definition: Stats.h:283
double mAvg
Definition: Stats.h:227
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:263
double mAux
Definition: Stats.h:227
void add(const Extrema &other)
Add the samples from the other Stats instance.
Definition: Stats.h:92
#define OPENVDB_VERSION_NAME
Definition: version.h:43
This class computes the minimum and maximum values of a population of floating-point values...
Definition: Stats.h:53
double mean() const
Return the arithmetic mean, i.e. average, value.
Definition: Stats.h:183
Definition: Exceptions.h:39
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
Definition: Stats.h:139
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:251
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Definition: Stats.h:236
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
bool isApproxEqual(const Hermite &lhs, const Hermite &rhs)
Definition: Hermite.h:470
void add(double val)
Add a single sample.
Definition: Stats.h:67
void add(const Stats &other)
Add the samples from the other Stats instance.
Definition: Stats.h:169
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
Definition: Stats.h:198
double mMin
Definition: Stats.h:127
Stats()
Definition: Stats.h:142
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
double mMax
Definition: Stats.h:127
double max() const
Return the upper bound of this histogram's value range.
Definition: Stats.h:287
double max(int n) const
Return the maximum value in the nth bin.
Definition: Stats.h:291
void join(const Extrema &other)
Definition: Stats.h:118
std::string str() const
String representation.
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
uint64_t count(int n) const
Return the number of samples in the nth bin.
Definition: Stats.h:293
Extrema()
Constructor.
Definition: Stats.h:59
double max() const
Return the maximum value.
Definition: Stats.h:89
uint64_t size() const
Return the population size, i.e., the total number of samples.
Definition: Stats.h:295
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:273