Skip to content

Commit

Permalink
- New random shift source (based on Colin Green's XorShift algorithm)
Browse files Browse the repository at this point in the history
- Fix on Box Muller transform
- Tests on distribution for Gaussian and Uniform samples
  • Loading branch information
bgarate committed Mar 7, 2016
1 parent 8e15d62 commit c1e4085
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Evolution/Evolution.Test/Evolution.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -53,6 +55,22 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accord, Version=3.0.2.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<HintPath>..\packages\Accord.3.0.2\lib\net35\Accord.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Accord.Math, Version=3.0.2.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<HintPath>..\packages\Accord.Math.3.0.2\lib\net35\Accord.Math.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Accord.Statistics, Version=3.0.2.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<HintPath>..\packages\Accord.Statistics.3.0.2\lib\net35\Accord.Statistics.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MathNet.Numerics, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MathNet.Numerics.3.11.0\lib\net35\MathNet.Numerics.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
Expand All @@ -61,6 +79,10 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\TaskParallelLibrary.1.0.2856.0\lib\Net35\System.Threading.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BreedersTest.cs" />
Expand Down Expand Up @@ -107,6 +129,13 @@
<Folder Include="Core\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Accord.3.0.2\build\Accord.targets" Condition="Exists('..\packages\Accord.3.0.2\build\Accord.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Accord.3.0.2\build\Accord.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Accord.3.0.2\build\Accord.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
66 changes: 66 additions & 0 deletions Evolution/Evolution.Test/RandomGeneratorTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Accord.Statistics.Testing;
using MathNet.Numerics.Statistics;
using NUnit.Framework;
using Singular.Evolution.Utils;

Expand Down Expand Up @@ -117,6 +119,7 @@ public void TestIntSequences()

Assert.GreaterOrEqual(min3, -400);
Assert.LessOrEqual(max3, -1);

}

[Test]
Expand Down Expand Up @@ -161,5 +164,68 @@ public void TestRandomGeneratorMock()
Assert.Throws(typeof (InvalidOperationException), () => rnd.NextInt());
Assert.Throws(typeof (Exception), () => rnd.NextDouble());
}

[Test]
public void TestXorShiftRandomSource()
{
XorShiftRandomSource source = new XorShiftRandomSource();
double[] values = Enumerable.Range(0, 1000000).Select(i => source.NextDouble()).ToArray();
UniformDistributionTest(values,0,1);
}

[Test]
public void TestSystemRandomSource()
{
SystemRandomSource source = new SystemRandomSource();
double[] values = Enumerable.Range(0, 1000000).Select(i => source.NextDouble()).ToArray();
UniformDistributionTest(values, 0, 1);
}

[Test]
public void TestBoxMullerTransformation()
{
BoxMullerTransformation boxMullerTransformation = new BoxMullerTransformation(RandomGenerator.GetInstance());
double[] values = Enumerable.Range(0, 1000000).Select(i => boxMullerTransformation.NextGaussian(3,5)).ToArray();
ShapiroWilkTest test = new ShapiroWilkTest(values);
Assert.False(test.Significant);
}

[Test]
public void TestBoxMullerTransformationClamped()
{
BoxMullerTransformation boxMullerTransformation = new BoxMullerTransformation(RandomGenerator.GetInstance());
double[] values = Enumerable.Range(0, 1000000).Select(i => boxMullerTransformation.NextBoundedGaussian(3,2,10)).ToArray();
ShapiroWilkTest test = new ShapiroWilkTest(values);
Assert.False(test.Significant);
}


public void UniformDistributionTest(double[] sampleArr, double lowerBound, double upperBound)
{
Array.Sort(sampleArr);
RunningStatistics runningStats = new RunningStatistics(sampleArr);

// Skewness should be pretty close to zero (evenly distributed samples)
if (Math.Abs(runningStats.Skewness) > 0.01) Assert.Fail();

// Mean test.
double range = upperBound - lowerBound;
double expectedMean = lowerBound + (range / 2.0);
double meanErr = expectedMean - runningStats.Mean;
double maxExpectedErr = range / 1000.0;

if (Math.Abs(meanErr) > maxExpectedErr) Assert.Fail();

for (double tau = 0; tau <= 1.0; tau += 0.01)
{
double quantile = SortedArrayStatistics.Quantile(sampleArr, tau);
double expectedQuantile = lowerBound + (tau * range);
double quantileError = expectedQuantile - quantile;
if (Math.Abs(quantileError) > maxExpectedErr) Assert.Fail();
}

ShapiroWilkTest test = new ShapiroWilkTest(sampleArr);
Assert.True(test.Significant);
}
}
}
6 changes: 5 additions & 1 deletion Evolution/Evolution.Test/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>

<packages>
<package id="Accord" version="3.0.2" targetFramework="net35" />
<package id="Accord.Math" version="3.0.2" targetFramework="net35" />
<package id="Accord.Statistics" version="3.0.2" targetFramework="net35" />
<package id="coveralls.net" version="0.6.0" targetFramework="net4" />
<package id="MathNet.Numerics" version="3.11.0" targetFramework="net35" />
<package id="NUnit" version="3.0.1" targetFramework="net35" />
<package id="OpenCover" version="4.6.166" targetFramework="net4" />
<package id="TaskParallelLibrary" version="1.0.2856.0" targetFramework="net35" />
</packages>
1 change: 1 addition & 0 deletions Evolution/Evolution/Evolution.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="Utils\RandomChooser.cs" />
<Compile Include="Utils\RandomGenerator.cs" />
<Compile Include="Utils\MockRandomSource.cs" />
<Compile Include="Utils\XorShiftRandomSource.cs" />
<Compile Include="Utils\SystemRandomSource.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Evolution/Evolution/Utils/RandomGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public static RandomGenerator GetInstance()
}
}

internal class BoxMullerTransformation
public class BoxMullerTransformation
{
private const int BOUNDED_DEVIATIONS = 3;

Expand Down Expand Up @@ -276,7 +276,7 @@ private double CalculateGaussianDistribution()
double f = Math.Sqrt(-2.0*Math.Log(squares)/squares);

secondDerivative = y*f;
return x;
return x*f;
}
}
}
Loading

0 comments on commit c1e4085

Please sign in to comment.