/// <inheritdoc/>
        /// <remarks>
        /// <para>
        /// This method has been overridden to support
        /// the optimization of functions whose arguments
        /// are continuous variables.
        /// </para>
        /// </remarks>
        protected internal override void PartialSample(
            double[] destinationArray,
            Tuple <int, int> sampleSubsetRange,
            RandomNumberGenerator randomNumberGenerator,
            DoubleMatrix parameter,
            int sampleSize)
        {
            // Must be Item1 included, Item2 excluded
            int subSampleSize =
                sampleSubsetRange.Item2 - sampleSubsetRange.Item1;

            for (int j = 0; j < this.StateDimension; j++)
            {
                var distribution = new
                                   GaussianDistribution(
                    mu: parameter[0, j],
                    sigma: parameter[1, j])
                {
                    RandomNumberGenerator = randomNumberGenerator
                };

                distribution.Sample(
                    subSampleSize,
                    destinationArray,
                    j * sampleSize + sampleSubsetRange.Item1);
            }
        }
Ejemplo n.º 2
0
        public static Matrix RandomOrthogonal(int size)
        {
            GaussianDistribution dist = new GaussianDistribution();
            Matrix m = new Matrix(size, size, (i, j) => dist.Sample());

            (Matrix q, Matrix r) = m.GramSchmidt();
            return(q);
        }
Ejemplo n.º 3
0
        public double Evaluate(Vector input)
        {
            double sum = 0;

            for (int i = 0; i < input.Length; ++i)
            {
                double xi = input[i];
                sum += (i + 1) * (xi * xi * xi * xi) + noise.Sample();
            }

            return(sum);
        }
        static TestableCombinationOptimizationContext01()
        {
            const int numberOfItems = 12;

            var target = DoubleMatrix.Dense(numberOfItems, 1,
                                            new double[numberOfItems]
            {
                0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
            });

            partition = IndexPartition.Create(target);
            data      = DoubleMatrix.Dense(numberOfItems, 7);

            // features 0 to 4
            var g = new GaussianDistribution(mu: 0, sigma: .1);

            for (int j = 0; j < 5; j++)
            {
                data[":", j] = g.Sample(sampleSize: numberOfItems);
            }

            var partIdentifiers = partition.Identifiers;

            // feature 5 to 6
            double mu = 1.0;

            for (int i = 0; i < partIdentifiers.Count; i++)
            {
                var part     = partition[partIdentifiers[i]];
                int partSize = part.Count;
                g.Mu          = mu;
                data[part, 5] = g.Sample(sampleSize: partSize);
                mu           += 2.0;
                g.Mu          = mu;
                data[part, 6] = g.Sample(sampleSize: partSize);
                mu           += 2.0;
            }
        }
        // Set how to sample system states
        // given the specified parameter.
        protected internal override void PartialSample(
            double[] destinationArray,
            Tuple <int, int> sampleSubsetRange,
            RandomNumberGenerator randomNumberGenerator,
            DoubleMatrix parameter,
            int sampleSize)
        {
            // Must be Item1 included, Item2 excluded
            int subSampleSize = sampleSubsetRange.Item2 - sampleSubsetRange.Item1;
            var distribution  = new GaussianDistribution(
                parameter[0], parameter[1])
            {
                RandomNumberGenerator = randomNumberGenerator
            };

            distribution.Sample(
                subSampleSize,
                destinationArray, sampleSubsetRange.Item1);
        }
