(defun sample (name &optional samp-int) (let* ((get-val (gethash name *dists*)) (mx (dist9-max get-val)) (mn (dist9-min get-val)) (pd (dist9-distribution get-val)) ; (tpd (pd-vals pd)) ;convert ~ to actual values (pcd (cd pd)) ;tpd (spd (sort-pd pd)) ;tpd (scd (sort-cd spd)) (fgoal (my-random (aref (reverse scd) 0))) (which-bin (find-bin1 fgoal pcd)) (b-range (bin-range mx mn pd which-bin)));tpd (if (equalp samp-int t) (floor(any9 b-range)) (any9 b-range)))) (defun newSample (name &optional (samp-int nil)) (let ((totalDistribution (reduce #'+ (dist9-distribution (gethash name *dists*))))) (let ((random-value (my-random totalDistribution)) (min (dist9-min (gethash name *dists*))) (max (dist9-max (gethash name *dists*))) (numBins (length (dist9-distribution (gethash name *dists*)))) (binSize 0) (spread 0) (index 0)) (setf spread (- max min)) (setf binSize (/ spread numBins)) (while (> random-value 0) (decf random-value (aref (dist9-distribution (gethash name *dists*)) index)) (incf index)) (decf index) (+ (my-random binSize) (* binSize index) min)))) (defun pd-vals (pd) (loop for n from 0 to (- (length pd) 1) collect (eval (aref pd n)) into trans-pd finally (return(values (make-array (length trans-pd) :initial-contents trans-pd))))) (defun sort-pd (distribution) (stable-sort distribution #'>)) (defun sort-cd (sorted-pd) (cum-array sorted-pd)) (defun get-counter (sorted-cd) (let* ((mn (aref sorted-cd 0)) (mx (aref sorted-cd (- (length sorted-cd) 1)))) (- mx mn))) (defun get-goal (sorted-cd) (let* ((mn (aref sorted-cd 0)) (mx (aref sorted-cd (- (length sorted-cd) 1))) (rng (make-range :max mx :min mn :ready? t))) (floor(any9 rng)))) (defun cd (distribution) (cum-array distribution)) (defun find-bin1 (goal arr) (loop for n from 0 to (- (length arr) 1) if (<= goal (aref arr n)) collect (+ 1 n) into bin finally(return(values (first bin))))) (defun find-bin (goal counter c-distribution) (if (<= counter goal) (length c-distribution) (loop for n from 0 to (- (length c-distribution) 1) with counter1 = (decf counter (aref c-distribution n)) if (<= counter1 goal) collect (+ n 1) into bin finally (return(values (first bin)))))) (defun bin-range (mx mn distribution bin-num) (let* ((a (/ (- mx mn) (length distribution))) (bmx (+ mn (* bin-num a))) (bmn (+ mn (* (- bin-num 1) a)))) (make-range :max bmx :min bmn :ready? t))) (defmethod any9 ((x range)) (range-ready! x) (let ((max (range-min x)) (min (range-max x))) (if (> min max) (+ max (my-random (- min max))) (+ min (my-random (- max min)))))) (defun cum-array (arr) (let ((total 0)) (loop for n from 0 to (- (length arr) 1) collect (incf total (aref arr n)) into cd ;eval finally (return (values (make-array (length cd) :initial-contents cd))))))