Class Probability
- All Implemented Interfaces:
Serializable,Comparable<Probability>
- Author:
- Isaac Lefebvre
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleThe maximum value for probabilities, corresponding to an event that always happens.static final doubleThe maximum value for log-odds, corresponding to an event that always happens.static final doubleThe maximum value for log-probabilities, corresponding to an event that always happens.static final doubleThe maximum value for odds, corresponding to an event that always happens.static final doubleThe maximum value for odds, corresponding to an event that always happens.static final doubleThe minimum value for probabilities, corresponding to an event that never happens.static final doubleThe minimum value for log-odds, corresponding to an event that never happens.static final doubleThe minimum value for log-probabilities, corresponding to an event that never happens.static final doubleThe minimum value for odds, corresponding to an event that never happens.static final doubleThe minimum value for probits, corresponding to an event that never happens. -
Constructor Summary
ConstructorsConstructorDescriptionProbability(double value) Constructs a newly allocated Probability object from the given value. -
Method Summary
Modifier and TypeMethodDescriptionadd(Probability addend) Returns the sum between the current probability and the given probability.booleanapply()Returnstrueat random, with a chance matching the value it holds.<T> booleanapply(T t) static Probabilitycertain()Returns a probability corresponding to a certain event.static intcompare(Probability p1, Probability p2) Compares thevalue()of the two given probabilities, usingDouble.compare(double, double).intcompareTo(Probability probability) Compares thevalue()of the caller Probability with that of the given Probability, usingDouble.compareTo(Double).Assuming the current Probability corresponds to an event E, this returns the probability of the complement event not(E).divide(Probability divisor) Returns the division between the current probability and the given probability.booleanReturns whether this probability is equal to the given obj.static ProbabilityfromLogOdds(double logOdds) Converts the given log-odds to a probability.static ProbabilityfromLogOdds(double logOdds, double base) Converts the given log-odds to a probability, using a specified logarithm base.static ProbabilityfromLogProbability(double logProba) Converts the given log-probability to a probability.static ProbabilityfromLogProbability(double logProba, double base) Converts the given log-probability to a probability, using a specified logarithm base.static ProbabilityfromOdds(double odds) Converts the given odds to a probability.static ProbabilityfromProbit(double probit) Converts the given probit to a probability.static ProbabilityReturns a probability corresponding to an impossible event.booleanReturns whether this probability corresponds to a certain event.booleanReturns whether this probability corresponds to an impossible event.protected static SecureRandomCreates and returns one of the strongest possible instances of SecureRandom.multiply(Probability factor) Returns the product between the current probability and the given probability.pow(double exponent) Returns the current probability raised to the power of the given exponent.booleanReturnstrueat random, with a chance matching the value it holds.subtract(Probability subtrahend) Returns the subtraction between the current probability and the given probability.doubleConverts a probability into log-odds.doubletoLogOdds(double base) Converts a probability into log-odds, using a specified logarithm base.doubleConverts a probability into a log-probability.doubletoLogProbability(double base) Converts a probability into a log-probability, using a specified logarithm base.doubletoOdds()Converts a probability into odds.doubletoProbit()Converts a probability into a probit.toString()Returns a String representation of this probability.doublevalue()Access the value of the probability.
-
Field Details
-
IMPOSSIBLE
public static final double IMPOSSIBLEThe minimum value for probabilities, corresponding to an event that never happens.- See Also:
-
CERTAIN
public static final double CERTAINThe maximum value for probabilities, corresponding to an event that always happens.- See Also:
-
IMPOSSIBLE_ODDS
public static final double IMPOSSIBLE_ODDSThe minimum value for odds, corresponding to an event that never happens.- See Also:
-
CERTAIN_ODDS
public static final double CERTAIN_ODDSThe maximum value for odds, corresponding to an event that always happens.- See Also:
-
IMPOSSIBLE_LOGPROBA
public static final double IMPOSSIBLE_LOGPROBAThe minimum value for log-probabilities, corresponding to an event that never happens. True for a logarithm base greater than 1. For a base smaller than 1, the values are inverted.- See Also:
-
CERTAIN_LOGPROBA
public static final double CERTAIN_LOGPROBAThe maximum value for log-probabilities, corresponding to an event that always happens. True for a logarithm base greater than 1. For a base smaller than 1, the values are inverted.- See Also:
-
IMPOSSIBLE_LOGODDS
public static final double IMPOSSIBLE_LOGODDSThe minimum value for log-odds, corresponding to an event that never happens. True for a logarithm base greater than 1. For a base smaller than 1, the values are inverted.- See Also:
-
CERTAIN_LOGODDS
public static final double CERTAIN_LOGODDSThe maximum value for log-odds, corresponding to an event that always happens. True for a logarithm base greater than 1. For a base smaller than 1, the values are inverted.- See Also:
-
IMPOSSIBLE_PROBIT
public static final double IMPOSSIBLE_PROBITThe minimum value for probits, corresponding to an event that never happens.- See Also:
-
CERTAIN_PROBIT
public static final double CERTAIN_PROBITThe maximum value for odds, corresponding to an event that always happens.- See Also:
-
-
Constructor Details
-
Probability
public Probability(double value) Constructs a newly allocated Probability object from the given value.- Parameters:
value- - a valid number between 0 and 1 included.
-
-
Method Details
-
fromOdds
Converts the given odds to a probability.- Parameters:
odds- - a valid number between 0 and +∞ included.- Returns:
- a new Probability instance converted from odds.
-
fromLogProbability
Converts the given log-probability to a probability. Natural logarithm (base e) is assumed.- Parameters:
logProba- - a valid number between -∞ and 0 included.- Returns:
- a new Probability instance converted from logProba.
- See Also:
-
fromLogProbability
Converts the given log-probability to a probability, using a specified logarithm base.- Parameters:
logProba- - a valid number between -∞ and 0 if base is greater than 1, between 0 and +∞ otherwise.base- - the base of the logarithm, a valid positive number except 1.- Returns:
- a new Probability instance converted from logProba.
- See Also:
-
fromLogOdds
Converts the given log-odds to a probability.- Parameters:
logOdds- - a valid number between -∞ and +∞ included.- Returns:
- a new Probability instance converted from logOdds.
- See Also:
-
fromLogOdds
Converts the given log-odds to a probability, using a specified logarithm base.- Parameters:
logOdds- - a valid number between -∞ and +∞ included.base- - the base of the logarithm, a valid positive number except 1.- Returns:
- a new Probability instance converted from logOdds.
- See Also:
-
fromProbit
Converts the given probit to a probability.- Parameters:
probit- - a valid number between -∞ and +∞ included.- Returns:
- a new Probability instance converted from probit.
-
impossible
Returns a probability corresponding to an impossible event.- Returns:
- a Probability instance corresponding to an event that never occurs.
-
certain
Returns a probability corresponding to a certain event.- Returns:
- a Probability instance corresponding to an event that always occurs.
-
makeSecureRandom
Creates and returns one of the strongest possible instances of SecureRandom.- Returns:
- A SecureRandom instance, as strong as possible.
-
value
public double value()Access the value of the probability.- Returns:
- a double from 0 to 1 included.
-
complement
Assuming the current Probability corresponds to an event E, this returns the probability of the complement event not(E).
Example :
pis the probability of getting an even number when throwing a dice. thenp.complement()is the probability of getting an odd number.- Returns:
- P(¬E) as a new Probability instance.
-
add
Returns the sum between the current probability and the given probability.- Parameters:
addend- - another Probability instance.- Returns:
- a Probability intance containing the sum between the values of the caller and of the given Probability.
-
subtract
Returns the subtraction between the current probability and the given probability.- Parameters:
subtrahend- - another Probability instance.- Returns:
- a Probability instance containing the value of the caller minus that of the given Probability.
-
multiply
Returns the product between the current probability and the given probability.- Parameters:
factor- - another Probability instance.- Returns:
- a Probability instance containing the product between the values of the caller and of the given Probability.
-
divide
Returns the division between the current probability and the given probability.- Parameters:
divisor- - another Probability instance.- Returns:
- a Probability instance containing the value of the caller divided by that of the given Probability.
-
pow
Returns the current probability raised to the power of the given exponent.- Parameters:
exponent- - the exponent.- Returns:
- a Probability instance containing the value of the caller Probability, raised to the power of the given exponent.
-
apply
public boolean apply()Returns
trueat random, with a chance matching the value it holds.Example:
Probability p = new Probability(0.5); if (p.apply()) { // this block has a 50% probability of being executed }Uses
ThreadLocalRandom.Not cryptographically secure! For these cases, use
secureApply()instead.- Returns:
truerandomly with a probability ofvalue(),falsein all other cases.
-
apply
public <T> boolean apply(T t) A
Predicatethat takes whatever object/value, and returns the result ofapply().For use in
Stream.filter():Probability p = new Probability(0.5); int[] randomInts = IntStream.range(0, 1000) .filter(p::apply) .toArray();- Type Parameters:
T- - the runtime type of t.- Parameters:
t- - the input argument.- Returns:
- a random boolean, independent from the input.
-
secureApply
public boolean secureApply()Returns
trueat random, with a chance matching the value it holds.Cryptographically secure using
SecureRandom.May block as entropy is being gathered. If cryptographic safety isn't required, use
apply()for better performance.- Returns:
truerandomly with a probability ofvalue(),falsein all other cases.
-
isImpossible
public boolean isImpossible()Returns whether this probability corresponds to an impossible event.- Returns:
trueif the caller Probability corresponds to an event that never occurs;falseotherwise.
-
isCertain
public boolean isCertain()Returns whether this probability corresponds to a certain event.- Returns:
trueif the caller Probability corresponds to an event that always occurs;falseotherwise.
-
toOdds
public double toOdds()Converts a probability into odds.- Returns:
- the caller Probability converted into odds, as a
double.
-
toLogProbability
public double toLogProbability()Converts a probability into a log-probability.- Returns:
- the caller Probability converted into a log-probability, as a
double.
-
toLogProbability
public double toLogProbability(double base) Converts a probability into a log-probability, using a specified logarithm base.- Parameters:
base- - the base of the logarithm, a valid positive number except 1.- Returns:
- the caller Probability converted into odds, as a
double.
-
toLogOdds
public double toLogOdds()Converts a probability into log-odds.- Returns:
- the caller Probability converted into log-odds, as a
double.
-
toLogOdds
public double toLogOdds(double base) Converts a probability into log-odds, using a specified logarithm base.- Parameters:
base- - the base of the logarithm, a valid positive number except 1.- Returns:
- the caller Probability converted into log-odds, as a
double.
-
toProbit
public double toProbit()Converts a probability into a probit.- Returns:
- the caller Probability converted into a probit, as a
double.
-
compareTo
Compares thevalue()of the caller Probability with that of the given Probability, usingDouble.compareTo(Double).- Specified by:
compareToin interfaceComparable<Probability>- Parameters:
probability- - the Probability to be compared.- Returns:
- 0 if the values are numerically identical; a value less than 0 if the caller is less than probability; and a value greater than 0 if the caller is greater than probability.
-
compare
Compares thevalue()of the two given probabilities, usingDouble.compare(double, double).- Parameters:
p1- - the first Probability to compare.p2- - the second Probability to compare.- Returns:
- 0 if the values are numerically identical; a value less than 0 if p1 is less than p2; and a value greater than 0 if p1 is greater than p2.
-
equals
Returns whether this probability is equal to the given obj. -
toString
Returns a String representation of this probability.
-