Hellinger { public static double distance(double[] v1, double[] v2){ Asserts.assertEqualLength(v1, v2); Asserts.assertGreaterOrEqualThanValue(v1, 0); Asserts.assertGreaterOrEqualThanValue(v2, 0); double meanV1 = CommonStats.mean(v1); double meanV2 = CommonStats.mean(v2); int n = v1.length; double sum = 0; for(int i=0;i<n;i++){ sum += Math.pow(Math.sqrt(v1[i]/meanV1)-Math.sqrt(v2[i]/meanV2),2); } return Math.sqrt(2*sum); }  static double distance(double[] v1, double[] v2);  }
@Test public void test1(){ double[] v1 = new double[]{8,3,7}; double[] v2 = new double[]{9,8,4}; Asserts.assertEqualLength(v1, v2); double res = Hellinger.distance(v1, v2); Asserts.assertEquals(res, 0.68779977213681889966); }
@Test public void test2(){ double[] v1 = new double[]{8,32,7,0,45}; double[] v2 = new double[]{97,78,41,4,78}; Asserts.assertEqualLength(v1, v2); double res = Hellinger.distance(v1, v2); Asserts.assertEquals(res, 1.18241145809914240699); }
@Test public void test3(){ double[] v1 = new double[]{248.124,321.574,788.14,0.001,459.298}; double[] v2 = new double[]{97.145,78.658,417.0004,4.02,7865.3594}; Asserts.assertEqualLength(v1, v2); double res = Hellinger.distance(v1, v2); Asserts.assertEquals(res, 2.4039112939980751295); }