Ejemplo n.º 6
0
        public void Main()
        {
            // Set the number of items and features under study.
            const int numberOfItems    = 12;
            int       numberOfFeatures = 7;

            // Define a partition that must be explained.
            // Three parts (clusters) are included,
            // containing, respectively, items 0 to 3,
            // 5 to 8, and 9 to 11.
            var partition = IndexPartition.Create(
                new double[numberOfItems]
            {
                0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
            });

            // Create a matrix that will represent
            // an artificial data set,
            // having 12 items (rows) and 7 features (columns).
            // This will store the observations which
            // explanation will be based on.
            var data = DoubleMatrix.Dense(
                numberOfRows: numberOfItems,
                numberOfColumns: numberOfFeatures);

            // The first 5 features are built to be almost
            // surely non informative, since they result
            // as samples drawn from a same distribution.
            var g = new GaussianDistribution(mu: 0, sigma: .01);

            for (int j = 0; j < 5; j++)
            {
                data[":", j] = g.Sample(sampleSize: numberOfItems);
            }

            // Features 5 to 6 are instead built to be informative,
            // since they are sampled from different distributions
            // while filling rows whose indexes are in different parts
            // of the partition to be explained.
            var    partIdentifiers = partition.Identifiers;
            double mu = 1.0;

            for (int i = 0; i < partIdentifiers.Count; i++)
            {
                var part     = partition[partIdentifiers[i]];
                int partSize = part.Count;
                g.Mu          = mu;
                data[part, 5] = g.Sample(sampleSize: partSize);
                mu           += 2.0;
                g.Mu          = mu;
                data[part, 6] = g.Sample(sampleSize: partSize);
                mu           += 2.0;
            }

            Console.WriteLine("The data set:");
            Console.WriteLine(data);

            // Define how many features must be selected
            // for explanation.
            int numberOfExplanatoryFeatures = 2;

            // Select the best features.
            IndexCollection optimalExplanatoryFeatureIndexes =
                Clusters.Explain(
                    data,
                    partition,
                    numberOfExplanatoryFeatures);

            // Show the results.
            Console.WriteLine();
            Console.WriteLine(
                "The {0} features best explaining the given partition have column indexes:",
                numberOfExplanatoryFeatures);
            Console.WriteLine(optimalExplanatoryFeatureIndexes);

            Console.WriteLine();
            Console.WriteLine("The Davies-Bouldin Index for the selected features:");
            var dbi = IndexPartition.DaviesBouldinIndex(
                data[":", optimalExplanatoryFeatureIndexes],
                partition);

            Console.WriteLine(dbi);
        }
        public void Main()
        {
            // Set the number of items and features under study.
            const int numberOfItems    = 12;
            int       numberOfFeatures = 7;

            // Create a matrix that will represent
            // an artificial data set,
            // having 12 items (rows) and 7 features (columns).
            // This will store the observations which
            // partition discovery will be based on.
            var data = DoubleMatrix.Dense(
                numberOfRows: numberOfItems,
                numberOfColumns: numberOfFeatures);

            // Fill the data rows by sampling from a different
            // distribution while, respectively, drawing observations
            // for items 0 to 3, 4 to 7, and 8 to 11: these will be the
            // three different parts expected to be included in the
            // optimal partition.
            double mu = 1.0;
            var    g  = new GaussianDistribution(mu: mu, sigma: .01);

            IndexCollection range = IndexCollection.Range(0, 3);

            for (int j = 0; j < numberOfFeatures; j++)
            {
                data[range, j] = g.Sample(sampleSize: range.Count);
            }

            mu   += 5.0;
            g.Mu  = mu;
            range = IndexCollection.Range(4, 7);
            for (int j = 0; j < numberOfFeatures; j++)
            {
                data[range, j] = g.Sample(sampleSize: range.Count);
            }

            mu   += 5.0;
            g.Mu  = mu;
            range = IndexCollection.Range(8, 11);
            for (int j = 0; j < numberOfFeatures; j++)
            {
                data[range, j] = g.Sample(sampleSize: range.Count);
            }

            Console.WriteLine("The data set:");
            Console.WriteLine(data);

            // Define the optimization problem as
            // the minimization of the Davies-Bouldin Index
            // of a candidate partition.
            double objectiveFunction(DoubleMatrix x)
            {
                // An argument x has 12 entries, each belonging
                // to the set {0,...,k-1}, where k is the
                // maximum number of allowed parts, so
                // x[j]==i signals that, at x, item j
                // has been assigned to part i.
                IndexPartition <double> selected =
                    IndexPartition.Create(x);

                var performance = IndexPartition.DaviesBouldinIndex(
                    data: data,
                    partition: selected);

                return(performance);
            }

            var optimizationGoal = OptimizationGoal.Minimization;

            // Define the maximum number of parts allowed in the
            // partition to be discovered.
            int maximumNumberOfParts = 3;

            // Create the required context.
            var context = new PartitionOptimizationContext(
                objectiveFunction: objectiveFunction,
                stateDimension: numberOfItems,
                partitionDimension: maximumNumberOfParts,
                probabilitySmoothingCoefficient: .8,
                optimizationGoal: optimizationGoal,
                minimumNumberOfIterations: 3,
                maximumNumberOfIterations: 1000);

            // Create the optimizer.
            var optimizer = new SystemPerformanceOptimizer()
            {
                PerformanceEvaluationParallelOptions = { MaxDegreeOfParallelism = -1 },
                SampleGenerationParallelOptions      = { MaxDegreeOfParallelism = -1 }
            };

            // Set optimization parameters.
            double rarity     = 0.01;
            int    sampleSize = 2000;

            // Solve the problem.
            var results = optimizer.Optimize(
                context,
                rarity,
                sampleSize);

            IndexPartition <double> optimalPartition =
                IndexPartition.Create(results.OptimalState);

            // Show the results.
            Console.WriteLine(
                "The Cross-Entropy optimizer has converged: {0}.",
                results.HasConverged);

            Console.WriteLine();
            Console.WriteLine("Initial guess parameter:");
            Console.WriteLine(context.InitialParameter);

            Console.WriteLine();
            Console.WriteLine("The minimizer of the performance is:");
            Console.WriteLine(results.OptimalState);

            Console.WriteLine();
            Console.WriteLine(
                "The optimal partition is:");
            Console.WriteLine(optimalPartition);

            Console.WriteLine();
            Console.WriteLine("The minimum performance is:");
            Console.WriteLine(results.OptimalPerformance);

            Console.WriteLine();
            Console.WriteLine("The Dunn Index for the optimal partition is:");
            var di = IndexPartition.DunnIndex(
                data,
                optimalPartition);

            Console.WriteLine(di);
        }
        public void Main()
        {
            // Set the number of items and features under study.
            const int numberOfItems    = 12;
            int       numberOfFeatures = 7;

            // Define a partition that must be explained.
            // Three parts (clusters) are included,
            // containing, respectively, items 0 to 3,
            // 4 to 7, and 8 to 11.
            var partition = IndexPartition.Create(
                new double[numberOfItems]
            {
                0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
            });

            // Create a matrix that will represent
            // an artificial data set,
            // having 12 items (rows) and 7 features (columns).
            // This will store the observations which
            // explanation will be based on.
            var data = DoubleMatrix.Dense(
                numberOfRows: numberOfItems,
                numberOfColumns: numberOfFeatures);

            // The first 5 features are built to be almost
            // surely non informative, since they result
            // as samples drawn from a same distribution.
            var g = new GaussianDistribution(mu: 0, sigma: .01);

            for (int j = 0; j < 5; j++)
            {
                data[":", j] = g.Sample(sampleSize: numberOfItems);
            }

            // Features 5 to 6 are instead built to be informative,
            // since they are sampled from different distributions
            // while filling rows whose indexes are in different parts
            // of the partition to be explained.
            var    partIdentifiers = partition.Identifiers;
            double mu = 1.0;

            for (int i = 0; i < partIdentifiers.Count; i++)
            {
                var part     = partition[partIdentifiers[i]];
                int partSize = part.Count;
                g.Mu          = mu;
                data[part, 5] = g.Sample(sampleSize: partSize);
                mu           += 2.0;
                g.Mu          = mu;
                data[part, 6] = g.Sample(sampleSize: partSize);
                mu           += 2.0;
            }

            Console.WriteLine("The data set:");
            Console.WriteLine(data);

            // Define the selection problem as
            // the maximization of the Dunn Index.
            double objectiveFunction(DoubleMatrix x)
            {
                // An argument x has entries equal to one,
                // signaling that the corresponding features
                // are selected at x. Otherwise, the entries
                // are zero.
                IndexCollection selected = x.FindNonzero();

                double performance =
                    IndexPartition.DunnIndex(
                        data: data[":", selected],
                        partition: partition);

                return(performance);
            }

            var optimizationGoal = OptimizationGoal.Maximization;

            // Define how many features must be selected
            // for explanation.
            int numberOfExplanatoryFeatures = 2;

            // Create the required context.
            var context = new CombinationOptimizationContext(
                objectiveFunction: objectiveFunction,
                stateDimension: numberOfFeatures,
                combinationDimension: numberOfExplanatoryFeatures,
                probabilitySmoothingCoefficient: .8,
                optimizationGoal: optimizationGoal,
                minimumNumberOfIterations: 3,
                maximumNumberOfIterations: 1000);

            // Create the optimizer.
            var optimizer = new SystemPerformanceOptimizer()
            {
                PerformanceEvaluationParallelOptions = { MaxDegreeOfParallelism = -1 },
                SampleGenerationParallelOptions      = { MaxDegreeOfParallelism = -1 }
            };

            // Set optimization parameters.
            double rarity     = 0.01;
            int    sampleSize = 1000;

            // Solve the problem.
            var results = optimizer.Optimize(
                context,
                rarity,
                sampleSize);

            IndexCollection optimalExplanatoryFeatureIndexes =
                results.OptimalState.FindNonzero();

            // Show the results.
            Console.WriteLine(
                "The Cross-Entropy optimizer has converged: {0}.",
                results.HasConverged);

            Console.WriteLine();
            Console.WriteLine("Initial guess parameter:");
            Console.WriteLine(context.InitialParameter);

            Console.WriteLine();
            Console.WriteLine("The maximizer of the performance is:");
            Console.WriteLine(results.OptimalState);

            Console.WriteLine();
            Console.WriteLine(
                "The {0} features best explaining the given partition have column indexes:",
                numberOfExplanatoryFeatures);
            Console.WriteLine(optimalExplanatoryFeatureIndexes);

            Console.WriteLine();
            Console.WriteLine("The maximum performance is:");
            Console.WriteLine(results.OptimalPerformance);

            Console.WriteLine();
            Console.WriteLine("This is the Dunn Index for the selected features:");
            var di = IndexPartition.DunnIndex(
                data[":", optimalExplanatoryFeatureIndexes],
                partition);

            Console.WriteLine(di);
        }
