Java Code Examples for org.apache.commons.math3.stat.regression.SimpleRegression#getSlope()

The following examples show how to use org.apache.commons.math3.stat.regression.SimpleRegression#getSlope() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GrowingWaitQueueDetector.java    From incubator-heron with Apache License 2.0 6 votes vote down vote up
private double computeWaitQueueSizeTrend(MeasurementsTable metrics) {
  double maxSlope = 0;
  for (String instance : metrics.uniqueInstances()) {

    if (metrics.instance(instance) == null || metrics.instance(instance).size() < 3) {
      // missing of insufficient data for creating a trend line
      continue;
    }

    Collection<Measurement> measurements
        = metrics.instance(instance).sort(false, MeasurementsTable.SortKey.TIME_STAMP).get();
    SimpleRegression simpleRegression = new SimpleRegression(true);

    for (Measurement m : measurements) {
      simpleRegression.addData(m.instant().getEpochSecond(), m.value());
    }

    double slope = simpleRegression.getSlope();

    if (maxSlope < slope) {
      maxSlope = slope;
    }
  }
  return maxSlope;
}
 
Example 2
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 3
Source File: TestDoubleRegrSlopeAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Object getExpectedValue(int start, int length)
{
    if (length <= 1) {
        return null;
    }
    SimpleRegression regression = new SimpleRegression();
    for (int i = start; i < start + length; i++) {
        regression.addData(i + 2, i);
    }
    return regression.getSlope();
}
 
Example 4
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 5
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 6
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 7
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 8
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 9
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 10
Source File: McLeanRegressionLineFit.java    From ET_Redux with Apache License 2.0 5 votes vote down vote up
public McLeanOrdinaryLeastSquaresRegressionLine(SimpleRegression regression) {
    this.regression = regression;

    a = new double[2][1];
    a[1][0] = regression.getIntercept();

    v = new double[2][1];
    v[1][0] = regression.getSlope();
}
 
Example 11
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 12
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 13
Source File: OLSTests.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the Morpheus OLS model yields the same results as Apache Math
 * @param frame      the data for regression
 * @param actual    the Morpheus results
 * @param expected  the Apache results
 */
private <R> void assertResultsMatch(DataFrame<String,String> frame, DataFrameLeastSquares<String,String> actual, SimpleRegression expected) {

    final double tss1 = actual.getTotalSumOfSquares();
    final double tss2 = expected.getTotalSumSquares();
    final double threshold = ((tss1 + tss2) / 2d) * 0.00001d;

    Assert.assertEquals(actual.getTotalSumOfSquares(), expected.getTotalSumSquares(), threshold, "Total sum of squares matches");
    Assert.assertEquals(actual.getRSquared(), expected.getRSquare(), 0.0000001, "R^2 values match");

    final double beta1 = actual.getBetaValue("X", Field.PARAMETER);
    final double beta2 = expected.getSlope();
    Assert.assertEquals(beta1, beta2, 0.000001, "Beta parameters match");

    final double intercept = expected.getIntercept();
    final double interceptStdError = expected.getInterceptStdErr();
    Assert.assertEquals(actual.getInterceptValue(Field.PARAMETER), intercept,  0.0000001, "The intercepts match");
    Assert.assertEquals(actual.getInterceptValue(Field.STD_ERROR), interceptStdError, 0.000000001, "The intercept std errors match");

    final double betaStdErr1 = actual.getBetaValue("X", Field.STD_ERROR);
    final double betaStdErr2 = expected.getSlopeStdErr();
    Assert.assertEquals(betaStdErr1, betaStdErr2, 0.00000001, "Beta Standard errors match");

    final DataFrame<String,String> residuals = actual.getResiduals();
    Assert.assertEquals(residuals.rows().count(), frame.rows().count(), "There are expected number of residuals");
    residuals.rows().forEach(row -> {
        final double x = frame.data().getDouble(row.ordinal(), "X");
        final double y = frame.data().getDouble(row.ordinal(), "Y");
        final double residual = row.getDouble(0);
        final double expect = y - expected.predict(x);
        Assert.assertEquals(residual, expect, 0.0000001, "Residual matches for x=" + x + " at row index " + row.ordinal());
    });
}
 
Example 14
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 15
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 16
Source File: TestRealRegrSlopeAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
private void testNonTrivialAggregation(Float[] y, Float[] x)
{
    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < x.length; i++) {
        regression.addData(x[i], y[i]);
    }
    float expected = (float) regression.getSlope();
    checkArgument(Float.isFinite(expected) && expected != 0.0f, "Expected result is trivial");
    testAggregation(expected, createBlockOfReals(y), createBlockOfReals(x));
}
 
