Browse Source

Rename Distribution to RandomVariable

pull/61/head
Gustavo Guerra 14 years ago
parent
commit
205c770c5c
  1. 2
      src/FSharp/FSharp.fsproj
  2. 30
      src/FSharp/RandomVariableMonad.fs
  3. 4
      src/FSharpPortable/FSharpPortable.fsproj
  4. 4
      src/FSharpUnitTests/FSharpUnitTests.fsproj
  5. 4
      src/FSharpUnitTests/PokerTests.fs
  6. 14
      src/FSharpUnitTests/RandomVariableTests.fs

2
src/FSharp/FSharp.fsproj

@ -61,7 +61,7 @@
<Compile Include="complex.fs" />
<Compile Include="q.fsi" />
<Compile Include="q.fs" />
<Compile Include="DistributionMonad.fs" />
<Compile Include="RandomVariableMonad.fs" />
</ItemGroup>
<ItemGroup>
<Reference Include="FSharp.Core" />

30
src/FSharp/DistributionMonad.fs → 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}

4
src/FSharpPortable/FSharpPortable.fsproj

@ -78,8 +78,8 @@
<Compile Include="..\FSharp\q.fs">
<Link>q.fs</Link>
</Compile>
<Compile Include="..\FSharp\DistributionMonad.fs">
<Link>DistributionMonad.fs</Link>
<Compile Include="..\FSharp\RandomVariableMonad.fs">
<Link>RandomVariableMonad.fs</Link>
</Compile>
</ItemGroup>
<ItemGroup>

4
src/FSharpUnitTests/FSharpUnitTests.fsproj

@ -48,8 +48,8 @@
<Compile Include="FsUnit.fs" />
<Compile Include="Utilities.fs" />
<Compile Include="BigRationalTests.fs" />
<Compile Include="DistributionTest.fs" />
<Compile Include="PokerDistributionTest.fs" />
<Compile Include="RandomVariableTests.fs" />
<Compile Include="PokerTests.fs" />
<Compile Include="Program.fs" />
<None Include="App.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

4
src/FSharpUnitTests/PokerDistributionTest.fs → 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

14
src/FSharpUnitTests/DistributionTest.fs → 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
[<Test>]
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 }
Loading…
Cancel
Save