Transform a sequence of uniform random numbers in the interval [0, 1)
into a sequence of normal random numbers with mean mu and standard
deviation sigma.
(defn normal-box-muller
"Transform a sequence of uniform random numbers in the interval [0, 1)
into a sequence of normal random numbers with mean mu and standard
deviation sigma."
[mu sigma]
(fn [rs]
(let [[u1 rs] (stream-next rs)
[u2 rs] (stream-next rs)
v1 (- (* 2.0 u1) 1.0)
v2 (- (* 2.0 u2) 1.0)
s (+ (* v1 v1) (* v2 v2))
ls (. Math sqrt (/ (* -2.0 (. Math log s)) s))
x1 (* v1 ls)
x2 (* v2 ls)]
(if (or (>= s 1) (= s 0))
(recur rs)
[x1 rs]))))
Comments top
No comments for normal-box-muller. Log in to add a comment.