@Test public void testDistance() { System.out.println("distance"); double[] x = {1.2793, -0.1029, -1.5852}; double[] y = {-0.2676, -0.1717, -1.8695}; MahalanobisDistance instance = new MahalanobisDistance(sigma); assertEquals(2.703861, instance.d(x, y), 1E-6); }
@Override public double d(double[] x, double[] y) { if (x.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array x[%d] has different dimension with Sigma[%d][%d].", x.length, sigma.nrows(), sigma.ncols())); } if (y.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array y[%d] has different dimension with Sigma[%d][%d].", y.length, sigma.nrows(), sigma.ncols())); } int n = x.length; double[] z = new double[n]; for (int i = 0; i < n; i++) z[i] = x[i] - y[i]; double dist = sigmaInv.xAx(z); return Math.sqrt(dist); }
MahalanobisDistance implements Metric<double[]> { @Override public double d(double[] x, double[] y) { if (x.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array x[%d] has different dimension with Sigma[%d][%d].", x.length, sigma.nrows(), sigma.ncols())); } if (y.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array y[%d] has different dimension with Sigma[%d][%d].", y.length, sigma.nrows(), sigma.ncols())); } int n = x.length; double[] z = new double[n]; for (int i = 0; i < n; i++) z[i] = x[i] - y[i]; double dist = sigmaInv.xAx(z); return Math.sqrt(dist); } }
MahalanobisDistance implements Metric<double[]> { @Override public double d(double[] x, double[] y) { if (x.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array x[%d] has different dimension with Sigma[%d][%d].", x.length, sigma.nrows(), sigma.ncols())); } if (y.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array y[%d] has different dimension with Sigma[%d][%d].", y.length, sigma.nrows(), sigma.ncols())); } int n = x.length; double[] z = new double[n]; for (int i = 0; i < n; i++) z[i] = x[i] - y[i]; double dist = sigmaInv.xAx(z); return Math.sqrt(dist); } MahalanobisDistance(double[][] cov); }
MahalanobisDistance implements Metric<double[]> { @Override public double d(double[] x, double[] y) { if (x.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array x[%d] has different dimension with Sigma[%d][%d].", x.length, sigma.nrows(), sigma.ncols())); } if (y.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array y[%d] has different dimension with Sigma[%d][%d].", y.length, sigma.nrows(), sigma.ncols())); } int n = x.length; double[] z = new double[n]; for (int i = 0; i < n; i++) z[i] = x[i] - y[i]; double dist = sigmaInv.xAx(z); return Math.sqrt(dist); } MahalanobisDistance(double[][] cov); @Override String toString(); @Override double d(double[] x, double[] y); }
MahalanobisDistance implements Metric<double[]> { @Override public double d(double[] x, double[] y) { if (x.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array x[%d] has different dimension with Sigma[%d][%d].", x.length, sigma.nrows(), sigma.ncols())); } if (y.length != sigma.nrows()) { throw new IllegalArgumentException(String.format("Array y[%d] has different dimension with Sigma[%d][%d].", y.length, sigma.nrows(), sigma.ncols())); } int n = x.length; double[] z = new double[n]; for (int i = 0; i < n; i++) z[i] = x[i] - y[i]; double dist = sigmaInv.xAx(z); return Math.sqrt(dist); } MahalanobisDistance(double[][] cov); @Override String toString(); @Override double d(double[] x, double[] y);  }