; column is one of num sym. contains class hash. which contains prim (defklass counts () (every (gensym "ALL")) (all (make-hash-table)) ) (defklass syms (counts)) (defklass nums (counts)) (defmethod counter ((x syms)) (make-instance 'sym)) (defmethod counter ((x nums)) (make-instance 'num)) (defun adds (c where new) (adds1 c new where) (adds1 c new (counts-every c)) c) (defun adds1 (c new where) (let ((old (gethash where (counts-all c)))) (setf old (add (or old (counter c)) new)))) (defklass count1 () (n 0)) (defklass sym (count1) (uniques (make-hash-table)) (cardinality 0) (m 1) ) (defklass num (count1) (min most-positive-long-float) (max least-negative-double-float) (sum 0) (sumSq 0) ready mean stdev) (defmethod add ((this sym) new) (in sym this (let ((old (incf (gethash new uniques 0)))) (incf n) (if (= 1 old) (incf cardinality)))) this) (defmethod add ((this num) new) (in num this (setf min (min min new) max (max max new) ready nil sum (+ sum new) sumSq (+ sumSq (* new new)) n (1+ n))) this) (defmethod probs ((this counts) where x) (prob1 (gethash where (counts-all this)) x)) (defmethod prob1 ((this sym) x) (/ (+ (sym-m this) (gethash x (sym-uniques this) 0)) (count1-n this))) (defmethod prob1 ((this num) x) (in num this (unless ready (setf ready t mean (/ sum n ) stdev (sqrt (/ (- sumSq (/ (expt sum 2) n)) (- n 1))))) (* (/ (* (sqrt (* 2 pi)) stdev)) (exp (* (- (/ (* 2 (expt stdev 2)))) (expt (- x mean) 2))))))