-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatistics.java
132 lines (105 loc) · 2.19 KB
/
Statistics.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
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
package tools;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* @author Antoine
* This class manage basic statistic computations: deviation, mean, variance, median of list of double numbers
* You can use basic typename like int, float, double, ...
* @warning statistics on integer data will be rounded!
* @param <Type>
*/
public class Statistics {
private List<Float> data;
private int size;
private String name;
public Statistics() {
size = 0;
data = new ArrayList<>();
name = "Statistics";
}
public Statistics(String name) {
size = 0;
data = new ArrayList<>();
this.name = name;
}
public Statistics(List<Float> list) {
size = list.size();
data = list;
name = "Statistics";
}
public Statistics(List<Float> list, String name) {
size = list.size();
data = list;
this.name = name;
}
public void addAll(Statistics stats) {
data.addAll(stats.getData());
size = data.size();
}
public void addAll(List<Float> list) {
data.addAll(list);
size = data.size();
}
public void add(Float value) {
data.add(value);
size = data.size();
}
public Float mean() {
if (data.isEmpty()) {
return 0f;
}
Float sum = 0f;
for (Float a: data) {
sum += a;
}
return sum/size;
}
public Float variance() {
if (data.isEmpty()) {
return 0f;
}
Float mean = mean();
Float tmp = 0f;
for(Float num: data) {
tmp += (mean - num)*(mean - num);
}
return tmp/size;
}
public Float StdDev() {
if (data.isEmpty()) {
return 0f;
}
return new Float (Math.sqrt(variance().doubleValue()));
}
public Float median() {
if (data.isEmpty()) {
return 0f;
}
Collections.sort(data);
if (size % 2 == 0) {
return (data.get((size / 2) - 1) + data.get(size / 2)) / 2.0f;
} else {
return data.get(size / 2);
}
}
public Float min() {
return (data.isEmpty())?0f:Collections.min(data);
}
public Float max() {
return (data.isEmpty())?0f:Collections.max(data);
}
public void setName(String name) {
this.name = name;
}
public List<Float> getData() {
return data;
}
public int getSize() {
return size;
}
public String getName() {
return name;
}
}