Browse Source

Statistics: TruncatedNormal minor formatting tweaks #344

truncatednormal
Christoph Ruegg 11 years ago
parent
commit
7e0b98c625
  1. 31
      src/Numerics/Distributions/TruncatedNormal.cs
  2. 4
      src/UnitTests/DistributionTests/CommonDistributionTests.cs
  3. 5
      src/UnitTests/DistributionTests/Continuous/TruncatedNormalTests.cs

31
src/Numerics/Distributions/TruncatedNormal.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2013 Math.NET // Copyright (c) 2009-2015 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -42,24 +42,27 @@ namespace MathNet.Numerics.Distributions {
/// </summary> /// </summary>
public class TruncatedNormal : IContinuousDistribution public class TruncatedNormal : IContinuousDistribution
{ {
System.Random _random; System.Random _random;
/// <summary> /// <summary>
/// Mean of the untruncated normal distribution. /// Mean of the untruncated normal distribution.
/// </summary> /// </summary>
readonly double _mu; readonly double _mu;
/// <summary> /// <summary>
/// Standard deviation of the uncorrected normal distribution. /// Standard deviation of the uncorrected normal distribution.
/// </summary> /// </summary>
readonly double _sigma; readonly double _sigma;
readonly double _lowerBound; readonly double _lowerBound;
readonly double _upperBound; readonly double _upperBound;
readonly Normal _standardNormal = new Normal(0.0, 1.0); readonly Normal _standardNormal = new Normal(0.0, 1.0);
/// <summary> /// <summary>
/// Position in the standard normal distribution of the lower bound. /// Position in the standard normal distribution of the lower bound.
/// </summary> /// </summary>
readonly double _alpha; readonly double _alpha;
/// <summary> /// <summary>
/// Position in the standard normal distribution of the upper bound. /// Position in the standard normal distribution of the upper bound.
/// </summary> /// </summary>
@ -82,9 +85,8 @@ namespace MathNet.Numerics.Distributions {
/// <param name="upperBound">The inclusive upper bound of the truncated distribution. Must be larger than <paramref name="lowerBound"/>. /// <param name="upperBound">The inclusive upper bound of the truncated distribution. Must be larger than <paramref name="lowerBound"/>.
/// Default is double.PositiveInfinity.</param> /// Default is double.PositiveInfinity.</param>
public TruncatedNormal(double mean, double stddev, double lowerBound = double.NegativeInfinity, double upperBound = double.PositiveInfinity) public TruncatedNormal(double mean, double stddev, double lowerBound = double.NegativeInfinity, double upperBound = double.PositiveInfinity)
:this(mean, stddev, SystemRandomSource.Default, lowerBound, upperBound) : this(mean, stddev, SystemRandomSource.Default, lowerBound, upperBound)
{ {
} }
/// <summary> /// <summary>
@ -128,14 +130,15 @@ namespace MathNet.Numerics.Distributions {
return normalRequirements && boundsAreOrdered; return normalRequirements && boundsAreOrdered;
} }
public override string ToString() { public override string ToString()
{
return "TruncatedNormal(μ = " + _mu + ", σ = " + _sigma +", LowerBound = " + _lowerBound + ", UpperBound = " + _upperBound + ")"; return "TruncatedNormal(μ = " + _mu + ", σ = " + _sigma +", LowerBound = " + _lowerBound + ", UpperBound = " + _upperBound + ")";
} }
/// <summary> /// <summary>
/// Gets the mode of the normal distribution. /// Gets the mode of the truncated normal distribution.
/// </summary> /// </summary>
public double Mode public double Mode
{ {
get get
{ {
@ -281,8 +284,7 @@ namespace MathNet.Numerics.Distributions {
public double DensityLn(double x) public double DensityLn(double x)
{ {
return _standardNormal.DensityLn((x - _mu) / _sigma) - Math.Log(_sigma) - Math.Log(_cumulativeDensityWithinBounds); return _standardNormal.DensityLn((x - _mu) / _sigma) - Math.Log(_sigma) - Math.Log(_cumulativeDensityWithinBounds);
} }
public double Sample() public double Sample()
{ {
@ -293,14 +295,16 @@ namespace MathNet.Numerics.Distributions {
public void Samples(double[] values) public void Samples(double[] values)
{ {
for(int i = 0; i < values.Length; i++) { for(int i = 0; i < values.Length; i++)
{
values[i] = Sample(); values[i] = Sample();
} }
} }
public IEnumerable<double> Samples() public IEnumerable<double> Samples()
{ {
while (true) { while (true)
{
yield return Sample(); yield return Sample();
} }
} }
@ -336,6 +340,5 @@ namespace MathNet.Numerics.Distributions {
return _standardNormal.InverseCumulativeDistribution(pUntruncated) * _sigma + _mu; return _standardNormal.InverseCumulativeDistribution(pUntruncated) * _sigma + _mu;
} }
} }
} }

4
src/UnitTests/DistributionTests/CommonDistributionTests.cs

@ -89,8 +89,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
new StudentT(0.0, 1.0, 5.0), new StudentT(0.0, 1.0, 5.0),
new Triangular(0, 1, 0.7), new Triangular(0, 1, 0.7),
new Weibull(1.0, 1.0), new Weibull(1.0, 1.0),
new TruncatedNormal(0, 1.0, -1.0, 1.5), //Finite new TruncatedNormal(0, 1.0, -1.0, 1.5), //Finite
new TruncatedNormal(0, 1.0, -0.5), //Semi-finite new TruncatedNormal(0, 1.0, -0.5), //Semi-finite
}; };
[Test] [Test]

5
src/UnitTests/DistributionTests/Continuous/TruncatedNormalTests.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2014 Math.NET // Copyright (c) 2009-2015 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -31,16 +31,13 @@
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
[TestFixture, Category("Distributions")] [TestFixture, Category("Distributions")]
public class TruncatedNormalTests public class TruncatedNormalTests
{ {
/// <summary> /// <summary>
/// Can create a truncated normal without bounds. /// Can create a truncated normal without bounds.
/// </summary> /// </summary>

Loading…
Cancel
Save