Browse Source

More unit tests for StudentT distribution.

la-knuth
Jurgen Van Gael 17 years ago
parent
commit
c66021cd15
  1. 2
      src/Numerics/Distributions/Continuous/Beta.cs
  2. 16
      src/Numerics/Distributions/Continuous/StudentT.cs
  3. 1
      src/Numerics/Numerics.csproj
  4. 169
      src/UnitTests/DistributionTests/Continuous/StudentTTests.cs
  5. 1
      src/UnitTests/UnitTests.csproj

2
src/Numerics/Distributions/Continuous/Beta.cs

@ -335,7 +335,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Median
{
get { throw new NotSupportedException(); }
get { throw new NotImplementedException(); }
}
/// <summary>

16
src/Numerics/Distributions/Continuous/StudentT.cs

@ -37,8 +37,8 @@ namespace MathNet.Numerics.Distributions
/// <a href="http://en.wikipedia.org/wiki/Student%27s_t-distribution">Wikipedia - Student's t-distribution</a>.
/// </summary>
/// <remarks><para>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.</para>
/// Namely, one which also parameterizes the location and scale. See the book "Bayesian Data Analysis" by Gelman
/// et al. for more details.</para>
/// <para>The distribution will use the <see cref="System.Random"/> by default.
/// Users can get/set the random number generator by using the <see cref="RandomSource"/> property.</para>
/// <para>The statistics classes will check all the incoming parameters whether they are in the allowed
@ -210,7 +210,17 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return _location; }
get
{
if (_dof > 1.0)
{
return _location;
}
else
{
return Double.NaN;
}
}
}
/// <summary>

1
src/Numerics/Numerics.csproj

@ -213,6 +213,7 @@
<LastGenOutput>SafeNativeMethods.cs</LastGenOutput>
</None>
<Compile Include="Distributions\Continuous\StudentT.cs" />
<Compile Include="Distributions\Multivariate\NormalGamma.cs" />
<Compile Include="Version.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>

169
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<double>(0.0, n.Location);
AssertEx.AreEqual<double>(1.0, n.Scale);
AssertEx.AreEqual<double>(1.0, n.DegreesOfFreedom);
AssertEx.AreEqual<double>(location, n.Location);
AssertEx.AreEqual<double>(scale, n.Scale);
AssertEx.AreEqual<double>(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<double>(mean, n.Mean);
AssertEx.AreEqual<double>(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<string>("Normal(Mean = 1, StdDev = 2)", n.ToString());
var n = new StudentT(1.0, 2.0, 1.0);
AssertEx.AreEqual<string>("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<double>(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<double>(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<double>(MathNet.Numerics.Constants.LogSqrt2PiE + Math.Log(n.StdDev), n.Entropy);

1
src/UnitTests/UnitTests.csproj

@ -103,6 +103,7 @@
<Compile Include="DistributionTests\Discrete\DiscreteUniformTests.cs" />
<Compile Include="DistributionTests\Multivariate\DirichletTests.cs" />
<Compile Include="DistributionTests\Multivariate\MultinomialTests.cs" />
<Compile Include="DistributionTests\Multivariate\NormalGammaTests.cs" />
<Compile Include="IntegralTransformsTests\HartleyTest.cs" />
<Compile Include="IntegralTransformsTests\FourierTest.cs" />
<Compile Include="IntegralTransformsTests\MatchingNaiveTransformTest.cs" />

Loading…
Cancel
Save