2
* Copyright (C) 2011 The Android Open Source Project
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#ifndef _CENTRAL_TENDENCY_STATISTICS_H
18
#define _CENTRAL_TENDENCY_STATISTICS_H
22
// Not multithread safe
23
class CentralTendencyStatistics {
27
CentralTendencyStatistics() :
28
mMean(NAN), mMedian(NAN), mMinimum(INFINITY), mMaximum(-INFINITY), mN(0), mM2(0),
29
mVariance(NAN), mVarianceKnownForN(0), mStddev(NAN), mStddevKnownForN(0) { }
31
~CentralTendencyStatistics() { }
33
// add x to the set of samples
34
void sample(double x);
36
// return the arithmetic mean of all samples so far
37
double mean() const { return mMean; }
39
// return the minimum of all samples so far
40
double minimum() const { return mMinimum; }
42
// return the maximum of all samples so far
43
double maximum() const { return mMaximum; }
45
// return the variance of all samples so far
46
double variance() const;
48
// return the standard deviation of all samples so far
49
double stddev() const;
51
// return the number of samples added so far
52
unsigned n() const { return mN; }
54
// reset the set of samples to be empty
62
unsigned mN; // number of samples so far
65
// cached variance, and n at time of caching
66
mutable double mVariance;
67
mutable unsigned mVarianceKnownForN;
69
// cached standard deviation, and n at time of caching
70
mutable double mStddev;
71
mutable unsigned mStddevKnownForN;
75
#endif // _CENTRAL_TENDENCY_STATISTICS_H