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);
}
}