test_class.declarator: log = LoggerFactory.getLogger(FiniteGMMTest.class)
test_class.modifier: private static final
test_class.original_string: private static final Logger log = LoggerFactory.getLogger(FiniteGMMTest.class);
test_class.type: Logger
test_class.var_name: log
test_class.file: contrib/src/test/java/macrobase/analysis/stats/mixture/FiniteGMMTest.java
test_class.identifier: FiniteGMMTest
test_case.body: @Test
      /**
       * Tests Bayesian Gaussian Mixture Model on a three well separated clusters.
       */
      public void bivariateWellSeparatedNormalTest() throws Exception {
          MacroBaseConf conf = new MacroBaseConf()
                  .set(MacroBaseConf.RANDOM_SEED, 44)
                  .set(MacroBaseConf.TRANSFORM_TYPE, "MEAN_FIELD_GMM")
                  .set(GMMConf.NUM_MIXTURES, 3)
                  .set(GMMConf.MIXTURE_CENTERS_FILE, "src/test/resources/data/3gaussians-700.points-centers.json")
                  .set(MacroBaseConf.DATA_LOADER_TYPE, "CSV_LOADER")
                  .set(MacroBaseConf.CSV_COMPRESSION, CSVIngester.Compression.GZIP)
                  .set(MacroBaseConf.CSV_INPUT_FILE, "src/test/resources/data/3gaussians-700points.csv.gz")
                  .set(MacroBaseConf.METRICS, "XX, YY")
                  .set(MacroBaseConf.ATTRIBUTES, "");
          List<Datum> data = Drainer.drainIngest(conf);
          assertEquals(700, data.size());
  
          FiniteGMM finiteGMM = new FiniteGMM(conf);
          List<RealVector> calculatedMeans;
  
          // Make sure we have 3 clusters. Sometimes initialization is not great.
          finiteGMM.train(data);
  
          calculatedMeans = finiteGMM.getClusterCenters();
          List<RealMatrix> calculatedCovariances = finiteGMM.getClusterCovariances();
  
          double[][] clusterMeans = {
                  {2, 11},
                  {1, 1},
                  {10, 3},
          };
          List<RealVector> vectorClusterMeans = new ArrayList<>(3);
          for (int k = 0; k < 3; k++) {
              vectorClusterMeans.add(new ArrayRealVector(clusterMeans[k]));
          }
          double[][][] clusterCovariances = {
                  {{0.5, 0.4}, {0.4, 0.5}},
                  {{0.3, 0}, {0, 0.6}},
                  {{0.9, 0.2}, {0.2, 0.3}},
          };
  
  
          ExpectMaxGMM gmm = new ExpectMaxGMM(conf);
          gmm.train(data);
          List<RealVector> emMeans = gmm.getClusterCenters();
          List<RealMatrix> emCovariances = gmm.getClusterCovariances();
  
          for (int i = 0; i < 3; i++) {
              boolean identified = false;
              for (int j = 0; j < 3; j++) {
                  if (calculatedMeans.get(i).getDistance(vectorClusterMeans.get(j)) < 0.1) {
                      for (int p = 0; p < 2; p++) {
                          for (int q = 0; q < 2; q++) {
                              // Make sure covariance is in the ballpark. Since we only had 700 points.
                              assertEquals(clusterCovariances[j][p][q], calculatedCovariances.get(i).getEntry(p, q), 0.6);
                          }
                      }
                      identified = true;
                      break;
                  }
              }
              assertEquals(true, identified);
              for (int z = 0; z < 3; z++) {
                  if (emMeans.get(z).getDistance(calculatedMeans.get(i)) < 0.1) {
                      for (int p = 0; p < 2; p++) {
                          for (int q = 0; q < 2; q++) {
                              // Make sure we have a closer estimate to EM algorithm means
                              assertEquals(emCovariances.get(z).getEntry(p, q), calculatedCovariances.get(i).getEntry(p, q), 0.1);
                          }
                      }
                      break;
                  }
              }
          }
  
          // Make sure centers belong to only one cluster.
          double[] maxProbas = {0, 0, 0};
          for (int i = 0; i < 3; i++) {
              double[] probas = finiteGMM.getClusterProbabilities(new Datum(new ArrayList<Integer>(), vectorClusterMeans.get(i)));
              for (int j=0; j< 3; j++) {
                  maxProbas[j] = Math.max(probas[j], maxProbas[j]);
              }
          }
          for (int j=0; j< 3; j++) {
              assertEquals(maxProbas[j], 1, 0.01);
          }
      }
