;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: uncertainty/algorithms/probability.lisp

;;;; Probability Distributions and Inference by Enumeration of the Joint

;;; A PROB-DIST is the distribution of one discrete random variable [Section
;;; 12.2]: its table is an alist ((value . probability) ...), kept in the
;;; order in which the values were added (see random-variables.lisp for the
;;; functions on bare alists).  A JOINT-PROB-DIST is the full joint
;;; distribution of several variables: its table maps each list of values,
;;; one per variable in the order of VARIABLES, to its probability.

(defstruct (prob-dist (:constructor create-prob-dist))
  (variable nil)
  (table nil))

(defun make-prob-dist (&optional variable frequencies)
  "A distribution of VARIABLE, from an alist of (value . frequency) pairs,
  which are normalized."
  (create-prob-dist :variable variable
		    :table (when frequencies
			     (normalize-distribution (copy-alist frequencies)))))

(defun prob-dist-probability (dist value)
  "P(variable = VALUE), 0 for a value that is not in the table."
  (probability-of value (prob-dist-table dist)))

(defun prob-dist-set (dist value p)
  "Set the probability (or the frequency) of VALUE, without normalizing."
  (let ((pair (assoc value (prob-dist-table dist) :test #'equal)))
    (if pair
	(setf (cdr pair) p)
      (setf (prob-dist-table dist)
	    (append (prob-dist-table dist) (list (cons value p))))))
  dist)

(defun prob-dist-normalize (dist)
  "Make the probabilities of DIST sum to 1, and return DIST."
  (setf (prob-dist-table dist) (normalize-distribution (prob-dist-table dist)))
  dist)

(defun prob-dist-values (dist)
  (mapcar #'car (prob-dist-table dist)))

;;;; The Full Joint Distribution

(defstruct joint-prob-dist
  (variables nil)
  (table nil))     ; alist (values-list . probability)

(defun joint-prob-dist-probability (joint values)
  "The probability of the atomic event that gives VALUES to the variables."
  (or (cdr (assoc values (joint-prob-dist-table joint) :test #'equal)) 0))

(defun joint-prob-dist-variable-values (joint variable)
  "The values of VARIABLE that appear in the table, in order of appearance."
  (let ((i (position variable (joint-prob-dist-variables joint))))
    (remove-duplicates (mapcar #'(lambda (entry) (nth i (car entry)))
			       (joint-prob-dist-table joint))
		       :test #'equal :from-end t)))

(defun enumerate-joint-ask (query e joint)
  "The distribution of the QUERY variable given the evidence E, an alist
  ((variable . value) ...), by summing the entries of the full JOINT
  distribution that agree with E. [Section 12.3]"
  (let ((variables (joint-prob-dist-variables joint)))
    (flet ((agrees? (values event)
	     (every #'(lambda (pair)
			(equal (nth (position (car pair) variables) values)
			       (cdr pair)))
		    event)))
      (make-prob-dist
       query
       (mapcar #'(lambda (q)
		   (let ((event (cons (cons query q) e)))
		     (cons q (reduce #'+ (joint-prob-dist-table joint)
				     :key #'(lambda (entry)
					      (if (agrees? (car entry) event)
						  (cdr entry)
						0))))))
	       (joint-prob-dist-variable-values joint query))))))

(defparameter *dentist-joint*
  (make-joint-prob-dist
   :variables '(toothache cavity catch)
   :table '(((t t t) . 0.108) ((t t nil) . 0.012)
	    ((t nil t) . 0.016) ((t nil nil) . 0.064)
	    ((nil t t) . 0.072) ((nil t nil) . 0.008)
	    ((nil nil t) . 0.144) ((nil nil nil) . 0.576)))
  "The full joint distribution of Toothache, Cavity and Catch [Fig. 12.3].")
