private double randomConditionalSurvivalTime(int x,int y,double t,double[] m) throws NoEventException {
  double U=MathUtils.nextDouble();
  double vLeft=0.0;
  double vRight=t;
  double fLeft=1.0 - U;
  double fRight=conditionalSurvivalProbability(x,t,y,t,m) - U;
  if (fRight >= 0.0) {
    if (x != y) {
      throw new IllegalArgumentException("Problem in randomConditionalSurvivalTime for x=" + x + ", y="+ y+ " t="+ t+ " U="+ U+ "fRight="+ fRight);
    }
    throw new NoEventException();
  }
  double vNew=vLeft;
  double vOld=vRight;
  double fNew=fLeft;
  double fOld=fRight;
  while (Math.abs(vNew - vOld) > 1e-9) {
    double vProp=vNew - fNew / ((fOld - fNew) / (vOld - vNew));
    if ((vLeft >= vProp) || (vProp >= vRight)) {
      vProp=vLeft - fLeft / ((fRight - fLeft) / (vRight - vLeft));
    }
    double fProp=conditionalSurvivalProbability(x,vProp,y,t,m) - U;
    vOld=vNew;
    fOld=fNew;
    vNew=vProp;
    fNew=fProp;
    if (fProp < 0.0) {
      vRight=vProp;
      fRight=fProp;
    }
 else {
      vLeft=vProp;
      fLeft=fProp;
    }
  }
  return vNew;
}

private double randomConditionalSurvivalTime ( int x , int y , double t , double [ ] m ) throws NoEventException { double U = MathUtils . nextDouble ( ) ; double vLeft = 0.0 ; double vRight = t ; double fLeft = 1.0 - U ; double fRight = conditionalSurvivalProbability ( x , t , y , t , m ) - U ; if ( fRight >= 0.0 ) { if ( x != y ) { throw new IllegalArgumentException ( "Problem in randomConditionalSurvivalTime for x=" + x + ", y=" + y + " t=" + t + " U=" + U + "fRight=" + fRight ) ; } throw new NoEventException ( ) ; } double vNew = vLeft ; double vOld = vRight ; double fNew = fLeft ; double fOld = fRight ; while ( Math . abs ( vNew - vOld ) > 1e-9 ) { double vProp = vNew - fNew / ( ( fOld - fNew ) / ( vOld - vNew ) ) ; if ( ( vLeft >= vProp ) || ( vProp >= vRight ) ) { vProp = vLeft - fLeft / ( ( fRight - fLeft ) / ( vRight - vLeft ) ) ; } double fProp = conditionalSurvivalProbability ( x , vProp , y , t , m ) - U ; vOld = vNew ; fOld = fNew ; vNew = vProp ; fNew = fProp ; if ( fProp < 0.0 ) { vRight = vProp ; fRight = fProp ; } else { vLeft = vProp ; fLeft = fProp ; } } return vNew ; }
the inverse of the conditionalsurvivalprobability function . returns the survival time until the next migration conditional on starting at state x at time 0 , being in state y at time t and having a survival probability of w .
