algorithms-for-computing-li.../LinearRegressionTool/src/main/java/de/wwwu/awolf/presenter/algorithms/naiv/NaivTheilSenEstimator.java

92 lines
2.5 KiB
Java

package de.wwwu.awolf.presenter.algorithms.naiv;
import de.wwwu.awolf.model.Line;
import de.wwwu.awolf.presenter.algorithms.Algorithm;
import de.wwwu.awolf.presenter.util.FastElementSelector;
import de.wwwu.awolf.presenter.util.Logging;
import java.util.ArrayList;
import java.util.List;
/**
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
*
* @Author: Armin Wolf
* @Email: a_wolf28@uni-muenster.de
* @Date: 15.09.2017.
*/
public class NaivTheilSenEstimator implements Algorithm {
private List<Line> lines;
private double slope;
private double yInterception;
/**
* Konstruktor
*
* @param lines Liste der Geraden
*/
public NaivTheilSenEstimator(List<Line> lines) {
this.lines = lines;
this.slope = 0d;
this.yInterception = 0d;
}
@Override
public void run() {
Logging.logInfo("=== S T A R T - naiv T S ===");
long start;
long end;
start = System.currentTimeMillis();
List<Double> slopesList = new ArrayList<>();
int cnt = 0;
for (int i = 0; i < lines.size(); i++) {
double x = lines.get(i).getM();
double y = lines.get(i).getB();
for (int j = i + 1; j < lines.size(); j++) {
if (x != lines.get(j).getM()) { // x must be different, otherwise slope becomes infinite
Double slope = (lines.get(j).getB() - y) / (lines.get(j).getM() - x);
slopesList.add(slope);
++cnt;
}
}
}
List<Double> list1 = new ArrayList<>();
List<Double> list2 = new ArrayList<>();
for (Line line : lines) {
list1.add(line.getM());
list2.add(line.getB());
}
double median1 = FastElementSelector.randomizedSelect(list1, list1.size() * 0.5);
double median2 = FastElementSelector.randomizedSelect(list2, list2.size() * 0.5);
slope = FastElementSelector.randomizedSelect(slopesList, slopesList.size() * 0.5);
yInterception = median2 - slope * median1;
end = System.currentTimeMillis();
Logging.logInfo("Zeit: " + ((end - start) / 1000));
}
@Override
public void pepareResult() {
}
/**
* @return Steigung
*/
public double getM() {
return slope * -1;
}
/**
* @return y-Achsenabschnitt
*/
public double getB() {
return yInterception * -1;
}
}