7adbe7255ed4cec61c22a343e0e7eb0b2bad91ae
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / analysis / timing / core / segmentstore / statistics / SegmentStoreStatistics.java
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics;
13
14 import org.eclipse.tracecompass.segmentstore.core.BasicSegment;
15 import org.eclipse.tracecompass.segmentstore.core.ISegment;
16
17 /**
18 * Class to calculate simple segment store statistics (min, max, average)
19 *
20 * @author Bernd Hufmann
21 */
22 public class SegmentStoreStatistics {
23 private ISegment fMin;
24 private ISegment fMax;
25 private long fNbSegments;
26 private double fAverage;
27 private double fVariance;
28
29 /**
30 * Constructor
31 */
32 public SegmentStoreStatistics() {
33 fMin = new BasicSegment(0, Long.MAX_VALUE);
34 fMax = new BasicSegment(Long.MIN_VALUE, 0);
35 fNbSegments = 0;
36 fAverage = 0.0;
37 fVariance = 0.0;
38 }
39
40 /**
41 * Get minimum value
42 *
43 * @return minimum value
44 */
45 public long getMin() {
46 return fMin.getLength();
47 }
48
49 /**
50 * Get maximum value
51 *
52 * @return maximum value
53 */
54 public long getMax() {
55 return fMax.getLength();
56 }
57
58 /**
59 * Get segment with minimum length
60 *
61 * @return segment with minimum length
62 */
63 public ISegment getMinSegment() {
64 return fMin;
65 }
66
67 /**
68 * Get segment with maximum length
69 *
70 * @return segment with maximum length
71 */
72 public ISegment getMaxSegment() {
73 return fMax;
74 }
75
76 /**
77 * Get number of segments analyzed
78 *
79 * @return number of segments analyzed
80 */
81 public long getNbSegments() {
82 return fNbSegments;
83 }
84
85 /**
86 * Gets the arithmetic average
87 *
88 * @return arithmetic average
89 */
90 public double getAverage() {
91 return fAverage;
92 }
93
94 /**
95 * Gets the standard deviation of the segments, uses the online algorithm
96 * shown here <a href=
97 * "https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm">
98 * Wikipedia article of dec 3 2015 </a>
99 *
100 * @return the standard deviation of the segment store, will return NaN if
101 * there are less than 3 elements
102 */
103 public double getStdDev() {
104 return fNbSegments > 2 ? Math.sqrt(fVariance / (fNbSegments - 1)) : Double.NaN;
105 }
106
107 /**
108 * Update the statistics based on a given segment
109 * <p>
110 * This is an online algorithm and must retain a complexity of O(1)
111 *
112 * @param segment
113 * the segment used for the update
114 */
115 public void update(ISegment segment) {
116 long value = segment.getLength();
117 /*
118 * Min and max are trivial, as well as number of segments
119 */
120 long min = fMin.getLength();
121 long max = fMax.getLength();
122 fMin = min <= value ? fMin : segment;
123 fMax = max >= value ? fMax : segment;
124
125 fNbSegments++;
126 /*
127 * The running mean is not trivial, see proof in javadoc.
128 */
129 double delta = value - fAverage;
130 fAverage += delta / fNbSegments;
131 fVariance += delta * (value - fAverage);
132 }
133 }
This page took 0.056307 seconds and 4 git commands to generate.