MSEImpurityMeasure implements ImpurityMeasure<MSEImpurityMeasure> { @Override public double impurity() { double impurity = 0; if (leftCnt > 0) impurity += leftY2 - 2.0 * leftY / leftCnt * leftY + Math.pow(leftY / leftCnt, 2) * leftCnt; if (rightCnt > 0) impurity += rightY2 - 2.0 * rightY / rightCnt * rightY + Math.pow(rightY / rightCnt, 2) * rightCnt; return impurity; } MSEImpurityMeasure(double leftY, double leftY2, long leftCnt, double rightY, double rightY2, long rightCnt); @Override double impurity(); @Override MSEImpurityMeasure add(MSEImpurityMeasure b); @Override MSEImpurityMeasure subtract(MSEImpurityMeasure b); double getLeftY(); double getLeftY2(); long getLeftCnt(); double getRightY(); double getRightY2(); long getRightCnt();  }
@Test public void testImpurityOnEmptyData() { MSEImpurityMeasure impurity = new MSEImpurityMeasure(0, 0, 0, 0, 0, 0); assertEquals(0.0, impurity.impurity(), 1e-10); }
@Test public void testImpurityLeftPart() { MSEImpurityMeasure impurity = new MSEImpurityMeasure(8, 12, 6, 0, 0, 0); assertEquals(1.333, impurity.impurity(), 1e-3); }
@Test public void testImpurityRightPart() { MSEImpurityMeasure impurity = new MSEImpurityMeasure(0, 0, 0, 8, 12, 6); assertEquals(1.333, impurity.impurity(), 1e-3); }
@Test public void testImpurityLeftAndRightPart() { MSEImpurityMeasure impurity = new MSEImpurityMeasure(5, 9, 3, 3, 3, 3); assertEquals(0.666, impurity.impurity(), 1e-3); }