From a7f591c2d31ba469d07acb941dae1543874f950b Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Mon, 18 Feb 2019 14:57:26 -0800 Subject: [PATCH] Add integration capabilities to RightContinuousStepFunction; use it for calculating mortality --- .../competingrisk/CompetingRiskFunctions.java | 29 +-------- ...ContinuousStepFunctionIntegrationTest.java | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 src/test/java/ca/joeltherrien/randomforest/utils/RightContinuousStepFunctionIntegrationTest.java diff --git a/src/main/java/ca/joeltherrien/randomforest/responses/competingrisk/CompetingRiskFunctions.java b/src/main/java/ca/joeltherrien/randomforest/responses/competingrisk/CompetingRiskFunctions.java index aaaea11..7dcddfb 100644 --- a/src/main/java/ca/joeltherrien/randomforest/responses/competingrisk/CompetingRiskFunctions.java +++ b/src/main/java/ca/joeltherrien/randomforest/responses/competingrisk/CompetingRiskFunctions.java @@ -42,33 +42,6 @@ public class CompetingRiskFunctions implements Serializable { public double calculateEventSpecificMortality(final int event, final double tau){ final RightContinuousStepFunction cif = getCumulativeIncidenceFunction(event); - - double summation = 0.0; - - Double previousTime = null; - Double previousY = null; - - final double[] cifTimes = cif.getX(); - for(int i=0; i tau){ - break; - } - - if(previousTime != null){ - summation += previousY * (time - previousTime); - } - previousTime = time; - previousY = cif.evaluateByIndex(i); - } - - // this is to ensure that we integrate over the proper range - if(previousTime != null){ - summation += cif.evaluate(tau) * (tau - previousTime); - } - - return summation; - + return cif.integrate(0, tau); } } diff --git a/src/test/java/ca/joeltherrien/randomforest/utils/RightContinuousStepFunctionIntegrationTest.java b/src/test/java/ca/joeltherrien/randomforest/utils/RightContinuousStepFunctionIntegrationTest.java new file mode 100644 index 0000000..04de18b --- /dev/null +++ b/src/test/java/ca/joeltherrien/randomforest/utils/RightContinuousStepFunctionIntegrationTest.java @@ -0,0 +1,63 @@ +package ca.joeltherrien.randomforest.utils; + +import ca.joeltherrien.randomforest.TestUtils; +import org.junit.jupiter.api.Test; + +public class RightContinuousStepFunctionIntegrationTest { + + private RightContinuousStepFunction createTestFunction(){ + + final double defaultY = 1; + final double[] x = new double[]{1.0, 2.0, 3.0, 4.0, 5.0}; + final double[] y = new double[]{2.0, 3.0, -0.5, -1.0, 3.0}; + + return new RightContinuousStepFunction(x, y, defaultY); + } + + @Test + public void testIntegration(){ + final RightContinuousStepFunction function = createTestFunction(); + + // Test whether it can handle both from and to being before all the x-values + TestUtils.closeEnough(0.9, + function.integrate(0, 0.9) + , 0.00000001); + + // Test whether it can handle both from and to being after all the x-values + TestUtils.closeEnough(3.0, + function.integrate(6, 7) + , 0.00000001); + + TestUtils.closeEnough(6.0, + function.integrate(0, 3) + , 0.00000001); + + TestUtils.closeEnough(4.5, + function.integrate(0, 2.5) + , 0.00000001); + + TestUtils.closeEnough(7.5, + function.integrate(0, 6) + , 0.00000001); + + TestUtils.closeEnough(-0.5, + function.integrate(3, 4) + , 0.00000001); + + TestUtils.closeEnough(-0.5*0.8, + function.integrate(3.1, 3.9) + , 0.00000001); + + TestUtils.closeEnough(1.5, + function.integrate(3, 6) + , 0.00000001); + + TestUtils.closeEnough(3.0, + function.integrate(2.5, 6) + , 0.00000001); + + + } + + +}