-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathStatFinder.java
90 lines (71 loc) · 2.18 KB
/
StatFinder.java
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
package com.team254.lib.util;
import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
/**
* Finds the stats (mean, standard deviation, etc.) of a list
* <p>
* Example use case: finding out how long a planner takes from the average of 100 tries
*/
public class StatFinder {
private final List<Double> numbers = new ArrayList<>();
private final int numberToIgnore;
private int numberIgnored = 0;
private boolean stopped = false;
/**
* Creates a new StatFinder
*
* @param numberToIgnore the number of entries to ignore before logging (like burning the top card of a deck before dealing)
*/
public StatFinder(int numberToIgnore) {
this.numberToIgnore = numberToIgnore;
}
public boolean add(double number) {
if (stopped) {
return false;
}
if (numberIgnored < numberToIgnore) {
numberIgnored++;
return false;
}
numbers.add(number);
return true;
}
public boolean addAndPrint(double number) {
boolean success = add(number);
if (success) {
System.out.println("added: " + number);
}
return success;
}
public DoubleSummaryStatistics getStats() {
return numbers.stream().mapToDouble(x -> x).summaryStatistics();
}
public double getMean() {
return getStats().getAverage();
}
public double getStandardDeviation() {
double mean = getMean();
return Math.sqrt(numbers.stream().mapToDouble(x -> Math.pow(x - mean, 2)).sum() / (getSize() - 1));
}
public double getSize() {
return numbers.size();
}
public void printStats() {
System.out.println("mean: " + getMean());
System.out.println("standard deviation: " + getStandardDeviation());
System.out.println("min: " + getStats().getMin());
System.out.println("max: " + getStats().getMax());
System.out.println("size: " + getSize());
}
public void stop() {
stopped = true;
}
public void stopAndPrint() {
if (stopped) {
return;
}
stop();
printStats();
}
}