[CM] variables for weights
Tiago Videira
tiago.videira at gmail.com
Tue Mar 27 01:29:20 PDT 2012
Hello!
So far the nifty function of Prof Taube has worked fine with numbers:
However it seems it can't handle variables in the place of weights.
How can I change that so I can use for instance the command
(setq z 30)
(gn-weight 2 '((a z) (b 5) (c 50)))
having defined z previously?
Basically i'm trying to achieve a function that can handle a list of
weighted probabilities, in which the weights can be variables.
Many thanks and Kind regards
----
;;MAKES A PROBABILITY-WEIGHTED LIST
(defun make-ptable (data)
(let ((total (loop for d in data sum (second d)))
(sum 0))
;; total holds sum of weights in data
(loop for d in data
for v = (first d) ; outcome to return
for w = (second d) ; relative weight
do (setq sum (+ sum w))
;; collect outcome and normalized probability
collect (list v (/ sum total)))))
(defun pick-tbl (table)
;; return outcome in table according
;; to its weighted probability
(let ((x (rnd2 0 1)))
;; x is uniform number < 1.
(loop for d in table
for p = (second d)
when (< x p ) ; x in this segment.
return (first d))))
(defun gn-weight (nb tbl)
(loop for i from 0 to (- nb 1)
collect (pick-tbl (make-ptable tbl))))
More information about the Cmdist
mailing list