From 205c770c5cae1bd028f7bfeeed8316bb23915c64 Mon Sep 17 00:00:00 2001 From: Gustavo Guerra Date: Sat, 1 Dec 2012 10:12:59 +0000 Subject: [PATCH] Rename Distribution to RandomVariable --- src/FSharp/FSharp.fsproj | 2 +- ...ibutionMonad.fs => RandomVariableMonad.fs} | 30 +++++++++---------- src/FSharpPortable/FSharpPortable.fsproj | 4 +-- src/FSharpUnitTests/FSharpUnitTests.fsproj | 4 +-- ...PokerDistributionTest.fs => PokerTests.fs} | 4 +-- ...ributionTest.fs => RandomVariableTests.fs} | 14 ++++----- 6 files changed, 29 insertions(+), 29 deletions(-) rename src/FSharp/{DistributionMonad.fs => RandomVariableMonad.fs} (74%) rename src/FSharpUnitTests/{PokerDistributionTest.fs => PokerTests.fs} (95%) rename src/FSharpUnitTests/{DistributionTest.fs => RandomVariableTests.fs} (83%) diff --git a/src/FSharp/FSharp.fsproj b/src/FSharp/FSharp.fsproj index 26d2f50d..b1b485eb 100644 --- a/src/FSharp/FSharp.fsproj +++ b/src/FSharp/FSharp.fsproj @@ -61,7 +61,7 @@ - + diff --git a/src/FSharp/DistributionMonad.fs b/src/FSharp/RandomVariableMonad.fs similarity index 74% rename from src/FSharp/DistributionMonad.fs rename to src/FSharp/RandomVariableMonad.fs index eb32c91b..da2842ec 100644 --- a/src/FSharp/DistributionMonad.fs +++ b/src/FSharp/RandomVariableMonad.fs @@ -6,16 +6,16 @@ open System open System.Collections open System.Collections.Generic -module Distribution = +module RandomVariable = type 'a Outcome = { Value: 'a Probability : BigRational } - type 'a Distribution = 'a Outcome seq + type 'a RandomVariable = 'a Outcome seq // P(A AND B) = P(A | B) * P(B) - let bind (f: 'a -> 'b Distribution) (dist:'a Distribution) = + let bind (f: 'a -> 'b RandomVariable) (dist:'a RandomVariable) = dist |> Seq.map (fun p1 -> f p1.Value @@ -23,40 +23,40 @@ module Distribution = { Value = p2.Value; Probability = p1.Probability * p2.Probability})) - |> Seq.concat : 'b Distribution + |> Seq.concat : 'b RandomVariable /// Sequentially compose two actions, passing any value produced by the first as an argument to the second. let inline (>>=) dist f = bind f dist /// Flipped >>= let inline (=<<) f dist = bind f dist - /// Inject a value into the Distribution type + /// Inject a value into the RandomVariable type let returnM (value:'a) = Seq.singleton { Value = value ; Probability = 1N/1N } - : 'a Distribution + : 'a RandomVariable - type DistributionMonadBuilder() = + type RandomVariableMonadBuilder() = member this.Bind (r, f) = bind f r member this.Return x = returnM x member this.ReturnFrom x = x - let distribution = DistributionMonadBuilder() + let randomVariable = RandomVariableMonadBuilder() // Create some helpers - let toUniformDistribution seq : 'a Distribution = + let toUniformDistribution seq : 'a RandomVariable = let l = Seq.length seq seq |> Seq.map (fun e -> { Value = e; Probability = 1N / bignum.FromInt l }) - let probability (dist:'a Distribution) = + let probability (dist:'a RandomVariable) = dist |> Seq.map (fun o -> o.Probability) |> Seq.sum let certainly = returnM - let impossible<'a> :'a Distribution = toUniformDistribution [] + let impossible<'a> :'a RandomVariable = toUniformDistribution [] let fairDice sides = toUniformDistribution [1..sides] @@ -66,15 +66,15 @@ module Distribution = let fairCoin = toUniformDistribution [Heads; Tails] - let filter predicate (dist:'a Distribution) : 'a Distribution = + let filter predicate (dist:'a RandomVariable) : 'a RandomVariable = dist |> Seq.filter (fun o -> predicate o.Value) let filterInAnyOrder items dist = items |> Seq.fold (fun d item -> filter (Seq.exists ((=) (item))) d) dist - /// Transforms a Distribution value by using a specified mapping function. - let map f (dist:'a Distribution) : 'b Distribution = + /// Transforms a RandomVariable value by using a specified mapping function. + let map f (dist:'a RandomVariable) : 'b RandomVariable = dist |> Seq.map (fun o -> { Value = f o.Value; Probability = o.Probability }) @@ -86,7 +86,7 @@ module Distribution = match n with | 0 -> certainly ([],values) | _ -> - distribution { + randomVariable { let! (x,c1) = selectOne values let! (xs,c2) = selectMany (n-1) c1 return x::xs,c2} diff --git a/src/FSharpPortable/FSharpPortable.fsproj b/src/FSharpPortable/FSharpPortable.fsproj index 420908c3..481ad7d9 100644 --- a/src/FSharpPortable/FSharpPortable.fsproj +++ b/src/FSharpPortable/FSharpPortable.fsproj @@ -78,8 +78,8 @@ q.fs - - DistributionMonad.fs + + RandomVariableMonad.fs diff --git a/src/FSharpUnitTests/FSharpUnitTests.fsproj b/src/FSharpUnitTests/FSharpUnitTests.fsproj index 4eefb688..6a51226a 100644 --- a/src/FSharpUnitTests/FSharpUnitTests.fsproj +++ b/src/FSharpUnitTests/FSharpUnitTests.fsproj @@ -48,8 +48,8 @@ - - + + Always diff --git a/src/FSharpUnitTests/PokerDistributionTest.fs b/src/FSharpUnitTests/PokerTests.fs similarity index 95% rename from src/FSharpUnitTests/PokerDistributionTest.fs rename to src/FSharpUnitTests/PokerTests.fs index b2702a71..4276d06b 100644 --- a/src/FSharpUnitTests/PokerDistributionTest.fs +++ b/src/FSharpUnitTests/PokerTests.fs @@ -1,7 +1,7 @@ -module MathNet.Numerics.Tests.PokerDistributionTest +module MathNet.Numerics.Tests.PokerTests open MathNet.Numerics -open MathNet.Numerics.Distribution +open MathNet.Numerics.RandomVariable open NUnit.Framework open FsUnit diff --git a/src/FSharpUnitTests/DistributionTest.fs b/src/FSharpUnitTests/RandomVariableTests.fs similarity index 83% rename from src/FSharpUnitTests/DistributionTest.fs rename to src/FSharpUnitTests/RandomVariableTests.fs index b511f18d..e9a80f20 100644 --- a/src/FSharpUnitTests/DistributionTest.fs +++ b/src/FSharpUnitTests/RandomVariableTests.fs @@ -1,16 +1,16 @@ -module MathNet.Numerics.Tests.DistributionTest +module MathNet.Numerics.Tests.RandomVariableTests open MathNet.Numerics -open MathNet.Numerics.Distribution +open MathNet.Numerics.RandomVariable open NUnit.Framework open FsUnit [] -let ``When creating a empty distribution, then the probability should be 1``() = - let actual = distribution { return () } +let ``When creating a empty randomVariable, then the probability should be 1``() = + let actual = randomVariable { return () } probability actual |> should equal (1N/1N) -let sumOfTwoFairDices = distribution { +let sumOfTwoFairDices = randomVariable { let! d1 = fairDice 6 let! d2 = fairDice 6 return d1 + d2 } @@ -22,7 +22,7 @@ let ``When creating two fair dices, then P(Sum of 2 dices = 7) should be 1/6``() |> probability |> should equal (1N/6N) -let fairCoinAndDice = distribution { +let fairCoinAndDice = randomVariable { let! d = fairDice 6 let! c = fairCoin return d,c } @@ -67,7 +67,7 @@ let ``When making the first choice in a MontyHall situation, the chances to win |> probability |> should equal (1N/3N) -let montyHallWithSwitch = distribution { +let montyHallWithSwitch = randomVariable { let! firstDoor = firstChoice return! switch firstDoor }