Example usage for org.apache.commons.math.stat.regression SimpleRegression getSlope

List of usage examples for org.apache.commons.math.stat.regression SimpleRegression getSlope

Introduction

In this page you can find the example usage for org.apache.commons.math.stat.regression SimpleRegression getSlope.

Prototype

public double getSlope() 

Source Link

Document

Returns the slope of the estimated regression line.

Usage

From source file:com.discursive.jccook.math.SimpleRegressionExample.java

public static void main(String[] args) throws MathException {

    SimpleRegression sr = new SimpleRegression();

    // Add data points       
    sr.addData(0, 0);/*from www . j a  v  a 2 s. c o m*/
    sr.addData(1, 1.2);
    sr.addData(2, 2.6);
    sr.addData(3, 3.2);
    sr.addData(4, 4);
    sr.addData(5, 5);

    NumberFormat format = NumberFormat.getInstance();

    // Print the value of y when line intersects the y axis
    System.out.println("Intercept: " + format.format(sr.getIntercept()));

    // Print the number of data points
    System.out.println("N: " + sr.getN());

    // Print the Slope and the Slop Confidence
    System.out.println("Slope: " + format.format(sr.getSlope()));
    System.out.println("Slope Confidence: " + format.format(sr.getSlopeConfidenceInterval()));

    // Print RSquare a measure of relatedness
    System.out.println("RSquare: " + format.format(sr.getRSquare()));

    sr.addData(400, 100);
    sr.addData(300, 105);
    sr.addData(350, 70);
    sr.addData(200, 50);
    sr.addData(150, 300);
    sr.addData(50, 500);

    System.out.println("Intercept: " + format.format(sr.getIntercept()));
    System.out.println("N: " + sr.getN());
    System.out.println("Slope: " + format.format(sr.getSlope()));
    System.out.println("Slope Confidence: " + format.format(sr.getSlopeConfidenceInterval()));
    System.out.println("RSquare: " + format.format(sr.getRSquare()));

}

From source file:com.griddynamics.jagger.engine.e1.scenario.DefaultWorkloadSuggestionMaker.java

private static Integer findClosestPoint(BigDecimal desiredTps, Map<Integer, Pair<Long, BigDecimal>> stats) {
    final int MAX_POINTS_FOR_REGRESSION = 10;

    SortedMap<Long, Integer> map = Maps.newTreeMap(new Comparator<Long>() {
        @Override//www .  j  a  v a  2 s .  co m
        public int compare(Long first, Long second) {
            return second.compareTo(first);
        }
    });
    for (Map.Entry<Integer, Pair<Long, BigDecimal>> entry : stats.entrySet()) {
        map.put(entry.getValue().getFirst(), entry.getKey());
    }

    if (map.size() < 2) {
        throw new IllegalArgumentException("Not enough stats to calculate point");
    }

    // <time><number of threads> - sorted by time
    Iterator<Map.Entry<Long, Integer>> iterator = map.entrySet().iterator();

    SimpleRegression regression = new SimpleRegression();
    Integer tempIndex;
    double previousValue = -1.0;
    double value;
    double measuredTps;

    log.debug("Selecting next point for balancing");
    int indx = 0;
    while (iterator.hasNext()) {

        tempIndex = iterator.next().getValue();

        if (previousValue < 0.0) {
            previousValue = tempIndex.floatValue();
        }
        value = tempIndex.floatValue();
        measuredTps = stats.get(tempIndex).getSecond().floatValue();

        regression.addData(value, measuredTps);

        log.debug(String.format("   %7.2f    %7.2f", value, measuredTps));

        indx++;
        if (indx > MAX_POINTS_FOR_REGRESSION) {
            break;
        }
    }

    double intercept = regression.getIntercept();
    double slope = regression.getSlope();

    double approxPoint;

    // if no slope => use previous number of threads
    if (Math.abs(slope) > 1e-12) {
        approxPoint = (desiredTps.doubleValue() - intercept) / slope;
    } else {
        approxPoint = previousValue;
    }

    // if approximation point is negative - ignore it
    if (approxPoint < 0) {
        approxPoint = previousValue;
    }

    log.debug(String.format("Next point   %7d    (target tps: %7.2f)", (int) Math.round(approxPoint),
            desiredTps.doubleValue()));

    return (int) Math.round(approxPoint);
}

From source file:com.netxforge.netxstudio.common.math.NativeFunctions.java

/**
 * Return a {@link GenericsTuple tuple} with a key being the slope and the
 * value being the intercept of the trendline.
 * //from ww  w. j  a  v a2s  .c o m
 */
public GenericsTuple<Double, Double> trend(double[][] dataPair) {

    SimpleRegression regression = new SimpleRegression();
    regression.addData(dataPair);
    double slope = regression.getSlope();
    double intercept = regression.getIntercept();

    return new GenericsTuple<Double, Double>(slope, intercept);
}

From source file:ch.usi.inf.lidr.merging.SSL.java

