/** * Adds one observation to the regression model. * * @param x the independent variables which form the design matrix * @param y the dependent or response variable * @throws ModelSpecificationException if the length of {@code x} does not equal * the number of independent variables in the model */ public void addObservation(final double[] x,final double y) throws ModelSpecificationException { if( x == null || x.length == 0 ){ throw new ModelSpecificationException(LocalizedFormats.INVALID_REGRESSION_OBSERVATION,x!=null?x.length:0, 1); } addData( x[0], y ); }
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(); } }
/** * Adds the observations represented by the elements in * <code>data</code>. * <p> * <code>(data[0][0],data[0][1])</code> will be the first observation, then * <code>(data[1][0],data[1][1])</code>, etc.</p> * <p> * This method does not replace data that has already been added. The * observations represented by <code>data</code> are added to the existing * dataset.</p> * <p> * To replace all data, use <code>clear()</code> before adding the new * data.</p> * * @param data array of observations to be added * @throws ModelSpecificationException if the length of {@code data[i]} is not * greater than or equal to 2 */ public void addData(final double[][] data) throws ModelSpecificationException { for (int i = 0; i < data.length; i++) { if( data[i].length < 2 ){ throw new ModelSpecificationException(LocalizedFormats.INVALID_REGRESSION_OBSERVATION, data[i].length, 2); } addData(data[i][0], data[i][1]); } }
@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.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 (float) regression.getIntercept(); }
addData( x[i][0], y[i] );
start = dp.getTimestamp(); simpleRegression.addData(dp.getTimestamp(), dp.getDoubleValue());
/** * 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(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(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.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; }
@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(); }
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)); } }