(defun nudge (x nval name &optional (distribution *dists*)) (let* ((get-val (gethash name distribution)) (max (dist9-max get-val)) (min (dist9-min get-val)) (pd (dist9-distribution get-val)) ; (tpd (pd-vals pd)) (cd (cum-array pd)) (nxp (nudgexp cd x min max (length cd) nval)) (newval (binrange cd nxp max min (length cd)))) (if (or (< x min) (> x max)) `(x is not in the range) newval))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun x-bin (x min max b) ;b = number of bins in distribution, x= # to nudge, * (ceiling (+ (/(- x min) (/ (- max min) (- b 1))) 1))) (defun xpos (cd x min max b) ;xpos finds position of x in cd (let ((pval (aref cd (- (x-bin x min max b) 1)))) (if (= pval 0) `(the x value entered has no frequency) pval))) (defun nudgexp (cd x min max b nval) ;nval is nudge% (let* ((pos (xpos cd x min max b)) (nmin (- pos (* nval pos))) (nmin! (progn (let ((fin (aref cd 0))) (if (< nmin fin) fin nmin)))) (nmax (+ pos (* nval pos))) (nmax! (progn (let ((fin (aref cd (- (length cd) 1)))) (if (> nmax fin) fin nmax)))) (prange (make-range :max nmax! :min nmin! :ready? t))) (any9 prange))) (defun bxpos (cd nxp) ;bin for new xpos (loop for n from 0 to (- (length cd) 1) if (<= nxp (aref cd n)) collect (+ n 1) into bin finally (return(values (first bin))))) (defun binsize (max min b) (/ (- max min) (- b 1))) (defun binrange (cd nxp max min b) "Returns random value from within range" (let* ((nmin (+ min (* (- (bxpos cd nxp) 2) (binsize max min b)))) (nmax (+ min (* (- (bxpos cd nxp) 1) (binsize max min b)))) (nrange (make-range :max nmax :min nmin :ready? t))) (any9 nrange)))