Browse Source

Distributions: Migrate PERT from Beta to BetaScaled #321

pull/353/head
Christoph Ruegg 11 years ago
parent
commit
206ee4179a
  1. 43
      src/Numerics/Distributions/Beta.cs
  2. 43
      src/Numerics/Distributions/BetaScaled.cs

43
src/Numerics/Distributions/Beta.cs

@ -92,49 +92,6 @@ namespace MathNet.Numerics.Distributions
_shapeB = b;
}
/// <summary>
/// Create a Beta PERT distribution, used in risk analysis and other domains where an expert forecast
/// is used to construct an underlying beta distribution.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="likely">The most likely value (mode).</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <returns>The Beta distribution derived from the PERT parameters.</returns>
public static Beta PERT(double min, double max, double likely, System.Random randomSource = null)
{
if( min > max || likely > max || likely < min )
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
// specified to make the formulas match the literature;
// traditionally set to 4 so that the range between min and max
// represents six standard deviations (sometimes called
// "the six-sigma assumption").
const double lambda = 4;
// calculate the mean
double mean = (min + max + lambda * likely) / (lambda + 2);
// derive the shape parameters a and b
double a;
// special case where mean and mode are identical
if (mean == likely)
{
a = (lambda/2) + 1;
}
else
{
a = ((mean - min) * (2 * likely - min - max)) / ((likely - mean) * (max - min));
}
double b = (a * (max - mean)) / (mean - min);
return new Beta(a, b, randomSource);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>

43
src/Numerics/Distributions/BetaScaled.cs

@ -88,6 +88,49 @@ namespace MathNet.Numerics.Distributions
_scale = scale;
}
/// <summary>
/// Create a Beta PERT distribution, used in risk analysis and other domains where an expert forecast
/// is used to construct an underlying beta distribution.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="likely">The most likely value (mode).</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <returns>The Beta distribution derived from the PERT parameters.</returns>
public static BetaScaled PERT(double min, double max, double likely, System.Random randomSource = null)
{
if (min > max || likely > max || likely < min)
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
// specified to make the formulas match the literature;
// traditionally set to 4 so that the range between min and max
// represents six standard deviations (sometimes called
// "the six-sigma assumption").
const double lambda = 4;
// calculate the mean
double mean = (min + max + lambda * likely) / (lambda + 2);
// derive the shape parameters a and b
double a;
// special case where mean and mode are identical
if (mean == likely)
{
a = (lambda / 2) + 1;
}
else
{
a = ((mean - min) * (2 * likely - min - max)) / ((likely - mean) * (max - min));
}
double b = (a * (max - mean)) / (mean - min);
return new BetaScaled(a, b, min, max - min, randomSource);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>

Loading…
Cancel
Save