Created
August 15, 2011 18:30
-
-
Save forki/1147375 to your computer and use it in GitHub Desktop.
The DistributionMonad in F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type 'a Outcome = { | |
Value: 'a | |
Probability : BigRational } | |
type 'a Distribution = 'a Outcome seq | |
// P(A AND B) = P(A | B) * P(B) | |
let bindD (dist:'a Distribution) (f: 'a -> 'b Distribution) = | |
dist | |
|> Seq.map (fun p1 -> | |
f p1.Value | |
|> Seq.map (fun p2 -> | |
{ Value = p2.Value; | |
Probability = | |
p1.Probability * p2.Probability})) | |
|> Seq.concat : 'b Distribution | |
let returnD (value:'a) : 'a Distribution = | |
Seq.singleton { Value = value ; Probability = 1N/1N } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment