MongoDB  2.7.0
descriptive_stats.h
1 /* Copyright 2012 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <cmath>
19 
20 #include "mongo/db/jsobj.h"
21 #include "mongo/util/assert_util.h"
22 
38 namespace mongo {
39 
48  template <class Sample>
50  public:
52 
56  BasicEstimators& operator <<(const Sample sample);
57 
61  inline size_t count() const { return _count; }
62 
67  inline double mean() const { return _sum / _count; }
68 
73  inline double stddev() const { return std::sqrt(_diff / _count); }
74 
79  inline Sample min() const { return _min; }
80 
85  inline Sample max() const { return _max; }
86 
91 
92  private:
93  size_t _count;
94  double _sum;
95  double _diff; // sum of squares of differences from the (then-current) mean
96  Sample _min;
97  Sample _max;
98  };
99 
110  template <std::size_t NumQuantiles>
112  public:
114 
115  DistributionEstimators& operator <<(const double sample);
116 
120  static const size_t numberOfQuantiles = NumQuantiles;
121 
125  inline double quantile(std::size_t i) const {
126  massert(16476, "the requested value is out of the range of the computed quantiles",
127  i <= NumQuantiles + 1);
128  return this->_heights[2 * i];
129  }
130 
134  inline bool quantilesReady() const {
135  return _count >= NumMarkers;
136  }
137 
143  inline double min() const {
144  return quantile(0);
145  }
146 
152  inline double max() const {
153  return quantile(NumQuantiles + 1);
154  }
155 
159  inline double median() const {
160  return icdf(.5);
161  }
162 
166  inline double probability(std::size_t i) const {
167  return i * 1. / (NumQuantiles + 1);
168  }
169 
173  inline double icdf(double prob) const {
174  int quant = static_cast<int>(prob * (NumQuantiles + 1) + 0.5);
175  return quantile(quant);
176  }
177 
183 
184  private:
185  inline double _positions_increments(std::size_t i) const;
186 
187  int _count;
188  enum { NumMarkers = 2 * NumQuantiles + 3 };
189  double _heights[NumMarkers]; // q_i
190  double _actual_positions[NumMarkers]; // n_i
191  double _desired_positions[NumMarkers]; // d_i
192  };
193 
197  template <class Sample, std::size_t NumQuantiles>
199  // Multiple-inheritance
200  public BasicEstimators<Sample>,
201  public DistributionEstimators<NumQuantiles> {
202  public:
203  // Dispatch samples to the inherited estimators
204  inline SummaryEstimators& operator<<(const Sample sample) {
207  return *this;
208  }
209 
210  // Expose the exact values
211  inline Sample min() const {
212  return this->BasicEstimators<Sample>::min();
213  }
214 
215  inline Sample max() const {
216  return this->BasicEstimators<Sample>::max();
217  }
218 
223  };
224 
225 } // namespace mongo
226 
227 #include "mongo/util/descriptive_stats-inl.h"
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:52
Collects count, minimum and maximum, calculates mean and standard deviation.
Definition: descriptive_stats.h:49
Sample max() const
Definition: descriptive_stats.h:85
double icdf(double prob) const
Definition: descriptive_stats.h:173
static const size_t numberOfQuantiles
Number of computed quantiles, excluding minimum and maximum.
Definition: descriptive_stats.h:120
double median() const
Definition: descriptive_stats.h:159
BSON classes.
Provides the funcionality of both BasicEstimators and DistributionEstimators.
Definition: descriptive_stats.h:198
double stddev() const
Definition: descriptive_stats.h:73
void appendQuantilesToBSONArrayBuilder(BSONArrayBuilder &arr) const
Appends the quantiles to the provided BSONArrayBuilder.
Definition: descriptive_stats-inl.h:177
double min() const
Definition: descriptive_stats.h:143
BSONObj statisticSummaryToBSONObj() const
Definition: descriptive_stats-inl.h:192
BasicEstimators & operator<<(const Sample sample)
Update estimators with another observed value.
Definition: descriptive_stats-inl.h:42
double quantile(std::size_t i) const
Updates the estimators with another observed value.
Definition: descriptive_stats.h:125
bool quantilesReady() const
Definition: descriptive_stats.h:134
double probability(std::size_t i) const
Definition: descriptive_stats.h:166
double max() const
Definition: descriptive_stats.h:152
Computes 'NumQuantiles' quantiles.
Definition: descriptive_stats.h:111
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition: bsonobj.h:77
Sample min() const
Definition: descriptive_stats.h:79
Definition: bsonobjbuilder.h:705
double mean() const
Definition: descriptive_stats.h:67
void appendBasicToBSONObjBuilder(BSONObjBuilder &b) const
Appends the basic estimators to the provided BSONObjBuilder.
Definition: descriptive_stats-inl.h:55
size_t count() const
Definition: descriptive_stats.h:61