public static double cleanProbability ( double prob , double epsilon , boolean allowOne ) { if ( prob < - epsilon || prob > ( 1.0 + epsilon ) ) { throw new InvalidProbabilityException ( prob ) ; } if ( prob < epsilon ) { prob = epsilon ; } else { final double limit = allowOne ? 1.0 : ( 1.0 - epsilon ) ; if ( prob > limit ) { prob = limit ; } } return prob ; }
Cleans up input which should be probabilities . Occasionally due to numerical stability issues you get input which should be a probability but could actually be very slightly less than 0 or more than 1 . 0 . This function will take values within epsilon of being good probabilities and fix them . If the prob is within epsilon of zero it is changed to + epsilon . One the upper end if allowOne is true anything between 1 . 0 and 1 . 0 + epsilon is mapped to 1 . 0 . If allowOne is false anything between 1 . 0 - epsilon and 1 . 0 + epsilon is mapped to 1 . 0 - epsilon . All other probability values throw an unchecked InvalidProbabilityException .