Ejemplo n.º 9
0
        public void ExplainTest()
        {
            // data is null
            {
                string parameterName = "data";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: null,
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: 2);;
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // data is null
            {
                string parameterName = "partition";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: null,
                        numberOfExplanatoryFeatures: 2);;
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // numberOfExplanatoryFeatures is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfExplanatoryFeatures";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfExplanatoryFeatures is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfExplanatoryFeatures";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: -1);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfExplanatoryFeatures is equal to the number of columns in data
            {
                var STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS"),
                        "numberOfExplanatoryFeatures",
                        "data");

                string parameterName = "numberOfExplanatoryFeatures";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: 5);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS,
                    expectedParameterName: parameterName);
            }

            // numberOfExplanatoryFeatures is greater than the number of columns in data
            {
                var STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS"),
                        "numberOfExplanatoryFeatures",
                        "data");

                string parameterName = "numberOfExplanatoryFeatures";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: 6);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS,
                    expectedParameterName: parameterName);
            }

            // Valid input
            {
                const int numberOfItems = 12;

                var source = DoubleMatrix.Dense(numberOfItems, 1,
                                                new double[numberOfItems]
                {
                    0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
                });

                var partition = IndexPartition.Create(source);
                var data      = DoubleMatrix.Dense(numberOfItems, 7);

                // features 0 to 4
                var g = new GaussianDistribution(mu: 0, sigma: .1);
                for (int j = 0; j < 5; j++)
                {
                    data[":", j] = g.Sample(sampleSize: numberOfItems);
                }

                var partIdentifiers = partition.Identifiers;

                // feature 5 to 6
                double mu = 1.0;
                for (int i = 0; i < partIdentifiers.Count; i++)
                {
                    var part     = partition[partIdentifiers[i]];
                    int partSize = part.Count;
                    g.Mu          = mu;
                    data[part, 5] = g.Sample(sampleSize: partSize);
                    mu           += 2.0;
                    g.Mu          = mu;
                    data[part, 6] = g.Sample(sampleSize: partSize);
                    mu           += 2.0;
                }

                IndexCollection actualFeatureIndexes =
                    Clusters.Explain(
                        data: data,
                        partition: partition,
                        numberOfExplanatoryFeatures: 2);

                IndexCollectionAssert.AreEqual(
                    expected: IndexCollection.Range(5, 6),
                    actual: actualFeatureIndexes);
            }
        }
