Skip to content

Commit 320c1cc

Browse files
committed
Merge commit '4966036b86efe39c7a96db67b375f2d7467fa6c1'
2 parents 3476699 + 4966036 commit 320c1cc

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/Numerics/Distributions/Weibull.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,49 @@ public static double CDF(double shape, double scale, double x)
420420
return -SpecialFunctions.ExponentialMinusOne(-Math.Pow(x, shape)*Math.Pow(scale, -shape));
421421
}
422422

423+
/// <summary>
424+
/// Implemented according to: Parameter estimation of the Weibull probability distribution, 1994, Hongzhu Qiao, Chris P. Tsokos
425+
/// </summary>
426+
/// <param name="samples"></param>
427+
/// <param name="randomSource"></param>
428+
/// <returns>Returns a Weibull distribution.</returns>
429+
public static Weibull Estimate(IEnumerable<double> samples, System.Random randomSource = null)
430+
{
431+
var samp = samples as double[] ?? samples.ToArray();
432+
double n = samp.Count(), s1 = 0, s2 = 0, s3 = 0, previousC = Int32.MinValue, QofC = 0;
433+
434+
if (n <= 1) throw new Exception("Observations not sufficient");
435+
436+
// Start values
437+
double c = 10; double b = 0;
438+
439+
while (Math.Abs(c - previousC) >= 0.0001)
440+
{
441+
s1 = s2 = s3 = 0;
442+
foreach (double x in samp)
443+
{
444+
if (x > 0)
445+
{
446+
s1 += Math.Log(x);
447+
s2 += Math.Pow(x, c);
448+
s3 += Math.Pow(x, c) * Math.Log(x);
449+
}
450+
}
451+
QofC = n * s2 / (n * s3 - s1 * s2);
452+
453+
previousC = c;
454+
c = (c + QofC) / 2;
455+
}
456+
457+
foreach (double x in samp)
458+
if (x > 0)
459+
b += Math.Pow(x, c);
460+
461+
b = Math.Pow(b / n, 1 / c);
462+
463+
return new Weibull(c, b, randomSource);
464+
}
465+
423466
/// <summary>
424467
/// Generates a sample from the Weibull distribution.
425468
/// </summary>

0 commit comments

Comments
 (0)