Returns the distribution of the reduction of f over n samples from the distribution dist.
; clojure/contrib/probabilities/monte_carlo.clj:181 (with-monad state-m (defn sample "Return the distribution of samples of length n from the distribution dist" [n dist] (m-seq (replicate n dist))) (defn sample-reduce "Returns the distribution of the reduction of f over n samples from the distribution dist." ([f n dist] (if (zero? n) (m-result (f)) (let [m-f (m-lift 2 f) sample (replicate n dist)] (reduce m-f sample)))) ([f val n dist] (let [m-f (m-lift 2 f) m-val (m-result val) sample (replicate n dist)] (reduce m-f m-val sample)))) (defn sample-sum "Return the distribution of the sum over n samples from the distribution dist." [n dist] (sample-reduce ga/+ n dist)) (defn sample-mean "Return the distribution of the mean over n samples from the distribution dist" [n dist] (let [div-by-n (m-lift 1 #(ga/* % (/ n)))] (div-by-n (sample-sum n dist)))) (defn sample-mean-variance "Return the distribution of the mean-and-variance (a vector containing the mean and the variance) over n samples from the distribution dist" [n dist] (let [extract (m-lift 1 (fn [mv] [(:mean mv) (:variance mv)]))] (extract (sample-reduce acc/add acc/empty-mean-variance n dist)))) )
Copyright (c) Rich Hickey. All rights reserved.
The use and distribution terms for this software are covered by the Eclipse Public License 1.0, which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.