Skip to content

Commit 206ee41

Browse files
committed
Distributions: Migrate PERT from Beta to BetaScaled #321
1 parent fc9f338 commit 206ee41

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

src/Numerics/Distributions/Beta.cs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -92,49 +92,6 @@ public Beta(double a, double b, System.Random randomSource)
9292
_shapeB = b;
9393
}
9494

95-
96-
/// <summary>
97-
/// Create a Beta PERT distribution, used in risk analysis and other domains where an expert forecast
98-
/// is used to construct an underlying beta distribution.
99-
/// </summary>
100-
/// <param name="min">The minimum value.</param>
101-
/// <param name="max">The maximum value.</param>
102-
/// <param name="likely">The most likely value (mode).</param>
103-
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
104-
/// <returns>The Beta distribution derived from the PERT parameters.</returns>
105-
public static Beta PERT(double min, double max, double likely, System.Random randomSource = null)
106-
{
107-
if( min > max || likely > max || likely < min )
108-
{
109-
throw new ArgumentException(Resources.InvalidDistributionParameters);
110-
}
111-
112-
// specified to make the formulas match the literature;
113-
// traditionally set to 4 so that the range between min and max
114-
// represents six standard deviations (sometimes called
115-
// "the six-sigma assumption").
116-
const double lambda = 4;
117-
118-
// calculate the mean
119-
double mean = (min + max + lambda * likely) / (lambda + 2);
120-
121-
// derive the shape parameters a and b
122-
double a;
123-
// special case where mean and mode are identical
124-
if (mean == likely)
125-
{
126-
a = (lambda/2) + 1;
127-
}
128-
else
129-
{
130-
a = ((mean - min) * (2 * likely - min - max)) / ((likely - mean) * (max - min));
131-
}
132-
133-
double b = (a * (max - mean)) / (mean - min);
134-
135-
return new Beta(a, b, randomSource);
136-
}
137-
13895
/// <summary>
13996
/// A string representation of the distribution.
14097
/// </summary>

src/Numerics/Distributions/BetaScaled.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,49 @@ public BetaScaled(double a, double b, double location, double scale, System.Rand
8888
_scale = scale;
8989
}
9090

91+
/// <summary>
92+
/// Create a Beta PERT distribution, used in risk analysis and other domains where an expert forecast
93+
/// is used to construct an underlying beta distribution.
94+
/// </summary>
95+
/// <param name="min">The minimum value.</param>
96+
/// <param name="max">The maximum value.</param>
97+
/// <param name="likely">The most likely value (mode).</param>
98+
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
99+
/// <returns>The Beta distribution derived from the PERT parameters.</returns>
100+
public static BetaScaled PERT(double min, double max, double likely, System.Random randomSource = null)
101+
{
102+
if (min > max || likely > max || likely < min)
103+
{
104+
throw new ArgumentException(Resources.InvalidDistributionParameters);
105+
}
106+
107+
// specified to make the formulas match the literature;
108+
// traditionally set to 4 so that the range between min and max
109+
// represents six standard deviations (sometimes called
110+
// "the six-sigma assumption").
111+
const double lambda = 4;
112+
113+
// calculate the mean
114+
double mean = (min + max + lambda * likely) / (lambda + 2);
115+
116+
// derive the shape parameters a and b
117+
double a;
118+
119+
// special case where mean and mode are identical
120+
if (mean == likely)
121+
{
122+
a = (lambda / 2) + 1;
123+
}
124+
else
125+
{
126+
a = ((mean - min) * (2 * likely - min - max)) / ((likely - mean) * (max - min));
127+
}
128+
129+
double b = (a * (max - mean)) / (mean - min);
130+
131+
return new BetaScaled(a, b, min, max - min, randomSource);
132+
}
133+
91134
/// <summary>
92135
/// A string representation of the distribution.
93136
/// </summary>

0 commit comments

Comments
 (0)