diff --git a/FastRng/Double/Distributions/Weibull.cs b/FastRng/Double/Distributions/Weibull.cs deleted file mode 100644 index 636798e..0000000 --- a/FastRng/Double/Distributions/Weibull.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace FastRng.Double.Distributions -{ - public sealed class Weibull : IDistribution - { - private double shape = 1.0; - private double scale = 1.0; - - public IRandom Random { get; set; } - - public double Shape - { - get => this.shape; - set - { - if(value <= 0.0) - throw new ArgumentOutOfRangeException(message: "Shape must be greater than 0", null); - - this.shape = value; - } - } - - public double Scale - { - get => this.scale; - set - { - if(value <= 0.0) - throw new ArgumentOutOfRangeException(message: "Scale must be greater than 0", null); - - this.scale = value; - } - } - - public async ValueTask GetDistributedValue(CancellationToken token = default) - { - if (this.Random == null) - return double.NaN; - - var value = await this.Random.GetUniform(token); - return this.Scale * Math.Pow(-Math.Log(value), 1.0 / this.Shape); - } - } -} \ No newline at end of file diff --git a/FastRng/Double/Distributions/WeibullK05La1.cs b/FastRng/Double/Distributions/WeibullK05La1.cs new file mode 100644 index 0000000..fad7460 --- /dev/null +++ b/FastRng/Double/Distributions/WeibullK05La1.cs @@ -0,0 +1,36 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace FastRng.Double.Distributions +{ + public sealed class WeibullK05La1 : IDistribution + { + private const double K = 0.5; + private const double LAMBDA = 1.0; + private const double CONSTANT = 0.221034183615129; + + private ShapeFitter fitter; + private IRandom random; + + public IRandom Random + { + get => this.random; + set + { + this.random = value; + this.fitter = new ShapeFitter(WeibullK05La1.ShapeFunction, this.random, 100); + } + } + + private static double ShapeFunction(double x) => CONSTANT * ( (K / LAMBDA) * Math.Pow(x / LAMBDA, K - 1.0d) * Math.Exp(-Math.Pow(x/LAMBDA, K))); + + public async ValueTask GetDistributedValue(CancellationToken token = default) + { + if (this.Random == null) + return double.NaN; + + return await this.fitter.NextNumber(token); + } + } +} \ No newline at end of file diff --git a/FastRngTests/Double/Distributions/Weibull.cs b/FastRngTests/Double/Distributions/WeibullK05La1.cs similarity index 58% rename from FastRngTests/Double/Distributions/Weibull.cs rename to FastRngTests/Double/Distributions/WeibullK05La1.cs index c02d764..8f2d506 100644 --- a/FastRngTests/Double/Distributions/Weibull.cs +++ b/FastRngTests/Double/Distributions/WeibullK05La1.cs @@ -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(() => dist.Scale = 0); - Assert.Throws(() => dist.Scale = -78); - Assert.DoesNotThrow(() => dist.Scale = 0.0001); - Assert.DoesNotThrow(() => dist.Scale = 4); - - Assert.Throws(() => dist.Shape = 0); - Assert.Throws(() => 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); }