Class Combinatorics
- java.lang.Object
-
- ca.cgjennings.math.Combinatorics
-
public class Combinatorics extends java.lang.Object
Basic combinatoric mathematics support.- Author:
- Chris Jennings
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
combinations(int n, int r)
Return C(n, r), the number of sets of sizer
that can be created from a set ofn
objects.static double
cumulativeDistribution(int successes, int trials, double pSuccess)
Returns the probability that out oftrials
attempts, a test that has apSuccess
probability of succeeding on each attempt will succeed at mostsuccesses
times.static long
factorial(int n)
Returns n! as along
value.static long
permutations(int n, int r)
Return P(n, r), the number of lists of sizer
that can be created from a set ofn
objects.static double
probabilityMass(int successes, int trials, double pSuccess)
Returns the probability that out oftrials
attempts, a test that has apSuccess
probability of succeeding on each attempt will succeed exactlysuccesses
times.static double
upperCumulativeDistribution(int successes, int trials, double pSuccess)
Returns the probability that out oftrials
attempts, a test that has apSuccess
probability of succeeding on each attempt will succeed at leastsuccesses
times.
-
-
-
Method Detail
-
factorial
public static final long factorial(int n)
Returns n! as along
value. In order to fit within along
, the value ofn
must not be greater thanMAXIMUM_LONG_FACTORIAL
.- Parameters:
n
- the value to compute the factorial of- Returns:
- the factorial of
n
, that is,n
! - Throws:
java.lang.ArrayIndexOutOfBoundsException
- ifn
< 0 orn
> MAXIMUM_LONG_FACTORIAL
-
permutations
public static final long permutations(int n, int r)
Return P(n, r), the number of lists of sizer
that can be created from a set ofn
objects. Since this method counts the number of unique lists, the order in which the elements are selected is important. For example, the selection (1,2,3) and the selection (3,1,2) are both counted by this method. In order to fit within along
, the value ofn
must not be greater thanMAXIMUM_LONG_FACTORIAL
.Example: The set {1,2,3} contains 3 elements. If we construct every possible list of length 2 that is composed of elements from that set, we would find that there are 6 possibilities:
(1,2), (1,3), (2,1), (2,3), (3,1) and (3,2)Hence,
permutations( 3, 2 )
would return 6.- Parameters:
n
- the size of the set from which the elements of lists are to be takenr
- the size of the lists to created- Returns:
- the number of unique lists that can be created
- Throws:
java.lang.ArrayIndexOutOfBoundsException
- ifn
< 0,n
> MAXIMUM_LONG_FACTORIAL,r
< 0, orr
>n
-
combinations
public static final long combinations(int n, int r)
Return C(n, r), the number of sets of sizer
that can be created from a set ofn
objects. Since this method counts the number of unique sets, the order in which the elements are selected is not important. For example, the selection (1,2,3) and the selection (3,1,2) would only be counted once by this method. In order to fit within along
, the value ofn
must not be greater thanMAXIMUM_LONG_FACTORIAL
.Example: The set {1,2,3} contains 3 elements. If we construct every possible set of length 2 that is composed of elements from that set, we would find that there are 3 possibilities:
{1,2}, {1,3}, {2,3}Hence,
combinations( 3, 2 )
would return 3.- Parameters:
n
- the size of the set from which the elements of lists are to be takenr
- the size of the lists to created- Returns:
- the number of unique lists that can be created
- Throws:
java.lang.ArrayIndexOutOfBoundsException
- ifn
< 0,n
> MAXIMUM_LONG_FACTORIAL,r
< 0, orr
>n
-
probabilityMass
public static final double probabilityMass(int successes, int trials, double pSuccess)
Returns the probability that out oftrials
attempts, a test that has apSuccess
probability of succeeding on each attempt will succeed exactlysuccesses
times. For example, the probability of rolling 10 dice and having exactly 2 of those dice land on 6 isprobabilityMass( 2, 10, 1/6d )
(about 0.29).- Parameters:
successes
- the exact number of successes requiredtrials
- the number of trials to attemptpSuccess
- the probability that a given trial will succeed- Returns:
- the probability that exactly
successes
trials will succeed - Throws:
java.lang.IllegalArgumentException
- ifpSuccess
is not in the range (0,1)java.lang.ArrayIndexOutOfBoundsException
- ifn
< 0,n
> MAXIMUM_LONG_FACTORIAL,r
< 0, orr
>n
-
cumulativeDistribution
public static final double cumulativeDistribution(int successes, int trials, double pSuccess)
Returns the probability that out oftrials
attempts, a test that has apSuccess
probability of succeeding on each attempt will succeed at mostsuccesses
times. For example, the probability of rolling 10 dice and having at most 2 of those dice land on 6 iscumulativeDistribution( 2, 10, 1/6d )
(about 0.78).- Parameters:
successes
- the maximum number of successes allowedtrials
- the number of trials to attemptpSuccess
- the probability that a given trial will succeed- Returns:
- the probability that at most
successes
trials will succeed - Throws:
java.lang.IllegalArgumentException
- ifpSuccess
is not in the range (0,1)java.lang.ArrayIndexOutOfBoundsException
- ifn
< 0,n
> MAXIMUM_LONG_FACTORIAL,r
< 0, orr
>n
-
upperCumulativeDistribution
public static final double upperCumulativeDistribution(int successes, int trials, double pSuccess)
Returns the probability that out oftrials
attempts, a test that has apSuccess
probability of succeeding on each attempt will succeed at leastsuccesses
times. For example, the probability of rolling 10 dice and having at least 2 of those dice land on 6 isupperCumulativeDistribution( 2, 10, 1/6d )
(about 0.52).Mathematically, this value can be computed as:
upperCumulativeDistribution(s,t,p) = probabilityMass(s,t,p) + 1-cumulativeDistribution(s,t,p)
Note that this method may not compute the value in exactly this way, and so may return a slightly different result if comprared to the above.
- Parameters:
successes
- the minimum number of successes requiredtrials
- the number of trials to attemptpSuccess
- the probability that a given trial will succeed- Returns:
- the probability that at least
successes
trials will succeed - Throws:
java.lang.IllegalArgumentException
- ifpSuccess
is not in the range (0,1)java.lang.ArrayIndexOutOfBoundsException
- ifn
< 0,n
> MAXIMUM_LONG_FACTORIAL,r
< 0, orr
>n
-
-