Stephen H. Anderson
Stephen H. Anderson

Reputation: 1068

Compute probability of union of n events from array

I have a PHP array containing N probabilities (of N different events). So it looks like:

$events = [0.85, 1, 0, 0.15, 0.49, 1, 1, 0, 0, 0.59, 0.93]

where each value in the array is the probability of an event. I need to compute the probability of the union of those events. That is, how do I compute the probability of at least one event happening ?

Upvotes: 0

Views: 595

Answers (1)

Nick
Nick

Reputation: 147206

To calculate the probability of at least one event occurring, you must calculate the probability of no events occurring, which is the product of (1 - p[event]) for each event and then subtract that from 1. You can do that with this code:

$events = [0.85, 1, 0, 0.15, 0.49, 1, 1, 0, 0, 0.59, 0.93];
$prob = 1.0;
foreach ($events as $event) {
    $prob *= (1.0 - $event);
}
echo "probability of at least one event is " . (1 - $prob) . "\n";

Output:

probability of at least one event is 1

Note that since the probability of some of your events is 1 (i.e. they are certain to happen), the probability of at least one event occurring is also 1.

Upvotes: 4

Related Questions