Migrated log normal distribution to shape fitter

This commit is contained in:
Thorsten Sommer 2020-10-31 19:42:52 +01:00
parent e2f292a6ff
commit a710226941
3 changed files with 71 additions and 69 deletions

View File

@ -1,35 +0,0 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace FastRng.Double.Distributions
{
public sealed class LogNormal : IDistribution
{
private double sigma = 1.0;
public IRandom Random { get; set; }
public double Mu { get; set; } = 0.0;
public double Sigma
{
get => this.sigma;
set
{
if(value <= 0.0)
throw new ArgumentOutOfRangeException(message: "Sigma must be greater than 0", null);
this.sigma = value;
}
}
public async ValueTask<double> GetDistributedValue(CancellationToken token = default)
{
if (this.Random == null)
return double.NaN;
var normal = await this.Random.NextNumber(new Normal(), token); // TODO: Check all distributions. Used distributions must be static readonly! Probably, after refactoring, no distribution should depend on any other!
return Math.Exp(normal);
}
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace FastRng.Double.Distributions
{
public sealed class LogNormalS1M0 : IDistribution
{
private const double SIGMA = 1.0;
private const double MU = 0.0;
private const double CONSTANT = 1.51998658387455;
private static readonly double FACTOR;
private ShapeFitter fitter;
private IRandom random;
static LogNormalS1M0()
{
FACTOR = SIGMA * Math.Sqrt(2 * Math.PI);
}
public IRandom Random
{
get => this.random;
set
{
this.random = value;
this.fitter = new ShapeFitter(LogNormalS1M0.ShapeFunction, this.random, 100);
}
}
private static double ShapeFunction(double x) => (CONSTANT / (x * FACTOR)) * Math.Exp( -(Math.Pow(Math.Log(x) - MU, 2) / (2 * Math.Pow(SIGMA, 2))));
public async ValueTask<double> GetDistributedValue(CancellationToken token = default)
{
if (this.Random == null)
return double.NaN;
return await this.fitter.NextNumber(token);
}
}
}

View File

@ -8,31 +8,40 @@ using NUnit.Framework;
namespace FastRngTests.Double.Distributions namespace FastRngTests.Double.Distributions
{ {
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public class LogNormal public class LogNormalS1M0
{ {
[Test] [Test]
[Category(TestCategories.COVER)] [Category(TestCategories.COVER)]
[Category(TestCategories.NORMAL)] [Category(TestCategories.NORMAL)]
public async Task TestLogNormalDistribution01() public async Task TestLogNormalDistribution01()
{ {
const double MU = 0.1; var dist = new FastRng.Double.Distributions.LogNormalS1M0();
const double SIGMA = 0.25; var fra = new FrequencyAnalysis();
var mean = Math.Exp(MU + SIGMA * SIGMA * 0.5);
var variance = Math.Abs(Math.Exp(SIGMA * SIGMA) - 1) * Math.Exp(2 * MU + SIGMA * SIGMA);
var dist = new FastRng.Double.Distributions.LogNormal{ Mu = MU, Sigma = SIGMA };
var stats = new RunningStatistics();
var rng = new MultiThreadedRng(); var rng = new MultiThreadedRng();
for (var n = 0; n < 100_000; n++) for (var n = 0; n < 100_000; n++)
stats.Push(await rng.NextNumber(dist)); fra.CountThis(await rng.NextNumber(dist));
rng.StopProducer(); rng.StopProducer();
TestContext.WriteLine($"mean={mean} vs. {stats.Mean}"); var result = fra.NormalizeAndPlotEvents(TestContext.WriteLine);
TestContext.WriteLine($"variance={variance} vs {stats.Variance}");
Assert.That(result[0], Is.EqualTo(0.001505531).Within(0.003));
Assert.That(result[1], Is.EqualTo(0.014408709).Within(0.01));
Assert.That(result[2], Is.EqualTo(0.043222256).Within(0.014));
Assert.That(stats.Mean, Is.EqualTo(mean).Within(0.1), "Mean is out of range"); Assert.That(result[21], Is.EqualTo(0.876212056).Within(0.099));
Assert.That(stats.Variance, Is.EqualTo(variance).Within(0.1), "Variance is out of range"); Assert.That(result[22], Is.EqualTo(0.895582226).Within(0.099));
Assert.That(result[23], Is.EqualTo(0.912837250).Within(0.099));
Assert.That(result[50], Is.EqualTo(0.948062005).Within(0.099));
Assert.That(result[75], Is.EqualTo(0.768584762).Within(0.089));
Assert.That(result[85], Is.EqualTo(0.697303612).Within(0.089));
Assert.That(result[90], Is.EqualTo(0.663570581).Within(0.089));
Assert.That(result[97], Is.EqualTo(0.618792767).Within(0.089));
Assert.That(result[98], Is.EqualTo(0.612636410).Within(0.089));
Assert.That(result[99], Is.EqualTo(0.606540679).Within(0.089));
} }
[Test] [Test]
@ -41,9 +50,10 @@ namespace FastRngTests.Double.Distributions
public async Task TestLogNormalGeneratorWithRange01() public async Task TestLogNormalGeneratorWithRange01()
{ {
var rng = new MultiThreadedRng(); var rng = new MultiThreadedRng();
var dist = new FastRng.Double.Distributions.LogNormalS1M0();
var samples = new double[1_000]; var samples = new double[1_000];
for (var n = 0; n < samples.Length; n++) for (var n = 0; n < samples.Length; n++)
samples[n] = await rng.NextNumber(-1.0, 1.0, new FastRng.Double.Distributions.LogNormal()); samples[n] = await rng.NextNumber(-1.0, 1.0, dist);
rng.StopProducer(); rng.StopProducer();
Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range"); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(-1.0), "Min out of range");
@ -56,9 +66,10 @@ namespace FastRngTests.Double.Distributions
public async Task TestLogNormalGeneratorWithRange02() public async Task TestLogNormalGeneratorWithRange02()
{ {
var rng = new MultiThreadedRng(); var rng = new MultiThreadedRng();
var dist = new FastRng.Double.Distributions.LogNormalS1M0();
var samples = new double[1_000]; var samples = new double[1_000];
for (var n = 0; n < samples.Length; n++) for (var n = 0; n < samples.Length; n++)
samples[n] = await rng.NextNumber(0.0, 1.0, new FastRng.Double.Distributions.LogNormal()); samples[n] = await rng.NextNumber(0.0, 1.0, dist);
rng.StopProducer(); rng.StopProducer();
Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range"); Assert.That(samples.Min(), Is.GreaterThanOrEqualTo(0.0), "Min is out of range");
@ -71,7 +82,7 @@ namespace FastRngTests.Double.Distributions
public async Task TestLogNormalGeneratorWithRange03() public async Task TestLogNormalGeneratorWithRange03()
{ {
var rng = new MultiThreadedRng(); var rng = new MultiThreadedRng();
var dist = new FastRng.Double.Distributions.LogNormal { Random = rng }; // Test default parameters var dist = new FastRng.Double.Distributions.LogNormalS1M0 { Random = rng }; // Test default parameters
var samples = new double[1_000]; var samples = new double[1_000];
for (var n = 0; n < samples.Length; n++) for (var n = 0; n < samples.Length; n++)
@ -82,29 +93,12 @@ namespace FastRngTests.Double.Distributions
Assert.That(samples.Max(), Is.LessThanOrEqualTo(1.0), "Max is out of range"); 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.LogNormal();
Assert.DoesNotThrow(() => dist.Mu = -45);
Assert.DoesNotThrow(() => dist.Mu = 15);
Assert.DoesNotThrow(() => dist.Mu = 0);
Assert.Throws<ArgumentOutOfRangeException>(() => dist.Sigma = 0);
Assert.Throws<ArgumentOutOfRangeException>(() => dist.Sigma = -78);
Assert.DoesNotThrow(() => dist.Sigma = 0.0001);
Assert.DoesNotThrow(() => dist.Sigma = 4);
}
[Test] [Test]
[Category(TestCategories.COVER)] [Category(TestCategories.COVER)]
[Category(TestCategories.NORMAL)] [Category(TestCategories.NORMAL)]
public async Task NoRandomNumberGenerator01() public async Task NoRandomNumberGenerator01()
{ {
var dist = new FastRng.Double.Distributions.LogNormal(); var dist = new FastRng.Double.Distributions.LogNormalS1M0();
Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue()); Assert.DoesNotThrowAsync(async () => await dist.GetDistributedValue());
Assert.That(await dist.GetDistributedValue(), Is.NaN); Assert.That(await dist.GetDistributedValue(), Is.NaN);
} }