/// <summary> /// Tests that methods /// of <see cref="RandomNumberGenerator"/> /// returning an <see cref="Int32"/> sample point from a /// statistical distribution /// has been properly implemented. /// </summary> /// <typeparam name="TParameters"> /// The type of the distribution parameters.</typeparam> /// <param name="distributionMean">The mean of the distribution /// from which the sample has been drawn.</param> /// <param name="distributionVariance"> /// The variance of the distribution /// from which the sample has been drawn.</param> /// <param name="sampleSize">The sample size.</param> /// <param name="delta">The required accuracy.</param> /// <param name="samplePointFunc"> /// A method returning a sample point from a /// statistical distribution having the specified /// parameters.</param> /// <param name="distributionParameters">The parameters of the distribution /// from which the sample is drawn.</param> public static void Int32Succeed <TParameters>( double distributionMean, double distributionVariance, int sampleSize, double delta, SamplePointFunc <int, TParameters> samplePointFunc, params TParameters[] distributionParameters) { bool CanCheckChebyshevInequality = !Double.IsInfinity(distributionMean) && !Double.IsPositiveInfinity(distributionVariance); // distribution.Sample() { var sample = DoubleMatrix.Dense(sampleSize, 1); for (int i = 0; i < sampleSize; i++) { sample[i] = samplePointFunc(distributionParameters); } if (CanCheckChebyshevInequality) { RandomNumberGeneratorTest.CheckChebyshevInequality( distributionMean: distributionMean, distributionVariance: distributionVariance, sampleMean: Stat.Mean(sample), sampleSize: sampleSize, delta: delta); } } }
/// <summary> /// Checks that the Chebyshev inequality holds true /// for the mean of a sample draw from the specified /// distribution. /// </summary> /// <param name="distribution">The distribution from which the /// sample has been drawn.</param> /// <param name="sampleMean">The observed sample mean.</param> /// <param name="sampleSize">The sample size.</param> /// <param name="delta">The delta: a positive, less than unity /// quantity such that the probability of observing the sample /// mean close to the population mean is at least /// <c>1 - delta</c>. /// </param> /// <remarks> /// <para> /// It is assumed that the /// <see cref="ProbabilityDistribution.Mean"/> and the /// <see cref="ProbabilityDistribution.Variance"/> of /// <paramref name="distribution"/> are finite, and /// <paramref name="delta"/> is in the open interval /// having extremes <c>0</c> and <c>1</c>. /// Then the event /// </para> /// <para> /// { | sampleMean - distribution.Mean() | < epsilon }, /// </para> /// <para> /// where epsilon = sampleStdDev / Math.Sqrt( sampleSize * delta ), /// is expected to happen with probability /// greater than or equal to 1 - delta. /// Hence this method asserts that the event under study /// holds true for the observed sample mean. /// </para> /// </remarks> /// <exception cref="AssertFailedException"> /// <paramref name="distribution"/> has infinite mean.<br/> /// -or-<br/> /// <paramref name="distribution"/> has infinite variance.<br/> /// -or-<br/> /// <paramref name="delta"/> is not in the open interval /// having extremes <c>0</c> and <c>1</c>.<br/> /// -or-<br/> /// The event did not happen for the mean of the observed /// sample. /// </exception> /// <seealso href="https://en.wikipedia.org/wiki/Chebyshev%27s_inequality"/> public static void CheckChebyshevInequality( ProbabilityDistribution distribution, double sampleMean, int sampleSize, double delta) { RandomNumberGeneratorTest.CheckChebyshevInequality( distribution.Mean(), distribution.Variance(), sampleMean, sampleSize, delta); }
/// <summary> /// Tests that methods /// of <see cref="RandomNumberGenerator"/> /// returning a sample of integers to an array from a /// statistical distribution /// terminates successfully when expected. /// </summary> /// <typeparam name="TParameters"> /// The type of the distribution parameters.</typeparam> /// <param name="distributionMean">The mean of the distribution /// from which the sample has been drawn.</param> /// <param name="distributionVariance"> /// The variance of the distribution /// from which the sample has been drawn.</param> /// <param name="sampleSize">The sample size.</param> /// <param name="delta">The required accuracy.</param> /// <param name="sampleArrayFunc"> /// A method returning a sample from a /// statistical distribution having the specified /// parameters.</param> /// <param name="distributionParameters">The parameters of the distribution /// from which the sample is drawn.</param> public static void Int32Succeed <TParameters>( double distributionMean, double distributionVariance, int sampleSize, double delta, SampleArrayFunc <int, TParameters> sampleArrayFunc, params TParameters[] distributionParameters) { bool CanCheckChebyshevInequality = !Double.IsInfinity(distributionMean) && !Double.IsPositiveInfinity(distributionVariance); // distribution.Sample(sampleSize, destinationArray, destinationIndex) { int destinationSize = sampleSize * 10; int partialSize = sampleSize; var sample = new int[destinationSize]; int[] destinationArray = sample; IndexCollection destinationIndexes = IndexCollection.Sequence(0, partialSize, destinationSize - 1); foreach (var destinationIndex in destinationIndexes) { sampleArrayFunc( partialSize, destinationArray, destinationIndex, distributionParameters); } if (CanCheckChebyshevInequality) { RandomNumberGeneratorTest.CheckChebyshevInequality( distributionMean: distributionMean, distributionVariance: distributionVariance, sampleMean: sample.Average(), sampleSize: sampleSize, delta: delta); } } }
/// <summary> /// Tests that methods /// of <see cref="RandomNumberGenerator"/> /// returning a sample of doubles to an array from a /// statistical distribution /// terminates successfully when expected. /// </summary> /// <typeparam name="TParameters"> /// The type of the distribution parameters.</typeparam> /// <param name="distributionMean">The mean of the distribution /// from which the sample has been drawn.</param> /// <param name="distributionVariance"> /// The variance of the distribution /// from which the sample has been drawn.</param> /// <param name="sampleSize">The sample size.</param> /// <param name="delta">The required accuracy.</param> /// <param name="sampleArrayFunc"> /// A method returning a sample from a /// statistical distribution having the specified /// parameters.</param> /// <param name="distributionParameters">The parameters of the distribution /// from which the sample is drawn.</param> public static void DoubleSucceed <TParameters>( double distributionMean, double distributionVariance, int sampleSize, double delta, SampleArrayFunc <double, TParameters> sampleArrayFunc, params TParameters[] distributionParameters) { bool canCheckChebyshevInequality = !Double.IsInfinity(distributionMean) && !Double.IsPositiveInfinity(distributionVariance); // distribution.Sample(sampleSize, destinationArray, destinationIndex) { int destinationSize = sampleSize * 10; int partialSize = sampleSize; var sample = DoubleMatrix.Dense(destinationSize, 1); double[] destinationArray = sample.GetStorage(); IndexCollection destinationIndexes = IndexCollection.Sequence(0, partialSize, destinationSize - 1); foreach (var destinationIndex in destinationIndexes) { sampleArrayFunc( partialSize, destinationArray, destinationIndex, distributionParameters); } if (canCheckChebyshevInequality) { RandomNumberGeneratorTest.CheckChebyshevInequality( distributionMean: distributionMean, distributionVariance: distributionVariance, sampleMean: Stat.Mean(sample), sampleSize: sampleSize, delta: delta); } } }