List of usage examples for org.apache.commons.math.stat.regression SimpleRegression SimpleRegression
public SimpleRegression()
From source file:playground.boescpa.converters.vissim.tools.TripMatcher.java
@Override public HashMap<Id<Trip>, Integer> matchTrips(HashMap<Id<Trip>, Long[]> msTrips, HashMap<Id<Trip>, Long[]> amTrips) { int matchesWithHighScores = 0; int matchesWithVeryHighScores = 0; int progressCounter = 0; int progressChecker = 2; HashMap<Id<Trip>, Integer> countsPerAnmTrip = new HashMap<>(); for (Id<Trip> amTrip : amTrips.keySet()) { countsPerAnmTrip.put(amTrip, 0); }//w w w . j ava2 s .com List<Id<Trip>> amTripsKeySet = new ArrayList<>(amTrips.keySet()); for (Id<Trip> msTrip : msTrips.keySet()) { progressCounter++; Long[] msTripZones = msTrips.get(msTrip); Id<Trip> bestMatchingAmTrip = null; int bestMatchScore = Integer.MIN_VALUE; // Shuffle key set: Collections.shuffle(amTripsKeySet); for (Id<Trip> amTrip : amTripsKeySet) { Long[] amTripZones = amTrips.get(amTrip); // Linear regression between the to trips: SimpleRegression simpleRegression = new SimpleRegression(); for (int i = 0; i < msTripZones.length; i++) { boolean foundNone = true; for (int j = 0; j < amTripZones.length; j++) { if (msTripZones[i].equals(amTripZones[j])) { simpleRegression.addData(i, j); foundNone = false; } } if (foundNone) { int yPos = -(msTripZones.length - i) - NEG_OFFSET_IF_NOT_FOUNG; simpleRegression.addData(i, yPos); } } // Scoring: int matchScore = 0; // Criterion 1.1: Difference in length of trips not greater than 10%. if (((double) Math.abs(msTripZones.length - amTripZones.length)) / ((double) msTripZones.length) <= 0.1) { matchScore += TEN_PRCT_SCORE; } // Criterion 1.2: The smaller the difference in length, the better. matchScore -= (Math.abs(msTripZones.length - amTripZones.length) * LENGTH_SCORE); // Criterion 2: The closer the intercept to zero, the better. matchScore -= (int) (Math.abs(simpleRegression.getIntercept()) * INTERCEPT_SCORE); // Criterion 3: The closer the slope to one, the better. matchScore -= (int) (Math.abs(1 - simpleRegression.getSlope()) * SLOPE_SCORE); // Criterion 4: The smaller the mean square error of the regression, the better. matchScore -= (int) (Math.abs(simpleRegression.getMeanSquareError()) * MSE_SCORE); if (matchScore > bestMatchScore) { bestMatchScore = matchScore; bestMatchingAmTrip = amTrip; } } countsPerAnmTrip.put(bestMatchingAmTrip, (countsPerAnmTrip.get(bestMatchingAmTrip) + 1)); if (bestMatchScore >= 0.9 * TEN_PRCT_SCORE) { matchesWithHighScores++; if (bestMatchScore >= 0.99 * TEN_PRCT_SCORE) { matchesWithVeryHighScores++; } } // log progress: if (progressCounter >= progressChecker) { log.info(progressChecker + " trips matched."); progressChecker *= 2; } } log.info("Of total " + msTrips.size() + " trips, " + matchesWithHighScores + " were matched with a high score above " + 0.9 * TEN_PRCT_SCORE + " points."); log.info("Of total " + msTrips.size() + " trips, " + matchesWithVeryHighScores + " were matched with a very high score above " + 0.99 * TEN_PRCT_SCORE + " points."); return countsPerAnmTrip; }
From source file:playground.johannes.coopsim.analysis.ScoreTask.java
@Override public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) { DescriptiveStatistics allScores = new DescriptiveStatistics(); for (Trajectory t : trajectories) allScores.addValue(t.getPerson().getSelectedPlan().getScore()); results.put("score", allScores); DescriptiveStatistics actScores = ActivityEvaluator.stopLogging(); results.put("score_act", actScores); DescriptiveStatistics legScores = LegEvaluator.stopLogging(); results.put("score_leg", legScores); Map<String, DescriptiveStatistics> jointScore = JointActivityEvaluator2.stopLogging(); // Map<String, DescriptiveStatistics> jointScore = JointActivityEvaluator.stopLogging(); for (Entry<String, DescriptiveStatistics> entry : jointScore.entrySet()) { results.put("score_join_" + entry.getKey(), entry.getValue()); }/*w w w . j a va2 s. c om*/ DescriptiveStatistics typeScore = ActivityTypeEvaluator.stopLogging(); results.put("score_type", typeScore); try { writeHistograms(allScores, "score", 50, 50); writeHistograms(actScores, "score_act", 50, 50); writeHistograms(legScores, "score_leg", 50, 50); for (Entry<String, DescriptiveStatistics> entry : jointScore.entrySet()) { writeHistograms(entry.getValue(), new LinearDiscretizer(0.5), "score_join_" + entry.getKey(), false); writeHistograms(entry.getValue(), "score_join_" + entry.getKey(), 50, 50); } writeHistograms(typeScore, new DummyDiscretizer(), "score_type", false); } catch (IOException e) { e.printStackTrace(); } scores.add(allScores.getMean()); if (scores.size() >= MIN_SAMPLES) { SimpleRegression reg = new SimpleRegression(); for (int i = scores.size() - MIN_SAMPLES; i < scores.size(); i++) { reg.addData(i, scores.get(i)); } if (reg.getSlope() < THRESHOLD) converged = true; } }
From source file:playground.johannes.studies.coopsim.Convergence.java
/** * @param args//ww w. j a v a 2 s . c om * @throws IOException */ public static void main(String[] args) throws IOException { String root = "/Volumes/cluster.math.tu-berlin.de/net/ils2/jillenberger/leisure/runs/run259/tasks/8/"; File outputDir = new File(root + "/output"); String property = "d_trip_culture"; File analysisDir = new File(root + "/analysis"); analysisDir.mkdirs(); BufferedWriter writer = new BufferedWriter( new FileWriter(analysisDir.getAbsolutePath() + "/" + property + ".txt")); writer.write("it\t"); writer.write(property); writer.newLine(); TDoubleArrayList yVals = new TDoubleArrayList(); TDoubleArrayList xVals = new TDoubleArrayList(); for (File file : outputDir.listFiles()) { if (file.isDirectory()) { File statsFile = new File(String.format("%1$s/statistics.txt", file.getAbsolutePath())); if (statsFile.exists()) { String iter = file.getName(); BufferedReader reader = new BufferedReader(new FileReader(statsFile)); String line = reader.readLine(); while ((line = reader.readLine()) != null) { String[] tokens = line.split("\t"); String key = tokens[0]; String val = tokens[1]; if (key.equals(property)) { writer.write(iter); writer.write("\t"); writer.write(val); writer.newLine(); xVals.add(Double.parseDouble(iter)); yVals.add(Double.parseDouble(val)); } } } } } writer.close(); for (int i = 40; i < yVals.size(); i++) { SimpleRegression reg = new SimpleRegression(); for (int k = i - 40; k < i; k++) { reg.addData(k, yVals.get(k)); } System.out.println(String.format("Slope after iteration %1$s: %2$s.", i, reg.getSlope())); } }
From source file:uk.ac.leeds.ccg.andyt.generic.visualisation.charts.Generic_ScatterPlotAndLinearRegression.java
/** * @param data double[2][] where: data[0][] are the y values data[1][] are * the x values//from w ww. ja va2 s. c om * @return double[] result where: <ul> <li>result[0] is the y axis * intercept;</li> <li>result[1] is the change in y relative to x (gradient * or slope);</li> <li>result[2] is the rank correlation coefficient * (RSquare);</li> <li>result[3] is data[0].length.</li> </ul> */ public static double[] getSimpleRegressionParameters(double[][] data) { double[] result = new double[4]; // org.apache.commons.math.stat.regression.SimpleRegression; SimpleRegression a_SimpleRegression = new SimpleRegression(); //System.out.println("data.length " + data[0].length); for (int i = 0; i < data[0].length; i++) { a_SimpleRegression.addData(data[1][i], data[0][i]); //aSimpleRegression.addData(data[0][i], data[1][i]); } result[0] = a_SimpleRegression.getIntercept(); result[1] = a_SimpleRegression.getSlope(); result[2] = a_SimpleRegression.getRSquare(); result[3] = data[0].length; return result; }
From source file:uk.ac.leeds.ccg.andyt.projects.moses.process.RegressionReport.java
/** * data[0][] = observed SAR/*from w ww . j a v a 2 s. c om*/ * data[1][] = expected CAS * * @param data * @return */ public static double[] printSimpleRegression(double[][] data) { double[] result = new double[3]; // org.apache.commons.math.stat.regression.SimpleRegression; SimpleRegression aSimpleRegression = new SimpleRegression(); System.out.println("data.length " + data[0].length); for (int i = 0; i < data[0].length; i++) { // aSimpleRegression.addData( data[1][i], data[0][i] ); aSimpleRegression.addData(data[0][i], data[1][i]); } double _Intercept = aSimpleRegression.getIntercept(); double _Slope = aSimpleRegression.getSlope(); double _RSquare = aSimpleRegression.getRSquare(); System.out.println(" y = " + _Slope + " * x + " + _Intercept); System.out.println(" RSquare " + _RSquare); result[0] = _Intercept; result[1] = _Slope; result[2] = _RSquare; return result; }