# ClojureDocs(beta)

## Clojure Contrib

### Namespaces

• clojure.contrib
• http
• probabilities

# sample-reduce

## clojure.contrib.probabilities.monte-carlo

• (sample-reduce f n dist)
• (sample-reduce f val n dist)
Returns the distribution of the reduction of f over n samples from the
distribution dist.

### Source clojure/contrib/probabilities/monte_carlo.clj:182 top

```(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))))

)```
Vars in clojure.contrib.probabilities.monte-carlo/sample-reduce: defn let reduce replicate zero?
Used in 0 other vars