Home | History | Annotate | Download | only in cpustats
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      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
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 #ifndef _CENTRAL_TENDENCY_STATISTICS_H
     18 #define _CENTRAL_TENDENCY_STATISTICS_H
     19 
     20 #include <math.h>
     21 
     22 // Not multithread safe
     23 class CentralTendencyStatistics {
     24 
     25 public:
     26 
     27     CentralTendencyStatistics() :
     28             mMean(NAN), mMedian(NAN), mMinimum(INFINITY), mMaximum(-INFINITY), mN(0), mM2(0),
     29             mVariance(NAN), mVarianceKnownForN(0), mStddev(NAN), mStddevKnownForN(0) { }
     30 
     31     ~CentralTendencyStatistics() { }
     32 
     33     // add x to the set of samples
     34     void sample(double x);
     35 
     36     // return the arithmetic mean of all samples so far
     37     double mean() const { return mMean; }
     38 
     39     // return the minimum of all samples so far
     40     double minimum() const { return mMinimum; }
     41 
     42     // return the maximum of all samples so far
     43     double maximum() const { return mMaximum; }
     44 
     45     // return the variance of all samples so far
     46     double variance() const;
     47 
     48     // return the standard deviation of all samples so far
     49     double stddev() const;
     50 
     51     // return the number of samples added so far
     52     unsigned n() const { return mN; }
     53 
     54     // reset the set of samples to be empty
     55     void reset();
     56 
     57 private:
     58     double mMean;
     59     double mMedian;
     60     double mMinimum;
     61     double mMaximum;
     62     unsigned mN;    // number of samples so far
     63     double mM2;
     64 
     65     // cached variance, and n at time of caching
     66     mutable double mVariance;
     67     mutable unsigned mVarianceKnownForN;
     68 
     69     // cached standard deviation, and n at time of caching
     70     mutable double mStddev;
     71     mutable unsigned mStddevKnownForN;
     72 
     73 };
     74 
     75 #endif // _CENTRAL_TENDENCY_STATISTICS_H
     76