Skip to content

Commit

Permalink
Merge pull request #301 from valbers/developer
Browse files Browse the repository at this point in the history
  • Loading branch information
bvenn authored Sep 30, 2023
2 parents 2bcde71 + 3ae0bea commit e8971fb
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/FSharp.Stats/Distributions/Continuous/Beta.fs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ type Beta =
/// Returns the support of the exponential distribution: [0.0, 1.0).
static member Support alpha beta =
Beta.CheckParam alpha beta
(0.0, 1.0)
Interval.CreateRightOpen<float>(0.0, 1.0)

/// A string representation of the distribution.
static member ToString alpha beta =
sprintf "Beta(α = %f, β = %f)" alpha beta
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Continuous/Chi.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type Chi =
/// Returns the support of the exponential distribution: [0, Positive Infinity).
static member Support dof =
Chi.CheckParam dof
(0., System.Double.PositiveInfinity)
Interval.CreateRightOpen<float>(0.0, Double.PositiveInfinity)


/// A string representation of the distribution.
Expand Down
5 changes: 4 additions & 1 deletion src/FSharp.Stats/Distributions/Continuous/Exponential.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ type Exponential =


/// Returns the support of the exponential distribution: [0, Positive Infinity).
[<Obsolete("Please use the function without the superfluous lambda parameter")>]
static member Support lambda =
Exponential.CheckParam lambda
Interval.CreateClosed<float> (0.0,System.Double.PositiveInfinity)
Interval.CreateRightOpen<float> (0.0, Double.PositiveInfinity)

static member Support () =
Interval.CreateRightOpen<float> (0.0, Double.PositiveInfinity)

/// A string representation of the distribution.
static member ToString lambda =
Expand Down
29 changes: 20 additions & 9 deletions src/FSharp.Stats/Distributions/Continuous/F.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ open FSharp.Stats.Ops
// wiki: "https://en.wikipedia.org/wiki/F-distribution"
// ######

/// F-distribution purely functional helper functions.
module internal F_Helpers =
let assertValidDof name dof1 =
if isNan(dof1) || dof1 <= 0.0 then
failwithf "Invalid definition of freedom %s \"%A\".%s"
name
dof1
"It must not be NaN and it must be positive"

/// F-distribution
type F =

// F-distribution helper functions.
static member CheckParam dof1 dof2 =
if dof1 <= 0.0 || dof2 <= 0.0 || isNan(dof1) || isNan(dof2) then
failwith "F-distribution should be parametrized by dof1 and dof2 > 0.0. and <> nan"
static member CheckParam (dof1) (dof2) : unit =
dof1 |> F_Helpers.assertValidDof "dof1"
dof2 |> F_Helpers.assertValidDof "dof2"

static member private CheckX x =
if x<0. || isNan(x) then
Expand Down Expand Up @@ -99,11 +107,14 @@ type F =
//Beta.lowerIncomplete (dof2 * 0.5) (dof1 * 0.5) u
failwithf "InvCDF not implemented yet"

/// Returns the support of the exponential distribution: (0., Positive Infinity).
static member Support dof1 dof2 =
F.CheckParam dof1 dof2
(0., System.Double.PositiveInfinity)

/// Returns the support of the exponential distribution: if dof1 = 1 then (0., Positive Infinity) else [0., Positive Infinity).
static member Support dof1 =
dof1 |> F_Helpers.assertValidDof "dof1"
if dof1 = 1 then
Interval.CreateOpen<float>(0.0, Double.PositiveInfinity)
else
Interval.CreateRightOpen<float>(0.0, Double.PositiveInfinity)

/// A string representation of the distribution.
static member ToString dof1 dof2 =
sprintf "FisherSnedecor(d1 = %f, d2 = %f" dof1 dof2
Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Stats/Distributions/Continuous/Gamma.fs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ type Gamma =
let alpha,beta = Gamma.Fit(observations,maxIter,tol)
Gamma.Init alpha beta

/// Returns the support of the exponential distribution: [0, Positive Infinity).
/// Returns the support of the gamma distribution: (0, Positive Infinity).
static member Support alpha beta =
Gamma.CheckParam alpha beta
(0.0, System.Double.PositiveInfinity)
Interval.CreateOpen<float>(0.0, Double.PositiveInfinity)

