void Start()
 {
     speed = GaussianDistribution.Generate(meanSpeed, 0);
 }
Exemple #2
0
 public void AtTests()
 {
     // Verified with WolframAlpha
     // (e.g. http://www.wolframalpha.com/input/?i=PDF%5BNormalDistribution%5B0%2C1%5D%2C+0.5%5D )
     Assert.AreEqual(0.352065, GaussianDistribution.At(0.5), ERROR_TOLERANCE);
 }
Exemple #3
0
 public void CumulativeToTests()
 {
     // Verified with WolframAlpha
     // (e.g. http://www.wolframalpha.com/input/?i=CDF%5BNormalDistribution%5B0%2C1%5D%2C+0.5%5D )
     Assert.AreEqual(0.691462, GaussianDistribution.CumulativeTo(0.5), ERROR_TOLERANCE);
 }
Exemple #4
0
 /// <summary>
 /// Given a difficulty, return the chance of success for it.
 /// </summary>
 /// <param name="difficulty"></param>
 /// <returns>Higher means easier</returns>
 public static double ChanceOfSuccess(double difficulty)
 {
     return(GaussianDistribution.CumulativeTo(difficulty, World.Mean, World.StandardDeviation));
 }
Exemple #5
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);
            }
        }
Exemple #6
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);
        }