/**
 * <b>IMPORTANT:</b> {@link #setSampleDocuments(List)} must be called before running normalization.
 * /*w ww.ja  v a  2  s  . c  o  m*/
 * @see ch.usi.inf.lidr.norm.ScoreNormalization#normalize(List<ScoredEntity<T>>)
 * @see #setSampleDocuments(List)
 */
@Override
public <T> List<ScoredEntity<T>> normalize(List<ScoredEntity<T>> unnormScoredDocs) {
    if (unnormScoredDocs == null) {
        throw new NullPointerException("The list of scored documents is null.");
    }

    SimpleRegression regression = getRegression(unnormScoredDocs);
    //TODO: backup with CORI
    if (regression.getN() < 3) {
        return new ArrayList<ScoredEntity<T>>();
    }

    List<ScoredEntity<T>> normScoredDocs = new ArrayList<ScoredEntity<T>>(unnormScoredDocs.size());
    for (int i = 0; i < unnormScoredDocs.size(); i++) {
        ScoredEntity<T> normScoredDoc = new ScoredEntity<T>(unnormScoredDocs.get(i).getEntity(),
                regression.getSlope() * unnormScoredDocs.get(i).getScore() + regression.getIntercept());
        normScoredDocs.add(normScoredDoc);
    }

    return normScoredDocs;
}

From source file:com.joliciel.jochre.graphics.SourceImageImpl.java

@Override
public double getMeanHorizontalSlope() {
    if (!meanHorizontalSlopeCalculated) {
        // Calculate the average regression to be used for analysis
        Mean meanForSlope = new Mean();
        StandardDeviation stdDevForSlope = new StandardDeviation();
        List<SimpleRegression> regressions = new ArrayList<SimpleRegression>();
        for (RowOfShapes row : this.getRows()) {
            SimpleRegression regression = row.getRegression();
            // only include rows for which regression was really calculated (more than 2 points)
            if (regression.getN() > 2) {
                meanForSlope.increment(regression.getSlope());
                stdDevForSlope.increment(regression.getSlope());
                regressions.add(regression);
            }//  w  w w.  j  a  v a2s.  c  o  m
        }

        double slopeMean = 0.0;
        double slopeStdDev = 0.0;

        if (meanForSlope.getN() > 0) {
            slopeMean = meanForSlope.getResult();
            slopeStdDev = stdDevForSlope.getResult();
        }
        LOG.debug("slopeMean: " + slopeMean);
        LOG.debug("slopeStdDev: " + slopeStdDev);

        if (regressions.size() > 0) {
            double minSlope = slopeMean - slopeStdDev;
            double maxSlope = slopeMean + slopeStdDev;
            meanForSlope = new Mean();
            for (SimpleRegression regression : regressions) {
                if (minSlope <= regression.getSlope() && regression.getSlope() <= maxSlope)
                    meanForSlope.increment(regression.getSlope());
            }

            meanHorizontalSlope = meanForSlope.getResult();
        } else {
            meanHorizontalSlope = 0.0;
        }
        LOG.debug("meanHorizontalSlope: " + meanHorizontalSlope);
        meanHorizontalSlopeCalculated = true;
    }
    return meanHorizontalSlope;
}

From source file:com.joliciel.jochre.boundaries.features.SlopeDifferenceFeature.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);

        for (int i = 1; i <= contourDistance; i++) {
            if (x + i < shape.getWidth()) {
                topRightRegression.addData(x + i, verticalContour[x + i][0]);
                bottomRightRegression.addData(x + i, verticalContour[x + i][1]);
            }//from w  w w .  j  av a2  s  .  com
            if (x - i >= 0) {
                topLeftRegression.addData(x - i, verticalContour[x - i][0]);
                bottomLeftRegression.addData(x - i, verticalContour[x - i][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.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   www . ja v a2  s  .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()) {//  www.j a v a 2s.  c om
        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());
    }
}

From source file:edu.indiana.soic.ts.crunch.CrunchDataReader.java

public PTable<String, String> extractText(PTable<ImmutableBytesWritable, Result> tableContent) {
    return tableContent.parallelDo("Read data",
            new DoFn<Pair<ImmutableBytesWritable, Result>, Pair<String, String>>() {
                @Override//w w w .j  a va2 s  . co m
                public void process(Pair<ImmutableBytesWritable, Result> row,
                        Emitter<Pair<String, String>> emitter) {
                    SimpleRegression regression;
                    NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = row.second()
                            .getMap();
                    System.out.println(map.size());
                    for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> columnFamilyMap : map
                            .entrySet()) {
                        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(row.second().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());
                        emitter.emit(new Pair<String, String>(String.valueOf(regression.getIntercept()),
                                String.valueOf(regression.getSlope())));
                    }
                }
            }, Writables.tableOf(Writables.strings(), Writables.strings()));
}

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  w w  . java  2  s  .  c  o  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;
}