add dispersion

This commit is contained in:
Anton Romanov 2023-01-09 23:56:28 +04:00
parent abb3289077
commit 207b1e1af3
3 changed files with 25 additions and 3 deletions

View File

@ -26,18 +26,27 @@ public class StatisticService {
return getOptionalValue(Double.valueOf(timeSeries.getLength())); return getOptionalValue(Double.valueOf(timeSeries.getLength()));
} }
public Optional<Double> getDispersion(TimeSeries timeSeries) {
Optional<Double> maybeAverage = getAverage(timeSeries);
return getOptionalValue(maybeAverage.isPresent()
? timeSeries.getValues().stream().mapToDouble(v -> Math.pow(v.getValue() - maybeAverage.get(), 2)).sum() / timeSeries.getLength()
: null);
}
private DoubleStream getDoubleStream(TimeSeries timeSeries) { private DoubleStream getDoubleStream(TimeSeries timeSeries) {
return timeSeries.getValues().stream().mapToDouble(TimeSeriesValue::getValue); return timeSeries.getValues().stream().mapToDouble(TimeSeriesValue::getValue);
} }
private Optional<Double> getOptionalValue(OptionalDouble optionalDouble) { private Optional<Double> getOptionalValue(OptionalDouble optionalDouble) {
return Optional.ofNullable(optionalDouble.isPresent() return getOptionalValue(optionalDouble.isPresent()
? optionalDouble.getAsDouble() ? optionalDouble.getAsDouble()
: null); : null);
} }
private Optional<Double> getOptionalValue(Double value) { private Optional<Double> getOptionalValue(Double value) {
value = (value == null)
? null
: ((double) Math.round(value * 100) / 100);
return Optional.ofNullable(value); return Optional.ofNullable(value);
} }
@ -45,6 +54,7 @@ public class StatisticService {
return new TimeSeriesStatistic(getMin(timeSeries), return new TimeSeriesStatistic(getMin(timeSeries),
getMax(timeSeries), getMax(timeSeries),
getAverage(timeSeries), getAverage(timeSeries),
getLength(timeSeries)); getLength(timeSeries),
getDispersion(timeSeries));
} }
} }

View File

@ -8,14 +8,18 @@ public class TimeSeriesStatistic {
private Optional<Double> average; private Optional<Double> average;
private Optional<Double> length; private Optional<Double> length;
private Optional<Double> dispersion;
public TimeSeriesStatistic(Optional<Double> min, public TimeSeriesStatistic(Optional<Double> min,
Optional<Double> max, Optional<Double> max,
Optional<Double> average, Optional<Double> average,
Optional<Double> length) { Optional<Double> length,
Optional<Double> dispersion) {
this.min = min; this.min = min;
this.max = max; this.max = max;
this.average = average; this.average = average;
this.length = length; this.length = length;
this.dispersion = dispersion;
} }
public Optional<Double> getMin() { public Optional<Double> getMin() {
@ -33,4 +37,8 @@ public class TimeSeriesStatistic {
public Optional<Double> getLength() { public Optional<Double> getLength() {
return length; return length;
} }
public Optional<Double> getDispersion() {
return dispersion;
}
} }

View File

@ -161,6 +161,10 @@
<span th:text="${statistic.average.present ? statistic.average.get : 'Не известно'}"/> <span th:text="${statistic.average.present ? statistic.average.get : 'Не известно'}"/>
</p> </p>
<hr/> <hr/>
<p> Дисперсия:
<span th:text="${statistic.dispersion.present ? statistic.dispersion .get : 'Не известно'}"/>
</p>
<hr/>
</div> </div>
</div> </div>
<div class="col-md-12 col-sm-12 col-lg-6"> <div class="col-md-12 col-sm-12 col-lg-6">