test_class.declarator: log = LoggerFactory.getLogger(MADTest.class)
test_class.modifier: private static final
test_class.original_string: private static final Logger log = LoggerFactory.getLogger(MADTest.class);
test_class.type: Logger
test_class.var_name: log
test_class.file: legacy/src/test/java/macrobase/analysis/stats/GaussianTest.java
test_class.identifier: GaussianTest
test_case.body: @Test
      public void testMahalanobis() {
          MultivariateNormalDistribution mvNormal = getSample3dNormal();
          Gaussian gaussian = new Gaussian(mvNormal.getMeans(), mvNormal.getCovariances());
  
          int N = 100000;
          int k = 3;
          double[][] testData = new double[N][k];
          for (int i = 0; i < N; i++) {
              testData[i] = mvNormal.sample();
          }
  
          double[] mScores = new double[N];
          long startTime = System.currentTimeMillis();
          for (int i = 0; i < N; i++) {
              mScores[i] = gaussian.mahalanobis(testData[i]);
          }
          long endTime = System.currentTimeMillis();
          log.debug("Mahalobis distance on {} in {}", N, endTime-startTime);
  
          double[] dScores = new double[N];
          startTime = System.currentTimeMillis();
          for (int i = 0; i < N; i++) {
              dScores[i] = -Math.log(mvNormal.density(testData[i]));
          }
          endTime = System.currentTimeMillis();
          log.debug("LogPDF on {} in {}", N, endTime-startTime);
  
          // Check that mahalonbis distance has same relative magnitude as -log(pdf)
          for (int i = 1; i < N; i++) {
              assertEquals(mScores[i] > mScores[i-1], dScores[i] > dScores[i-1]);
          }
      }
test_case.class_method_signature: GaussianTest.testMahalanobis()
test_case.full_signature: @Test public void testMahalanobis()
test_case.identifier: testMahalanobis
test_case.invocations: getSample3dNormal getMeans getCovariances sample currentTimeMillis mahalanobis currentTimeMillis debug currentTimeMillis log density currentTimeMillis debug assertEquals
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testMahalanobis()
focal_class.declarator: k=0
focal_class.modifier: public
focal_class.original_string: public int k=0;
focal_class.type: int
focal_class.var_name: k
focal_class.declarator: mean
focal_class.modifier: public
focal_class.original_string: public double[] mean;
focal_class.type: double[]
focal_class.var_name: mean
focal_class.declarator: cov
focal_class.modifier: public
focal_class.original_string: public RealMatrix cov;
focal_class.type: RealMatrix
focal_class.var_name: cov
focal_class.declarator: inverseCov
focal_class.modifier: public
focal_class.original_string: public double[][] inverseCov;
focal_class.type: double[][]
focal_class.var_name: inverseCov
focal_class.declarator: singular = false
focal_class.modifier: public
focal_class.original_string: public boolean singular = false;
focal_class.type: boolean
focal_class.var_name: singular
focal_class.file: legacy/src/main/java/macrobase/analysis/stats/Gaussian.java
focal_class.identifier: Gaussian
focal_class.class_method_signature: Gaussian.Gaussian()
focal_class.full_signature: public  Gaussian()
focal_class.identifier: Gaussian
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.signature:  Gaussian()
focal_class.class_method_signature: Gaussian.Gaussian(double[] mean, RealMatrix cov)
focal_class.full_signature: public  Gaussian(double[] mean, RealMatrix cov)
focal_class.identifier: Gaussian
focal_class.modifiers: public
focal_class.parameters: (double[] mean, RealMatrix cov)
focal_class.signature:  Gaussian(double[] mean, RealMatrix cov)
focal_class.class_method_signature: Gaussian.fit(List<double[]> data)
focal_class.full_signature: public Gaussian fit(List<double[]> data)
focal_class.identifier: fit
focal_class.modifiers: public
focal_class.parameters: (List<double[]> data)
focal_class.return: Gaussian
focal_class.signature: Gaussian fit(List<double[]> data)
focal_class.class_method_signature: Gaussian.initialize()
focal_class.full_signature: public void initialize()
focal_class.identifier: initialize
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void initialize()
focal_class.class_method_signature: Gaussian.mahalanobis(double[] query)
focal_class.full_signature: public double mahalanobis(double[] query)
focal_class.identifier: mahalanobis
focal_class.modifiers: public
focal_class.parameters: (double[] query)
focal_class.return: double
focal_class.signature: double mahalanobis(double[] query)
focal_class.class_method_signature: Gaussian.getMean()
focal_class.full_signature: public double[] getMean()
focal_class.identifier: getMean
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: double[]
focal_class.signature: double[] getMean()
focal_class.class_method_signature: Gaussian.getCovariance()
focal_class.full_signature: public RealMatrix getCovariance()
focal_class.identifier: getCovariance
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: RealMatrix
focal_class.signature: RealMatrix getCovariance()
focal_method.body: public double mahalanobis(double[] query) {
          double[] delta = new double[k];
  
          for (int i = 0; i < k; i++) {
              delta[i] = query[i] - mean[i];
          }
  
          double diagSum = 0, nonDiagSum = 0;
  
          for (int d1 = 0; d1 < k; ++d1) {
              for (int d2 = d1; d2 < k; ++d2) {
                  double v = delta[d1] * delta[d2] * inverseCov[d1][d2];
                  if (d1 == d2) {
                      diagSum += v;
                  } else {
                      nonDiagSum += v;
                  }
              }
          }
  
          return diagSum + 2 * nonDiagSum;
      }
focal_method.class_method_signature: Gaussian.mahalanobis(double[] query)
focal_method.full_signature: public double mahalanobis(double[] query)
focal_method.identifier: mahalanobis
focal_method.modifiers: public
focal_method.parameters: (double[] query)
focal_method.return: double
focal_method.signature: double mahalanobis(double[] query)