OpenVDB  1.2.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  mMin = std::min<double>(mMin, other.mMin);
92  mMax = std::max<double>(mMax, other.mMax);
93  const double denom = 1.0/double(mSize + other.mSize);
94  const double delta = other.mAvg - mAvg;
95  mAvg += denom*delta*other.mSize;
96  mAux += other.mAux + denom*delta*delta*mSize*other.mSize;
97  mSize += other.mSize;
98  }
99 
101  uint64_t size() const { return mSize; }
103  double min() const { return mMin; }
105  double max() const { return mMax; }
107  double mean() const { return mAvg; }
110  double variance() const { return mSize<2 ? 0.0 : mAux/double(mSize); }
113  double stdDev() const { return sqrt(this->variance()); }
114 
116  void print(const std::string &name= "", std::ostream &strm=std::cout, int precision=3) const
117  {
118  // Write to a temporary string stream so as not to affect the state
119  // (precision, field width, etc.) of the output stream.
120  std::ostringstream os;
121  os << std::setprecision(precision) << std::setiosflags(std::ios::fixed);
122  os << "Statistics ";
123  if (!name.empty()) os << "for \"" << name << "\" ";
124  os << "with " << mSize << " samples:\n"
125  << " Min=" << mMin
126  << ", Max=" << mMax
127  << ", Ave=" << mAvg
128  << ", Std=" << this->stdDev()
129  << ", Var=" << this->variance() << std::endl;
130  strm << os.str();
131  }
132 
133 private:
134  uint64_t mSize;
135  double mAvg, mAux, mMin, mMax;
136 }; // end Stats
137 
138 
140 
141 
145 {
146 public:
148  Histogram(double min, double max, size_t numBins = 10)
149  : mSize(0), mMin(min), mMax(max+1e-10),
150  mDelta(double(numBins)/(max-min)), mBins(numBins)
151  {
152  assert(numBins > 1);
153  assert(mMax-mMin > 1e-10);
154  for (size_t i=0; i<numBins; ++i) mBins[i]=0;
155  }
156 
159  Histogram(const Stats& s, size_t numBins = 10):
160  mSize(0), mMin(s.min()), mMax(s.max()+1e-10),
161  mDelta(double(numBins)/(mMax-mMin)), mBins(numBins)
162  {
163  assert(numBins > 1);
164  assert(mMax-mMin > 1e-10);
165  for (size_t i=0; i<numBins; ++i) mBins[i]=0;
166  }
167 
171  bool add(double val, uint64_t n = 1)
172  {
173  if (val<mMin || val>mMax) return false;
174  mBins[size_t(mDelta*(val-mMin))] += n;
175  mSize += n;
176  return true;
177  }
178 
181  bool add(const Histogram& other)
182  {
183  if (!isApproxEqual(mMin, other.mMin) || !isApproxEqual(mMax, other.mMax) ||
184  mBins.size() != other.mBins.size()) return false;
185  for (size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
186  mSize += other.mSize;
187  return true;
188  }
189 
191  size_t numBins() const { return mBins.size(); }
193  double min() const { return mMin; }
195  double max() const { return mMax; }
197  double min(int n) const { return mMin+n/mDelta; }
199  double max(int n) const { return mMin+(n+1)/mDelta; }
201  uint64_t count(int n) const { return mBins[n]; }
203  uint64_t size() const { return mSize; }
204 
206  void print(const std::string& name = "", std::ostream& strm = std::cout) const
207  {
208  // Write to a temporary string stream so as not to affect the state
209  // (precision, field width, etc.) of the output stream.
210  std::ostringstream os;
211  os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
212  os << "Histogram ";
213  if (!name.empty()) os << "for \"" << name << "\" ";
214  os << "with " << mSize << " samples:\n";
215  os << "==============================================================\n";
216  os << "|| # | Min | Max | Frequency | % ||\n";
217  os << "==============================================================\n";
218  for (size_t i=0, e=mBins.size(); i!=e; ++i) {
219  os << "|| " << std::setw(4) << i << " | " << std::setw(14) << this->min(i) << " | "
220  << std::setw(14) << this->max(i) << " | " << std::setw(9) << mBins[i] << " | "
221  << std::setw(3) << (100*mBins[i]/mSize) << " ||\n";
222  }
223  os << "==============================================================\n";
224  strm << os.str();
225  }
226 
227 private:
228  uint64_t mSize;
229  double mMin, mMax, mDelta;
230  std::vector<uint64_t> mBins;
231 };
232 
233 } // namespace math
234 } // namespace OPENVDB_VERSION_NAME
235 } // namespace openvdb
236 
237 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
238 
239 // Copyright (c) 2012-2013 DreamWorks Animation LLC
240 // All rights reserved. This software is distributed under the
241 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )