Exemple #1
0
        public void UniformTest()
        {
            var values = DoubleMatrix.Dense(3, 2,
                                            new double[6] {
                1, 2, 3, 4, 5, 6
            });

            var distribution = FiniteDiscreteDistribution.Uniform(values);

            DoubleMatrixAssert.AreEqual(
                expected: values,
                actual: (DoubleMatrix)distribution.Values,
                delta: DoubleMatrixTest.Accuracy);
            DoubleMatrixAssert.AreEqual(
                expected: DoubleMatrix.Dense(3, 2, 1.0 / 6.0),
                actual: (DoubleMatrix)distribution.Masses,
                delta: DoubleMatrixTest.Accuracy);
        }
Exemple #2
0
        public void SetMassesTest()
        {
            // Valid input
            {
                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    .1, .2, .3, .2, .1, .1
                });

                distribution.SetMasses(masses: masses);

                DoubleMatrixAssert.AreEqual(
                    expected: masses,
                    actual: (DoubleMatrix)distribution.Masses,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // masses is null
            {
                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "masses");
            }

            // The number of rows in masses is not equal to that of values
            {
                string STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS"),
                        "values");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(1, 2,
                                                new double[2] {
                    .5, .5
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: "masses");
            }

            // The number of columns in masses is not equal to that of values
            {
                string STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS"),
                        "values");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 1,
                                                new double[3] {
                    .2, .5, .3
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS,
                    expectedParameterName: "masses");
            }

            // At least an entry in masses is negative
            {
                string STR_EXCEPT_PAR_ENTRIES_NOT_IN_CLOSED_INTERVAL =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_ENTRIES_NOT_IN_CLOSED_INTERVAL"),
                        "0", "1");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    .2, .5, .3, -.1, .1, 0
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_ENTRIES_NOT_IN_CLOSED_INTERVAL,
                    expectedParameterName: "masses");
            }

            // The sum of the masses is not 1
            {
                string STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1 =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    .2, .5, .3, .1, .1, 0
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: "masses");
            }
        }