public void Test1() { // SplitMix64 var seed = 1234567ul; Xoshiro256StarStar.SplitMix64(ref seed).Is(6457827717110365317ul); Xoshiro256StarStar.SplitMix64(ref seed).Is(3203168211198807973ul); Xoshiro256StarStar.SplitMix64(ref seed).Is(9817491932198370423ul); Xoshiro256StarStar.SplitMix64(ref seed).Is(4593380528125082431ul); Xoshiro256StarStar.SplitMix64(ref seed).Is(16408922859458223821ul); var xo = new Xoshiro256StarStar(42); DoubleToString(xo.NextDouble()).Is("0.0838629710598822"); DoubleToString(xo.NextDouble()).Is("0.3789802506626686"); DoubleToString(xo.NextDouble()).Is("0.6800434110281394"); DoubleToString(xo.NextDouble()).Is("0.9246929453253876"); DoubleToString(xo.NextDouble()).Is("0.9918039142821028"); string DoubleToString(double d) => d.ToString("F16"); xo.NextUInt64().Is(14199186830065750584ul); xo.NextUInt64().Is(13267978908934200754ul); xo.NextUInt64().Is(15679888225317814407ul); xo.NextUInt64().Is(14044878350692344958ul); xo.NextUInt64().Is(10760895422300929085ul); }
public static void QuickStart_Xoshiro256StarStar() { // xoshiro256** is a pseudo-random number generator. var xo = new Xoshiro256StarStar(42); var ul = xo.NextUInt64(); // [0, 2^64-1] var d = xo.NextDouble(); // [0,1) var bytes = new byte[10]; xo.NextBytes(bytes); }
public static void TestQuantileTrap() { Xoshiro256StarStar rand = new Xoshiro256StarStar(8675309); const int numberOfDists = 10; //const double meanOfNormals = 50; //const double stdDevOfNormals = 0.002; double Shape() => 2 *rand.NextDouble() - 1; double Scale() => 0.002 *rand.NextDouble() + 0; double Location() => 180 *rand.NextDouble(); IDistributionWrapper[] dists = new IDistributionWrapper[numberOfDists]; for (int i = 0; i < dists.Length; i++) { //dists[i] = new WrappedDistribution(new Normal(rand.NextDouble() * meanOfNormals * 2, 0 + Math.Abs(rand.NextDouble() * stdDevOfNormals)), -100, 200); // Bounds are unused here dists[i] = new WrappedDistribution(new GEV(Location(), Scale(), Shape(), rand), -100, 100); } double[] exact = DiscardProbabilityComputation.ComplementsTrapezoid(dists, 50000); Console.WriteLine("Exact:"); for (int i = 0; i < exact.Length; i++) { Console.WriteLine($"{i}: {dists[i].GetWrappedDistribution()} 1-P(D_i) = {exact[i]}"); } exact = DiscardProbabilityComputation.ComplementsMonteCarloMaximizing(dists); Console.WriteLine("MC:"); for (int i = 0; i < exact.Length; i++) { Console.WriteLine($"{i}: {dists[i].GetWrappedDistribution()} 1-P(D_i) = {exact[i]}"); } double[] est; int[] its = new int[] { 10, 20, 30, 40, 50, 75, 100, 200, 500, 1000, 2000, 20000 }; for (int i = 0; i < its.Length; i++) { int size = its[i]; est = DiscardProbabilityComputation.ComplementsQuantileTrapRule(dists, size); Console.WriteLine($"Size {size}:"); for (int j = 0; j < est.Length; j++) { Console.WriteLine($"{j}: {dists[j].GetWrappedDistribution()} 1-P(D_i) = {est[j]}"); } } }
public static void TestGEVComplementComputations() { double ep = Math.Pow(2, -50); double complementEp = 1.0 - ep; int testSize = 20; //GEV[] dists = new GEV[] { new GEV(0,200,-1), new GEV(0,100,-1) }; GEV[] dists = new GEV[testSize]; Random rand = new Xoshiro256StarStar(8675309); for (int i = 0; i < dists.Length; i++) { dists[i] = new GEV(rand.NextDouble(), rand.NextDouble(), -rand.NextDouble(), rand); } IDistributionWrapper[] wrappedDists = new IDistributionWrapper[dists.Length]; for (int i = 0; i < dists.Length; i++) { wrappedDists[i] = new WrappedDistribution(dists[i], dists[i].InverseCumulativeDistribution(ep), dists[i].InverseCumulativeDistribution(complementEp)); } double[] complements = DiscardProbabilityComputation.ComplementsClenshawCurtisAutomatic(wrappedDists); double[] complemetnsTrap = DiscardProbabilityComputation.ComplementsTrapezoid(wrappedDists, 10000); double[] mcComplements = DiscardProbabilityComputation.ComplementsMonteCarlo(wrappedDists, iterations: 10000000); double totalc = 0; double totalmc = 0; double totalTrap = 0; for (int i = 0; i < complements.Length; i++) { GEV dist = dists[i]; Program.logger.WriteLine($"Distribution Scale: {dist.scale} Loc {dist.location} Shape {dist.shape} " + $"1-P(D) {complements[i]} MC {mcComplements[i]} Trap {complemetnsTrap[i]}"); totalc += complements[i]; totalmc += mcComplements[i]; totalTrap += complemetnsTrap[i]; } Program.logger.WriteLine($"Total probability: {totalc} Total by MC: {totalmc} Total by Trap 10k: {totalTrap}"); }
public static void P3() { Xoshiro256StarStar rand = new Xoshiro256StarStar(); int RunProcess(int time) { int popsize = 1; for (int t = 0; t < time; t++) { int newpopSize = popsize; for (int i = 0; i < popsize; i++) { double val = rand.NextDouble(); if (val < 0.25) { newpopSize--; } if (val > 0.5) { newpopSize++; } if (val > 0.75) { newpopSize++; } } popsize = newpopSize; } return(popsize); } int sum = 0; int tests = 10000000; /* * for (int i = 0; i < tests; i++) * { * sum += RunProcess(5); * } * double avgAfter5 = sum *1.0 / tests; * * Console.WriteLine($"E(5) = {avgAfter5}"); * * int count = 0; * tests = 5000; * int limit = 30; * for (int i = 0; i < tests; i++) * { * if (RunProcess(limit) == 0) { count++; } * } * double proportionDead = count * 1.0 / tests; * * Console.WriteLine($"Pi_0 = {proportionDead}"); */ tests = 100000000; sum = 0; double lambda1 = 1.0 / 11; double lambda2 = 1.0 / 9; double lambda3 = 1.0 / 8; for (int i = 0; i < tests; i++) { double s1, s2, s3; s1 = Exponential.Sample(rand, lambda1); s2 = Exponential.Sample(rand, lambda2); s3 = Exponential.Sample(rand, lambda3); if (s1 > 10 && s2 > 10 && s3 > 10) { sum++; } } double proportionLess = sum * 1.0 / tests; Console.WriteLine($"Proportion = {proportionLess}"); //Console.WriteLine($"L1 / (L1 + L2) = {lambda1 / (lambda1 + lambda2)}"); Console.ReadLine(); }