diff options
author | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-02-18 08:21:55 +0000 |
---|---|---|
committer | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-02-18 08:21:55 +0000 |
commit | 0b8c49ab33e7163e4fee94452f03dc3f47df99c2 (patch) | |
tree | 8cf24cb21fe92209f119cef6bec02f1f9637794b /src/clojure/contrib/probabilities | |
parent | e3c982f1a9c5cde3e7e8a894889ed6367242892f (diff) |
monads+probabilities: added an -m suffix to all monad names
Diffstat (limited to 'src/clojure/contrib/probabilities')
-rw-r--r-- | src/clojure/contrib/probabilities/dist.clj | 17 | ||||
-rw-r--r-- | src/clojure/contrib/probabilities/dist/examples.clj | 18 |
2 files changed, 17 insertions, 18 deletions
diff --git a/src/clojure/contrib/probabilities/dist.clj b/src/clojure/contrib/probabilities/dist.clj index 4f9100fd..4c1d321f 100644 --- a/src/clojure/contrib/probabilities/dist.clj +++ b/src/clojure/contrib/probabilities/dist.clj @@ -1,7 +1,7 @@ ;; Finite probability distributions ;; by Konrad Hinsen -;; last updated January 30, 2009 +;; last updated February 18, 2009 ;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use ;; and distribution terms for this software are covered by the Eclipse @@ -12,7 +12,6 @@ ;; remove this notice, or any other, from this software. (ns clojure.contrib.probabilities.dist - (:refer-clojure :exclude (sequence)) (:use clojure.contrib.monads clojure.contrib.macros clojure.contrib.monads clojure.contrib.def)) @@ -20,12 +19,12 @@ ; distributions (e.g. there is a finite number of possible value), which ; are represented as maps from values to probabilities. -(defmonad dist +(defmonad dist-m "Monad describing computations on fuzzy quantities, represented by a finite probability distribution for the possible values. A distribution is represented by a map from values to probabilities." [m-result (fn m-result-dist [v] - {v 1}) + {v 1}) m-bind (fn m-bind-dist [mv f] (letfn [add-prob [dist [x p]] (assoc dist x (+ (get dist x 0) p))] @@ -40,8 +39,8 @@ ; The function normalize takes this probability out of the distribution and ; re-distributes its weight over the valid values. -(defvar cond-dist - (maybe-t dist) +(defvar cond-dist-m + (maybe-t dist-m) "Variant of the dist monad that can handle undefined values.") ; Normalization @@ -100,7 +99,7 @@ :else (assoc dist v p))] (reduce add-choice {} (partition 2 choices)))) -(with-monad dist +(with-monad dist-m (defn certainly "Returns a distribution in which the single value v has probability 1." @@ -119,7 +118,7 @@ the predicate pred." [pred dist] (normalize-cond - (with-monad cond-dist + (with-monad cond-dist-m (m-bind dist (fn [v] (m-result (when (pred v) v))))))) ; Select (with equal probability) N items from a sequence @@ -130,7 +129,7 @@ (let [[h t] (split-at n xs)] (list (first t) (concat h (rest t))))) -(with-monad dist +(with-monad dist-m (defn- select-n [n xs] (letfn [select-1 [[s xs]] diff --git a/src/clojure/contrib/probabilities/dist/examples.clj b/src/clojure/contrib/probabilities/dist/examples.clj index 6fb1b5be..947f5366 100644 --- a/src/clojure/contrib/probabilities/dist/examples.clj +++ b/src/clojure/contrib/probabilities/dist/examples.clj @@ -27,13 +27,13 @@ ; The sum of two dice using a monad comprehension (assert (= two-dice - (domonad dist + (domonad dist-m [d1 die d2 die] (+ d1 d2)))) ; The two values separately, but as an ordered pair -(domonad dist +(domonad dist-m [d1 die d2 die] (if (< d1 d2) (list d1 d2) (list d2 d1))) @@ -42,7 +42,7 @@ (cond-prob odd? two-dice) ; A two-step experiment: throw a die, and then add 1 with probability 1/2 -(domonad dist +(domonad dist-m [d die x (choose (/ 1 2) d :else (inc d))] @@ -50,7 +50,7 @@ ; The sum of n dice (defn dice [n] - (domonad dist + (domonad dist-m [ds (m-seq (replicate n die))] (apply + ds))) @@ -84,7 +84,7 @@ (def doors #{:A :B :C}) ; A simulation of the game, step by step: -(domonad dist +(domonad dist-m [; The prize is hidden behind one of the doors. prize (uniform doors) ; The player make his initial choice. @@ -126,7 +126,7 @@ ; Multiple evolution steps can be chained together with m-chain, ; since each step's input is the output of the previous step. -(with-monad dist +(with-monad dist-m (defn evolve [n tree] ((m-chain (replicate n evolve-1)) tree))) @@ -136,7 +136,7 @@ (evolve 2 new-tree) ; We can also get a distribution of the height only: -(with-monad dist +(with-monad dist-m ((m-lift 1 :height) (evolve 2 new-tree))) @@ -170,7 +170,7 @@ ; 4) Normalize the distribution for the non-nil values. (defn add-observation [prior observation] (normalize-cond - (domonad cond-dist + (domonad cond-dist-m [die prior number (get dice die)] (when (= number observation) die)))) @@ -188,7 +188,7 @@ ; With Bayesian inference, it is most efficient to eliminate choices ; as early as possible. (defn add-observations [prior observations] - (with-monad cond-dist + (with-monad cond-dist-m (let [n-nums #(m-seq (replicate (count observations) (get dice %)))] (normalize-cond (domonad |