org.apache.commons.math3.stat.regression.SimpleRegression Java Examples

The following examples show how to use org.apache.commons.math3.stat.regression.SimpleRegression. 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: AlgorithmUtil.java    From LibreAlarm with GNU General Public License v3.0 6 votes vote down vote up
@NonNull
private static PredictionData getPredictionData(int attempt, String tagId, ArrayList<GlucoseData> trendList) {
    PredictionData predictedGlucose = new PredictionData();
    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < trendList.size(); i++) {
        regression.addData(trendList.size() - i, (trendList.get(i)).glucoseLevel);
    }
    predictedGlucose.glucoseLevel = (int)regression.predict(15 + PREDICTION_TIME);
    predictedGlucose.trend = regression.getSlope();
    predictedGlucose.confidence = regression.getSlopeConfidenceInterval();
    predictedGlucose.errorCode = PredictionData.Result.OK;
    predictedGlucose.realDate = trendList.get(0).realDate;
    predictedGlucose.sensorId = tagId;
    predictedGlucose.attempt = attempt;
    predictedGlucose.sensorTime = trendList.get(0).sensorTime;
    return predictedGlucose;
}
 
Example #2
Source File: PredictionData.java    From OpenLibre with GNU General Public License v3.0 6 votes vote down vote up
private void makePrediction(List<GlucoseData> trendList) {
    if (trendList.size() == 0) {
        return;
    }
    regression = new SimpleRegression();
    for (int i = 0; i < trendList.size(); i++) {
        regression.addData(i, (trendList.get(i)).getGlucoseLevelRaw());
    }
    int glucoseLevelRaw =
            (int) regression.predict(regression.getN() - 1 + PREDICTION_TIME);
    glucoseSlopeRaw = regression.getSlope();
    confidenceInterval = regression.getSlopeConfidenceInterval();
    int ageInSensorMinutes =
            trendList.get(trendList.size() - 1).getAgeInSensorMinutes() + PREDICTION_TIME;
    glucoseData = new GlucoseData(trendList.get(0).getSensor(), ageInSensorMinutes, trendList.get(0).getTimezoneOffsetInMinutes(), glucoseLevelRaw, true);
}
 
Example #3
Source File: Example1.java    From Java-Data-Analysis with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    SimpleRegression sr = getData("data/Data1.dat");
    double m = sr.getSlope();
    double b = sr.getIntercept();
    double r = sr.getR();  // correlation coefficient
    double r2 = sr.getRSquare();
    double sse = sr.getSumSquaredErrors();
    double tss = sr.getTotalSumSquares();

    System.out.printf("y = %.6fx + %.4f%n", m, b);
    System.out.printf("r = %.6f%n", r);
    System.out.printf("r2 = %.6f%n", r2);
    System.out.printf("EV = %.5f%n", tss - sse);
    System.out.printf("UV = %.4f%n", sse);
    System.out.printf("TV = %.3f%n", tss);
}
 