/// A string representation of the distribution.
static member ToString alpha beta =
Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Stats/Distributions/Continuous/LogNormal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ type LogNormal =
LogNormal.CheckParam mu sigma
Math.Exp (Normal.InvCDF mu sigma x)

/// Returns the support of the exponential distribution: [0, Positive Infinity).
/// Returns the support of the log normal distribution: (0, Positive Infinity).
static member Support mu sigma =
LogNormal.CheckParam mu sigma
(0., System.Double.PositiveInfinity)
Interval.CreateOpen<float>(0., Double.PositiveInfinity)

/// A string representation of the distribution.
static member ToString mu sigma =
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Continuous/Normal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ type Normal =
/// Returns the support of the exponential distribution: [0, Positive Infinity).
static member Support mu sigma =
Normal.CheckParam mu sigma
(System.Double.NegativeInfinity, System.Double.PositiveInfinity)
Interval.CreateRightOpen<float>(Double.NegativeInfinity, Double.PositiveInfinity)


/// A string representation of the distribution.
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Continuous/StudentT.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type StudentT =
/// Returns the support of the exponential distribution: (Negative Infinity, Positive Infinity).
static member Support mu tau dof =
StudentT.CheckParam mu tau dof
(System.Double.NegativeInfinity, System.Double.PositiveInfinity)
Interval.CreateOpen<float>(Double.NegativeInfinity, Double.PositiveInfinity)

/// A string representation of the distribution.
static member ToString mu tau dof =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type NegativeBinomial_trials =
/// <summary>Returns the support of the NegativeBinomial distribution: [0, max Int32).</summary>
static member Support r p =
NegativeBinomial_trials.CheckParam r p
(r, System.Int32.MaxValue)
Interval.CreateRightOpen<int>(r, Int32.MaxValue)

/// A string representation of the distribution.
static member ToString r p =
Expand Down
61 changes: 55 additions & 6 deletions tests/FSharp.Stats.Tests/DistributionsContinuous.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,24 +1038,73 @@ let FDistributionTests =
let dof1 = 10.
let dof2 = 25.
let testcase =
Continuous.F.Support dof1 dof2
let r_value = (0., System.Double.PositiveInfinity)
Continuous.F.Support dof1
let r_value = Interval.CreateRightOpen<float>(0., System.Double.PositiveInfinity)

Expect.isTrue
((fst testcase=fst r_value) && (snd testcase=snd r_value))
((testcase.GetStart() = r_value.GetStart()) &&
( testcase.GetEnd() = r_value.GetEnd()))
"Continuous.F.Support does not return the expected Tupel"

testCase "Continuous.F.Support_infinity" <| fun () ->
let dof1 = infinity
let dof2 = infinity
let testcase =
Continuous.F.Support dof1 dof2
let r_value = (0., System.Double.PositiveInfinity)
Continuous.F.Support dof1
let r_value = Interval.CreateRightOpen<float>(0., Double.PositiveInfinity)

Expect.isTrue
((fst testcase=fst r_value) && (snd testcase=snd r_value))
((testcase.GetStart() = r_value.GetStart()) &&
(testcase.GetEnd() = r_value.GetEnd()))
"Continuous.F.Support does not return the expected Tupel"

testCase "Continuous.F.Support_when_dof1_equals_1" <| fun() ->
// Arrange
let dof1 = 1.0

// Act
let result =
F.Support dof1

// Assert
match result with
| Interval.Open (start, end_) ->
Expect.equal
start
0.0
"Expectation on \"Start\":"
Expect.equal
end_
Double.PositiveInfinity
"Expectation on \"End\":"
| other ->
Expect.isTrue
false
(sprintf "Expected a fully open range (true), but it was %A (false)" other)

testCase "Continuous.F.Support_when_dof1_is_not_equal_to_1" <| fun() ->
// Arrange
let dof1 = 2.0

// Act
let result =
F.Support dof1

// Assert
match result with
| Interval.RightOpen (start, end_) ->
Expect.equal
start
0.0
"Expectation on \"Start\":"
Expect.equal
end_
Double.PositiveInfinity
"Expectation on \"End\":"
| other ->
Expect.isTrue
false
(sprintf "Expected a right open range (true), but it was %A (false)" other)

]

Expand Down

0 comments on commit e8971fb

Please sign in to comment.