Advertisement
krusader74

bayes_legal.pl

Nov 17th, 2014
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 3.13 KB | None | 0 0
  1. #!/usr/bin/swipl -f -q
  2.  
  3. % This is a PROLOG translation of the examples in the paper
  4. % "Teaching an Application of Bayes' Rule for Legal Decision-Making: Measuring the Strength of Evidence"
  5. % Source: http://www.amstat.org/publications/jse/v22n1/satake.pdf
  6.  
  7. % It uses Ivan Bratko's interpreter for belief networks, figure 15.11,
  8. % from the book "Prolog Programming for Artificial Intelligence"
  9. % Source: http://media.pearsoncmg.com/intl/ema/ema_uk_he_bratko_prolog_3/prolog/ch15/fig15_11.txt
  10. % That program should be renamed fig15_11.pl and put in the same directory to load properly.
  11. % Additionally, if you're using a recent version of SWI Prolog, then
  12. % change the three occurrences (on lines 31, 33, 67) of "not X" (or "not Y") in
  13. % fig15_11.pl to "not(X)" (or "not(Y)" respectively). And change "Cond" on line 49
  14. % to an underbar "_" to avoid the "Singleton variables" warning.
  15. :- [fig15_11].
  16.  
  17. % Setup the belief network...
  18. parent(guilty, evidence1).
  19. parent(evidence1, evidence2).
  20. parent(evidence2, evidence3).
  21.  
  22. % 3.1.1 Calculation of Posterior Probability after Evidence 1, p. 10
  23. p(guilty, 0.5).
  24. p(evidence1, [guilty], 1.0).
  25. p(evidence1, [not(guilty)], 0.45).
  26.  
  27. % 3.2.1 Calculation of Posterior Probability after the Second Piece of Evidence, p. 11
  28. p(evidence2, [guilty, evidence1], 1.0).
  29. p(evidence2, [not(guilty), evidence1], 0.21).
  30.  
  31. % 3.3.1 Calculation of Posterior Probability after Evidence 3, p. 12
  32. p(evidence3, [guilty, evidence1, evidence2], 1.0).
  33. p(evidence3, [not(guilty), evidence1, evidence2], 0.17).
  34.  
  35. % Run queries...
  36. :- initialization main.
  37.  
  38. main :-
  39.   prob(not(guilty), [], P1),                          % Should be 0.5
  40.   format('Pr(~~guilty) = ~f (should be 0.5)~n', [P1]),
  41.   prob(guilty, [evidence1], P2),                      % Should be 0.69
  42.   format('Pr(guilty|evidence1) = ~f (should be 0.69)~n', [P2]),
  43.   prob(not(guilty), [evidence1], P3),                 % Should be 0.31
  44.   format('Pr(~~guilty|evidence1) = ~f (should be 0.31)~n', [P3]),
  45.   prob(guilty, [evidence1, evidence2], P4),           % Should be 0.913
  46.   format('Pr(guilty|evidence1, evidence2) = ~f (should be 0.914)~n', [P4]),
  47.   prob(not(guilty), [evidence1, evidence2], P5),      % Should be 0.086
  48.   format('Pr(~~guilty|evidence1,evidence2) = ~f (should be 0.086)~n', [P5]),
  49.   prob(guilty, [evidence1, evidence2, evidence3], P6),% Should be 0.984
  50.   format('Pr(guilty|evidence1, evidence2, evidence3) = ~f (should be 0.984)~n', [P6]),
  51.   prob(not(guilty), [evidence1, evidence2, evidence3], P7),
  52.   format('Pr(~~guilty|evidence1, evidence2, evidence3) = ~f (should be 0.016)~n', [P7]),
  53.   halt.
  54.  
  55. /*
  56.    Running this script in bash with swipl 6.6.6 produces the following output...
  57.  
  58.    l@ubuntu:~/work/Prolog$ ./bayes_legal.pl
  59.    Pr(~guilty) = 0.500000 (should be 0.5)
  60.    Pr(guilty|evidence1) = 0.689655 (should be 0.69)
  61.    Pr(~guilty|evidence1) = 0.310345 (should be 0.31)
  62.    Pr(guilty|evidence1, evidence2) = 0.913659 (should be 0.914)
  63.    Pr(~guilty|evidence1,evidence2) = 0.086341 (should be 0.086)
  64.    Pr(guilty|evidence1, evidence2, evidence3) = 0.984189 (should be 0.984)
  65.    Pr(~guilty|evidence1, evidence2, evidence3) = 0.015811 (should be 0.016)
  66.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement