List of usage examples for org.apache.commons.math.stat.regression SimpleRegression addData
public void addData(double x, double y)
From source file:guineu.modules.filter.Alignment.RANSACGCGC.RANSACGCGC.java
/** * Build the model creating a line with the 2 points * @param data vector with the points which represent all possible alignments. *///from w w w . j a v a2 s .co m private void getAllModelPoints(List<AlignGCGCStructMol> data) { // Create the regression line using the two points SimpleRegression regression = new SimpleRegression(); for (int i = 0; i < data.size(); i++) { AlignGCGCStructMol point = data.get(i); if (point.ransacMaybeInLiers) { regression.addData(point.RT, point.RT2); } } // Add all the points which fit the model (the difference between the point // and the regression line is less than "t" for (AlignGCGCStructMol point : data) { double y = point.RT2; double bestY = regression.predict(point.RT); if (Math.abs(y - bestY) < t) { point.ransacAlsoInLiers = true; AlsoNumber++; } else { point.ransacAlsoInLiers = false; } } }
From source file:guineu.modules.filter.Alignment.RANSACGCGC.RansacGCGCAlignerTask.java
private List<GCGCRTs> smooth(List<GCGCRTs> list, Range RTrange) { // Add one point at the begining and another at the end of the list to // ampliate the RT limits to cover the RT range completly try {// w w w . j av a 2s . c o m Collections.sort(list, new GCGCRTs()); GCGCRTs firstPoint = list.get(0); GCGCRTs lastPoint = list.get(list.size() - 1); double min = Math.abs(firstPoint.RT - RTrange.getMin()); double RTx = firstPoint.RT - min; double RTy = firstPoint.RT2 - min; GCGCRTs newPoint = new GCGCRTs(RTx, RTy); list.add(newPoint); double max = Math.abs(RTrange.getMin() - lastPoint.RT); RTx = lastPoint.RT + max; RTy = lastPoint.RT2 + max; newPoint = new GCGCRTs(RTx, RTy); list.add(newPoint); } catch (Exception exception) { } // Add points to the model in between of the real points to smooth the regression model Collections.sort(list, new GCGCRTs()); for (int i = 0; i < list.size() - 1; i++) { GCGCRTs point1 = list.get(i); GCGCRTs point2 = list.get(i + 1); if (point1.RT < point2.RT - 2) { SimpleRegression regression = new SimpleRegression(); regression.addData(point1.RT, point1.RT2); regression.addData(point2.RT, point2.RT2); double rt = point1.RT + 1; while (rt < point2.RT) { GCGCRTs newPoint = new GCGCRTs(rt, regression.predict(rt)); list.add(newPoint); rt++; } } } return list; }
From source file:guineu.modules.filter.Alignment.RANSAC.RANSAC.java
/** * Build the model creating a line with the 2 points * * @param data vector with the points which represent all possible * alignments.// w w w.j a v a 2 s . c o m */ private void getAllModelPoints(List<AlignStructMol> data) { // Create the regression line using the two points SimpleRegression regression = new SimpleRegression(); for (int i = 0; i < data.size(); i++) { AlignStructMol point = data.get(i); if (point.ransacMaybeInLiers) { regression.addData(point.RT, point.RT2); } } // Add all the points which fit the model (the difference between the point // and the regression line is less than "t" for (AlignStructMol point : data) { double y = point.RT2; double bestY = regression.predict(point.RT); if (Math.abs(y - bestY) < t) { point.ransacAlsoInLiers = true; AlsoNumber++; } else { point.ransacAlsoInLiers = false; } } }
From source file:ch.usi.inf.lidr.merging.SSL.java
/** * Creates and returns a {@link SimpleRegression} * for a given list of scored documents <code>scoredDocs</code>. * This regression maps unnormalized scores in <code>scoredDocs</code> * to normalized/centralized scores in <code>centrScores</code>. * Documents that appear both in <code>scoredDocs</code> * and <code>centrScores</code> are used as a training for the regression. * According to the original paper, only first 10 * documents are considered for training. * //from w ww. ja va 2 s . c o m * @param scoredDocs The list of scored documents. * * @return The {@link SimpleRegression} with filled-in training data. */ private <T> SimpleRegression getRegression(List<ScoredEntity<T>> scoredDocs) { SimpleRegression regression = new SimpleRegression(); Set<Double> xData = new HashSet<Double>(); for (ScoredEntity<T> scoredDocument : scoredDocs) { Object docId = scoredDocument.getEntity(); double specificScore = scoredDocument.getScore(); if (centrScores.containsKey(docId) && !xData.contains(specificScore)) { regression.addData(specificScore, centrScores.get(docId)); xData.add(specificScore); if (regression.getN() >= 10) { return regression; } } } return regression; }
From source file:com.joliciel.jochre.boundaries.features.TwoPointSlopeDifferenceFeature.java
@Override public FeatureResult<Double> checkInternal(Split split, RuntimeEnvironment env) { FeatureResult<Double> result = null; FeatureResult<Integer> contourDistanceResult = contourDistanceFeature.check(split, env); if (contourDistanceResult != null) { int contourDistance = contourDistanceResult.getOutcome(); int[][] verticalContour = split.getShape().getVerticalContour(); int x = split.getPosition(); Shape shape = split.getShape(); int topStart = verticalContour[x][0]; int bottomStart = verticalContour[x][1]; SimpleRegression topRightRegression = new SimpleRegression(); SimpleRegression bottomRightRegression = new SimpleRegression(); SimpleRegression topLeftRegression = new SimpleRegression(); SimpleRegression bottomLeftRegression = new SimpleRegression(); topRightRegression.addData(x, topStart); topLeftRegression.addData(x, topStart); bottomRightRegression.addData(x, bottomStart); bottomLeftRegression.addData(x, bottomStart); int[] minTopRight = new int[] { x, topStart }; int[] minTopLeft = new int[] { x, topStart }; int[] maxTopRight = new int[] { x, topStart }; int[] maxTopLeft = new int[] { x, topStart }; int[] minBottomRight = new int[] { x, bottomStart }; int[] minBottomLeft = new int[] { x, bottomStart }; int[] maxBottomRight = new int[] { x, bottomStart }; int[] maxBottomLeft = new int[] { x, bottomStart }; for (int i = 1; i <= contourDistance; i++) { if (x + i < shape.getWidth()) { if (verticalContour[x + i][0] < minTopRight[1]) minTopRight = new int[] { x + i, verticalContour[x + i][0] }; if (verticalContour[x + i][0] > maxTopRight[1]) maxTopRight = new int[] { x + i, verticalContour[x + i][0] }; if (verticalContour[x + i][1] < minBottomRight[1]) minBottomRight = new int[] { x + i, verticalContour[x + i][1] }; if (verticalContour[x + i][1] > maxBottomRight[1]) maxBottomRight = new int[] { x + i, verticalContour[x + i][1] }; }// w ww . j a v a2 s . co m if (x - i >= 0) { if (verticalContour[x - i][0] < minTopLeft[1]) minTopLeft = new int[] { x - i, verticalContour[x - i][0] }; if (verticalContour[x - i][0] > maxTopLeft[1]) maxTopLeft = new int[] { x - i, verticalContour[x - i][0] }; if (verticalContour[x - i][1] < minBottomLeft[1]) minBottomLeft = new int[] { x - i, verticalContour[x - i][1] }; if (verticalContour[x - i][1] > maxBottomLeft[1]) maxBottomLeft = new int[] { x - i, verticalContour[x - i][1] }; } } if (minTopRight[0] == x) topRightRegression.addData(maxTopRight[0], maxTopRight[1]); else topRightRegression.addData(minTopRight[0], minTopRight[1]); if (minTopLeft[0] == x) topLeftRegression.addData(maxTopLeft[0], maxTopLeft[1]); else topLeftRegression.addData(minTopLeft[0], minTopLeft[1]); if (maxBottomRight[0] == x) bottomRightRegression.addData(minBottomRight[0], minBottomRight[1]); else bottomRightRegression.addData(maxBottomRight[0], maxBottomRight[1]); if (maxBottomLeft[0] == x) bottomLeftRegression.addData(minBottomLeft[0], minBottomLeft[1]); else bottomLeftRegression.addData(maxBottomLeft[0], maxBottomLeft[1]); // get the slopes double topRightSlope = topRightRegression.getSlope(); double bottomRightSlope = bottomRightRegression.getSlope(); double topLeftSlope = topLeftRegression.getSlope(); double bottomLeftSlope = bottomLeftRegression.getSlope(); // convert slopes to angles double topRightAngle = Math.atan(topRightSlope); double bottomRightAngle = Math.atan(bottomRightSlope); double topLeftAngle = Math.atan(topLeftSlope); double bottomLeftAngle = Math.atan(bottomLeftSlope); // calculate the right & left-hand differences double rightDiff = Math.abs(topRightAngle - bottomRightAngle); double leftDiff = Math.abs(topLeftAngle - bottomLeftAngle); // normalise the differences from 0 to 1 rightDiff = rightDiff / Math.PI; leftDiff = leftDiff / Math.PI; double product = rightDiff * leftDiff; if (LOG.isTraceEnabled()) { LOG.trace("topRightAngle: " + topRightAngle); LOG.trace("bottomRightAngle: " + bottomRightAngle); LOG.trace("topLeftAngle: " + topLeftAngle); LOG.trace("bottomLeftAngle: " + bottomLeftAngle); LOG.trace("rightDiff: " + rightDiff); LOG.trace("leftDiff: " + leftDiff); LOG.trace("product: " + product); } result = this.generateResult(product); } return result; }
From source file:com.joliciel.jochre.boundaries.features.TrueContourSlopeDifferenceFeature.java
@Override public FeatureResult<Double> checkInternal(Split split, RuntimeEnvironment env) { FeatureResult<Double> result = null; FeatureResult<Integer> contourDistanceResult = contourDistanceFeature.check(split, env); if (contourDistanceResult != null) { int contourDistance = contourDistanceResult.getOutcome(); int[][] verticalContour = split.getShape().getVerticalContour(); int startX = split.getPosition(); Shape shape = split.getShape(); int topStart = verticalContour[startX][0]; int bottomStart = verticalContour[startX][1]; SimpleRegression topRightRegression = new SimpleRegression(); SimpleRegression bottomRightRegression = new SimpleRegression(); SimpleRegression topLeftRegression = new SimpleRegression(); SimpleRegression bottomLeftRegression = new SimpleRegression(); topRightRegression.addData(startX, topStart); topLeftRegression.addData(startX, topStart); bottomRightRegression.addData(startX, bottomStart); bottomLeftRegression.addData(startX, bottomStart); int lastTopRight = topStart; int lastTopLeft = topStart; int lastBottomRight = bottomStart; int lastBottomLeft = bottomStart; for (int i = 1; i <= contourDistance; i++) { int x = startX + i; if (x < shape.getWidth()) { if (shape.isPixelBlack(x, lastTopRight)) { for (int y = lastTopRight - 1; y >= -1; y--) { if (y < 0 || !shape.isPixelBlack(x, y)) { lastTopRight = y + 1; topRightRegression.addData(x, lastTopRight); break; }/*w w w.ja v a 2 s . c o m*/ } } else { for (int y = lastTopRight + 1; y <= shape.getHeight(); y++) { if (y == shape.getHeight() || shape.isPixelBlack(x, y)) { lastTopRight = y; topRightRegression.addData(x, lastTopRight); break; } } } if (shape.isPixelBlack(x, lastBottomRight)) { for (int y = lastBottomRight + 1; y <= shape.getHeight(); y++) { if (y == shape.getHeight() || !shape.isPixelBlack(x, y)) { lastBottomRight = y - 1; bottomRightRegression.addData(x, lastBottomRight); break; } } } else { for (int y = lastBottomRight - 1; y >= -1; y--) { if (y < 0 || shape.isPixelBlack(x, y)) { lastBottomRight = y; bottomRightRegression.addData(x, lastBottomRight); break; } } } } x = startX - i; if (x >= 0) { if (shape.isPixelBlack(x, lastTopLeft)) { for (int y = lastTopLeft - 1; y >= -1; y--) { if (y < 0 || !shape.isPixelBlack(x, y)) { lastTopLeft = y + 1; topLeftRegression.addData(x, lastTopLeft); break; } } } else { for (int y = lastTopLeft + 1; y <= shape.getHeight(); y++) { if (y == shape.getHeight() || shape.isPixelBlack(x, y)) { lastTopLeft = y; topLeftRegression.addData(x, lastTopLeft); break; } } } if (shape.isPixelBlack(x, lastBottomLeft)) { for (int y = lastBottomLeft + 1; y <= shape.getHeight(); y++) { if (y == shape.getHeight() || !shape.isPixelBlack(x, y)) { lastBottomLeft = y - 1; bottomLeftRegression.addData(x, lastBottomLeft); break; } } } else { for (int y = lastBottomLeft - 1; y >= -1; y--) { if (y < 0 || shape.isPixelBlack(x, y)) { lastBottomLeft = y; bottomLeftRegression.addData(x, lastBottomLeft); break; } } } } } // get the slopes double topRightSlope = topRightRegression.getSlope(); double bottomRightSlope = bottomRightRegression.getSlope(); double topLeftSlope = topLeftRegression.getSlope(); double bottomLeftSlope = bottomLeftRegression.getSlope(); // convert slopes to angles double topRightAngle = Math.atan(topRightSlope); double bottomRightAngle = Math.atan(bottomRightSlope); double topLeftAngle = Math.atan(topLeftSlope); double bottomLeftAngle = Math.atan(bottomLeftSlope); // calculate the right & left-hand differences double rightDiff = Math.abs(topRightAngle - bottomRightAngle); double leftDiff = Math.abs(topLeftAngle - bottomLeftAngle); // normalise the differences from 0 to 1 rightDiff = rightDiff / Math.PI; leftDiff = leftDiff / Math.PI; double product = rightDiff * leftDiff; if (LOG.isTraceEnabled()) { LOG.trace("topRightAngle: " + topRightAngle); LOG.trace("bottomRightAngle: " + bottomRightAngle); LOG.trace("topLeftAngle: " + topLeftAngle); LOG.trace("bottomLeftAngle: " + bottomLeftAngle); LOG.trace("rightDiff: " + rightDiff); LOG.trace("leftDiff: " + leftDiff); LOG.trace("product: " + product); } result = this.generateResult(product); } return result; }
From source file:net.sf.mzmine.modules.peaklistmethods.identification.metamsecorrelate.MetaMSEcorrelateTask.java
/** * feature shape correlation/*from www . ja v a2 s. c om*/ * @param f1 * @param f2 * @return feature shape correlation * or null if not possible * not enough data points for a correlation * @throws Exception */ public static FeatureShapeCorrelationData corrFeatureShape(Feature f1, Feature f2, boolean sameRawFile) throws Exception { //Range<Double> rt1 = f1.getRawDataPointsRTRange(); //Range<Double> rt2 = f2.getRawDataPointsRTRange(); if (sameRawFile) { // scan numbers (not necessary 1,2,3...) int[] sn1 = f1.getScanNumbers(); int[] sn2 = f2.getScanNumbers(); int offsetI1 = 0; int offsetI2 = 0; // find corresponding value if (sn2[0] > sn1[0]) { for (int i = 1; i < sn1.length; i++) { if (sn1[i] == sn2[0]) { offsetI1 = i; break; } } // peaks are not overlapping if (offsetI1 == 0) return null; } if (sn2[0] < sn1[0]) { for (int i = 1; i < sn2.length; i++) { if (sn1[0] == sn2[i]) { offsetI2 = i; break; } } // peaks are not overlapping if (offsetI2 == 0) return null; } // only correlate intercepting areas 0-max int max = 0; if (sn1.length - offsetI1 <= sn2.length - offsetI2) max = sn1.length - offsetI1; if (sn1.length - offsetI1 > sn2.length - offsetI2) max = sn2.length - offsetI2; if (max - offsetI1 > minCorrelatedDataPoints && max - offsetI2 > minCorrelatedDataPoints) { RawDataFile raw = f1.getDataFile(); SimpleRegression reg = new SimpleRegression(); // save max and min of intensity of val1(x) double maxX = 0; double minX = Double.POSITIVE_INFINITY; Vector<Double> I1 = new Vector<Double>(); Vector<Double> I2 = new Vector<Double>(); // add all data points over a given threshold // raw data (not smoothed) for (int i = 0; i < max; i++) { if (sn1[i + offsetI1] != sn2[i + offsetI2]) throw new Exception("Scans are not the same for peak shape corr"); double val1 = f1.getDataPoint(sn1[i + offsetI1]).getIntensity(); double val2 = f2.getDataPoint(sn2[i + offsetI2]).getIntensity(); if (val1 >= noiseLevelShapeCorr && val2 >= noiseLevelShapeCorr) { reg.addData(val1, val2); if (val1 < minX) minX = val1; if (val1 > maxX) maxX = val1; I1.add(val1); I2.add(val2); } } // return pearson r if (reg.getN() >= minCorrelatedDataPoints) { Double[][] data = new Double[][] { I1.toArray(new Double[I1.size()]), I2.toArray(new Double[I2.size()]) }; return new FeatureShapeCorrelationData(reg, data, minX, maxX); } } } else { // TODO if different raw file search for same rt // impute rt/I values if between 2 data points } return null; }
From source file:com.userweave.module.methoden.iconunderstandability.service.ComputeIconTestStatisticsImpl.java
/** * return regression, if regression can be computed * @return//ww w . j ava2 s . co m */ private OverallStatistics computeOverallStatistics() { SimpleRegression regression = new SimpleRegression(); DescriptiveStatistics overallStatistics = DescriptiveStatistics.newInstance(); Map<Integer, DescriptiveStatistics> iconCount2Statistics = new HashMap<Integer, DescriptiveStatistics>(); List<Object[]> executionTimesIconCount = testResultDao.findAllValidExecutionTimesAndIconCount(); if (!executionTimesIconCount.isEmpty()) { // check, if there is variation in x (only one x value for all observation yield NaN!) boolean canComputeRegression = false; int iconCountForFirstResult = ((Long) executionTimesIconCount.get(0)[1]).intValue(); for (Object[] executionTimeIconCount : executionTimesIconCount) { int iconCount = ((Long) executionTimeIconCount[1]).intValue(); if (iconCount != iconCountForFirstResult) { canComputeRegression = true; } double executionTime = (Long) executionTimeIconCount[0]; if (isValid(executionTime)) { regression.addData(iconCount, executionTime); overallStatistics.addValue(executionTime); getStatisticsForIconCount(iconCount2Statistics, iconCount).addValue(executionTime); } } if (canComputeRegression) { return new OverallStatistics(regression, overallStatistics.getMean(), iconCount2Statistics); } else { return new OverallStatistics(null, overallStatistics.getMean(), iconCount2Statistics); } } else { return null; } }
From source file:com.joliciel.jochre.graphics.SourceImageImpl.java
/** * Returns the slope of the current image's horizontal inclination. * Assumes an initial stab has already been made at group shapes into rows, * and that rows are grouped from top to bottom. * @return//from w w w. j av a 2s.c om */ public double getInclination() { LOG.debug("#### getInclination ####"); // It may well be that rows have been grouped together // wrongly if the image has several columns. // The only rows that are fairly reliable are very long horizontal bars // and the first long row, in which all letters are aligned to the same baseline, // regardless of their size. // let's get the medium width first Mean widthMean = new Mean(); for (RowOfShapes row : this.getRows()) { widthMean.increment(row.getRight() - row.getLeft()); } double meanWidth = widthMean.getResult(); LOG.debug("meanWidth: " + meanWidth); double minWidth = meanWidth * 0.75; // find the first row with a pretty wide width RowOfShapes theRow = null; for (RowOfShapes row : this.getRows()) { int width = row.getRight() - row.getLeft(); if (width > minWidth) { theRow = row; break; } } // calculate a regression for average shapes on this row double minHeight = theRow.getAverageShapeHeight() - theRow.getAverageShapeHeightMargin(); double maxHeight = theRow.getAverageShapeHeight() + theRow.getAverageShapeHeightMargin(); SimpleRegression regression = new SimpleRegression(); for (Shape shape : theRow.getShapes()) { if (shape.getHeight() >= minHeight && shape.getHeight() <= maxHeight) { for (int x = 0; x < shape.getWidth(); x++) { for (int y = 0; y < shape.getHeight(); y++) { if (shape.isPixelBlack(x, y, this.getBlackThreshold())) { regression.addData(shape.getLeft() + x, shape.getTop() + y); } } } } } return regression.getSlope(); }
From source file:msc.fall2015.stock.kmeans.hbase.mapreduce.StockDataReaderMapper.java
public void map(ImmutableBytesWritable row, Result value, Context context) throws InterruptedException, IOException { SimpleRegression regression; for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> columnFamilyMap : value.getMap() .entrySet()) {//from w w w. j a va 2 s. c o m regression = new SimpleRegression(); int count = 1; for (Map.Entry<byte[], NavigableMap<Long, byte[]>> entryVersion : columnFamilyMap.getValue() .entrySet()) { for (Map.Entry<Long, byte[]> entry : entryVersion.getValue().entrySet()) { String rowKey = Bytes.toString(value.getRow()); String column = Bytes.toString(entryVersion.getKey()); byte[] val = entry.getValue(); String valOfColumn = new String(val); System.out.println( "RowKey : " + rowKey + " Column Key : " + column + " Column Val : " + valOfColumn); if (!valOfColumn.isEmpty()) { String[] priceAndCap = valOfColumn.split("_"); if (priceAndCap.length > 1) { String pr = priceAndCap[0]; if (pr != null && !pr.equals("null")) { double price = Double.valueOf(pr); if (price < 0) { price = price - 2 * price; } System.out.println("Price : " + price + " count : " + count); regression.addData(count, price); } } } } count++; } // displays intercept of regression line System.out.println("Intercept : " + regression.getIntercept()); // displays slope of regression line System.out.println("Slope : " + regression.getSlope()); // displays slope standard error System.out.println("Slope STD Error : " + regression.getSlopeStdErr()); } }