Accord.Tests.Statistics.MixtureDistributionTest.MixtureWeightsFitTest C# (CSharp) Method

MixtureWeightsFitTest() private method

private MixtureWeightsFitTest ( ) : void
return void
        public void MixtureWeightsFitTest()
        {
            // Randomly initialize some mixture components
            NormalDistribution[] components = new NormalDistribution[2];
            components[0] = new NormalDistribution(2, 1);
            components[1] = new NormalDistribution(5, 1);

            // Create an initial mixture
            Mixture<NormalDistribution> mixture = new Mixture<NormalDistribution>(components);

            // Now, suppose we have a weighted data set. Those will be the input points:
            double[] points = { 0, 3, 1, 7, 3, 5, 1, 2, -1, 2, 7, 6, 8, 6 }; // (14 points)

            // And those are their respective unnormalized weights:
            double[] weights = { 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 2, 3, 1, 1 }; // (14 weights)

            // Let's normalize the weights so they sum up to one:
            weights = weights.Divide(weights.Sum());

            // Now we can fit our model to the data:
            mixture.Fit(points, weights);   // done!

            // Our model will be:
            double mean1 = mixture.Components[0].Mean; // 1.41126
            double mean2 = mixture.Components[1].Mean; // 6.53301

            // With mixture coefficients
            double pi1 = mixture.Coefficients[0]; // 0.51408489193241225
            double pi2 = mixture.Coefficients[1]; // 0.48591510806758775

            Assert.AreEqual(1.4112610766836411, mean1);
            Assert.AreEqual(6.5330177004151064, mean2);

            Assert.AreEqual(0.51408489193241225, pi1);
            Assert.AreEqual(0.48591510806758775, pi2);

            /*
                        // If we need the GaussianMixtureModel functionality, we can
                        // use the estimated mixture to initialize a new model:
                        GaussianMixtureModel gmm = new GaussianMixtureModel(mixture);

                        Assert.AreEqual(mean1, gmm.Gaussians[0].Mean[0]);
                        Assert.AreEqual(mean2, gmm.Gaussians[1].Mean[0]);

                        Assert.AreEqual(mean1, 1.4112610766836404, 1e-15);
                        Assert.AreEqual(mean2, 6.5330177004151082, 1e-14);

                        Assert.AreEqual(mixture.Coefficients[0], gmm.Gaussians[0].Proportion);
                        Assert.AreEqual(mixture.Coefficients[1], gmm.Gaussians[1].Proportion);
            */
        }