Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/swipl -f -q
- % This is a PROLOG translation of the examples in the paper
- % "Teaching an Application of Bayes' Rule for Legal Decision-Making: Measuring the Strength of Evidence"
- % Source: http://www.amstat.org/publications/jse/v22n1/satake.pdf
- % It uses Ivan Bratko's interpreter for belief networks, figure 15.11,
- % from the book "Prolog Programming for Artificial Intelligence"
- % Source: http://media.pearsoncmg.com/intl/ema/ema_uk_he_bratko_prolog_3/prolog/ch15/fig15_11.txt
- % That program should be renamed fig15_11.pl and put in the same directory to load properly.
- % Additionally, if you're using a recent version of SWI Prolog, then
- % change the three occurrences (on lines 31, 33, 67) of "not X" (or "not Y") in
- % fig15_11.pl to "not(X)" (or "not(Y)" respectively). And change "Cond" on line 49
- % to an underbar "_" to avoid the "Singleton variables" warning.
- :- [fig15_11].
- % Setup the belief network...
- parent(guilty, evidence1).
- parent(evidence1, evidence2).
- parent(evidence2, evidence3).
- % 3.1.1 Calculation of Posterior Probability after Evidence 1, p. 10
- p(guilty, 0.5).
- p(evidence1, [guilty], 1.0).
- p(evidence1, [not(guilty)], 0.45).
- % 3.2.1 Calculation of Posterior Probability after the Second Piece of Evidence, p. 11
- p(evidence2, [guilty, evidence1], 1.0).
- p(evidence2, [not(guilty), evidence1], 0.21).
- % 3.3.1 Calculation of Posterior Probability after Evidence 3, p. 12
- p(evidence3, [guilty, evidence1, evidence2], 1.0).
- p(evidence3, [not(guilty), evidence1, evidence2], 0.17).
- % Run queries...
- :- initialization main.
- main :-
- prob(not(guilty), [], P1), % Should be 0.5
- format('Pr(~~guilty) = ~f (should be 0.5)~n', [P1]),
- prob(guilty, [evidence1], P2), % Should be 0.69
- format('Pr(guilty|evidence1) = ~f (should be 0.69)~n', [P2]),
- prob(not(guilty), [evidence1], P3), % Should be 0.31
- format('Pr(~~guilty|evidence1) = ~f (should be 0.31)~n', [P3]),
- prob(guilty, [evidence1, evidence2], P4), % Should be 0.913
- format('Pr(guilty|evidence1, evidence2) = ~f (should be 0.914)~n', [P4]),
- prob(not(guilty), [evidence1, evidence2], P5), % Should be 0.086
- format('Pr(~~guilty|evidence1,evidence2) = ~f (should be 0.086)~n', [P5]),
- prob(guilty, [evidence1, evidence2, evidence3], P6),% Should be 0.984
- format('Pr(guilty|evidence1, evidence2, evidence3) = ~f (should be 0.984)~n', [P6]),
- prob(not(guilty), [evidence1, evidence2, evidence3], P7),
- format('Pr(~~guilty|evidence1, evidence2, evidence3) = ~f (should be 0.016)~n', [P7]),
- halt.
- /*
- Running this script in bash with swipl 6.6.6 produces the following output...
- l@ubuntu:~/work/Prolog$ ./bayes_legal.pl
- Pr(~guilty) = 0.500000 (should be 0.5)
- Pr(guilty|evidence1) = 0.689655 (should be 0.69)
- Pr(~guilty|evidence1) = 0.310345 (should be 0.31)
- Pr(guilty|evidence1, evidence2) = 0.913659 (should be 0.914)
- Pr(~guilty|evidence1,evidence2) = 0.086341 (should be 0.086)
- Pr(guilty|evidence1, evidence2, evidence3) = 0.984189 (should be 0.984)
- Pr(~guilty|evidence1, evidence2, evidence3) = 0.015811 (should be 0.016)
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement