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()));
}
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) {
return timeSeries.getValues().stream().mapToDouble(TimeSeriesValue::getValue);
}
private Optional<Double> getOptionalValue(OptionalDouble optionalDouble) {
return Optional.ofNullable(optionalDouble.isPresent()
return getOptionalValue(optionalDouble.isPresent()
? optionalDouble.getAsDouble()
: null);
}
private Optional<Double> getOptionalValue(Double value) {
value = (value == null)
? null
: ((double) Math.round(value * 100) / 100);
return Optional.ofNullable(value);
}
@ -45,6 +54,7 @@ public class StatisticService {
return new TimeSeriesStatistic(getMin(timeSeries),
getMax(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> length;
private Optional<Double> dispersion;
public TimeSeriesStatistic(Optional<Double> min,
Optional<Double> max,
Optional<Double> average,
Optional<Double> length) {
Optional<Double> length,
Optional<Double> dispersion) {
this.min = min;
this.max = max;
this.average = average;
this.length = length;
this.dispersion = dispersion;
}
public Optional<Double> getMin() {
@ -33,4 +37,8 @@ public class TimeSeriesStatistic {
public Optional<Double> getLength() {
return length;
}
public Optional<Double> getDispersion() {
return dispersion;
}
}

View File

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