Example 17
Source File: TestRealRegrSlopeAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Object getExpectedValue(int start, int length)
{
    if (length <= 1) {
        return null;
    }
    SimpleRegression regression = new SimpleRegression();
    for (int i = start; i < start + length; i++) {
        regression.addData(i + 2, i);
    }
    return (float) regression.getSlope();
}
 
Example 18
Source File: StraightLineProblem.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Directly solve the linear problem, using the {@link SimpleRegression}
 * class.
 */
public double[] solve() {
    final SimpleRegression regress = new SimpleRegression(true);
    for (double[] d : points) {
        regress.addData(d[0], d[1]);
    }

    final double[] result = { regress.getSlope(), regress.getIntercept() };
    return result;
}
 
Example 19
Source File: CTSlinearRegression.java    From systemsgenetics with GNU General Public License v3.0 4 votes vote down vote up
public CTSlinearRegression(ArrayList<IndividualSnpData> all_individuals) {
   
    //basic information, get the zero instance.
    snpName = all_individuals.get(0).getSnpName();
    chromosome = all_individuals.get(0).getChromosome();
    position = all_individuals.get(0).getPosition();
    
    //isolate heterozygotes
    ArrayList<IndividualSnpData> het_individuals = UtilityMethods.isolateValidHeterozygotesFromIndividualSnpData(all_individuals);
    numberOfHets = het_individuals.size();
    
            hetSampleNames = new ArrayList<String>();
    asRef = new ArrayList<Integer>(); 
    asAlt = new ArrayList<Integer>(); 
    asNo  = new ArrayList<Integer>(); 
    
  
    cellProp   = new ArrayList<Double>();
    int total_overlap = 0;
    
    //Get the basic data without doing any tests.
    for (IndividualSnpData temp_het : het_individuals) {
        //Do nothing if there is no data in het_individuals

        hetSampleNames.add(temp_het.getSampleName());
        
        asRef.add(temp_het.getRefNum());
        asAlt.add(temp_het.getAltNum());
        asNo.add(temp_het.getNoNum());
        cellProp.add(temp_het.getCellTypeProp());
        
        //this is used to check if we will continue with calculations.
        //BASED on the minHets and MinReads
        total_overlap += temp_het.getRefNum() + temp_het.getAltNum();
    }
    
    
    //Check if we do a test.
    if((total_overlap >= GlobalVariables.minReads) && (numberOfHets >= GlobalVariables.minHets) && (numberOfHets >= 3)){
        
        ASScatterPlot plotThis = null;
        
        if(!GlobalVariables.plotDir.equals("")){
            plotThis = new ASScatterPlot(400);
        }
        
        
        SimpleRegression thisRegression = new SimpleRegression();
        for(int i=0; i< asRef.size(); i++ ){
            Double asRatio;
            //do this check, otherwise the denominator will be zero.
            
            if(asRef.get(i) != 0){
                asRatio = ((double)asRef.get(i)) / ((double)(asRef.get(i) + asAlt.get(i)));
            }else{
                asRatio = 0.0;
            }
            
            Double phenoRatio =  cellProp.get(i);
            thisRegression.addData(phenoRatio, asRatio);
            if(!GlobalVariables.plotDir.equals("")){
                plotThis.plot(asRatio, phenoRatio);
            }
        }
        
        if(!GlobalVariables.plotDir.equals("")){
            plotThis.draw(GlobalVariables.plotDir + "/" + snpName +  "_ASratio_Pheno_Plot.png");
        } 
        
        slope = thisRegression.getSlope();
        intercept  = thisRegression.getIntercept();
        Rsquared = thisRegression.getRSquare();
        stdErrorIntercept = thisRegression.getInterceptStdErr();
        stdErrorSlope = thisRegression.getSlopeStdErr();
        
        pValue = thisRegression.getSignificance();
        
        
        if(GlobalVariables.verbosity >= 10){
                System.out.println("\n--- Starting cell type specific linear regression ---");
                System.out.println("\tSlope:                   " + Double.toString(slope));
                System.out.println("\tStdError of Slope:       " + Double.toString(stdErrorSlope) + "\n");
                System.out.println("\tIntercept:               " + Double.toString(intercept));
                System.out.println("\tStdError of Intercept:   " + Double.toString(stdErrorIntercept) + "\n");   
                System.out.println("\tP value:                 " + Double.toString(pValue));
                System.out.println("--------------------------------------------------------------");
                
        }
        
        
        testPerformed = true;
    }
    
}
 
Example 20
Source File: Indicator.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public double estSkew(int k) {
  SimpleRegression regression = new SimpleRegression();
  int[] idx = { 1 };
  getTopK(k).forEachOrdered(freq -> regression.addData(Math.log(idx[0]++), Math.log(freq)));
  return -regression.getSlope();
}