public void NextNumberSequenceUniform_LeapfrogEstimatedMean_ConvidenceInterval(int sampleSize, double a, double b, RandomNumberSequence.UniformGenerationMode generatorMode) { double expectedMean = (a + b) / 2.0; double expectedStandardDeviation = (b - a) / (2.0 * Math.Sqrt(3.0)); IRandomNumberStream randomStream = GetRandomStream(); Assume.That(randomStream.SplittingApproach.HasFlag(RandomNumberSequence.SplittingApproach.LeapFrog), String.Format("Random Number Generator {0} does not support Leapfrog approach.", randomStream.ToString())); int numberOfStreams = 5; // use five sub-streams for Pseudo-Random-Number Generators if (randomStream.Generator is IQuasiRandomNumberGenerator) { numberOfStreams = (int)randomStream.Dimension; } var sample = new List <double>(sampleSize); for (int j = 0; j < numberOfStreams; j++) { var stream = randomStream.CreateLeapfrogStream(j, numberOfStreams); int subSampleSize = sampleSize / numberOfStreams; var subSample = new double[subSampleSize]; stream.NextNumberSequence.Uniform(subSampleSize, subSample, a, b, generatorMode); sample.AddRange(subSample); } /* compute estimated mean and standard deviation: */ double estimatedMean = sample.Average(); double estimatedStandardDeviation = GetEstimatedStandardDeviation(sample.ToArray(), sampleSize, estimatedMean); double epsilon = 2.0 * estimatedStandardDeviation / Math.Sqrt(sampleSize); // size of the confidence interval w.r.t. normal distribution N{-1}(0.975) \approx 1.96 \approx 2.0, i.e. \alpha/2 quantile with \alpha = 5% /* test whether E(X) \in [ estimatedMean - \epsilon, estimatedMean + \epsilon], where \epsilon = \sigma/\sqrt(n} */ Assert.That((expectedMean >= estimatedMean - epsilon) && (expectedMean <= estimatedMean + epsilon), String.Format("a: {0}, b: {1}, Expectation: {2}, Standard deviation: {3}, estimated mean: {4}, estimated Standard Deviation: {5}, epsilon: {6}", a, b, expectedMean, expectedStandardDeviation, estimatedMean, estimatedStandardDeviation, epsilon)); }
public void NextNumberSequenceUniform_SkipAheadEstimatedMean_ConvidenceInterval(int sampleSize, double a, double b, RandomNumberSequence.UniformGenerationMode generatorMode) { double expectedMean = (a + b) / 2.0; double expectedStandardDeviation = (b - a) / (2.0 * Math.Sqrt(3.0)); IRandomNumberStream randomStream = GetRandomStream(); Assume.That(randomStream.SplittingApproach.HasFlag(RandomNumberSequence.SplittingApproach.SkipAhead), String.Format("Random Number Generator {0} does not support Leapfrog approach.", randomStream.ToString())); var sample = new List <double>(); int numberOfStreams = 2; int nskip = sampleSize * numberOfStreams; for (int j = 0; j < numberOfStreams; j++) { var stream = randomStream.CreateSkipAheadStream(j * nskip); int subSampleSize = sampleSize / numberOfStreams; var subSample = new double[subSampleSize]; stream.NextNumberSequence.Uniform(subSampleSize, subSample, a, b, generatorMode); sample.AddRange(subSample); } sampleSize = sample.Count; /* alternativly, use: */ /* * var stream1 = randomStream.CreateSkipAheadStream(5); * var stream2 = stream1.CreateSkipAheadStream(5); * var stream3 = stream2.CreateSkipAheadStream(5); * * int subSampleSize = sampleSize / 3; * var sample1 = new double[subSampleSize]; * stream1.NextNumberSequence.Uniform(subSampleSize, sample1, a, b, generatorMode); * * var sample2 = new double[subSampleSize]; * stream2.NextNumberSequence.Uniform(subSampleSize, sample2, a, b, generatorMode); * * var sample3 = new double[subSampleSize]; * stream3.NextNumberSequence.Uniform(subSampleSize, sample3, a, b, generatorMode); * * var sample = new List<double>(); * sample.AddRange(sample1); * sample.AddRange(sample2); * sample.AddRange(sample3); */ /* compute estimated mean and standard deviation: */ double estimatedMean = sample.Average(); double estimatedStandardDeviation = GetEstimatedStandardDeviation(sample.ToArray(), sampleSize, estimatedMean); double epsilon = 2.0 * estimatedStandardDeviation / Math.Sqrt(sampleSize); // size of the confidence interval w.r.t. normal distribution N{-1}(0.975) \approx 1.96 \approx 2.0, i.e. \alpha/2 quantile with \alpha = 5% /* * Even for a fixed seed the results of the random sampling are not identically (for Intel's MKL Library)! * * Therefore we enlarge the convidence interval - this unit test is not part of a test suite for Random Number Generators. * We focus on the integration with the native Dll. */ epsilon *= 2.0; // special adjustment, in most cases not necessary /* test whether E(X) \in [ estimatedMean - \epsilon, estimatedMean + \epsilon], where \epsilon = \sigma/\sqrt(n} */ Assert.That(estimatedMean - epsilon, Is.LessThanOrEqualTo(expectedMean), String.Format("a: {0}, b: {1}, Expectation: {2}, Standard deviation: {3}, estimated mean: {4}, estimated Standard Deviation: {5}, epsilon: {6}", a, b, expectedMean, expectedStandardDeviation, estimatedMean, estimatedStandardDeviation, epsilon)); Assert.That(estimatedMean + epsilon, Is.GreaterThanOrEqualTo(expectedMean), String.Format("a: {0}, b: {1}, Expectation: {2}, Standard deviation: {3}, estimated mean: {4}, estimated Standard Deviation: {5}, epsilon: {6}", a, b, expectedMean, expectedStandardDeviation, estimatedMean, estimatedStandardDeviation, epsilon)); }