public void Main()
        {
            // Create the values.
            var values = DoubleMatrix.Dense(
                3, 2, new double[6] {
                2, -1, 0, 1, 3, -4
            });

            // Create the corresponding probabilities.
            var probabilities = DoubleMatrix.Dense(
                3, 2, new double[6] {
                1.0 / 8, 0.0, 2.0 / 8, 1.0 / 8, 3.0 / 8, 1.0 / 8
            });

            // Create the finite discrete distribution.
            var distribution = new FiniteDiscreteDistribution(values, probabilities);

            Console.WriteLine("Values:");
            Console.WriteLine(distribution.Values);
            Console.WriteLine();
            Console.WriteLine("Probabilities:");
            Console.WriteLine(distribution.Masses);
            Console.WriteLine();

            // Compute the mean value.
            double mean = distribution.Mean();

            Console.WriteLine("Expected value: {0}", mean);
            Console.WriteLine();

            // Draw a sample from the distribution.
            int sampleSize = 100000;
            var sample     = distribution.Sample(sampleSize);

            // Compute the sample mean.
            double sampleMean = Stat.Mean(sample);

            Console.WriteLine("Sample mean: {0}", sampleMean);
            Console.WriteLine();

            // Get a specific value.
            int    valueIndex = 3;
            double value      = distribution.Values[valueIndex];

            // Get its mass.
            double mass = distribution.Masses[valueIndex];

            Console.WriteLine("Mass of value {0}: {1}", value, mass);
            Console.WriteLine();

            // Compute its sample frequency.
            IndexCollection valuePositions = sample.Find(value);
            double          frequency      = (valuePositions == null) ? 0.0
                : (double)valuePositions.Count / sampleSize;

            Console.WriteLine("Sample frequency of value {0}: {1}", value, frequency);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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");
            }
        }