test_case.class_method_signature: FiniteGMMTest.bivariateWellSeparatedNormalTest()
test_case.full_signature: @Test /** * Tests Bayesian Gaussian Mixture Model on a three well separated clusters. */ public void bivariateWellSeparatedNormalTest()
test_case.identifier: bivariateWellSeparatedNormalTest
test_case.invocations: set set set set set set set set set drainIngest assertEquals size train getClusterCenters getClusterCovariances add train getClusterCenters getClusterCovariances getDistance get get assertEquals getEntry get assertEquals getDistance get get assertEquals getEntry get getEntry get getClusterProbabilities get max assertEquals
test_case.modifiers: @Test /** * Tests Bayesian Gaussian Mixture Model on a three well separated clusters. */ public
test_case.parameters: ()
test_case.return: void
test_case.signature: void bivariateWellSeparatedNormalTest()
focal_class.declarator: log = LoggerFactory.getLogger(FiniteGMM.class)
focal_class.modifier: private static final
focal_class.original_string: private static final Logger log = LoggerFactory.getLogger(FiniteGMM.class);
focal_class.type: Logger
focal_class.var_name: log
focal_class.declarator: K
focal_class.modifier: protected
focal_class.original_string: protected int K;
focal_class.type: int
focal_class.var_name: K
focal_class.declarator: mixingComponents
focal_class.modifier: protected
focal_class.original_string: protected MultiComponents mixingComponents;
focal_class.type: MultiComponents
focal_class.var_name: mixingComponents
focal_class.file: contrib/src/main/java/macrobase/analysis/stats/mixture/FiniteGMM.java
focal_class.identifier: FiniteGMM
focal_class.class_method_signature: FiniteGMM.FiniteGMM(MacroBaseConf conf)
focal_class.full_signature: public  FiniteGMM(MacroBaseConf conf)
focal_class.identifier: FiniteGMM
focal_class.modifiers: public
focal_class.parameters: (MacroBaseConf conf)
focal_class.signature:  FiniteGMM(MacroBaseConf conf)
focal_class.class_method_signature: FiniteGMM.trainTest(List<Datum> trainData, List<Datum> testData)
focal_class.full_signature: @Override public void trainTest(List<Datum> trainData, List<Datum> testData)
focal_class.identifier: trainTest
focal_class.modifiers: @Override public
focal_class.parameters: (List<Datum> trainData, List<Datum> testData)
focal_class.return: void
focal_class.signature: void trainTest(List<Datum> trainData, List<Datum> testData)
focal_class.class_method_signature: FiniteGMM.getClusterProportions()
focal_class.full_signature: @Override public double[] getClusterProportions()
focal_class.identifier: getClusterProportions
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: double[]
focal_class.signature: double[] getClusterProportions()
focal_class.class_method_signature: FiniteGMM.getNormClusterContrib()
focal_class.full_signature: @Override protected double[] getNormClusterContrib()
focal_class.identifier: getNormClusterContrib
focal_class.modifiers: @Override protected
focal_class.parameters: ()
focal_class.return: double[]
focal_class.signature: double[] getNormClusterContrib()
focal_class.class_method_signature: FiniteGMM.getPriorAdjustedClusterProportions()
focal_class.full_signature: public double[] getPriorAdjustedClusterProportions()
focal_class.identifier: getPriorAdjustedClusterProportions
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: double[]
focal_class.signature: double[] getPriorAdjustedClusterProportions()
focal_class.class_method_signature: FiniteGMM.getClusterProbabilities(Datum d)
focal_class.full_signature: @Override public double[] getClusterProbabilities(Datum d)
focal_class.identifier: getClusterProbabilities
focal_class.modifiers: @Override public
focal_class.parameters: (Datum d)
focal_class.return: double[]
focal_class.signature: double[] getClusterProbabilities(Datum d)
focal_class.superclass: extends VarGMM
focal_method.body: @Override
      public double[] getClusterProbabilities(Datum d) {
          double[] probas = new double[K];
          double[] weights = getNormClusterContrib();
          double normalizingConstant = 0;
          for (int i = 0; i < K; i++) {
              probas[i] = weights[i] * predictiveDistributions.get(i).density(d.metrics());
              normalizingConstant += probas[i];
          }
          for (int i = 0; i < K; i++) {
              probas[i] /= normalizingConstant;
          }
          return probas;
      }
focal_method.class_method_signature: FiniteGMM.getClusterProbabilities(Datum d)
focal_method.full_signature: @Override public double[] getClusterProbabilities(Datum d)
focal_method.identifier: getClusterProbabilities
focal_method.invocations: getNormClusterContrib density get metrics
focal_method.modifiers: @Override public
focal_method.parameters: (Datum d)
focal_method.return: double[]
focal_method.signature: double[] getClusterProbabilities(Datum d)