View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.clustering.evaluation;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertFalse;
27  import static org.junit.Assert.assertTrue;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import org.hipparchus.clustering.Cluster;
33  import org.hipparchus.clustering.DoublePoint;
34  import org.hipparchus.clustering.distance.EuclideanDistance;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  public class SumOfClusterVariancesTest {
39  
40      private ClusterEvaluator<DoublePoint> evaluator;
41  
42      @Before
43      public void setUp() {
44          evaluator = new SumOfClusterVariances<DoublePoint>(new EuclideanDistance());
45      }
46  
47      @Test
48      public void testScore() {
49          final DoublePoint[] points1 = new DoublePoint[] {
50                  new DoublePoint(new double[] { 1 }),
51                  new DoublePoint(new double[] { 2 }),
52                  new DoublePoint(new double[] { 3 })
53          };
54  
55          final DoublePoint[] points2 = new DoublePoint[] {
56                  new DoublePoint(new double[] { 1 }),
57                  new DoublePoint(new double[] { 5 }),
58                  new DoublePoint(new double[] { 10 })
59          };
60  
61          final List<Cluster<DoublePoint>> clusters = new ArrayList<Cluster<DoublePoint>>();
62  
63          final Cluster<DoublePoint> cluster1 = new Cluster<DoublePoint>();
64          for (DoublePoint p : points1) {
65              cluster1.addPoint(p);
66          }
67          clusters.add(cluster1);
68  
69          assertEquals(1.0/3.0, evaluator.score(clusters), 1e-6);
70  
71          final Cluster<DoublePoint> cluster2 = new Cluster<DoublePoint>();
72          for (DoublePoint p : points2) {
73              cluster2.addPoint(p);
74          }
75          clusters.add(cluster2);
76  
77          assertEquals(6.148148148, evaluator.score(clusters), 1e-6);
78      }
79  
80      @Test
81      public void testOrdering() {
82          assertTrue(evaluator.isBetterScore(10, 20));
83          assertFalse(evaluator.isBetterScore(20, 1));
84      }
85  }