1 package org.djutils.stats.summarizers;
2
3 import java.util.Random;
4
5 import org.djutils.stats.summarizers.quantileaccumulator.FixedBinsAccumulator;
6 import org.djutils.stats.summarizers.quantileaccumulator.FullStorageAccumulator;
7 import org.djutils.stats.summarizers.quantileaccumulator.TDigestAccumulator;
8
9
10
11
12
13
14
15
16
17
18
19 public final class TallyDemo
20 {
21
22 private TallyDemo()
23 {
24
25 }
26
27
28
29
30
31 public static void main(final String[] args)
32 {
33 Tally tally = new Tally("Example tally");
34 Random random = new Random(1234);
35 for (int i = 0; i < 1000; i++)
36 {
37 tally.register(random.nextDouble());
38 }
39 System.out.println("minimum: " + tally.getMin());
40 System.out.println("maximum: " + tally.getMax());
41 System.out.println("count: " + tally.getN());
42 System.out.println("sum: " + tally.getSum());
43 System.out.println("sample mean: " + tally.getSampleMean());
44 System.out.println("sample variance: " + tally.getSampleVariance());
45 System.out.println("sample standard deviation: " + tally.getSampleStDev());
46 System.out.println("sample skewness: " + tally.getSampleSkewness());
47 System.out.println("sample kurtosis: " + tally.getSampleKurtosis());
48 System.out.println("sample excess kurtosis: " + tally.getSampleExcessKurtosis());
49 System.out.println("population mean: " + tally.getPopulationMean());
50 System.out.println("population variance: " + tally.getPopulationVariance());
51 System.out.println("population standard deviation: " + tally.getPopulationStDev());
52 System.out.println("population skewness: " + tally.getPopulationSkewness());
53 System.out.println("population kurtosis: " + tally.getPopulationKurtosis());
54 System.out.println("population excess kurtosis: " + tally.getPopulationExcessKurtosis());
55 System.out.println("first quartile: " + tally.getQuantile(0.25));
56 System.out.println("median: " + tally.getQuantile(0.5));
57 System.out.println("third quartile: " + tally.getQuantile(0.75));
58 for (int bin = 0; bin <= 10; bin++)
59 {
60 double value = bin / 10.0;
61 System.out.println("Cumulative probability at " + value + " " + tally.getCumulativeProbability(value));
62 }
63
64 System.out.println("End of NoStorageAccumulator output.\n");
65 tally = new Tally("Example tally with full storage accumulator", new FullStorageAccumulator());
66 random = new Random(1234);
67 for (int i = 0; i < 1000; i++)
68 {
69 tally.register(random.nextDouble());
70 }
71 System.out.println("minimum: " + tally.getMin());
72 System.out.println("maximum: " + tally.getMax());
73 System.out.println("count: " + tally.getN());
74 System.out.println("sum: " + tally.getSum());
75 System.out.println("sample mean: " + tally.getSampleMean());
76 System.out.println("sample variance: " + tally.getSampleVariance());
77 System.out.println("sample standard deviation: " + tally.getSampleStDev());
78 System.out.println("sample skewness: " + tally.getSampleSkewness());
79 System.out.println("sample kurtosis: " + tally.getSampleKurtosis());
80 System.out.println("sample excess kurtosis: " + tally.getSampleExcessKurtosis());
81 System.out.println("population mean: " + tally.getPopulationMean());
82 System.out.println("population variance: " + tally.getPopulationVariance());
83 System.out.println("population standard deviation: " + tally.getPopulationStDev());
84 System.out.println("population skewness: " + tally.getPopulationSkewness());
85 System.out.println("population kurtosis: " + tally.getPopulationKurtosis());
86 System.out.println("population excess kurtosis: " + tally.getPopulationExcessKurtosis());
87 System.out.println("first quartile: " + tally.getQuantile(0.25));
88 System.out.println("median: " + tally.getQuantile(0.5));
89 System.out.println("third quartile: " + tally.getQuantile(0.75));
90 for (int bin = 0; bin <= 10; bin++)
91 {
92 double value = bin / 10.0;
93 System.out.println("Cumulative probability at " + value + " " + tally.getCumulativeProbability(value));
94 }
95
96 System.out.println("End of FullStorageAccumulator output.\n");
97 tally = new Tally("Example tally with TDigest accumulator", new TDigestAccumulator());
98 random = new Random(1234);
99 for (int i = 0; i < 1000; i++)
100 {
101 tally.register(random.nextDouble());
102 }
103 System.out.println("minimum: " + tally.getMin());
104 System.out.println("maximum: " + tally.getMax());
105 System.out.println("count: " + tally.getN());
106 System.out.println("sum: " + tally.getSum());
107 System.out.println("sample mean: " + tally.getSampleMean());
108 System.out.println("sample variance: " + tally.getSampleVariance());
109 System.out.println("sample standard deviation: " + tally.getSampleStDev());
110 System.out.println("sample skewness: " + tally.getSampleSkewness());
111 System.out.println("sample kurtosis: " + tally.getSampleKurtosis());
112 System.out.println("sample excess kurtosis: " + tally.getSampleExcessKurtosis());
113 System.out.println("population mean: " + tally.getPopulationMean());
114 System.out.println("population variance: " + tally.getPopulationVariance());
115 System.out.println("population standard deviation: " + tally.getPopulationStDev());
116 System.out.println("population skewness: " + tally.getPopulationSkewness());
117 System.out.println("population kurtosis: " + tally.getPopulationKurtosis());
118 System.out.println("population excess kurtosis: " + tally.getPopulationExcessKurtosis());
119 System.out.println("first quartile: " + tally.getQuantile(0.25));
120 System.out.println("median: " + tally.getQuantile(0.5));
121 System.out.println("third quartile: " + tally.getQuantile(0.75));
122 for (int bin = 0; bin <= 10; bin++)
123 {
124 double value = bin / 10.0;
125 System.out.println("Cumulative probability at " + value + " " + tally.getCumulativeProbability(value));
126 }
127
128 System.out.println();
129 tally = new Tally("Example tally with TDigest accumulator with higher precision", new TDigestAccumulator(1000));
130 random = new Random(1234);
131 for (int i = 0; i < 1000; i++)
132 {
133 tally.register(random.nextDouble());
134 }
135 System.out.println("minimum: " + tally.getMin());
136 System.out.println("maximum: " + tally.getMax());
137 System.out.println("count: " + tally.getN());
138 System.out.println("sum: " + tally.getSum());
139 System.out.println("sample mean: " + tally.getSampleMean());
140 System.out.println("sample variance: " + tally.getSampleVariance());
141 System.out.println("sample standard deviation: " + tally.getSampleStDev());
142 System.out.println("sample skewness: " + tally.getSampleSkewness());
143 System.out.println("sample kurtosis: " + tally.getSampleKurtosis());
144 System.out.println("sample excess kurtosis: " + tally.getSampleExcessKurtosis());
145 System.out.println("population mean: " + tally.getPopulationMean());
146 System.out.println("population variance: " + tally.getPopulationVariance());
147 System.out.println("population standard deviation: " + tally.getPopulationStDev());
148 System.out.println("population skewness: " + tally.getPopulationSkewness());
149 System.out.println("population kurtosis: " + tally.getPopulationKurtosis());
150 System.out.println("population excess kurtosis: " + tally.getPopulationExcessKurtosis());
151 System.out.println("first quartile: " + tally.getQuantile(0.25));
152 System.out.println("median: " + tally.getQuantile(0.5));
153 System.out.println("third quartile: " + tally.getQuantile(0.75));
154 for (int bin = 0; bin <= 10; bin++)
155 {
156 double value = bin / 10.0;
157 System.out.println("Cumulative probability at " + value + " " + tally.getCumulativeProbability(value));
158 }
159
160 System.out.println("End of TDigestAccumulator output.\n");
161
162 tally = new Tally("Example tally with FixedBinsAccumulator using 1000 bins",
163 new FixedBinsAccumulator(0.0005, 0.001, 1000));
164 random = new Random(1234);
165 for (int i = 0; i < 1000000; i++)
166 {
167 double d = random.nextDouble();
168 tally.register(d);
169 }
170 System.out.println("0% quantile (should be close to 0.0): " + tally.getQuantile(0.0));
171 System.out.println("25% quantile (should be close to 0.25): " + tally.getQuantile(0.25));
172 System.out.println("50% quantile (should be close to 0.5): " + tally.getQuantile(0.50));
173 System.out.println("100% quantile (should be close to 1.0): " + tally.getQuantile(1.0));
174 for (int bin = 0; bin < 10; bin++)
175 {
176 System.out.println("Number of values in bin " + bin + " " + (int) Math.rint(tally.getN()
177 * (tally.getCumulativeProbability((bin + 1) / 1000.0) - tally.getCumulativeProbability(bin / 1000.0))));
178 }
179
180 System.out.println("");
181
182 tally = new Tally("Example tally with FixedBinsAccumulator using 1001 bins",
183 new FixedBinsAccumulator(1.0, (Math.E - 1.0) / 1000, 1001));
184
185 random = new Random(1234);
186 for (int i = 0; i <= 1000000; i++)
187 {
188 tally.register(Math.exp(1.0 * i / 1000000));
189 }
190 System.out.println("0% quantile (should be 1.0): " + tally.getQuantile(0.0));
191 System.out.println("25% quantile (should be close to sqrt(sqrt(e))): " + tally.getQuantile(0.25));
192 System.out.println("50% quantile (should be close to sqrt(e)): " + tally.getQuantile(0.50));
193 System.out.println("100% quantile (should be close to e): " + tally.getQuantile(1.0));
194 for (int bin = 0; bin < 10; bin++)
195 {
196 double value = 1 + bin / 10.0 * (Math.E - 1);
197 System.out.println(String.format("Cumulative probability at %8f ", value)
198 + tally.getCumulativeProbability(value));
199 }
200
201 System.out.println("End of FixedBinsAccumulator output.\n");
202 }
203
204 }