package Tutorials; import java.util.Locale; import java.util.Vector; import Tools.KMeans; import jMEF.*; public class Tutorial1 { /** * Converts valid points (>0) to integer. * @param points point set in \f$R^d\f$. * @return a point set in \f$N^d\f$. */ private static PVector[] checkPoints(PVector[] points){ // Count how many point are >0 int n = 0; for (int i=0; i0.5) n++; // Convert valid points as integer PVector[] new_points = new PVector[n]; int idx = 0; for (int i=0; i0.5){ PVector p = new PVector(1); p.array[0] = Math.round( points[i].array[0] ); new_points[idx] = p; idx++; } return new_points; } /** * Compute the NMI. * @param points set of points * @param f initial mixture model * @param g estimated mixture model * @return NMI */ private static double NMI(PVector[] points, MixtureModel f, MixtureModel g){ int m = points.length; int n = f.size; double[][] p = new double[n][n]; for (int i=0; if_max){ f_max = f_tmp; f_label = j; } if (g_tmp>g_max){ g_max = g_tmp; g_label = j; } } p[f_label][g_label]++; } // Normalization for (int i=0; i0) mi += p[i][j] * Math.log( p[i][j] / (fl[i] * gl[j]) ); double hf = 0; double hg = 0; for (int i=0; i[] clusters = KMeans.run(points, 3); // Estimation of the mixture of Gaussians MixtureModel mog; mog = BregmanSoftClustering.initialize(clusters, new UnivariateGaussianFixedVariance(25)); mog = BregmanSoftClustering.run(points, mog); out[0] = NMI(points, f, mog); // Estimation of the mixture of Poisson MixtureModel mop; mop = BregmanSoftClustering.initialize(clusters, new Poisson()); mop = BregmanSoftClustering.run(points, mop); out[1] = NMI(points, f, mop); // Estimation of the mixture of Poisson MixtureModel mob; mob = BregmanSoftClustering.initialize(clusters, new BinomialFixedN(100)); mob = BregmanSoftClustering.run(points, mob); out[2] = NMI(points, f, mob); // Return return out; } /** * Compute the mixtures from points drawn from a mixture of Poisson distributions. * @param m number of points drawn from the mixture. */ private static double[] testPoisson(int m){ // double[] out = new double[3]; // Initial model : Poisson MixtureModel f = new MixtureModel(3); f.EF = new Poisson(); f.weight[0] = 1.0/3.0; f.weight[1] = 1.0/3.0; f.weight[2] = 1.0/3.0; PVector p1 = new PVector(1); PVector p2 = new PVector(1); PVector p3 = new PVector(1); p1.array[0] = 10; p2.array[0] = 20; p3.array[0] = 40; f.param[0] = p1; f.param[1] = p2; f.param[2] = p3; // Draw points from the mixture PVector[] points = f.drawRandomPoints(m); points = checkPoints(points); // K-means Vector[] clusters = KMeans.run(points, 3); // Estimation of the mixture of Gaussians MixtureModel mog; mog = BregmanSoftClustering.initialize(clusters, new UnivariateGaussianFixedVariance(25)); mog = BregmanSoftClustering.run(points, mog); out[0] = NMI(points, f, mog); // Estimation of the mixture of Poisson MixtureModel mop; mop = BregmanSoftClustering.initialize(clusters, new Poisson()); mop = BregmanSoftClustering.run(points, mop); out[1] = NMI(points, f, mop); // Estimation of the mixture of Poisson MixtureModel mob; mob = BregmanSoftClustering.initialize(clusters, new BinomialFixedN(100)); mob = BregmanSoftClustering.run(points, mob); out[2] = NMI(points, f, mob); // Return return out; } /** * Compute the mixtures from points drawn from a mixture of Binomial distributions. * @param m number of points drawn from the mixture. */ private static double[] testBinomial(int m){ // Output vector double[] out = new double[3]; // Initial model : Binomial MixtureModel f = new MixtureModel(3); f.EF = new BinomialFixedN(100); f.weight[0] = 1.0/3.0; f.weight[1] = 1.0/3.0; f.weight[2] = 1.0/3.0; PVector p1 = new PVector(1); PVector p2 = new PVector(1); PVector p3 = new PVector(1); p1.array[0] = 0.1; p2.array[0] = 0.2; p3.array[0] = 0.4; f.param[0] = p1; f.param[1] = p2; f.param[2] = p3; // Draw points from the mixture PVector[] points = f.drawRandomPoints(m); points = checkPoints(points); // K-means Vector[] clusters = KMeans.run(points, 3); // Estimation of the mixture of Gaussians MixtureModel mog; mog = BregmanSoftClustering.initialize(clusters, new UnivariateGaussianFixedVariance(25)); mog = BregmanSoftClustering.run(points, mog); out[0] = NMI(points, f, mog); // Estimation of the mixture of Poisson MixtureModel mop; mop = BregmanSoftClustering.initialize(clusters, new Poisson()); mop = BregmanSoftClustering.run(points, mop); out[1] = NMI(points, f, mop); // Estimation of the mixture of Poisson MixtureModel mob; mob = BregmanSoftClustering.initialize(clusters, new BinomialFixedN(100)); mob = BregmanSoftClustering.run(points, mob); out[2] = NMI(points, f, mob); // Return return out; } /** * Main function. * @param args */ public static void main(String[] args) { // Display String title = ""; title += "+-------------------------+\n"; title += "| Bregman soft clustering |\n"; title += "+-------------------------+\n"; System.out.print(title); // Variables int m = 1000; int loop = 100; // NMI arrays double[][] NMI_Gaussian = new double[loop][3]; double[][] NMI_Poisson = new double[loop][3]; double[][] NMI_Binomial = new double[loop][3]; // Computation of NMI for (int l=0; l