Skip to content

Instantly share code, notes, and snippets.

@mrc
Created November 16, 2011 10:05
Show Gist options
  • Save mrc/1369739 to your computer and use it in GitHub Desktop.
Save mrc/1369739 to your computer and use it in GitHub Desktop.
Dinky probability helpers for ai-class
(defun bayes-transform-form (PB!A PA PB)
"Return a form to compute P(A|B)
given P(B|A), P(A) and P(B)."
`(/ (* ,(or PB!A 'PB!A) ,(or PA 'PA))
,(or PB 'PB)))
(defun total-probability-complementary (PA!B PA!notB PB)
"Return a form to compute the total probability of
complementary events P(B) = P(A|B)P(B) + P(A|!B)P(!B)."
`(+ (* ,(or PA!B 'PA!B)
,(or PB 'PB))
(* ,(or PA!notB 'PA!notB)
(- 1,(or PB 'PB)))))
;; AICLASS> (bayes-transform-form
;; 2/5
;; (total-probability-complementary 2/5 9/10 2/5)
;; (total-probability-complementary 3/5 1/5 1/2))
;; ; (/ (* 2/5 (+ (* 3/5 1/2) (* 1/5 (- 1 1/2)))) (+ (* 2/5 2/5) (* 9/10 (- 1 2/5))))
;; AICLASS> (eval *)
;; ; 8/35
@mrc
Copy link
Author

mrc commented Nov 16, 2011

same answer, another session:

;; AICLASS> (defparameter PR0 1/2)
;; ; PR0
;; AICLASS> (defparameter PR!R 3/5)
;; ; PR!R
;; AICLASS> (defparameter PS!S 4/5)
;; ; PS!S
;; AICLASS> (defparameter PH!S 9/10)
;; ; PH!S
;; AICLASS> (defparameter PH!R 2/5)
;; ; PH!R
;; AICLASS> (bayes-transform-form PH!R 'PR1 'PH1)
;; ; (/ (* 2/5 PR1) PH1)
;; AICLASS> (defparameter PR1 (total-probability-complementary PR!R (- 1 PS!S) PR0))
;; ; PR1
;; AICLASS> (defparameter PH1 (total-probability-complementary PH!R PH!S PR1))
;; ; PH1
;; AICLASS> (bayes-transform-form PH!R PR1 PH1)
;; ; (/ (* 2/5 2/5) 7/10)
;; AICLASS> (eval *)
;; ; 8/35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment