public class Probability extends java.lang.Object implements XCloneable, java.lang.Comparable, java.io.Serializable
When working with large numbers of small probabilities, it is helpful to work in "log space", e.g., the natural log of the probabilities, to avoid problems with overflow and underflow. This class provides for storing a probability in log form, and adding, substracting, multiplying, and dividing the log probabilities to minimize accuracy loss.
Probabilities must be floating point values from zero through one inclusive, e.g., [0 , 1].
A probability of zero is stored as -infinity, which correctly propagates through arithmetic operations. The value of exp(-infinity) is 0.0 as it should be.
Modifier and Type | Field and Description |
---|---|
protected double |
logProbability
Natural log of the probability.
|
static Probability |
ONE_PROBABILITY
One probability.
|
static Probability |
ZERO_PROBABILITY
Zero probability.
|
Constructor and Description |
---|
Probability()
Create a Probability.
|
Probability(double probability)
Create a Probability.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
clone()
Get clone of this object.
|
int |
compareTo(java.lang.Object object)
Compare this probability to another.
|
boolean |
equals(java.lang.Object object)
Test for this probability equaling another.
|
double |
getLogProbability()
Get log probability.
|
double |
getProbability()
Get probability.
|
int |
hashCode()
Get a hash code for this object.
|
Probability |
multiply(Probability otherProbability)
Multiply this probability by another.
|
Probability |
multiply(Probability otherProbability1,
Probability otherProbability2)
Multiply this probability by two others.
|
Probability |
multiply(Probability otherProbability1,
Probability otherProbability2,
Probability otherProbability3)
Multiply this probability by three others.
|
void |
setLogProbability(double logProbability)
Set the log probability.
|
void |
setProbability(double probability)
Set the probability.
|
java.lang.String |
toString()
Return string version of probability.
|
protected double logProbability
public static final Probability ZERO_PROBABILITY
public static final Probability ONE_PROBABILITY
public Probability() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public Probability(double probability) throws java.lang.IllegalArgumentException
probability
- Probability value in range 0 through 1
inclusive.java.lang.IllegalArgumentException
public void setProbability(double probability) throws java.lang.IllegalArgumentException
probability
- Probability value in range 0 through 1
inclusive.java.lang.IllegalArgumentException
- If probability is not in the range 0 through 1 inclusive.public void setLogProbability(double logProbability) throws java.lang.IllegalArgumentException
logProbability
- Log probability value in range -infinity
through 0 inclusive.java.lang.IllegalArgumentException
- If probability is not in the range -infinity through 0 inclusive.public double getProbability()
Note: This can overflow!
public double getLogProbability()
public Probability multiply(Probability otherProbability)
otherProbability
- The other probability.public Probability multiply(Probability otherProbability1, Probability otherProbability2)
otherProbability1
- First other probability.otherProbability2
- Second other probability.public Probability multiply(Probability otherProbability1, Probability otherProbability2, Probability otherProbability3)
otherProbability1
- First other probability.otherProbability2
- Second other probability.otherProbability3
- Third other probability.public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.Object clone()
clone
in interface XCloneable
clone
in class java.lang.Object
public int compareTo(java.lang.Object object)
compareTo
in interface java.lang.Comparable
object
- The other probability.public boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- Other object, presumably a Probability.public int hashCode()
hashCode
in class java.lang.Object
Hash code same as for a Double.