final double lo = 2;
final double hi = 3;
final Logit f = new Logit(lo, hi);
final Sigmoid g = new Sigmoid(lo, hi);
RandomGenerator random = new Well1024a(0x96885e9c1f81cea5l);
final UnivariateDifferentiableFunction id =
FunctionUtils.compose((UnivariateDifferentiableFunction) g, (UnivariateDifferentiableFunction) f);
for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
double max = 0;
for (int i = 0; i < 10; i++) {
final double x = lo + random.nextDouble() * (hi - lo);
final DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x);
max = FastMath.max(max, FastMath.abs(dsX.getPartialDerivative(maxOrder) -
id.value(dsX).getPartialDerivative(maxOrder)));
Assert.assertEquals(dsX.getPartialDerivative(maxOrder),
id.value(dsX).getPartialDerivative(maxOrder),
epsilon[maxOrder]);
}
// each function evaluates correctly near boundaries,
// but combination leads to NaN as some intermediate point is infinite
final DerivativeStructure dsLo = new DerivativeStructure(1, maxOrder, 0, lo);
if (maxOrder == 0) {
Assert.assertTrue(Double.isInfinite(f.value(dsLo).getPartialDerivative(maxOrder)));
Assert.assertEquals(lo, id.value(dsLo).getPartialDerivative(maxOrder), epsilon[maxOrder]);
} else if (maxOrder == 1) {
Assert.assertTrue(Double.isInfinite(f.value(dsLo).getPartialDerivative(maxOrder)));
Assert.assertTrue(Double.isNaN(id.value(dsLo).getPartialDerivative(maxOrder)));
} else {
Assert.assertTrue(Double.isNaN(f.value(dsLo).getPartialDerivative(maxOrder)));
Assert.assertTrue(Double.isNaN(id.value(dsLo).getPartialDerivative(maxOrder)));
}
final DerivativeStructure dsHi = new DerivativeStructure(1, maxOrder, 0, hi);
if (maxOrder == 0) {
Assert.assertTrue(Double.isInfinite(f.value(dsHi).getPartialDerivative(maxOrder)));
Assert.assertEquals(hi, id.value(dsHi).getPartialDerivative(maxOrder), epsilon[maxOrder]);
} else if (maxOrder == 1) {
Assert.assertTrue(Double.isInfinite(f.value(dsHi).getPartialDerivative(maxOrder)));
Assert.assertTrue(Double.isNaN(id.value(dsHi).getPartialDerivative(maxOrder)));
} else {
Assert.assertTrue(Double.isNaN(f.value(dsHi).getPartialDerivative(maxOrder)));
Assert.assertTrue(Double.isNaN(id.value(dsHi).getPartialDerivative(maxOrder)));
}
}
}