Search code examples
prologprobability

Probability of a disjunction on N dependent events in Prolog


Does anybody know where to find a Prolog algorithm for computing the probability of a disjunction for N dependent events? For N = 2 i know that P(E1 OR E2) = P(E1) + P(E2) - P(E1) * P(E2), so one could do:

prob_disjunct(E1, E2, P):- P is E1 + E2 - E1 * E2

But how can this predicate be generalised to N events when the input is a list? Maybe there is a package which does this?

Kinds regards/JCR


Solution

  • The recursive formula from Robert Dodier's answer directly translates to

    p_or([], 0).
    p_or([P|Ps], Or) :-
        p_or(Ps, Or1),
        Or is P + Or1*(1-P).
    

    Although this works fine, e.g.

    ?- p_or([0.5,0.3,0.7,0.1],P).
    P = 0.9055
    

    hardcore Prolog programmers can't help noticing that the definition isn't . This would really only be a problem when you are processing very long lists, but since the order of list elements doesn't matter, it is easy to turn things around. This is a standard technique, using an auxiliary predicate and an "accumulator pair" of arguments:

    p_or(Ps, Or) :-
        p_or(Ps, 0, Or).
    
    p_or([], Or, Or).
    p_or([P|Ps], Or0, Or) :-
        Or1 is P + Or0*(1-P),
        p_or(Ps, Or1, Or).       % tail-recursive call