Add integration capabilities to RightContinuousStepFunction; use it for calculating mortality
This commit is contained in:
parent
e74ba23177
commit
a7f591c2d3
2 changed files with 64 additions and 28 deletions
|
@ -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<cifTimes.length; i++){
|
||||
final double time = cifTimes[i];
|
||||
|
||||
if(time > 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue