Browse Source
Added Bernoulli distribution. Signed-off-by: jvangael <jurgen.vangael@gmail.com>pull/2/head
7 changed files with 765 additions and 6 deletions
@ -0,0 +1,169 @@ |
|||
// <copyright file="Vector.fs" company="Math.NET"> |
|||
// Math.NET Numerics, part of the Math.NET Project |
|||
// http://mathnet.opensourcedotnet.info |
|||
// |
|||
// Copyright (c) 2009 Math.NET |
|||
// |
|||
// Permission is hereby granted, free of charge, to any person |
|||
// obtaining a copy of this software and associated documentation |
|||
// files (the "Software"), to deal in the Software without |
|||
// restriction, including without limitation the rights to use, |
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
// copies of the Software, and to permit persons to whom the |
|||
// Software is furnished to do so, subject to the following |
|||
// conditions: |
|||
// |
|||
// The above copyright notice and this permission notice shall be |
|||
// included in all copies or substantial portions of the Software. |
|||
// |
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|||
// OTHER DEALINGS IN THE SOFTWARE. |
|||
// </copyright> |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Double |
|||
|
|||
open MathNet.Numerics.LinearAlgebra |
|||
|
|||
/// A module which implements functional vector operations. |
|||
module Vector = |
|||
|
|||
/// Transform a vector into an array. |
|||
let inline to_array (v: #Vector) = |
|||
let n = v.Count |
|||
Array.init n (fun i -> v.Item(i)) |
|||
|
|||
/// Transform a vector into an array. |
|||
let inline to_list (v: #Vector) = |
|||
let n = v.Count |
|||
List.init n (fun i -> v.Item(i)) |
|||
|
|||
/// In-place mutation by applying a function to every element of the vector. |
|||
let inline mapInPlace (f: float -> float) (v: #Vector) = |
|||
for i=0 to v.Count-1 do |
|||
v.Item(i) <- f (v.Item(i)) |
|||
() |
|||
|
|||
/// In-place mutation by applying a function to every element of the vector. |
|||
let inline mapiInPlace (f: int -> float -> float) (v: #Vector) = |
|||
for i=0 to v.Count-1 do |
|||
v.Item(i) <- f i (v.Item(i)) |
|||
() |
|||
|
|||
/// In-place vector addition. |
|||
let inline addInPlace (v: #Vector) (w: #Vector) = |
|||
v.Add w |
|||
|
|||
/// In place vector subtraction. |
|||
let inline subInPlace (v: #Vector) (w: #Vector) = |
|||
v.Subtract w |
|||
|
|||
/// Functional map operator for vectors. |
|||
/// <include file='../../../../FSharpExamples/DenseVector.xml' path='example'/> |
|||
let inline map f (v: #Vector) = |
|||
let w = v.Clone() |
|||
inplace_mapi (fun _ x -> f x) w |
|||
w |
|||
|
|||
/// Applies a function to all elements of the vector. |
|||
let inline iter (f: float -> unit) (v: #Vector) = |
|||
for i=0 to v.Count-1 do |
|||
f (v.Item i) |
|||
|
|||
/// Applies a function to all elements of the vector. |
|||
let inline iteri (f: int -> float -> unit) (v: #Vector) = |
|||
for i=0 to v.Count-1 do |
|||
f i (v.Item i) |
|||
|
|||
/// Maps a vector to a new vector by applying a function to every element. |
|||
let inline mapi (f: int -> float -> float) (v: #Vector) = |
|||
let w = v.Clone() |
|||
inplace_mapi f w |
|||
w |
|||
|
|||
/// Fold all entries of a vector. |
|||
let inline fold (f: 'a -> float -> 'a) (acc0: 'a) (v: #Vector) = |
|||
let mutable acc = acc0 |
|||
for i=0 to v.Count-1 do |
|||
acc <- f acc (v.Item(i)) |
|||
acc |
|||
|
|||
/// Fold all entries of a vector using a position dependent folding function. |
|||
let inline foldi (f: int -> 'a -> float -> 'a) (acc0: 'a) (v: #Vector) = |
|||
let mutable acc = acc0 |
|||
for i=0 to v.Count-1 do |
|||
acc <- f i acc (v.Item(i)) |
|||
acc |
|||
|
|||
/// Checks whether a predicate is satisfied for every element in the vector. |
|||
let inline forall (p: float -> bool) (v: #Vector) = |
|||
let mutable b = true |
|||
let mutable i = 0 |
|||
while b && i < v.Count do |
|||
b <- b && (p (v.Item(i))) |
|||
i <- i+1 |
|||
b |
|||
|
|||
/// Checks whether there is an entry in the vector that satisfies a given predicate. |
|||
let inline exists (p: float -> bool) (v: #Vector) = |
|||
let mutable b = false |
|||
let mutable i = 0 |
|||
while not(b) && i < v.Count do |
|||
b <- b || (p (v.Item(i))) |
|||
i <- i+1 |
|||
b |
|||
|
|||
/// Checks whether a predicate is true for all entries in a vector. |
|||
let inline foralli (p: int -> float -> bool) (v: #Vector) = |
|||
let mutable b = true |
|||
let mutable i = 0 |
|||
while b && i < v.Count do |
|||
b <- b && (p i (v.Item(i))) |
|||
i <- i+1 |
|||
b |
|||
|
|||
/// Checks whether there is an entry in the vector that satisfies a given position dependent predicate. |
|||
let inline existsi (p: int -> float -> bool) (v: #Vector) = |
|||
let mutable b = false |
|||
let mutable i = 0 |
|||
while not(b) && i < v.Count do |
|||
b <- b || (p i (v.Item(i))) |
|||
i <- i+1 |
|||
b |
|||
|
|||
/// Scans a vector; like fold but returns the intermediate result. |
|||
let inline scan (f: float -> float -> float) (v: #Vector) = |
|||
let w = v.Clone() |
|||
let mutable p = v.Item(0) |
|||
for i=1 to v.Count-1 do |
|||
p <- f p (v.Item(i)) |
|||
w.[i] <- p |
|||
w |
|||
|
|||
/// Scans a vector; like fold but returns the intermediate result. |
|||
let inline scanBack (f: float -> float -> float) (v: #Vector) = |
|||
let w = v.Clone() |
|||
let mutable p = v.Item(v.Count-1) |
|||
for i=2 to v.Count do |
|||
p <- f (v.Item(v.Count - i)) p |
|||
w.[v.Count - i] <- p |
|||
w |
|||
|
|||
/// Reduces a vector: the result of this function will be f(...f(f(v[0],v[1]), v[2]),..., v[n]). |
|||
let inline reduce (f: float -> float -> float) (v: #Vector) = |
|||
let mutable p = v.Item(0) |
|||
for i=1 to v.Count-1 do |
|||
p <- f p (v.Item(i)) |
|||
p |
|||
|
|||
/// Reduces a vector: the result of this function will be f(v[1], ..., f(v[n-2], f(v[n-1],v[n]))...). |
|||
let inline reduceBack (f: float -> float -> float) (v: #Vector) = |
|||
let mutable p = v.Item(v.Count-1) |
|||
for i=2 to v.Count do |
|||
p <- f (v.Item(v.Count - i)) p |
|||
p |
|||
@ -0,0 +1,252 @@ |
|||
// <copyright file="BernoulliTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://mathnet.opensourcedotnet.info
|
|||
//
|
|||
// Copyright (c) 2009 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.DistributionTests |
|||
{ |
|||
using System; |
|||
using System.Linq; |
|||
using MbUnit.Framework; |
|||
using MathNet.Numerics.Distributions; |
|||
|
|||
[TestFixture] |
|||
public class BernoulliTests |
|||
{ |
|||
[SetUp] |
|||
public void SetUp() |
|||
{ |
|||
Control.CheckDistributionParameters = true; |
|||
} |
|||
|
|||
[Test] |
|||
[Row(0.0)] |
|||
[Row(0.3)] |
|||
[Row(1.0)] |
|||
public void CanCreateBernoulli(double p) |
|||
{ |
|||
var bernoulli = new Bernoulli(p); |
|||
AssertEx.AreEqual<double>(p, bernoulli.P); |
|||
} |
|||
|
|||
[Test] |
|||
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
|||
[Row(Double.NaN)] |
|||
[Row(-1.0)] |
|||
[Row(2.0)] |
|||
public void NormalCreateFailsWithBadParameters(double p) |
|||
{ |
|||
var bernoulli = new Bernoulli(p); |
|||
} |
|||
|
|||
[Test] |
|||
public void ValidateToString() |
|||
{ |
|||
var b = new Bernoulli(0.3); |
|||
AssertEx.AreEqual<string>("Bernoulli(P = 0.3)", n.ToString()); |
|||
} |
|||
|
|||
[Test] |
|||
[Row(0.0)] |
|||
[Row(0.3)] |
|||
[Row(1.0)] |
|||
public void CanSetProbabilityOfOne(double p) |
|||
{ |
|||
var b = new Bernoulli(0.3); |
|||
b.P = p; |
|||
} |
|||
|
|||
[Test] |
|||
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
|||
[Row(Double.NaN)] |
|||
[Row(-1.0)] |
|||
[Row(2.0)] |
|||
public void SetProbabilityOfOneFails(double p) |
|||
{ |
|||
var b = new Bernoulli(0.3); |
|||
b.P = p; |
|||
} |
|||
|
|||
[Test] |
|||
[Row(0.0)] |
|||
[Row(0.3)] |
|||
[Row(1.0)] |
|||
public void ValidateEntropy(double p) |
|||
{ |
|||
var b = new Bernoulli(p); |
|||
AssertEx.AreEqual<double>((1.0 - p) * Math.Log(1.0 - p) + p * Math.Log(p), b.Entropy); |
|||
} |
|||
|
|||
[Test] |
|||
[Row(0.0)] |
|||
[Row(0.3)] |
|||
[Row(1.0)] |
|||
public void ValidateSkewness(double p) |
|||
{ |
|||
var b = new Bernoulli(p); |
|||
AssertEx.AreEqual<double>((1.0 - 2.0 * p) / Math.Sqrt(p * (1.0 - p)), n.Skewness); |
|||
} |
|||
|
|||
[Test] |
|||
[Row(0.0, 0)] |
|||
[Row(0.3, 0)] |
|||
[Row(1.0, 1)] |
|||
public void ValidateMode(double p, double m) |
|||
{ |
|||
var b = new Bernoulli(p); |
|||
AssertEx.AreEqual<double>(mean, n.Mode); |
|||
} |
|||
|
|||
[Test] |
|||
[ExpectedException(typeof(Exception))] |
|||
public void ValidateMedian() |
|||
{ |
|||
var b = new Bernoulli(0.3); |
|||
} |
|||
|
|||
[Test] |
|||
public void ValidateMinimum() |
|||
{ |
|||
var b = new Bernoulli(0.3); |
|||
AssertEx.AreEqual<double>(0.0, n.Minimum); |
|||
} |
|||
|
|||
[Test] |
|||
public void ValidateMaximum() |
|||
{ |
|||
var b = new Bernoulli(0.3); |
|||
AssertEx.AreEqual<double>(1.0, n.Maximum); |
|||
} |
|||
|
|||
[Test] |
|||
[Row(0.0, -1.0, 0.0)] |
|||
[Row(0.0, 0.0, 1.0)] |
|||
[Row(0.0, 0.5, 0.0)] |
|||
[Row(0.0, 1.0, 0.0)] |
|||
[Row(0.0, 2.0, 0.0)] |
|||
[Row(0.3, -1.0, 0.0)] |
|||
[Row(0.3, 0.0, 0.7)] |
|||
[Row(0.3, 0.5, 0.0)] |
|||
[Row(0.3, 1.0, 0.3)] |
|||
[Row(0.3, 2.0, 0.0)] |
|||
[Row(1.0, -1.0, 0.0)] |
|||
[Row(1.0, 0.0, 0.0)] |
|||
[Row(1.0, 0.5, 0.0)] |
|||
[Row(1.0, 1.0, 1.0)] |
|||
[Row(1.0, 2.0, 0.0)] |
|||
public void ValidateProbability(double p, double x, double d) |
|||
{ |
|||
var b = new Bernoulli(p); |
|||
AssertEx.AreEqual(d, b.Probability(x)); |
|||
} |
|||
|
|||
[Test] |
|||
[Row(0.0, -1.0, Double.NegativeInfinity)] |
|||
[Row(0.0, 0.0, 0.0)] |
|||
[Row(0.0, 0.5, Double.NegativeInfinity)] |
|||
[Row(0.0, 1.0, Double.NegativeInfinity)] |
|||
[Row(0.0, 2.0, Double.NegativeInfinity)] |
|||
[Row(0.3, -1.0, Double.NegativeInfinity)] |
|||
[Row(0.3, 0.0, -0.35667494393873244235395440410727451457180907089949815)] |
|||
[Row(0.3, 0.5, Double.NegativeInfinity)] |
|||
[Row(0.3, 1.0, -1.2039728043259360296301803719337238685164245381839102)] |
|||
[Row(0.3, 2.0, Double.NegativeInfinity)] |
|||
[Row(1.0, -1.0, Double.NegativeInfinity)] |
|||
[Row(1.0, 0.0, Double.NegativeInfinity)] |
|||
[Row(1.0, 0.5, Double.NegativeInfinity)] |
|||
[Row(1.0, 1.0, 0.0)] |
|||
[Row(1.0, 2.0, Double.NegativeInfinity)] |
|||
public void ValidateProbabilityLn(double p, double x, double dln) |
|||
{ |
|||
var b = new Bernoulli(p); |
|||
AssertEx.AreEqual(dln, b.ProbabilityLn(x)); |
|||
} |
|||
|
|||
[Test] |
|||
public void CanSampleStatic() |
|||
{ |
|||
var d = Bernoulli.Sample(new Random(), 0.3); |
|||
} |
|||
|
|||
[Test] |
|||
public void CanSampleSequenceStatic() |
|||
{ |
|||
var ied = Bernoulli.Samples(new Random(), 0.3); |
|||
var arr = ied.Take(5).ToArray(); |
|||
} |
|||
|
|||
[Test] |
|||
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
|||
public void FailSampleStatic() |
|||
{ |
|||
var d = Bernoulli.Sample(new Random(), -1.0); |
|||
} |
|||
|
|||
[Test] |
|||
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
|||
public void FailSampleSequenceStatic() |
|||
{ |
|||
var ied = Bernoulli.Samples(new Random(), -1.0).First(); |
|||
} |
|||
|
|||
[Test] |
|||
public void CanSample() |
|||
{ |
|||
var n = new Bernoulli(); |
|||
var d = n.Sample(); |
|||
} |
|||
|
|||
[Test] |
|||
public void CanSampleSequence() |
|||
{ |
|||
var n = new Bernoulli(); |
|||
var ied = n.Samples(); |
|||
var e = ied.Take(5).ToArray(); |
|||
} |
|||
|
|||
[Test] |
|||
[Row(0.0, -1.0, 0.0)] |
|||
[Row(0.0, 0.0, 1.0)] |
|||
[Row(0.0, 0.5, 1.0)] |
|||
[Row(0.0, 1.0, 1.0)] |
|||
[Row(0.0, 2.0, 1.0)] |
|||
[Row(0.3, -1.0, 0.0)] |
|||
[Row(0.3, 0.0, 0.7)] |
|||
[Row(0.3, 0.5, 0.7)] |
|||
[Row(0.3, 1.0, 1.0)] |
|||
[Row(0.3, 2.0, 1.0)] |
|||
[Row(1.0, -1.0, 0.0)] |
|||
[Row(1.0, 0.0, 0.0)] |
|||
[Row(1.0, 0.5, 0.0)] |
|||
[Row(1.0, 1.0, 1.0)] |
|||
[Row(1.0, 2.0, 1.0)] |
|||
public void ValidateCumulativeDistribution(double p, double x, double cdf) |
|||
{ |
|||
var b = new Bernoulli(p); |
|||
AssertEx.AreEqual(cdf, n.CumulativeDistribution(x)); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue