public void GaussianMixtureModelTest4() { // Suppose we have a weighted data set. Those are the input points: double[][] points = { new double[] { 0 }, new double[] { 3 }, new double[] { 1 }, new double[] { 7 }, new double[] { 3 }, new double[] { 5 }, new double[] { 1 }, new double[] { 2 }, new double[] { -1 }, new double[] { 2 }, new double[] { 7 }, new double[] { 6 }, new double[] { 8 }, new double[] { 6 } // (14 points) }; // And those are their respective unnormalized weights: double[] weights = { 10, 1, 1, 2, 2, 1, 1, 1, 8, 1, 2, 5, 1, 1 }; // (14 weights) // with weights { Accord.Math.Tools.SetupGenerator(0); // If we need the GaussianMixtureModel functionality, we can // use the estimated mixture to initialize a new model: GaussianMixtureModel gmm = new GaussianMixtureModel(2); gmm.Initializations = 1; var options = new GaussianMixtureModelOptions(); options.Weights = weights; options.ParallelOptions.MaxDegreeOfParallelism = 1; gmm.Compute(points, options); int a = 1; int b = 0; double tol = 1e-6; if (!gmm.Gaussians[a].Mean[0].IsRelativelyEqual(6.41922, 1e-4)) { var t = a; a = b; b = t; } Assert.AreEqual(6.4192285647145395, gmm.Gaussians[a].Mean[0], tol); Assert.AreEqual(0.2888226129013588, gmm.Gaussians[b].Mean[0], tol); Assert.AreEqual(0.32321638614859777, gmm.Gaussians[a].Proportion, tol); Assert.AreEqual(0.67678361385140218, gmm.Gaussians[b].Proportion, tol); Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion); } // with weights, new style { Accord.Math.Tools.SetupGenerator(0); // If we need the GaussianMixtureModel functionality, we can // use the estimated mixture to initialize a new model: GaussianMixtureModel gmm = new GaussianMixtureModel(2); gmm.Initializations = 1; gmm.UseLogarithm = false; gmm.ParallelOptions.MaxDegreeOfParallelism = 1; gmm.Learn(points, weights); int a = 1; int b = 0; double tol = 1e-6; if (!gmm.Gaussians[a].Mean[0].IsRelativelyEqual(6.41922, 1e-4)) { var t = a; a = b; b = t; } Assert.AreEqual(6.4192285647145395, gmm.Gaussians[a].Mean[0], tol); Assert.AreEqual(0.2888226129013588, gmm.Gaussians[b].Mean[0], tol); Assert.AreEqual(0.32321638614859777, gmm.Gaussians[a].Proportion, tol); Assert.AreEqual(0.67678361385140218, gmm.Gaussians[b].Proportion, tol); Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion); } // without weights { Accord.Math.Random.Generator.Seed = 0; // If we need the GaussianMixtureModel functionality, we can // use the estimated mixture to initialize a new model: GaussianMixtureModel gmm = new GaussianMixtureModel(2); gmm.Initializations = 1; var options = new GaussianMixtureModelOptions(); options.ParallelOptions.MaxDegreeOfParallelism = 1; gmm.Compute(points, options); int a = 1; int b = 0; double tol = 1e-2; if (!6.5149525060859848.IsRelativelyEqual(gmm.Gaussians[a].Mean[0], tol)) { var t = a; a = b; b = t; } Assert.AreEqual(6.5149525060859848, gmm.Gaussians[a].Mean[0], tol); Assert.AreEqual(1.4191977895308987, gmm.Gaussians[b].Mean[0], tol); Assert.AreEqual(0.4195042394315267, gmm.Gaussians[a].Proportion, tol); Assert.AreEqual(0.58049576056847307, gmm.Gaussians[b].Proportion, tol); Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion, 1e-8); } }
public void GenerateGMM() { double[][] mixture = new double[DeltaScores.Count][]; for (int i = 0; i < DeltaScores.Count; i++) { mixture[i] = new double[] { DeltaScores[i] }; } gmm = new GaussianMixtureModel(2); // If available, initialize with k-means //if (kmeans != null) gmm.Initialize(kmeans); // Compute the model GaussianMixtureModelOptions gmmo = new GaussianMixtureModelOptions(); gmmo.Logarithm = true; gmm.Compute(mixture, 0.000000000000000000000001); //Find out who is the big median gaussian and small media gaussian double[] meanTmp1 = gmm.Gaussians[0].Mean; double[] meanTmp2 = gmm.Gaussians[1].Mean; ProportionSmallGaussian = -1; ProportionBigGaussian = -1; if (meanTmp1[0] < meanTmp2[0]) { GaussianSmallMean = gmm.Gaussians[0].ToDistribution(); ProportionSmallGaussian = gmm.Gaussians[0].Proportion; GaussianBigMean = gmm.Gaussians[1].ToDistribution(); ProportionBigGaussian = gmm.Gaussians[1].Proportion; } else { GaussianSmallMean = gmm.Gaussians[1].ToDistribution(); ProportionSmallGaussian = gmm.Gaussians[1].Proportion; GaussianBigMean = gmm.Gaussians[0].ToDistribution(); ProportionBigGaussian = gmm.Gaussians[0].Proportion; } //And now find GMM intersect and the cumulative intersect double min = DeltaScores.Min(); double max = DeltaScores.Max(); List <double> smallMeanValues = new List <double>(); List <double> largeMeanValues = new List <double>(); //Determine intersect of the two Gaussians double GMintersect = double.PositiveInfinity; double GMdistance2 = double.PositiveInfinity; double Cintersect = double.PositiveInfinity; double Cdistance2 = double.PositiveInfinity; for (double i = GaussianSmallMean.Median[0]; i <= GaussianBigMean.Median[0]; i += 0.02) { //Gaussian Intersect //double GMredG = GaussianSmallMean.ProbabilityDensityFunction(i) * ProportionSmallGaussian; //double GMblueG = GaussianBigMean.ProbabilityDensityFunction(i) * ProportionBigGaussian; double GMredG = GaussianSmallMean.ProbabilityDensityFunction(i); double GMblueG = GaussianBigMean.ProbabilityDensityFunction(i); double GMdistanceThis = Math.Abs(GMredG - GMblueG); if (GMdistanceThis < Math.Abs(GMdistance2)) { GMdistance2 = GMdistanceThis; GMintersect = i; } //Cumulative Intersect //double CredG = GaussianSmallMean.ComplementaryDistributionFunction(i) * ProportionSmallGaussian; //double CblueG = GaussianBigMean.DistributionFunction(i) * ProportionBigGaussian; double CredG = GaussianSmallMean.ComplementaryDistributionFunction(i); double CblueG = GaussianBigMean.DistributionFunction(i); double CdistanceThis = Math.Abs(CredG - CblueG); if (CdistanceThis < Math.Abs(Cdistance2)) { Cdistance2 = CdistanceThis; Cintersect = i; } } Console.WriteLine("Gaussian Meeting point : " + GMintersect); Console.WriteLine("Cumulative Meeting point : " + Cintersect); GaussianIntersect = GMintersect; CumulativeIntersect = Cintersect; }