@@ -0,0 +1,2638 @@
+/*%0A * LensKit, a reference implementation of recommender algorithms.%0A * Copyright 2010-2011 Regents of the University of Minnesota%0A *%0A * This program is free software; you can redistribute it and/or modify%0A * it under the terms of the GNU Lesser General Public License as%0A * published by the Free Software Foundation; either version 2.1 of the%0A * License, or (at your option) any later version.%0A *%0A * This program is distributed in the hope that it will be useful, but WITHOUT%0A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS%0A * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more%0A * details.%0A *%0A * You should have received a copy of the GNU General Public License along with%0A * this program; if not, write to the Free Software Foundation, Inc., 51%0A * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.%0A */%0Apackage org.grouplens.lenskit.knn;%0A%0A%0Aimport static org.grouplens.lenskit.knn.SpearmanRankCorrelation.rank;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport org.grouplens.lenskit.data.vector.MutableSparseVector;%0Aimport org.grouplens.lenskit.data.vector.SparseVector;%0Aimport org.junit.Test;%0A%0A/**%0A * @author Michael Ekstrand %3Cekstrand@cs.umn.edu%3E%0A *%0A */%0Apublic class SpearmanRankCorrelationTest %7B%0A    %0A    @Test%0A    public void testRankEmpty() %7B%0A        assertTrue(rank(new MutableSparseVector()).isEmpty());%0A    %7D%0A    %0A    @Test%0A    public void testRankSingle() %7B%0A        SparseVector v = SparseVector.wrap(new long%5B%5D%7B1%7D, new double%5B%5D%7B5%7D);%0A        SparseVector r = rank(v);%0A        assertEquals(1, r.size());%0A        assertEquals(1, r.get(1), 1.0e-6);%0A    %7D%0A    %0A    @Test%0A    public void testRankSeveral() %7B%0A        long%5B%5D keys = %7B 1, 2, 3, 4, 5 %7D;%0A        double%5B%5D values = %7B 7, 2, 3, 1, 5%7D;%0A        SparseVector v = SparseVector.wrap(keys, values);%0A        SparseVector r = rank(v);%0A        assertEquals(5, r.size());%0A        assertEquals(1, r.get(1), 1.0e-6);%0A        assertEquals(2, r.get(5), 1.0e-6);%0A        assertEquals(3, r.get(3), 1.0e-6);%0A        assertEquals(4, r.get(2), 1.0e-6);%0A        assertEquals(5, r.get(4), 1.0e-6);%0A    %7D%0A    %0A    @Test%0A    public void testRankTie() %7B%0A        long%5B%5D keys = %7B 1, 2, 3, 4, 5 %7D;%0A        double%5B%5D values = %7B 7, 2, 3, 1, 3%7D;%0A        SparseVector v = SparseVector.wrap(keys, values);%0A        SparseVector r = rank(v);%0A        assertEquals(5, r.size());%0A        assertEquals(1, r.get(1), 1.0e-6);%0A        assertEquals(2.5, r.get(3), 1.0e-6);%0A        assertEquals(2.5, r.get(5), 1.0e-6);%0A        assertEquals(4, r.get(2), 1.0e-6);%0A        assertEquals(5, r.get(4), 1.0e-6);%0A    %7D%0A%0A%7D%0A

Test Spearman rank correlation