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(); } }
@Override public 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(); }
@Override public 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(); }
@Override public 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.getIntercept(); }
@Override public 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.getIntercept(); }
DataPoint second = null; int count = 0; SimpleRegression simpleRegression = new SimpleRegression(true);
/** * 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(); } }
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)); } }
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)); } }
private void testNonTrivialAggregation(Double[] y, Double[] x) { SimpleRegression regression = new SimpleRegression(); for (int i = 0; i < x.length; i++) { regression.addData(x[i], y[i]); } double expected = regression.getSlope(); checkArgument(Double.isFinite(expected) && expected != 0.0, "Expected result is trivial"); testAggregation(expected, createDoublesBlock(y), createDoublesBlock(x)); } }
private void testNonTrivialAggregation(Double[] y, Double[] x) { SimpleRegression regression = new SimpleRegression(); for (int i = 0; i < x.length; i++) { regression.addData(x[i], y[i]); } double expected = regression.getIntercept(); checkArgument(Double.isFinite(expected) && expected != 0., "Expected result is trivial"); testAggregation(expected, createDoublesBlock(y), createDoublesBlock(x)); } }
@Procedure(name = "apoc.math.regr", mode = Mode.READ) @Description("apoc.math.regr(label, propertyY, propertyX) - It calculates the coefficient " + "of determination (R-squared) for the values of propertyY and propertyX in the " + "provided label") public Stream<Output> regr(@Name("label") String label, @Name("propertyY") String y, @Name("propertyX") String x) { SimpleRegression regr = new SimpleRegression(false); double regrAvgX = 0; double regrAvgY = 0; int count = 0; try (ResourceIterator it = db.findNodes(Label.label(label))) { while (it.hasNext()) { Node node = (Node) it.next(); Number propX = (Number) node.getProperty(x, null); Number propY = (Number) node.getProperty(y, null); if (propX != null && propY != null) { regrAvgX = regrAvgX + propX.doubleValue(); regrAvgY = regrAvgY + propY.doubleValue(); regr.addData(propX.doubleValue(), propY.doubleValue()); count++; } } } regrAvgX = regrAvgX / count; regrAvgY = regrAvgY / count; return Stream.of(new Output(regr.getRSquare(), regrAvgX, regrAvgY, regr.getSlope())); } }
@Test public void testCalculateRegr() throws Throwable { db.execute("CREATE " + "(:REGR_TEST {x_property: 1 , y_property: 2 })," + "(:REGR_TEST {x_property: 2 , y_property: 3 })," + "(:REGR_TEST {y_property: 10000 })," + "(:REGR_TEST {x_property: 3 , y_property: 6 })").close(); SimpleRegression expectedRegr = new SimpleRegression(false); expectedRegr.addData(new double[][]{ {1, 1}, {2, 3}, //{3, 10000}, {3, 6} }); TestUtil.testCall(db, "CALL apoc.math.regr('REGR_TEST', 'y_property', 'x_property')", result -> { assertEquals(expectedRegr.getRSquare(), (Double)result.get("r2"), 0.1); assertEquals(2.0, (Double)result.get("avgX"), 0.1); assertEquals(3.67, (Double)result.get("avgY"), 0.1); assertEquals(expectedRegr.getSlope(), (Double)result.get("slope"), 0.1); }); }
@Test public void testRegrR2isOne() throws Throwable { db.execute("CREATE " + "(:REGR_TEST2 {x_property: 1 , y_property: 1 })," + "(:REGR_TEST2 {x_property: 1 , y_property: 1 })," + "(:REGR_TEST2 {y_property: 10000 })," + "(:REGR_TEST2 {x_property: 1 , y_property: 1 })").close(); SimpleRegression expectedRegr = new SimpleRegression(false); expectedRegr.addData(new double[][]{ {1, 1}, {1, 1}, //{3, 10000}, {1, 1} }); TestUtil.testCall(db, "CALL apoc.math.regr('REGR_TEST2', 'y_property', 'x_property')", result -> { assertEquals(expectedRegr.getRSquare(), (Double)result.get("r2"), 0.1); assertEquals(expectedRegr.getSlope(), (Double)result.get("slope"), 0.1); }); } }
public static SimpleRegression createLinearRegression(final double[] x, final double[] y) { SimpleRegression regression = new SimpleRegression(); for (int i = 0; i < x.length; i++) { regression.addData(x[i], y[i]); } return regression; }
public Comparator(final Profile profile1, final Profile profile2) { validateProfile(profile1, profile2); this.profile1 = profile1; this.profile2 = profile2; nPoints = Math.min(profile1.size(), profile2.size()); p1Counts = profile1.countsAsArray(); p2Counts = profile2.countsAsArray(); regression = new SimpleRegression(); for (int i = 0; i < nPoints; i++) regression.addData(p1Counts[i], p2Counts[i]); }
@Override public 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.getIntercept(); }
@Override public 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(); }
@Override public 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.getIntercept(); }
@Override public 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.getIntercept(); }