summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/util/StatSummary.java
blob: 3b4713c8a8daa36e8829b072be6bdce765a7d7ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (c) 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.vts.util;

import com.android.vts.proto.VtsReportMessage.VtsProfilingRegressionMode;

/** Helper object for storing statistics. */
public class StatSummary {
    private String label;
    private double min;
    private double max;
    private double mean;
    private double sumSq;
    private int n;
    private VtsProfilingRegressionMode regression_mode;

    /**
     * Create a statistical summary.
     *
     * <p>Sets the label, min, max, mean, sum of squared error, n, and mode as provided.
     *
     * @param label The (String) label to assign to the summary.
     * @param min The minimum observed value.
     * @param max The maximum observed value.
     * @param mean The average observed value.
     * @param sumSq The sum of squared error.
     * @param n The number of values observed.
     * @param mode The VtsProfilingRegressionMode to use when analyzing performance.
     */
    public StatSummary(
            String label,
            double min,
            double max,
            double mean,
            double sumSq,
            int n,
            VtsProfilingRegressionMode mode) {
        this.label = label;
        this.min = min;
        this.max = max;
        this.mean = mean;
        this.sumSq = sumSq;
        this.n = n;
        this.regression_mode = mode;
    }

    /**
     * Initializes the statistical summary.
     *
     * <p>Sets the label as provided. Initializes the mean, variance, and n (number of values seen)
     * to 0.
     *
     * @param label The (String) label to assign to the summary.
     * @param mode The VtsProfilingRegressionMode to use when analyzing performance.
     */
    public StatSummary(String label, VtsProfilingRegressionMode mode) {
        this(label, Double.MAX_VALUE, Double.MIN_VALUE, 0, 0, 0, mode);
    }

    /**
     * Update the mean and variance using Welford's single-pass method.
     *
     * @param value The observed value in the stream.
     */
    public void updateStats(double value) {
        n += 1;
        double oldMean = mean;
        mean = oldMean + (value - oldMean) / n;
        sumSq = sumSq + (value - mean) * (value - oldMean);
        if (value < min) min = value;
        if (value > max) max = value;
    }

    /**
     * Combine the mean and variance with another StatSummary.
     *
     * @param stat The StatSummary to combine with.
     */
    public void merge(StatSummary stat) {
        double delta = stat.getMean() - mean;
        int newN = n + stat.getCount();
        sumSq = sumSq + stat.getSumSq() + delta / newN * delta * n * stat.getCount();
        double recipN = 1.0 / newN;
        mean = n * recipN * mean + stat.getCount() * recipN * stat.getMean();
        n = newN;
    }

    /**
     * Gets the best case of the stream.
     *
     * @return The min or max.
     */
    public double getBestCase() {
        switch (regression_mode) {
            case VTS_REGRESSION_MODE_DECREASING:
                return getMax();
            default:
                return getMin();
        }
    }

    /**
     * Gets the calculated min of the stream.
     *
     * @return The min.
     */
    public double getMin() {
        return min;
    }

    /**
     * Gets the calculated max of the stream.
     *
     * @return The max.
     */
    public double getMax() {
        return max;
    }

    /**
     * Gets the calculated mean of the stream.
     *
     * @return The mean.
     */
    public double getMean() {
        return mean;
    }

    /**
     * Gets the calculated sum of squared error of the stream.
     *
     * @return The sum of squared error.
     */
    public double getSumSq() {
        return sumSq;
    }

    /**
     * Gets the calculated standard deviation of the stream.
     *
     * @return The standard deviation.
     */
    public double getStd() {
        return Math.sqrt(sumSq / (n - 1));
    }

    /**
     * Gets the number of elements that have passed through the stream.
     *
     * @return Number of elements.
     */
    public int getCount() {
        return n;
    }

    /**
     * Gets the label for the summarized statistics.
     *
     * @return The (string) label.
     */
    public String getLabel() {
        return label;
    }

    /**
     * Gets the regression mode.
     *
     * @return The VtsProfilingRegressionMode value.
     */
    public VtsProfilingRegressionMode getRegressionMode() {
        return regression_mode;
    }
}