Skip to content
Snippets Groups Projects
Commit 7fd96b46 authored by Eilert Tunheim's avatar Eilert Tunheim
Browse files

Added confidence interval and reset the x-axis back to index instead of hours

parent 143d9a7d
Branches
No related tags found
No related merge requests found
...@@ -52,5 +52,6 @@ ...@@ -52,5 +52,6 @@
<orderEntry type="library" name="Maven: com.google.cloud:google-cloud-storage:2.4.0" level="project" /> <orderEntry type="library" name="Maven: com.google.cloud:google-cloud-storage:2.4.0" level="project" />
<orderEntry type="library" name="Maven: com.google.apis:google-api-services-storage:v1-rev20211201-1.32.1" level="project" /> <orderEntry type="library" name="Maven: com.google.apis:google-api-services-storage:v1-rev20211201-1.32.1" level="project" />
<orderEntry type="library" name="Maven: com.google.auto.value:auto-value-annotations:1.9" level="project" /> <orderEntry type="library" name="Maven: com.google.auto.value:auto-value-annotations:1.9" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.6.1" level="project" />
</component> </component>
</module> </module>
\ No newline at end of file
...@@ -29,6 +29,13 @@ ...@@ -29,6 +29,13 @@
<groupId>com.google.cloud</groupId> <groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId> <artifactId>google-cloud-storage</artifactId>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
......
...@@ -5,9 +5,11 @@ import javafx.scene.chart.LineChart; ...@@ -5,9 +5,11 @@ import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis; import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart; import javafx.scene.chart.XYChart;
import java.text.SimpleDateFormat; import org.apache.commons.math3.distribution.TDistribution;
import java.util.Date; import org.apache.commons.math3.exception.MathIllegalArgumentException;
import java.util.Map; import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
import java.util.*;
public class LineChartFunctionality { public class LineChartFunctionality {
...@@ -15,17 +17,18 @@ public class LineChartFunctionality { ...@@ -15,17 +17,18 @@ public class LineChartFunctionality {
private static CategoryAxis xAxis; private static CategoryAxis xAxis;
private static NumberAxis yAxis; private static NumberAxis yAxis;
private static final double CONFIDENCE_INTERVAL = 0.90;
public LineChartFunctionality() { public LineChartFunctionality() {
xAxis = new CategoryAxis(); xAxis = new CategoryAxis();
yAxis = new NumberAxis(); yAxis = new NumberAxis();
lineChart = new LineChart<>(xAxis, yAxis); lineChart = new LineChart<>(xAxis, yAxis);
xAxis.setLabel("Hours"); xAxis.setLabel("Data Points");
xAxis.setAnimated(false); xAxis.setAnimated(false);
yAxis.setLabel("Kwh"); yAxis.setLabel("Kwh");
yAxis.setAnimated(false); yAxis.setAnimated(false);
lineChart.setTitle("Drying Processes"); lineChart.setTitle("Drying Processes");
lineChart.setCreateSymbols(false);
} }
public static void setLineChart(LineChart<String, Number> lineChart) { public static void setLineChart(LineChart<String, Number> lineChart) {
...@@ -44,99 +47,81 @@ public class LineChartFunctionality { ...@@ -44,99 +47,81 @@ public class LineChartFunctionality {
lineChart.getData().clear(); lineChart.getData().clear();
} }
private static double findDifference(String start_date, String end_date) { private static Map<Integer, ArrayList<Double>> statistics(Map<Integer, ArrayList<Double>> multiMap){
// Defining a simple date format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{ System.out.println("\n\nMultimap: \n");
// try to convert the string to Date datatype for (Map.Entry<Integer, ArrayList<Double>> entry : multiMap.entrySet()) {
Date dateStart = dateFormat.parse(start_date); System.out.printf("\nIndex: \t%s\t\t\tkWh: \t%s\n", entry.getKey(), entry.getValue());
Date dateEnd = dateFormat.parse(end_date);
if(entry.getValue().size()>1){
SummaryStatistics stats = new SummaryStatistics();
for (double val : entry.getValue()) {
stats.addValue(val);
}
// Finds the difference in millis // Calculate 95% confidence interval
double differenceMillis = dateEnd.getTime() - dateStart.getTime(); double ci = calcMeanCI(stats, CONFIDENCE_INTERVAL);
System.out.println(String.format("Mean: %f", stats.getMean()));
double lower = stats.getMean() - ci;
double upper = stats.getMean() + ci;
System.out.println(String.format("Confidence Interval 95%%: %f, %f", lower, upper));
// Finds the difference in minutes // Deletes entries if they are out of bounds with the confidence interval
return (differenceMillis / (1000 * 60 )) % 60; entry.getValue().removeIf(value -> Double.compare(value, lower) < 0 || Double.compare(value, upper) > 0);
}
}
return multiMap;
}
} catch (Exception e) { private static double calcMeanCI(SummaryStatistics stats, double level) {
System.out.println(e.getMessage()); try {
// Create T Distribution with N-1 degrees of freedom
TDistribution tDist = new TDistribution(stats.getN() - 1);
// Calculate critical value
double critVal = tDist.inverseCumulativeProbability(1.0 - (1 - level) / 2);
// Calculate confidence interval
return critVal * stats.getStandardDeviation() / Math.sqrt(stats.getN());
} catch (MathIllegalArgumentException e) {
return Double.NaN;
} }
return 0;
} }
public static LineChart<String, Number> loadSingleSeries(Map<Integer, Map<String, Number>> userInput) throws Exception { public static LineChart<String, Number> loadSingleSeries(Map<Integer, Map<String, Number>> userInput) throws Exception {
clearLineChart(); clearLineChart();
//Map<Integer, Map<String, Number>> kWh = userInput; Map<Integer, ArrayList<Double>> multiMap = new HashMap<>();
//Map<Integer, Map<String, Number>> kWh = DB.setInputParameters();
//System.out.println(kWh.size());
for (Map.Entry<Integer, Map<String, Number>> entryKwh : userInput.entrySet()) { for (Map.Entry<Integer, Map<String, Number>> entryKwh : userInput.entrySet()) {
Map data = entryKwh.getValue(); Map data = entryKwh.getValue();
//System.out.println(data.size()); //System.out.println(data.size());
XYChart.Series<String, Number> newSeries = new XYChart.Series<String, Number>(); XYChart.Series<String, Number> newSeries = new XYChart.Series<String, Number>();
long minutes = 0; int index = 0;
long hours;
String previouseDate = "";
for (Object entryData : data.entrySet()) { for (Object entryData : data.entrySet()) {
//System.out.println("data: \t"+entryData); //System.out.println("data: \t"+entryData);
String entryString = entryData.toString(); String entryString = entryData.toString();
String[] arr = entryString.split("="); String[] arr = entryString.split("=");
String currentDate = arr[0]; String date = arr[0];
int kwhValue = Integer.parseInt(arr[1]); Double kwhValue = Double.parseDouble(arr[1]);
//System.out.printf("previouse date: \t%s\n",previouseDate);
//System.out.printf("Current date: \t\t%s\n",currentDate);
//System.out.printf("is prev empty?: \t%s\n",previouseDate.isEmpty());
minutes += findDifference(previouseDate, currentDate);
hours = minutes/60;
System.out.println(hours);
previouseDate = currentDate;
//System.out.printf("Date: \t%s\t\t\tkWh: \t%s\n",date,kwhValue); //System.out.printf("Date: \t%s\t\t\tkWh: \t%s\n",date,kwhValue);
//System.out.printf("Hours: \t\t%s\n",hours);
// Checks if the index already got an arraylist, if not one is created
multiMap.computeIfAbsent(index, k -> new ArrayList<Double>());
multiMap.get(index).add(kwhValue);
// Connect the data to a series // Connect the data to a series
newSeries.getData().add(new XYChart.Data<String, Number>(String.valueOf(hours), kwhValue)); newSeries.getData().add(new XYChart.Data<String, Number>(String.valueOf(index), kwhValue));
index += 1;
} }
updateLineChart(newSeries); updateLineChart(newSeries);
} }
return getLineChart();
}
public static LineChart<String, Number> loadMultipleSeries(Map<Integer, Map<String, Number>> userInput) throws Exception { Map<Integer, ArrayList<Double>> confidenceIntervalData = statistics(multiMap);
//Map<Integer, Map<String, Number>> kWh = DB.setInputParameters(); System.out.println(confidenceIntervalData);
//System.out.println(kWh.size());
for (Map.Entry<Integer, Map<String, Number>> entryKwh : userInput.entrySet()) {
Map data = entryKwh.getValue();
//System.out.println(data.size());
XYChart.Series<String, Number> newSeries = new XYChart.Series<String, Number>();
for (Object entryData : data.entrySet()) {
//System.out.println("data: \t"+entryData);
String entryString = entryData.toString();
String[] arr = entryString.split("=");
String date = arr[0];
int kwhValue = Integer.parseInt(arr[1]);
//System.out.printf("Date: \t%s\t\t\tkWh: \t%s\n",date,kwhValue);
// Connect the data to a series
newSeries.getData().add(new XYChart.Data<String, Number>(date, kwhValue));
}
updateLineChart(newSeries);
}
return getLineChart(); return getLineChart();
} }
} }
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment