Changed implementation to return correct inverse probabilities for p=0,1 (per discussion on commons-dev).