Пример #1
0
        public void BinomialCoefficientTest()
        {
            // n is negative
            {
                string STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE =
                    (string)Reflector.ExecuteStaticMember(
                        typeof(ImplementationServices),
                        "GetResourceString",
                        new string[] { "STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE" });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpecialFunctions.BinomialCoefficient(
                        n: -1,
                        k: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: "n");
            }

            // k is negative
            {
                string STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE =
                    (string)Reflector.ExecuteStaticMember(
                        typeof(ImplementationServices),
                        "GetResourceString",
                        new string[] { "STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE" });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpecialFunctions.BinomialCoefficient(
                        n: 0,
                        k: -1);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: "k");
            }

            // n < k
            {
                string STR_EXCEPT_PAR_MUST_BE_NOT_GREATER_THAN_OTHER =
                    string.Format(
                        (string)Reflector.ExecuteStaticMember(
                            typeof(ImplementationServices),
                            "GetResourceString",
                            new string[] { "STR_EXCEPT_PAR_MUST_BE_NOT_GREATER_THAN_OTHER" }),
                        "k",
                        "n");

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    SpecialFunctions.BinomialCoefficient(
                        n: 3,
                        k: 4);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_BE_NOT_GREATER_THAN_OTHER,
                    expectedParameterName: "k");
            }

            // valid input
            {
                var n = new int[]
                {
                    0,
                    1,
                    2,
                    10,
                    20
                };

                var k = new int[]
                {
                    0,
                    1,
                    2,
                    7,
                    8
                };

                var expected = new double[]
                {
                    1.0,
                    1.0,
                    1.0,
                    120.0,
                    125970.0
                };

                var actual = new double[5];

                for (int i = 0; i < n.Length; i++)
                {
                    actual[i] =
                        SpecialFunctions.BinomialCoefficient(n[i], k[i]);
                }

                DoubleArrayAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: 1e-8);
            }
        }
Пример #2
0
            /// <summary>
            /// Tests that method
            /// <see cref="RandomSampling.NextDoubleMatrix"/>
            /// terminates successfully as expected.
            /// </summary>
            /// <param name="testableRandomSampling">
            /// The testable random sampling providing the instance
            /// on which to invoke the methods to test and their expected
            /// behaviors.
            /// </param>
            /// <param name="numberOfSamples">
            /// The number of samples to draw.
            /// </param>
            /// <param name="delta">The required accuracy.
            /// Defaults to <c>.01</c>.</param>
            public static void Succeed(
                TestableRandomSampling testableRandomSampling,
                int numberOfSamples,
                double delta = .01)
            {
                var randomSampling = testableRandomSampling.RandomSampling;

                // Generate samples

                var samples = DoubleMatrix.Dense(
                    numberOfSamples,
                    randomSampling.PopulationSize);

                for (int i = 0; i < numberOfSamples; i++)
                {
                    samples[i, ":"] = randomSampling.NextDoubleMatrix();
                }

                // Compute the actual inclusion probabilities

                DoubleMatrix actualInclusionProbabilities =
                    DoubleMatrix.Dense(randomSampling.PopulationSize, 1);

                var sampleIndexes = IndexCollection.Default(numberOfSamples - 1);

                for (int j = 0; j < randomSampling.PopulationSize; j++)
                {
                    var samplesContainingUnit =
                        IndexPartition.Create(
                            sampleIndexes,
                            (i) => { return(samples[i, j] == 1.0); });

                    actualInclusionProbabilities[j] =
                        (double)samplesContainingUnit[true].Count
                        /
                        (double)numberOfSamples;
                }

                // Check the number of distinct generated samples

                var distinctSamples =
                    IndexPartition.Create(samples.AsRowCollection());

                int numberOfDistinctSamples =
                    distinctSamples.Count;

                Assert.AreEqual(
                    SpecialFunctions.BinomialCoefficient(
                        randomSampling.PopulationSize,
                        randomSampling.SampleSize),
                    numberOfDistinctSamples);

                // Check that the Chebyshev Inequality holds true
                // for each inclusion probability

                var expectedInclusionProbabilities =
                    testableRandomSampling.InclusionProbabilities;

                for (int j = 0; j < randomSampling.PopulationSize; j++)
                {
                    ProbabilityDistributionTest.CheckChebyshevInequality(
                        new BernoulliDistribution(expectedInclusionProbabilities[j]),
                        actualInclusionProbabilities[j],
                        numberOfSamples,
                        delta);
                }

                // Check how good the actual inclusion probabilities fit
                // the expected ones

                ProbabilityDistributionTest.CheckGoodnessOfFit(
                    expectedInclusionProbabilities,
                    actualInclusionProbabilities,
                    testableRandomSampling.GoodnessOfFitCriticalValue);
            }