|
|
@ -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; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|