Migrated weibull distribution to shape fitter

This commit is contained in:
tsommer committed 2020-10-31 22:11:42 +01:00
1 parent d5404d65c8
commit 81b8467639
3 files changed
+64 -82

No files matched your search

@@ -8,31 +8,40 @@ using NUnit.Framework;
namespace FastRngTests.Double.Distributions
{
[ExcludeFromCodeCoverage]
public class Weibull
public class WeibullK05La1
{
[Test]
[Category(TestCategories.COVER)]
[Category(TestCategories.NORMAL)]
public async Task TestWeibullDistribution01()
{
const double SHAPE = 2;
const double SCALE = 3;
const double VARIANCE = 9 * (1 - Math.PI / 4);
var mean = 3 * Math.Sqrt(Math.PI) / 2;
var dist = new FastRng.Double.Distributions.Weibull{ Shape = SHAPE, Scale = SCALE };
var stats = new RunningStatistics();
var dist = new FastRng.Double.Distributions.WeibullK05La1();
var fra = new FrequencyAnalysis();
var rng = new MultiThreadedRng();
for (var n = 0; n < 100_000; n++)
stats.Push(await rng.NextNumber(dist));
fra.CountThis(await rng.NextNumber(dist));
rng.StopProducer();
TestContext.WriteLine($"mean={mean} vs. {stats.Mean}");
TestContext.WriteLine($"variance={VARIANCE} vs {stats.Variance}");
var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine);
Assert.That(result[0], Is.EqualTo(1.000000000).Within(0.2));
Assert.That(result[1], Is.EqualTo(0.678415772).Within(0.09));
Assert.That(result[2], Is.EqualTo(0.536595233).Within(0.09));
Assert.That(stats.Mean, Is.EqualTo(mean).Within(0.2), "Mean is out of range");
Assert.That(stats.Variance, Is.EqualTo(VARIANCE).Within(0.2), "Variance is out of range");
Assert.That(result[21], Is.EqualTo(0.147406264).Within(0.01));
Assert.That(result[22], Is.EqualTo(0.142654414).Within(0.01));
Assert.That(result[23], Is.EqualTo(0.138217760).Within(0.01));
Assert.That(result[50], Is.EqualTo(0.075769787).Within(0.095));
Assert.That(result[75], Is.EqualTo(0.053016799).Within(0.05));
Assert.That(result[85], Is.EqualTo(0.047144614).Within(0.05));
Assert.That(result[90], Is.EqualTo(0.044629109).Within(0.05));
Assert.That(result[97], Is.EqualTo(0.041484591).Within(0.05));
Assert.That(result[98], Is.EqualTo(0.041067125).Within(0.05));
Assert.That(result[99], Is.EqualTo(0.040656966).Within(0.05));
}
[Test]
@@ -41,9 +50,10 @@ namespace FastRngTests.Double.Distributions
public async Task TestWeibullGeneratorWithRange01()
{
var rng = new MultiThreadedRng();
var dist = new FastRng.Double.Distributions.WeibullK05La1();
var samples = new double[1_000];
for (var n = 0; n < samples.Length; n++)
samples[n] = await rng.NextNumber(-1.0, 1.0, new FastRng.Double.Distributions.Weibull());
samples[n] = await rng.NextNumber(-1.0, 1.0, dist);
rng.StopProducer();
Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range");
@@ -56,9 +66,10 @@ namespace FastRngTests.Double.Distributions
public async Task TestWeibullGeneratorWithRange02()
{
var rng = new MultiThreadedRng();
var dist = new FastRng.Double.Distributions.WeibullK05La1();
var samples = new double[1_000];
for (var n = 0; n < samples.Length; n++)
samples[n] = await rng.NextNumber(0.0, 1.0, new FastRng.Double.Distributions.Weibull());
samples[n] = await rng.NextNumber(0.0, 1.0, dist);
rng.StopProducer();
Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range");
@@ -71,7 +82,7 @@ namespace FastRngTests.Double.Distributions
public async Task TestWeibullGeneratorWithRange03()
{
var rng = new MultiThreadedRng();
var dist = new FastRng.Double.Distributions.Weibull { Random = rng }; // Test default parameters
var dist = new FastRng.Double.Distributions.WeibullK05La1 { Random = rng }; // Test default parameters
var samples = new double[1_000];
for (var n = 0; n < samples.Length; n++)
@@ -82,30 +93,12 @@ namespace FastRngTests.Double.Distributions
Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range");
}
[Test]
[Category(TestCategories.COVER)]
[Category(TestCategories.NORMAL)]
public void ParameterTest01()
{
var dist = new FastRng.Double.Distributions.Weibull();
Assert.Throws<ArgumentOutOfRangeException>(() => dist.Scale = 0);
Assert.Throws<ArgumentOutOfRangeException>(() => dist.Scale = -78);
Assert.DoesNotThrow(() => dist.Scale = 0.0001);
Assert.DoesNotThrow(() => dist.Scale = 4);
Assert.Throws<ArgumentOutOfRangeException>(() => dist.Shape = 0);
Assert.Throws<ArgumentOutOfRangeException>(() => dist.Shape = -78);
Assert.DoesNotThrow(() => dist.Shape = 0.0001);
Assert.DoesNotThrow(() => dist.Shape = 4);
}
[Test]
[Category(TestCategories.COVER)]
[Category(TestCategories.NORMAL)]
public async Task NoRandomNumberGenerator01()
{
var dist = new FastRng.Double.Distributions.Weibull();
var dist = new FastRng.Double.Distributions.WeibullK05La1();
Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue());
Assert.That(await dist.GetDistributedValue(), Is.NaN);
}