From 207b1e1af3e3983a6125e56e457d7a259fa56b30 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 9 Jan 2023 23:56:28 +0400 Subject: [PATCH] add dispersion --- .../java/ru/ulstu/statistic/StatisticService.java | 14 ++++++++++++-- .../ru/ulstu/statistic/TimeSeriesStatistic.java | 10 +++++++++- src/main/resources/templates/index.html | 4 ++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/ulstu/statistic/StatisticService.java b/src/main/java/ru/ulstu/statistic/StatisticService.java index aca1ea8..a2c4f1a 100644 --- a/src/main/java/ru/ulstu/statistic/StatisticService.java +++ b/src/main/java/ru/ulstu/statistic/StatisticService.java @@ -26,18 +26,27 @@ public class StatisticService { return getOptionalValue(Double.valueOf(timeSeries.getLength())); } + public Optional getDispersion(TimeSeries timeSeries) { + Optional 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 getOptionalValue(OptionalDouble optionalDouble) { - return Optional.ofNullable(optionalDouble.isPresent() + return getOptionalValue(optionalDouble.isPresent() ? optionalDouble.getAsDouble() : null); } private Optional 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)); } } diff --git a/src/main/java/ru/ulstu/statistic/TimeSeriesStatistic.java b/src/main/java/ru/ulstu/statistic/TimeSeriesStatistic.java index 7162ad2..398bee0 100644 --- a/src/main/java/ru/ulstu/statistic/TimeSeriesStatistic.java +++ b/src/main/java/ru/ulstu/statistic/TimeSeriesStatistic.java @@ -8,14 +8,18 @@ public class TimeSeriesStatistic { private Optional average; private Optional length; + private Optional dispersion; + public TimeSeriesStatistic(Optional min, Optional max, Optional average, - Optional length) { + Optional length, + Optional dispersion) { this.min = min; this.max = max; this.average = average; this.length = length; + this.dispersion = dispersion; } public Optional getMin() { @@ -33,4 +37,8 @@ public class TimeSeriesStatistic { public Optional getLength() { return length; } + + public Optional getDispersion() { + return dispersion; + } } diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index bc0323e..324624a 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -161,6 +161,10 @@


+

Дисперсия: + +

+