public void testRandom() throws Exception { SimpleRegression regression = new SimpleRegression(); Random random = new Random(1); int n = 100; for (int i = 0; i < n; i++) { regression.addData(((double) i) / (n - 1), random.nextDouble()); } assertTrue(0.0 < regression.getSignificance() && regression.getSignificance() < 1.0); }
public void testPerfectNegative() throws Exception { SimpleRegression regression = new SimpleRegression(); int n = 100; for (int i = 0; i < n; i++) { regression.addData(-((double) i) / (n - 1), i); } assertEquals(0.0, regression.getSignificance(), 1.0e-5); assertTrue(regression.getSlope() < 0.0); }
public void testInference() throws Exception { // ---------- verified against R, version 1.8.1 ----- // infData SimpleRegression regression = new SimpleRegression(); regression.addData(infData); assertEquals("slope std err", 0.011448491, regression.getSlopeStdErr(), 1E-10); assertEquals("std err intercept", 0.286036932, regression.getInterceptStdErr(), 1E-8); assertEquals("significance", 4.596e-07, regression.getSignificance(), 1E-8); assertEquals( "slope conf interval half-width", 0.0270713794287, regression.getSlopeConfidenceInterval(), 1E-8); // infData2 regression = new SimpleRegression(); regression.addData(infData2); assertEquals("slope std err", 1.07260253, regression.getSlopeStdErr(), 1E-8); assertEquals("std err intercept", 4.17718672, regression.getInterceptStdErr(), 1E-8); assertEquals("significance", 0.261829133982, regression.getSignificance(), 1E-11); assertEquals( "slope conf interval half-width", 2.97802204827, regression.getSlopeConfidenceInterval(), 1E-8); // ------------- End R-verified tests ------------------------------- // FIXME: get a real example to test against with alpha = .01 assertTrue( "tighter means wider", regression.getSlopeConfidenceInterval() < regression.getSlopeConfidenceInterval(0.01)); try { regression.getSlopeConfidenceInterval(1); fail("expecting IllegalArgumentException for alpha = 1"); } catch (IllegalArgumentException ex) { // ignored } }
// Test remove multiple observations public void testRemoveMultiple() throws Exception { // Create regression with inference data then remove to test SimpleRegression regression = new SimpleRegression(); regression.addData(infData); regression.removeData(removeMultiple); regression.addData(removeMultiple); // Use the inference assertions to make sure that everything worked assertEquals("slope std err", 0.011448491, regression.getSlopeStdErr(), 1E-10); assertEquals("std err intercept", 0.286036932, regression.getInterceptStdErr(), 1E-8); assertEquals("significance", 4.596e-07, regression.getSignificance(), 1E-8); assertEquals( "slope conf interval half-width", 0.0270713794287, regression.getSlopeConfidenceInterval(), 1E-8); }