Example #4
Source File: Calibration.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
public List<double[]> getPairs(BufferedImage bi1, BufferedImage bi2, int u, int v,  int s, int n) throws IOException {
    List<double[]> pairList = new ArrayList<>(bi1.getWidth()*bi1.getHeight());
    Raster r1 = bi1.getRaster().createTranslatedChild(0,0);
    Raster r2 = bi2.getRaster().createTranslatedChild(0,0);
    if (r1.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    if (r2.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    SimpleRegression reg = new SimpleRegression(true);
    int minX = u<0?u*-1:0;
    int minY = v<0?v*-1:0;
    int maxX = u>0?bi1.getWidth()-u: bi1.getWidth();
    int maxY = v>0?bi1.getHeight()-v: bi1.getHeight();
    for (int x=minX; x<maxX; x++) {
        for (int y=minY; y<maxY; y++) {
            double d1 = r1.getSampleDouble(x+u,y+v,0);
            if (d1> intensityThreshold) {
                double d2 = r2.getSampleDouble(x, y, 0);
                double[] pair = new double[]{d2,d1};
                pairList.add(pair);
            }
        }
    }

    return pairList;
}
 
Example #5
Source File: Calibration.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
public double getSlope(BufferedImage bi1, BufferedImage bi2, int u, int v,  int s, int n) throws IOException {
   
    Raster r1 = bi1.getRaster().createTranslatedChild(0,0);
    Raster r2 = bi2.getRaster().createTranslatedChild(0,0);
    if (r1.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    if (r2.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    SimpleRegression reg = new SimpleRegression(true);
    int minX = u<0?u*-1:0;
    int minY = v<0?v*-1:0;
    int maxX = u>0?bi1.getWidth()-u: bi1.getWidth();
    int maxY = v>0?bi1.getHeight()-v: bi1.getHeight();
    for (int x=minX; x<maxX; x++) {
         for (int y=minY; y<maxY; y++) {
             double d1 = r1.getSampleDouble(x+u,y+v,0);
             if (d1> intensityThreshold) {
                 double d2 = r2.getSampleDouble(x, y, 0);
                 reg.addData(d2, d1);
             }
         }
     }

    double slope = reg.getSlope();
    double intercept = reg.getIntercept();
    logger.info("i,j: "+s+","+n+": "+ "slope: "+slope+" ; intercept: "+intercept);
    return slope;
}
 
Example #6
Source File: Example1.java    From Java-Data-Analysis with MIT License 6 votes vote down vote up
public static SimpleRegression getData(String data) {
    SimpleRegression sr = new SimpleRegression();
    try {
        Scanner fileScanner = new Scanner(new File(data));
        fileScanner.nextLine();  // read past title line
        int n = fileScanner.nextInt();
        fileScanner.nextLine();  // read past line of labels
        fileScanner.nextLine();  // read past line of labels
        for (int i = 0; i < n; i++) {
            String line = fileScanner.nextLine();
            Scanner lineScanner = new Scanner(line).useDelimiter("\\t");
            double x = lineScanner.nextDouble();
            double y = lineScanner.nextDouble();
            sr.addData(x, y);
        }
    } catch (FileNotFoundException e) {
        System.err.println(e);
    }
    return sr;
}
 
Example #7
Source File: LRTest.java    From ml-models with Apache License 2.0 6 votes vote down vote up
private void checkPredictions() {
    SimpleRegression R = new SimpleRegression();
    R.addData(1.0, 1.345);
    R.addData(2.0, 2.596);
    R.addData(3.0, 3.259);
    HashMap<Double, Double> expected = new HashMap<>();
    expected.put(4.0, R.predict(4.0));
    expected.put(5.0, R.predict(5.0));

    String gatherPredictedValues = "MATCH () - [r:WORKS_FOR] -> () WHERE exists(r.time) AND " +
            "exists(r.predictedProgress) RETURN r.time as time, r.predictedProgress as predictedProgress";

    Result result = db.execute(gatherPredictedValues);

    check(result, expected);
}
 
Example #8
Source File: RegressionEvaluatorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws IOException {
  double[] l1 = new double[] {3.4, 4.5, 6.7};
  double[] l2 = new double[] {1.2, 3.2, 3};
  
  values.clear();
  values.put("l1", l1);
  values.put("l2", l2);

  Tuple result = (Tuple)factory.constructEvaluator("regress(l1,l2)").evaluate(new Tuple(values));
  
  SimpleRegression regression = new SimpleRegression();
  regression.addData(l1[0],l2[0]);
  regression.addData(l1[1],l2[1]);
  regression.addData(l1[2],l2[2]);
  
  Assert.assertEquals(result.getDouble("slope"), regression.getSlope());
  
}
 
Example #9
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 #10
Source File: SimpleLinearRegressionIndicatorTest.java    From ta4j-origins with MIT License 6 votes vote down vote up
@Test
public void calculateLinearRegressionOn4Observations() {

    SimpleLinearRegressionIndicator reg = new SimpleLinearRegressionIndicator(closePrice, 4);
    assertDecimalEquals(reg.getValue(1), 20);
    assertDecimalEquals(reg.getValue(2), 30);
    
    SimpleRegression origReg = buildSimpleRegression(10, 20, 30, 40);
    assertDecimalEquals(reg.getValue(3), 40);
    assertDecimalEquals(reg.getValue(3), origReg.predict(3));
    
    origReg = buildSimpleRegression(30, 40, 30, 40);
    assertDecimalEquals(reg.getValue(5), origReg.predict(3));
    
    origReg = buildSimpleRegression(30, 20, 30, 50);
    assertDecimalEquals(reg.getValue(9), origReg.predict(3));
}
 
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: AseVariantAppendable.java    From systemsgenetics with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void calculateStatistics() {

	double zscoreSum = 0;

	SimpleRegression regression = new SimpleRegression();

	for (int i = 0 ; i < a1Counts.size() ; ++i){

		regression.addData(a1Counts.getQuick(i), a2Counts.getQuick(i));

		final double pvalue = pValues.getQuick(i);

		// we used 2 sided test so divide by 2
		//double zscore = normalDist.inverseCumulativeProbability(pvalue/2);
		final double pvalueDiv2 = pvalue / 2;
		final double zscore;
		if (pvalueDiv2 < Double.MIN_NORMAL){
			zscore = LARGEST_ZSCORE;	
		} else {
			zscore = Probability.normalInverse(pvalueDiv2);
		}
		// Min / plus might look counter intuative but i omit 1 - p/2 above so here I have to swap
		if(a1Counts.getQuick(i) < a2Counts.getQuick(i)){
			zscoreSum -= zscore;
		} else {
			zscoreSum += zscore;
		}
	}

	countPearsonR = regression.getR();
	metaZscore = zscoreSum / Math.sqrt(a1Counts.size());
	metaPvalue = 2 * Probability.normal(-Math.abs(metaZscore));
	mle = new AseMleBeta(a1Counts, a2Counts);

}
 
Example #13
Source File: SimpleLinearRegressionIndicatorTest.java    From ta4j-origins with MIT License 5 votes vote down vote up
@Test
public void calculateLinearRegression() {
    double[] values = new double[] { 1, 2, 1.3, 3.75, 2.25 };
    ClosePriceIndicator indicator = new ClosePriceIndicator(new MockTimeSeries(values));
    SimpleLinearRegressionIndicator reg = new SimpleLinearRegressionIndicator(indicator, 5);
    
    SimpleRegression origReg = buildSimpleRegression(values);
    assertDecimalEquals(reg.getValue(4), origReg.predict(4));
}
 
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: 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 #17
Source File: Display.java    From joinery with GNU General Public License v3.0 5 votes vote down vote up
private static void addTrend(final Chart chart, final Series series, final List<Object> xdata) {
    final SimpleRegression model = new SimpleRegression();
    final Iterator<? extends Number> y = series.getYData().iterator();
    for (int x = 0; y.hasNext(); x++) {
        model.addData(x, y.next().doubleValue());
    }
    final Color mc = series.getMarkerColor();
    final Color c = new Color(mc.getRed(), mc.getGreen(), mc.getBlue(), 0x60);
    final Series trend = chart.addSeries(series.getName() + " (trend)",
            Arrays.asList(xdata.get(0), xdata.get(xdata.size() - 1)),
            Arrays.asList(model.predict(0), model.predict(xdata.size() - 1))
        );
    trend.setLineColor(c);
    trend.setMarker(SeriesMarker.NONE);
}
 
Example #18
Source File: PerformInteractionAnalysisPermutationTask.java    From systemsgenetics with GNU General Public License v3.0 5 votes vote down vote up
private double correlateCovariateWithGenotype(int snp){
		SimpleRegression simpleRegression = new SimpleRegression();
		double[] expression = datasetCovariatesPCAForceNormal.rawData[covToTest];
		double[] genotypes = datasetGenotypes.rawData[snp];
		for (int s = 0; s < expression.length; s++) {
			simpleRegression.addData(expression[s], genotypes[s]);
		}
		//This is not working now that we have the _rs next to the gene names
//		if (datasetGenotypes.probeNames[snp].equals(datasetCovariatesPCAForceNormal.probeNames[covToTest])){
//			System.out.println("Same gene! " + datasetGenotypes.probeNames[snp] + "\t" + datasetCovariatesPCAForceNormal.probeNames[covToTest] + "\t" + simpleRegression.getSignificance() + "\t" + simpleRegression.getR());
//		}
		return simpleRegression.getSignificance();
	}
 
Example #19
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Pearson's product-moment correlation coefficient between two arrays.
 *
 * <p>Throws MathIllegalArgumentException if the arrays do not have the same length
 * or their common length is less than 2.  Returns {@code NaN} if either of the arrays
 * has zero variance (i.e., if one of the arrays does not contain at least two distinct
 * values).</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Pearson's correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if there is insufficient data
 */
public double correlation(final double[] xArray, final double[] yArray) {
    SimpleRegression regression = new SimpleRegression();
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        for(int i=0; i<xArray.length; i++) {
            regression.addData(xArray[i], yArray[i]);
        }
        return regression.getR();
    }
}
 
Example #20
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 #21
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 #22
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Pearson's product-moment correlation coefficient between the two arrays.
 *
 * </p>Throws IllegalArgumentException if the arrays do not have the same length
 * or their common length is less than 2</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Pearson's correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if there is insufficient data
 */
public double correlation(final double[] xArray, final double[] yArray) {
    SimpleRegression regression = new SimpleRegression();
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        for(int i=0; i<xArray.length; i++) {
            regression.addData(xArray[i], yArray[i]);
        }
        return regression.getR();
    }
}
 
Example #23
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 #24
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Pearson's product-moment correlation coefficient between the two arrays.
 *
 * </p>Throws IllegalArgumentException if the arrays do not have the same length
 * or their common length is less than 2</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Pearson's correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if there is insufficient data
 */
public double correlation(final double[] xArray, final double[] yArray) {
    SimpleRegression regression = new SimpleRegression();
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        for(int i=0; i<xArray.length; i++) {
            regression.addData(xArray[i], yArray[i]);
        }
        return regression.getR();
    }
}
 
Example #25
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 #26
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the Pearson's product-moment correlation coefficient between the two arrays.
 *
 * </p>Throws IllegalArgumentException if the arrays do not have the same length
 * or their common length is less than 2</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @return Returns Pearson's correlation coefficient for the two arrays
 * @throws DimensionMismatchException if the arrays lengths do not match
 * @throws MathIllegalArgumentException if there is insufficient data
 */
public double correlation(final double[] xArray, final double[] yArray) {
    SimpleRegression regression = new SimpleRegression();
    if (xArray.length != yArray.length) {
        throw new DimensionMismatchException(xArray.length, yArray.length);
    } else if (xArray.length < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION,
                                               xArray.length, 2);
    } else {
        for(int i=0; i<xArray.length; i++) {
            regression.addData(xArray[i], yArray[i]);
        }
        return regression.getR();
    }
}
 
Example #27
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 #28
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 #29
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 #30
Source File: TestRealRegrInterceptAggregation.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.getIntercept();
    checkArgument(Float.isFinite(expected) && expected != 0.f, "Expected result is trivial");
    testAggregation(expected, createBlockOfReals(y), createBlockOfReals(x));
}