diff --git a/src/Numerics/Distributions/Multivariate/NormalGamma.cs b/src/Numerics/Distributions/Multivariate/NormalGamma.cs index 73b3adf8..b7f70bfe 100644 --- a/src/Numerics/Distributions/Multivariate/NormalGamma.cs +++ b/src/Numerics/Distributions/Multivariate/NormalGamma.cs @@ -201,6 +201,7 @@ namespace MathNet.Numerics.Distributions public double PrecisionShape { get { return _precisionShape; } + set { SetParameters(_meanLocation, _meanScale, value, _precisionInvScale); } } /// @@ -209,6 +210,28 @@ namespace MathNet.Numerics.Distributions public double PrecisionInverseScale { get { return _precisionInvScale; } + set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); } + } + + /// + /// Gets or sets the random number generator which is used to draw random samples. + /// + public Random RandomSource + { + get + { + return _random; + } + + set + { + if (value == null) + { + throw new ArgumentNullException(); + } + + _random = value; + } } /// @@ -217,7 +240,14 @@ namespace MathNet.Numerics.Distributions /// public StudentT MeanMarginal() { - return new StudentT(_meanLocation, _meanScale * _precisionShape / _precisionInvScale, 2.0 * _precisionShape); + if (Double.IsPositiveInfinity(_precisionInvScale)) + { + return new StudentT(_meanLocation, _meanScale * _precisionShape, Double.PositiveInfinity); + } + else + { + return new StudentT(_meanLocation, _meanScale * _precisionShape / _precisionInvScale, 2.0 * _precisionShape); + } } /// @@ -229,7 +259,6 @@ namespace MathNet.Numerics.Distributions return new Gamma(_precisionShape, _precisionInvScale); } - /* /// /// Gets the mean of the distribution. /// @@ -249,11 +278,7 @@ namespace MathNet.Numerics.Distributions } } - /// - /// Gets or sets the random number generator. - /// - /// The random number generator used to generate a random sample. - public System.Random RandomNumberGenerator { get; set; } + /* /// /// The mode of the distribution. @@ -378,31 +403,6 @@ namespace MathNet.Numerics.Distributions return NormalGamma.Sample(RandomNumberGenerator, size, _meanLocation, _meanScale, _precisionShape, _precisionInvScale); } - /// - /// Checks the parameters of a NormalGamma distribution. - /// - /// The scale of the mean. - /// The shape of the precision. - /// The inverse scale of the precision. - /// If the mean scale is negative. - /// If the inverse precision scale is negative. - /// If the precision shape is negative. - private static void CheckParameters(double meanScale, double precShape, double precInvScale) - { - if (meanScale < 0.0) - { - throw new ArgumentOutOfRangeException("meanScale", Resources.ParameterCannotBeNegative); - } - else if (precShape <= 0.0) - { - throw new ArgumentOutOfRangeException("precShape", Resources.ParameterCannotBeNegative); - } - else if (precInvScale <= 0.0) - { - throw new ArgumentOutOfRangeException("precInvScale", Resources.ParameterCannotBeNegative); - } - } - /// /// Samples an array of NormalGamma distributed random variables. /// diff --git a/src/UnitTests/DistributionTests/Continuous/BetaTests.cs b/src/UnitTests/DistributionTests/Continuous/BetaTests.cs index 283fa501..20e763df 100644 --- a/src/UnitTests/DistributionTests/Continuous/BetaTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/BetaTests.cs @@ -191,7 +191,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests } [Test] - [ExpectedException(typeof(NotSupportedException))] + [ExpectedException(typeof(NotImplementedException))] [Row(0.0, 0.0)] [Row(0.0, 0.1)] [Row(1.0, 0.0)] diff --git a/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs b/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs index 17943614..2b69d31b 100644 --- a/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs +++ b/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs @@ -80,14 +80,14 @@ namespace MathNet.Numerics.UnitTests.DistributionTests } [Test] - public void HasRandomSource(int i) + public void HasRandomSource() { Dirichlet d = new Dirichlet(0.3, 5); Assert.IsNotNull(d.RandomSource); } [Test] - public void CanSetRandomSource(int i) + public void CanSetRandomSource() { Dirichlet d = new Dirichlet(0.3, 5); d.RandomSource = new Random(); @@ -95,7 +95,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests [Test] [ExpectedException(typeof(ArgumentNullException))] - public void FailSetRandomSourceWithNullReference(int i) + public void FailSetRandomSourceWithNullReference() { Dirichlet d = new Dirichlet(0.3, 5); d.RandomSource = null; diff --git a/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs b/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs index c38895db..db3a8883 100644 --- a/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs +++ b/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs @@ -103,5 +103,101 @@ namespace MathNet.Numerics.UnitTests.DistributionTests AssertEx.AreEqual(precShape, ng.PrecisionShape); AssertEx.AreEqual(precInvScale, ng.PrecisionInverseScale); } + + [Test] + [Row(0.0, 1.0, 1.0, 1.0)] + [Row(10.0, 1.0, 2.0, 2.0)] + public void CanGetPrecisionShape(double meanLocation, double meanScale, double precShape, double precInvScale) + { + NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale); + AssertEx.AreEqual(precShape, ng.PrecisionShape); + } + + [Test, MultipleAsserts] + [Row(0.0, 1.0, 1.0, 1.0)] + [Row(10.0, 1.0, 2.0, 2.0)] + public void CanSetPrecisionShape(double meanLocation, double meanScale, double precShape, double precInvScale) + { + NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale); + ng.PrecisionShape = 5.0; + AssertEx.AreEqual(meanLocation, ng.MeanLocation); + AssertEx.AreEqual(meanScale, ng.MeanScale); + AssertEx.AreEqual(5.0, ng.PrecisionShape); + AssertEx.AreEqual(precInvScale, ng.PrecisionInverseScale); + } + + [Test] + [Row(0.0, 1.0, 1.0, 1.0)] + [Row(10.0, 1.0, 2.0, 2.0)] + public void CanGetPrecisionInverseScale(double meanLocation, double meanScale, double precShape, double precInvScale) + { + NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale); + AssertEx.AreEqual(precInvScale, ng.PrecisionInverseScale); + } + + [Test, MultipleAsserts] + [Row(0.0, 1.0, 1.0, 1.0)] + [Row(10.0, 1.0, 2.0, 2.0)] + public void CanSetPrecisionPrecisionInverseScale(double meanLocation, double meanScale, double precShape, double precInvScale) + { + NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale); + ng.PrecisionInverseScale = 5.0; + AssertEx.AreEqual(meanLocation, ng.MeanLocation); + AssertEx.AreEqual(meanScale, ng.MeanScale); + AssertEx.AreEqual(precShape, ng.PrecisionShape); + AssertEx.AreEqual(5.0, ng.PrecisionInverseScale); + } + + [Test, MultipleAsserts] + [Row(0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 2.0)] + [Row(10.0, 1.0, 2.0, 2.0, 10.0, 1.0, 4.0)] + [Row(10.0, 1.0, 2.0, Double.PositiveInfinity, 10.0, 2.0, Double.PositiveInfinity)] + public void CanGetMeanMarginal(double meanLocation, double meanScale, double precShape, double precInvScale, + double meanMarginalMean, double meanMarginalScale, double meanMarginalDoF) + { + NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale); + var mm = ng.MeanMarginal(); + AssertEx.AreEqual(meanMarginalMean, mm.Location); + AssertEx.AreEqual(meanMarginalScale, mm.Scale); + AssertEx.AreEqual(meanMarginalDoF, mm.DegreesOfFreedom); + } + + [Test, MultipleAsserts] + [Row(0.0, 1.0, 1.0, 1.0)] + [Row(10.0, 1.0, 2.0, 2.0)] + [Row(10.0, 1.0, 2.0, Double.PositiveInfinity)] + public void CanGetPrecisionMarginal(double meanLocation, double meanScale, double precShape, double precInvScale) + { + NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale); + var pm = ng.PrecisionMarginal(); + AssertEx.AreEqual(precShape, pm.Shape); + AssertEx.AreEqual(precInvScale, pm.InvScale); + } + + [Test, MultipleAsserts] + [Row(0.0, 1.0, 1.0, 1.0, 0.0, 1.0)] + [Row(10.0, 1.0, 2.0, 2.0, 10.0, 1.0)] + [Row(10.0, 1.0, 2.0, Double.PositiveInfinity, 10.0, 2.0)] + public void CanGetMean(double meanLocation, double meanScale, double precShape, double precInvScale, + double meanMean, double meanPrecision) + { + NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale); + AssertEx.AreEqual(meanMean, ng.Mean.Mean); + AssertEx.AreEqual(meanPrecision, ng.Mean.Precision); + } + + [Test] + public void HasRandomSource() + { + NormalGamma ng = new NormalGamma(0.0, 1.0, 1.0, 1.0); + Assert.IsNotNull(ng.RandomSource); + } + + [Test] + public void CanSetRandomSource() + { + NormalGamma ng = new NormalGamma(0.0, 1.0, 1.0, 1.0); + ng.RandomSource = new Random(); + } } } \ No newline at end of file