statistics - Using php to calculate odds for events to happen one or more times with different odds -
i got set of different events, own odds happen. there way calculate odds them combined, odds 0, 1 2 , on happen.
the math easy, number of calculations grow quick, hoping there function me.
example 3 factors :
event | yes | no | 3% | 97% b | 4% | 96% c | 5% | 95%
0 happening : $a[no] * $ b[no] * $c[no]
1 happening : $a[yes] * $ b[no] * $c[no] + $a[no] * $ b[yes] * $c[no] + $a[no] * $ b[no] * $c[yes]
2 happening = $a[yes] * $ b[yes] * $c[no] + $a[yes] * $ b[no] * $c[yes] + $a[no] * $ b[yes] * $c[yes]
3 happening : $a[yes] * $ b[yes] * $c[yes]
this easy write in php, problem how scale if add more events. adding 1 more doubles number of caclulations, , code long.
so there easier way this? i'll grateful tips or ideas.
this quite slow implementation.
let's consider case 5 events.
odds of 0 events happening is:
$no[0] * $no[1] * $no[2] * $no[3] * $no[4]
odds of 1 event happening is:
$no[0] * $no[1] * $no[2] * $no[3] * $yes[4] + $no[0] * $no[1] * $no[2] * $yes[3] * $no[4] + $no[0] * $no[1] * $yes[2] * $no[3] * $no[4] + ...
where go through multiplications there 1 'yes' choice.
odds of 2 events happening is:
$no[0] * $no[1] * $no[2] * $yes[3] * $yes[4] + $no[0] * $no[1] * $yes[2] * $no[3] * $yes[4] + $no[0] * $no[1] * $yes[2] * $yes[3] * $no[4] + ...
where go through multiplications there 2 'yes' choices.
this can generalized: calculate odds of n events happening go through multiplications there n 'yes' choices.
now when need calculate odds 0 5 events happening, need go through possible combinations of yes/no choices , add each multiplication $odds[$yescount]
.
$no[0] * $no[1] * $no[2] * $no[3] * $no[4] ; added $odds[0] $no[0] * $no[1] * $no[2] * $no[3] * $yes[4] ; added $odds[1] $no[0] * $no[1] * $no[2] * $yes[3] * $no[4] ; added $odds[1] $no[0] * $no[1] * $no[2] * $yes[3] * $yes[4] ; added $odds[2] $no[0] * $no[1] * $yes[2] * $no[3] * $no[4] ; added $odds[1] ... $yes[0] * $yes[1] * $yes[2] * $yes[3] * $yes[4] ; added $odds[5]
there total of 2**5 = 32
different multiplications here, or 2**$eventcount
.
it easy go through these cases if assign number each case 0
2**$eventcount-1
, , use bits of number select whether 'yes' or 'no' choice of each event included in multiplication, , add each multiplication result $odds[$yescount]
:
// number of events $eventcount = 5; // odds of each event happening $yes = [ 0.10, 0.50, 0.32, 0.66, 0.99 ]; // odds of each event not happening $no = []; ($eventnumber = 0; $eventnumber < $eventcount; $eventnumber++) { $no[$eventnumber] = 1 - $yes[$eventnumber]; } // initialize combined $odds 0 $odds = []; ($n = 0; $n <= $eventcount; $n++) { $odds[$n] = 0; } // calculate combined odds ($case = 0; $case < 2 ** $eventcount; $case++) { $multiplication = 1; $yescount = 0; ($eventnumber = 0; $eventnumber < $eventcount; $eventnumber++) { if ($case & (1 << $eventnumber)) { $yescount++; $multiplication *= $yes[$eventnumber]; } else { $multiplication *= $no[$eventnumber]; } } $odds[$yescount] += $multiplication; } // show combined odds ($n = 0; $n <= $eventcount; $n++) { echo "odds of " . $n . " events happening " . $odds[$n] . "<br>\n"; }
Comments
Post a Comment