diff --git a/src/Numerics/Distributions/Continuous/Beta.cs b/src/Numerics/Distributions/Continuous/Beta.cs index ad48a83a..86a8d28c 100644 --- a/src/Numerics/Distributions/Continuous/Beta.cs +++ b/src/Numerics/Distributions/Continuous/Beta.cs @@ -335,7 +335,7 @@ namespace MathNet.Numerics.Distributions /// public double Median { - get { throw new NotSupportedException(); } + get { throw new NotImplementedException(); } } /// diff --git a/src/Numerics/Distributions/Continuous/StudentT.cs b/src/Numerics/Distributions/Continuous/StudentT.cs index 6caef011..b514c175 100644 --- a/src/Numerics/Distributions/Continuous/StudentT.cs +++ b/src/Numerics/Distributions/Continuous/StudentT.cs @@ -37,8 +37,8 @@ namespace MathNet.Numerics.Distributions /// Wikipedia - Student's t-distribution. /// /// We use a slightly generalized version (compared to Wikipedia) of the Student t-distribution. - /// Namely, one which also parameterizes the location and scale. See the book "Bayesian Data Analysis" for more - /// details. + /// Namely, one which also parameterizes the location and scale. See the book "Bayesian Data Analysis" by Gelman + /// et al. for more details. /// The distribution will use the by default. /// Users can get/set the random number generator by using the property. /// The statistics classes will check all the incoming parameters whether they are in the allowed @@ -210,7 +210,17 @@ namespace MathNet.Numerics.Distributions /// public double Mean { - get { return _location; } + get + { + if (_dof > 1.0) + { + return _location; + } + else + { + return Double.NaN; + } + } } /// diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index a74a9378..cd2e7f9c 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -213,6 +213,7 @@ SafeNativeMethods.cs + True True diff --git a/src/UnitTests/DistributionTests/Continuous/StudentTTests.cs b/src/UnitTests/DistributionTests/Continuous/StudentTTests.cs index c6a0ed3d..a5621fda 100644 --- a/src/UnitTests/DistributionTests/Continuous/StudentTTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/StudentTTests.cs @@ -54,7 +54,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests [Test, MultipleAsserts] [Row(0.0, 1.0, 1.0)] [Row(0.0, 0.1, 1.0)] - [Row(0.0, 1.0, 1.0)] + [Row(0.0, 1.0, 3.0)] [Row(0.0, 10.0, 1.0)] [Row(0.0, 10.0, Double.PositiveInfinity)] [Row(10.0, 1.0, 1.0)] @@ -63,153 +63,128 @@ namespace MathNet.Numerics.UnitTests.DistributionTests public void CanCreateStudentT(double location, double scale, double dof) { var n = new StudentT(location, scale, dof); - AssertEx.AreEqual(0.0, n.Location); - AssertEx.AreEqual(1.0, n.Scale); - AssertEx.AreEqual(1.0, n.DegreesOfFreedom); + AssertEx.AreEqual(location, n.Location); + AssertEx.AreEqual(scale, n.Scale); + AssertEx.AreEqual(dof, n.DegreesOfFreedom); } - /* - [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] - [Row(Double.NaN, 1.0)] - [Row(1.0, Double.NaN)] - [Row(Double.NaN, Double.NaN)] - [Row(1.0, -1.0)] - public void NormalCreateFailsWithBadParameters(double mean, double sdev) + [Row(Double.NaN, 1.0, 1.0)] + [Row(0.0, Double.NaN, 1.0)] + [Row(0.0, 1.0, Double.NaN)] + [Row(0.0, -10.0, 1.0)] + [Row(0.0, 10.0, -1.0)] + public void StudentTCreateFailsWithBadParameters(double location, double scale, double dof) { - var n = new Normal(mean, sdev); - } - - [Test, MultipleAsserts] - [Row(0.0, 0.0)] - [Row(0.0, 0.1)] - [Row(0.0, 1.0)] - [Row(0.0, 10.0)] - [Row(10.0, 1.0)] - [Row(-5.0, 100.0)] - [Row(0.0, Double.PositiveInfinity)] - public void CanCreateNormalFromMeanAndStdDev(double mean, double sdev) - { - var n = Normal.WithMeanStdDev(mean, sdev); - AssertEx.AreEqual(mean, n.Mean); - AssertEx.AreEqual(sdev, n.StdDev); - } - - [Test, MultipleAsserts] - [Row(0.0, 0.0)] - [Row(0.0, 0.1)] - [Row(0.0, 1.0)] - [Row(0.0, 10.0)] - [Row(10.0, 1.0)] - [Row(-5.0, 100.0)] - [Row(0.0, Double.PositiveInfinity)] - public void CanCreateNormalFromMeanAndVariance(double mean, double var) - { - var n = Normal.WithMeanVariance(mean, var); - AssertHelpers.AlmostEqual(mean, n.Mean, 16); - AssertHelpers.AlmostEqual(var, n.Variance, 16); - } - - [Test, MultipleAsserts] - [Row(0.0, 0.0)] - [Row(0.0, 0.1)] - [Row(0.0, 1.0)] - [Row(0.0, 10.0)] - [Row(10.0, 1.0)] - [Row(-5.0, 100.0)] - [Row(0.0, Double.PositiveInfinity)] - public void CanCreateNormalFromMeanAndPrecision(double mean, double prec) - { - var n = Normal.WithMeanPrecision(mean, prec); - AssertHelpers.AlmostEqual(mean, n.Mean, 15); - AssertHelpers.AlmostEqual(prec, n.Precision, 15); + var n = new StudentT(location, scale, dof); } [Test] public void ValidateToString() { - var n = new Normal(1.0, 2.0); - AssertEx.AreEqual("Normal(Mean = 1, StdDev = 2)", n.ToString()); + var n = new StudentT(1.0, 2.0, 1.0); + AssertEx.AreEqual("StudentT(Location = 1, Scale = 2, DoF = 1)", n.ToString()); } [Test] + [Row(Double.NegativeInfinity)] + [Row(-5.0)] [Row(-0.0)] [Row(0.0)] [Row(0.1)] [Row(1.0)] [Row(10.0)] [Row(Double.PositiveInfinity)] - public void CanSetPrecision(double prec) + public void CanSetLocation(double loc) { - var n = new Normal(); - n.Precision = prec; - } - - [Test] - [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void SetPrecisionFailsWithNegativePrecision() - { - var n = new Normal(); - n.Precision = -1.0; + var n = new StudentT(); + n.Location = loc; } [Test] - [Row(-0.0)] - [Row(0.0)] [Row(0.1)] [Row(1.0)] [Row(10.0)] [Row(Double.PositiveInfinity)] - public void CanSetVariance(double var) + public void CanSetScale(double scale) { - var n = new Normal(); - n.Variance = var; + var n = new StudentT(); + n.Scale = scale; } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void SetVarianceFailsWithNegativeVariance() + [Row(-1.0)] + [Row(-0.0)] + [Row(0.0)] + public void SetScaleFailsWithNonPositiveScale(double scale) { - var n = new Normal(); - n.Variance = -1.0; + { + var n = new StudentT(); + n.Scale = scale; + } } [Test] - [Row(-0.0)] - [Row(0.0)] [Row(0.1)] [Row(1.0)] [Row(10.0)] [Row(Double.PositiveInfinity)] - public void CanSetStdDev(double sdev) + public void CanSetDoF(double dof) { - var n = new Normal(); - n.StdDev = sdev; + var n = new StudentT(); + n.DegreesOfFreedom = dof; } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void SetStdDevFailsWithNegativeStdDev() + [Row(-1.0)] + [Row(-0.0)] + [Row(0.0)] + public void SetDofFailsWithNonPositiveDoF(double dof) { - var n = new Normal(); - n.StdDev = -1.0; + { + var n = new StudentT(); + n.DegreesOfFreedom = dof; + } } [Test] - [Row(Double.NegativeInfinity)] - [Row(-0.0)] - [Row(0.0)] - [Row(0.1)] - [Row(1.0)] - [Row(10.0)] - [Row(Double.PositiveInfinity)] - public void CanSetMean(double mean) + [Row(0.0, 1.0, 1.0, Double.NaN)] + [Row(0.0, 0.1, 1.0, Double.NaN)] + [Row(0.0, 1.0, 3.0, 0.0)] + [Row(0.0, 10.0, 1.0, Double.NaN)] + [Row(0.0, 10.0, 2.0, 0.0)] + [Row(0.0, 10.0, Double.PositiveInfinity, 0.0)] + [Row(10.0, 1.0, 1.0, Double.NaN)] + [Row(-5.0, 100.0, 1.5, -5.0)] + [Row(0.0, Double.PositiveInfinity, 1.0)] + public void ValidateMean(double location, double scale, double dof, double mean) { - var n = new Normal(); - n.Mean = mean; + var n = new StudentT(location, scale, dof); + AssertEx.AreEqual(n.Mean, mean); + } +/* + [Test] + [Row(0.0, 1.0, 1.0)] + [Row(0.0, 0.1, 1.0)] + [Row(0.0, 1.0, 3.0)] + [Row(0.0, 10.0, 1.0)] + [Row(0.0, 10.0, 2.0)] + [Row(0.0, 10.0, 3.0)] + [Row(0.0, 10.0, Double.PositiveInfinity)] + [Row(10.0, 1.0, 1.0)] + [Row(-5.0, 100.0, 1.0)] + [Row(0.0, Double.PositiveInfinity, 1.0)] + public void ValidateVariance(double location, double scale, double dof, double var) + { + var n = new StudentT(location, scale, dof); + AssertEx.AreEqual(n.Variance, location); } + + [Test] [Row(-0.0)] [Row(0.0)] @@ -217,7 +192,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests [Row(1.0)] [Row(10.0)] [Row(Double.PositiveInfinity)] - public void ValidateEntropy(double sdev) + public void Entropy(double sdev) { var n = new Normal(1.0, sdev); AssertEx.AreEqual(MathNet.Numerics.Constants.LogSqrt2PiE + Math.Log(n.StdDev), n.Entropy); diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index d396294a..d1d0c70d 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -103,6 +103,7 @@ +