Ejemplo n.º 10
0
        public void Main()
        {
            // Set the number of items and features under study.
            const int numberOfItems    = 12;
            int       numberOfFeatures = 7;

            // Create a matrix that will represent
            // an artificial data set,
            // having 12 items (rows) and 7 features (columns).
            // This will store the observations which
            // partition discovery will be based on.
            var data = DoubleMatrix.Dense(
                numberOfRows: numberOfItems,
                numberOfColumns: numberOfFeatures);

            // Fill the data rows by sampling from a different
            // distribution while, respectively, drawing observations
            // for items 0 to 3, 4 to 7, and 8 to 11: these will be the
            // three different parts expected to be included in the
            // optimal partition.
            double mu = 1.0;
            var    g  = new GaussianDistribution(mu: mu, sigma: .01);

            IndexCollection range = IndexCollection.Range(0, 3);

            for (int j = 0; j < numberOfFeatures; j++)
            {
                data[range, j] = g.Sample(sampleSize: range.Count);
            }

            mu   += 5.0;
            g.Mu  = mu;
            range = IndexCollection.Range(4, 7);
            for (int j = 0; j < numberOfFeatures; j++)
            {
                data[range, j] = g.Sample(sampleSize: range.Count);
            }

            mu   += 5.0;
            g.Mu  = mu;
            range = IndexCollection.Range(8, 11);
            for (int j = 0; j < numberOfFeatures; j++)
            {
                data[range, j] = g.Sample(sampleSize: range.Count);
            }

            Console.WriteLine("The data set:");
            Console.WriteLine(data);

            // Define the maximum number of parts allowed in the
            // partition to be discovered.
            int maximumNumberOfParts = 3;

            // Select the best partition.
            IndexPartition <double> optimalPartition =
                Clusters.Discover(
                    data,
                    maximumNumberOfParts);

            // Show the results.
            Console.WriteLine();
            Console.WriteLine(
                "The optimal partition:");
            Console.WriteLine(optimalPartition);

            Console.WriteLine();
            Console.WriteLine("The Davies-Bouldin Index for the optimal partition:");
            var dbi = IndexPartition.DaviesBouldinIndex(
                data,
                optimalPartition);

            Console.WriteLine(dbi);
        }