On 24.12.2009, at 05:18, jim wrote:

> I was looking at the probability monad today and think this definition
> of m-bind might be easier to understand.
>
> (defmonad dist-m
>  [m-result (fn m-result-dist [v]
>              {v 1})
>   m-bind   (fn m-bind-dist [mv f]
>              (reduce (partial merge-with +)
>                      (for [[x p] mv
>                            [y q] (f x)]
>                        {y (* q p)})))
>   ])
>
> What do you think?

I agree. In fact, I had reinvented merge-with because I wasn't aware  
of its existence.

> Also, I was thinking about cond-dist-m. What if dist-m was redefined
> to be this
>
> (defmonad dist-m
>  [m-result (fn m-result-dist [v]
>              {v 1})
>   m-bind   (fn m-bind-dist [mv f]
>              (if (empty? mv)
>                {}
>                (reduce (partial merge-with +)
>                        (for [[x p] mv
>                              [y q] (f x)]
>                          {y (* q p)}))))
>   m-zero {}
>   m-plus   (fn m-plus-dist [& mvs]
>              (if-let [mv (first (drop-while empty? mvs))]
>                mv
>                {}))
>   ])
>
> I think that would roll cond-dist-m into dist-m, eliminating the need
> for a seperate monad. Don't know if you'd thought of that and
> discarded it or not.

At first glance I doubt that this works. Did you try it?

Your m-bind looks equivalent to the one of my original dist-m, with  
just an optimization for the case of an empty map. If that is true,  
then your dist-m can't include the features of cond-dist-m, which  
require a different m-bind.

It is in fact essential for cond-dist-m not to eliminate invalid  
values from the distributions immediately, but to accumulate their  
weights into the map entry for some special value (nil in my  
implementation). Otherwise the probabilities come out wrong for multi- 
step computations containing more than one :when clause.

Konrad.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to