diff --git a/src/Numerics/Distributions/FisherSnedecor.cs b/src/Numerics/Distributions/FisherSnedecor.cs index 3d1e8627..cc0cfe17 100644 --- a/src/Numerics/Distributions/FisherSnedecor.cs +++ b/src/Numerics/Distributions/FisherSnedecor.cs @@ -35,7 +35,7 @@ using MathNet.Numerics.Properties; namespace MathNet.Numerics.Distributions { /// - /// Continuous Univariate FisherSnedecor distribution. + /// Continuous Univariate F-distribution, also known as Fisher-Snedecor distribution. /// For details about this distribution, see /// Wikipedia - FisherSnedecor distribution. /// @@ -83,17 +83,6 @@ namespace MathNet.Numerics.Distributions return "FisherSnedecor(d1 = " + _freedom1 + ", d2 = " + _freedom2 + ")"; } - /// - /// Checks whether the parameters of the distribution are valid. - /// - /// The first degree of freedom (d1) of the distribution. Range: d1 > 0. - /// The second degree of freedom (d2) of the distribution. Range: d2 > 0. - /// true when the parameters are valid, false otherwise. - static bool IsValidParameterSet(double d1, double d2) - { - return d1 > 0.0 && d2 > 0.0; - } - /// /// Sets the parameters of the distribution after checking their validity. /// @@ -102,7 +91,7 @@ namespace MathNet.Numerics.Distributions /// When the parameters are out of range. void SetParameters(double d1, double d2) { - if (Control.CheckDistributionParameters && !IsValidParameterSet(d1, d2)) + if (d1 <= 0.0 || d2 <= 0.0 || Double.IsNaN(d1) || Double.IsNaN(d2)) { throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); } @@ -247,6 +236,7 @@ namespace MathNet.Numerics.Distributions /// /// The location at which to compute the density. /// the density at . + /// public double Density(double x) { return Math.Sqrt(Math.Pow(_freedom1*x, _freedom1)*Math.Pow(_freedom2, _freedom2)/Math.Pow((_freedom1*x) + _freedom2, _freedom1 + _freedom2))/(x*SpecialFunctions.Beta(_freedom1/2.0, _freedom2/2.0)); @@ -257,6 +247,7 @@ namespace MathNet.Numerics.Distributions /// /// The location at which to compute the log density. /// the log density at . + /// public double DensityLn(double x) { return Math.Log(Density(x)); @@ -267,23 +258,12 @@ namespace MathNet.Numerics.Distributions /// /// The location at which to compute the cumulative distribution function. /// the cumulative distribution at location . + /// public double CumulativeDistribution(double x) { return SpecialFunctions.BetaRegularized(_freedom1/2.0, _freedom2/2.0, _freedom1*x/((_freedom1*x) + _freedom2)); } - /// - /// Generates one sample from the FisherSnedecor distribution without parameter checking. - /// - /// The random number generator to use. - /// The first degree of freedom (d1) of the distribution. Range: d1 > 0. - /// The second degree of freedom (d2) of the distribution. Range: d2 > 0. - /// a FisherSnedecor distributed random number. - static double SampleUnchecked(System.Random rnd, double d1, double d2) - { - return (ChiSquared.Sample(rnd, d1)/d1)/(ChiSquared.Sample(rnd, d2)/d2); - } - /// /// Generates a sample from the FisherSnedecor distribution. /// @@ -305,6 +285,61 @@ namespace MathNet.Numerics.Distributions } } + /// + /// Generates one sample from the FisherSnedecor distribution without parameter checking. + /// + /// The random number generator to use. + /// The first degree of freedom (d1) of the distribution. Range: d1 > 0. + /// The second degree of freedom (d2) of the distribution. Range: d2 > 0. + /// a FisherSnedecor distributed random number. + static double SampleUnchecked(System.Random rnd, double d1, double d2) + { + return (ChiSquared.Sample(rnd, d1) / d1) / (ChiSquared.Sample(rnd, d2) / d2); + } + + /// + /// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x. + /// + /// The first degree of freedom (d1) of the distribution. Range: d1 > 0. + /// The second degree of freedom (d2) of the distribution. Range: d2 > 0. + /// The location at which to compute the density. + /// the density at . + /// + public static double PDF(double d1, double d2, double x) + { + if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); + + return Math.Sqrt(Math.Pow(d1*x, d1)*Math.Pow(d2, d2)/Math.Pow((d1*x) + d2, d1 + d2))/(x*SpecialFunctions.Beta(d1/2.0, d2/2.0)); + } + + /// + /// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x). + /// + /// The first degree of freedom (d1) of the distribution. Range: d1 > 0. + /// The second degree of freedom (d2) of the distribution. Range: d2 > 0. + /// The location at which to compute the density. + /// the log density at . + /// + public static double PDFLn(double d1, double d2, double x) + { + return Math.Log(PDF(d1, d2, x)); + } + + /// + /// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X ≤ x). + /// + /// The location at which to compute the cumulative distribution function. + /// The first degree of freedom (d1) of the distribution. Range: d1 > 0. + /// The second degree of freedom (d2) of the distribution. Range: d2 > 0. + /// the cumulative distribution at location . + /// + public static double CDF(double d1, double d2, double x) + { + if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); + + return SpecialFunctions.BetaRegularized(d1/2.0, d2/2.0, d1*x/((d1*x) + d2)); + } + /// /// Generates a sample from the distribution. /// @@ -314,10 +349,7 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double d1, double d2) { - if (Control.CheckDistributionParameters && !IsValidParameterSet(d1, d2)) - { - throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); - } + if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); return SampleUnchecked(rnd, d1, d2); } @@ -331,10 +363,7 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double d1, double d2) { - if (Control.CheckDistributionParameters && !IsValidParameterSet(d1, d2)) - { - throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); - } + if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); while (true) { diff --git a/src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs b/src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs index a83410e9..767e6f80 100644 --- a/src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs @@ -361,7 +361,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous public void ValidateDensity(double d1, double d2, double x) { var n = new FisherSnedecor(d1, d2); - Assert.AreEqual(Math.Sqrt(Math.Pow(d1 * x, d1) * Math.Pow(d2, d2) / Math.Pow((d1 * x) + d2, d1 + d2)) / (x * SpecialFunctions.Beta(d1 / 2.0, d2 / 2.0)), n.Density(x)); + double expected = Math.Sqrt(Math.Pow(d1*x, d1)*Math.Pow(d2, d2)/Math.Pow((d1*x) + d2, d1 + d2))/(x*SpecialFunctions.Beta(d1/2.0, d2/2.0)); + Assert.AreEqual(expected, n.Density(x)); + Assert.AreEqual(expected, FisherSnedecor.PDF(d1, d2, x)); } /// @@ -397,7 +399,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous public void ValidateDensityLn(double d1, double d2, double x) { var n = new FisherSnedecor(d1, d2); - Assert.AreEqual(Math.Log(n.Density(x)), n.DensityLn(x)); + double expected = Math.Log(Math.Sqrt(Math.Pow(d1*x, d1)*Math.Pow(d2, d2)/Math.Pow((d1*x) + d2, d1 + d2))/(x*SpecialFunctions.Beta(d1/2.0, d2/2.0))); + Assert.AreEqual(expected, n.DensityLn(x)); + Assert.AreEqual(expected, FisherSnedecor.PDFLn(d1, d2, x)); } /// @@ -442,7 +446,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous public void ValidateCumulativeDistribution(double d1, double d2, double x) { var n = new FisherSnedecor(d1, d2); - Assert.AreEqual(SpecialFunctions.BetaRegularized(d1 / 2.0, d2 / 2.0, d1 * x / (d2 + (x * d1))), n.CumulativeDistribution(x)); + double expected = SpecialFunctions.BetaRegularized(d1/2.0, d2/2.0, d1*x/(d2 + (x*d1))); + Assert.AreEqual(expected, n.CumulativeDistribution(x)); + Assert.AreEqual(expected, FisherSnedecor.CDF(d1, d2, x)); } } }