Skip to content

Instantly share code, notes, and snippets.

@forki
Created August 15, 2011 18:30
Show Gist options
  • Save forki/1147375 to your computer and use it in GitHub Desktop.
Save forki/1147375 to your computer and use it in GitHub Desktop.
The DistributionMonad in F#
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