Consider the following sets of probabilities (the three events are NOT mutually exclusive):
- 0.05625 success, 0.94375 failure
- 0.05625 success, 0.94375 failure
- 0.05625 success, 0.94375 failure
How do I compute the probability of at least one event happening (i.e. the union)?
If possible I would prefer a generic, self-contained solution that could also handle 4 or more events. In this case the answer I'm looking for is:
0.05625 + 0.05625 + 0.05625 -
0.05625*0.05625 - 0.05625*0.05625 - 0.05625*0.05625 +
0.05625*0.05625*0.05625
##[1] 0.1594358
My question is ultimately a bit broader than the title, as I'm looking for functions that can compute the probability of union, intersection (0.05625*0.05625*0.05625 = 0.0001779785
), no event happening (1 - 0.1594358 = 0.8405642
) or exactly one event happening (0.150300
). In other words an R solution to this on-line Conjunction of three events calculator. I've already looked into the prob
package, but it seems to have an interface too complicated for such a simplistic use-case.
Equal Probabilities
You can get the probability of exactly 0, 1, 2, or 3 of these occurring with the binomial density function
dbinom
, which returns the probability of getting exactly the number of specified successes (first argument) given the total number of independent attempts (second argument) and the probability of success for each attempt (third argument):So if you want the probability of at least one happening, that is:
or
The
dbinom
function can address your other questions as well. For instance, the probability of all happening is:The probability of exactly one is:
The probability of none is:
Unequal Probabilities -- Some Easy Cases
If you have unequal probabilities stored in a vector
p
and each item is selected independently, you need to do a bit more work, because thedbinom
function doesn't apply. Still, some of the computations are pretty simple.The probability of none of the items being selected is simply the product of 1 minus the probabilities (the probability that at least one is selected is simply one minus this):
The probability of all is the product of the probabilities:
Finally, the probability of exactly one being selected is the sum across all the elements of its probability times the probability of all the other elements not being selected:
Similarly, the probability of exactly
n-1
being selected (wheren
is the number of probabilities) is:Unequal Probabilities -- Complete Case
If you want the probability of every single possible count of successes, one option could be computing all
2^n
event combinations (which is what A. Webb does in their answer). Instead, the following is a O(n^2) scheme:Basically, we define P_ij to be the probability that we have exactly
i
successes, all of which are in positionj
or greater. Base cases fori=0
andi=1
are relatively simple to compute, and then we have the following recurrence:In the function
cp.quadratic
, we loop with increasingi
, filling out theP
matrix (which isn
xn
). Therefore the total operation count is O(n^2).This enables you, for instance, to compute the distribution for pretty large numbers of options in under a second:
We can compute the distribution from 1,000 elements in a fraction of a second and from 10,000 elements in under a minute; computing 2^1000 or 2^10000 possible outcomes would take a prohibitively long time (the number of subsets are a 301-digit and 3010-digit number, respectively).