1 
;; $Id: filter.lisp,v 1.1.1.1 2003/11/25 15:55:34 aventimiglia Exp $

2 
;;

3 
;; bayescl  A Common Lisp Bayesian Filtering Library

4 
;; (c) 2003 Anthony J Ventimiglia.

5 
;;

6 
;; This library is free software; you can redistribute it and/or

7 
;; modify it under the terms of the terms of the Lisp Lesser GNU

8 
;; Public License, known as the LLGPL

9 
;;

10 
;; This library is distributed in the hope that it will be useful,

11 
;; but WITHOUT ANY WARRANTY; without even the implied warranty of

12 
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

13 
;;

14 
;; You should have received a copy of the Lisp Lesser General Public

15 
;; License along with this library; if not, please see

16 
;; http://opensource.franz.com/preamble.html.

17 
;;

18 
;; email: aventimiglia@commonlisp.net

19 
;; HomePage: http://commonlisp.net/project/bayescl

20 

21 
(inpackage :net.commonlisp.bayescl)

22 

23 
(defclass filter ()

24 
((buckets :accessor buckets

25 
:type list

26 
:initarg :buckets

27 
:documentation "A list of buckets used for filtering")))

28 

29 
(defun makefilter (buckets)

30 
(if (< (length buckets) 2) 'error

31 
(makeinstance 'filter :buckets buckets)))

32 

33 
(defmethod scoretoken ((self filter) token)

34 
"Returns an a list of scores for each bucket in the filter"

35 
(let ((bucketct (length (buckets self))))

36 
(let (; Prior probabilities

37 
(P[Hx] (makearray bucketct

38 
:elementtype 'rational

39 
:initialelement (/ 1 bucketct)))

40 
; Conditional probabilities

41 
(P[D/Hx] (makearray bucketct :elementtype 'rational

42 
:initialcontents

43 
(loop for bucket in (buckets self)

44 
collect (scoretoken bucket token)))))

45 
(let ((dividend (apply #'+

46 
(loop for i from 0 to (1 bucketct)

47 
collect (* (aref P[Hx] i)

48 
(aref P[D/Hx] i))))))

49 
;; Return all posterior probabilities

50 
(loop for i from 0 to (1 bucketct)

51 
collect (if (zerop dividend) *minimumscore*

52 
(/ (* (aref P[Hx] i) (aref P[D/Hx] i))

53 
dividend)))))))

54 

55 
