|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-18 diirt developers. See COPYRIGHT.TXT |
| 3 | + * All rights reserved. Use is subject to license terms. See LICENSE.TXT |
| 4 | + */ |
| 5 | +package org.epics.util.stats; |
| 6 | + |
| 7 | +import java.util.Iterator; |
| 8 | +import java.util.List; |
| 9 | +import org.epics.util.array.CollectionNumber; |
| 10 | +import org.epics.util.array.IteratorNumber; |
| 11 | + |
| 12 | +/** |
| 13 | + * Utility class to calculate statistical information. |
| 14 | + * |
| 15 | + * @author carcassi |
| 16 | + */ |
| 17 | +public class StatisticsUtil { |
| 18 | + |
| 19 | + private static class StatisticsImpl extends Statistics { |
| 20 | + |
| 21 | + private final int count; |
| 22 | + private final Range range; |
| 23 | + private final double average; |
| 24 | + private final double stdDev; |
| 25 | + |
| 26 | + public StatisticsImpl(Range range, int count, double average, double stdDev) { |
| 27 | + this.count = count; |
| 28 | + this.range = range; |
| 29 | + this.average = average; |
| 30 | + this.stdDev = stdDev; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public int getCount() { |
| 35 | + return count; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Range getRange() { |
| 40 | + return range; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public double getAverage() { |
| 45 | + return average; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public double getStdDev() { |
| 50 | + return stdDev; |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Calculates data statistics, excluding NaN values. |
| 57 | + * |
| 58 | + * @param data the data |
| 59 | + * @return the calculated statistics |
| 60 | + */ |
| 61 | + public static Statistics statisticsOf(CollectionNumber data) { |
| 62 | + IteratorNumber iterator = data.iterator(); |
| 63 | + if (!iterator.hasNext()) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + int count = 0; |
| 67 | + double min = iterator.nextDouble(); |
| 68 | + while (Double.isNaN(min)) { |
| 69 | + if (!iterator.hasNext()) { |
| 70 | + return null; |
| 71 | + } else { |
| 72 | + min = iterator.nextDouble(); |
| 73 | + } |
| 74 | + } |
| 75 | + double max = min; |
| 76 | + double total = min; |
| 77 | + double totalSquare = min*min; |
| 78 | + count++; |
| 79 | + |
| 80 | + while (iterator.hasNext()) { |
| 81 | + double value = iterator.nextDouble(); |
| 82 | + if (!Double.isNaN(value)) { |
| 83 | + if (value > max) |
| 84 | + max = value; |
| 85 | + if (value < min) |
| 86 | + min = value; |
| 87 | + total += value; |
| 88 | + totalSquare += value*value; |
| 89 | + count++; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + double average = total/count; |
| 94 | + double stdDev = Math.sqrt(totalSquare / count - average * average); |
| 95 | + |
| 96 | + return new StatisticsImpl(Range.of(min, max), count, average, stdDev); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Aggregates statistical information. |
| 101 | + * |
| 102 | + * @param data a list of statistical information |
| 103 | + * @return the aggregate of all |
| 104 | + */ |
| 105 | + public static Statistics statisticsOf(List<Statistics> data) { |
| 106 | + if (data.isEmpty()) |
| 107 | + return null; |
| 108 | + |
| 109 | + Iterator<Statistics> iterator = data.iterator(); |
| 110 | + if (!iterator.hasNext()) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + Statistics first = null; |
| 114 | + while (first == null && iterator.hasNext()) { |
| 115 | + first = iterator.next(); |
| 116 | + } |
| 117 | + if (first == null) |
| 118 | + return null; |
| 119 | + |
| 120 | + int count = first.getCount(); |
| 121 | + Range range = first.getRange(); |
| 122 | + double total = first.getAverage() * first.getCount(); |
| 123 | + double totalSquare = (first.getStdDev() * first.getStdDev() + first.getAverage() * first.getAverage()) * first.getCount(); |
| 124 | + |
| 125 | + while (iterator.hasNext()) { |
| 126 | + Statistics stats = iterator.next(); |
| 127 | + range = range.combine(stats.getRange()); |
| 128 | + total += stats.getAverage() * stats.getCount(); |
| 129 | + totalSquare += ( stats.getStdDev() * stats.getStdDev() + stats.getAverage() * stats.getAverage() ) * stats.getCount(); |
| 130 | + count += stats.getCount(); |
| 131 | + } |
| 132 | + |
| 133 | + double average = total/count; |
| 134 | + double stdDev = Math.sqrt(totalSquare / count - average * average); |
| 135 | + |
| 136 | + return new StatisticsImpl(range, count, average, stdDev); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Creates the statistics, excluding NaN values, but the values |
| 141 | + * are actually calculated when requested. |
| 142 | + * |
| 143 | + * @param data the data |
| 144 | + * @return the calculated statistics |
| 145 | + */ |
| 146 | + public static Statistics lazyStatisticsOf(CollectionNumber data) { |
| 147 | + return new LazyStatistics(data); |
| 148 | + } |
| 149 | + |
| 150 | + private static class LazyStatistics extends Statistics { |
| 151 | + |
| 152 | + private Statistics stats; |
| 153 | + private CollectionNumber data; |
| 154 | + |
| 155 | + public LazyStatistics(CollectionNumber data) { |
| 156 | + this.data = data; |
| 157 | + } |
| 158 | + |
| 159 | + private void calculateStats() { |
| 160 | + if (stats == null) { |
| 161 | + stats = statisticsOf(data); |
| 162 | + data = null; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public int getCount() { |
| 168 | + calculateStats(); |
| 169 | + return stats.getCount(); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public double getAverage() { |
| 174 | + calculateStats(); |
| 175 | + return stats.getAverage(); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public double getStdDev() { |
| 180 | + calculateStats(); |
| 181 | + return stats.getStdDev(); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public Range getRange() { |
| 186 | + calculateStats(); |
| 187 | + return stats.getRange(); |
| 188 | + } |
| 189 | + |
| 190 | + } |
| 191 | +} |
0 commit comments