John Collins

John Collins

Developer

© 2024

Bayes Theorem

Consider a classic example of Bayes rule. Suppose we have a patient who has been tested for a particular desease and the test came out positive. We know that one in every thousand people has this disease given that the test came out positive. If we let the positional variable D stand for “the patient has the disease” and the propositional variable T stand for “the test came out positive”, out goal is then to compute Pr(D | T).

(defpdf BayesExample [^{:range [:sick :fine]} Patient
                      ^{:range [:pos :neg]} Result])

(def bayes-example
  (BayesExample [:sick :pos (* 1/1000   95/100)  ;; correct-positive
                 :sick :neg (* 1/1000   5/100)   ;; false-negative
                 :fine :pos (* 999/1000 2/100)   ;; false-positive.
                 :fine :neg (* 999/1000 98/100)  ;; correct-negative.
                 ]))

(def Is Equal)

(assert (= (Pr (Given (Is :Patient :sick)
                      (Is :Result :pos))
               bayes-example)
           95/2093))

We know that there is a 1/1000 chance that the patient has the disease and we know the test is unreliable; it has a false positive rate of 2% and a false negative rate of 5%.

(def bayes-example
  (BayesExample [:sick :pos (* 1/1000   95/100)  ;; correct-positive
                 :sick :neg (* 1/1000   5/100)   ;; false-negative
                 :fine :pos (* 999/1000 2/100)   ;; false-positive.
                 :fine :neg (* 999/1000 98/100)  ;; correct-negative.
                 ]))

(def Is Equal)

(assert (= (Pr (Given (Is :Patient :sick)
                      (Is :Result :pos))
               bayes-example)
           95/2093))