AItest
Форк от xnbtv/AItest
/
qqqq.java
101 строка · 2.4 Кб
1package ru.pm.dashboard.helper;
2
3import lombok.Getter;
4import lombok.RequiredArgsConstructor;
5
6import java.io.Serializable;
7import java.util.Comparator;
8
9/**
10* Класс предназначен для расчёта статистических показателей "на лету" без необходимости сохранять значения в списке.
11*/
12@RequiredArgsConstructor
13@Getter
14public class Stats implements Serializable, Comparable<Stats> {
15
16private static final long serialVersionUID = 5189576175560182641L;
17
18private final String x1;
19private final String x2;
20
21private long mN = 0L;
22private double mM = 0.0;
23private double mS = 0.0;
24
25private double min = Double.POSITIVE_INFINITY;
26private double max = Double.NEGATIVE_INFINITY;
27private double sum = 0.0;
28
29/**
30* Добавить очередное значение из исходных данных
31*
32* @param s Значение
33*/
34public void push(String s) {
35++mN;
36double x;
37try {
38x = Double.parseDouble(s);
39} catch (Exception e) {
40return;
41}
42double nextM = mM + (x - mM) / mN;
43mS += (x - mM) * (x - nextM);
44mM = nextM;
45
46sum += x;
47
48if (x < min) {
49min = x;
50}
51
52if (x > max) {
53max = x;
54}
55}
56
57public long numSamples() {
58return mN;
59}
60
61public double mean() {
62return mM;
63}
64
65public double variance() {
66return mN > 1 ? mS / mN : 0.0;
67}
68
69public double varianceUnbiased() {
70return mN > 1 ? mS / (mN - 1) : 0.0;
71}
72
73public double standardDeviation() {
74return Math.sqrt(variance());
75}
76
77public double standardDeviationUnbiased() {
78return Math.sqrt(varianceUnbiased());
79}
80
81public double min() {
82return min;
83}
84
85public double max() {
86return max;
87}
88
89public double sum() {
90return sum;
91}
92
93private static final Comparator<String> NULL_SAFE_STRING_COMPARATOR = Comparator.nullsFirst(String::compareTo);
94
95@Override
96public int compareTo(Stats stats) {
97return Comparator.comparing(Stats::getX2, NULL_SAFE_STRING_COMPARATOR)
98.thenComparing(Stats::getX1)
99.compare(this, stats);
100}
101}