blob: 42ab62b4efd7cfe21c1a8d9d95f7dcdbca229b96 [file] [log] [blame]
Glenn Kasten131515b2011-07-08 14:36:46 -07001/*
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#include <stdlib.h>
18
19#include <cpustats/CentralTendencyStatistics.h>
20
21void CentralTendencyStatistics::sample(double x)
22{
23 // update min and max
24 if (x < mMinimum)
25 mMinimum = x;
26 if (x > mMaximum)
27 mMaximum = x;
28 // Knuth
29 if (mN == 0) {
30 mMean = 0;
31 }
32 ++mN;
33 double delta = x - mMean;
34 mMean += delta / mN;
35 mM2 += delta * (x - mMean);
36}
37
38void CentralTendencyStatistics::reset()
39{
40 mMean = NAN;
41 mMedian = NAN;
42 mMinimum = INFINITY;
43 mMaximum = -INFINITY;
44 mN = 0;
45 mM2 = 0;
46 mVariance = NAN;
47 mVarianceKnownForN = 0;
48 mStddev = NAN;
49 mStddevKnownForN = 0;
50}
51
52double CentralTendencyStatistics::variance() const
53{
54 double variance;
55 if (mVarianceKnownForN != mN) {
56 if (mN > 1) {
57 // double variance_n = M2/n;
58 variance = mM2 / (mN - 1);
59 } else {
60 variance = NAN;
61 }
62 mVariance = variance;
63 mVarianceKnownForN = mN;
64 } else {
65 variance = mVariance;
66 }
67 return variance;
68}
69
70double CentralTendencyStatistics::stddev() const
71{
72 double stddev;
73 if (mStddevKnownForN != mN) {
74 stddev = sqrt(variance());
75 mStddev = stddev;
76 mStddevKnownForN = mN;
77 } else {
78 stddev = mStddev;
79 }
80 return stddev;
81}