All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.librec.similarity.KRCCSimilarity Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2016 LibRec
 * 

* This file is part of LibRec. * LibRec is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. *

* LibRec is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. *

* You should have received a copy of the GNU General Public License * along with LibRec. If not, see . */ package net.librec.similarity; import net.librec.math.structure.Vector; import net.librec.math.structure.VectorBasedSequentialSparseVector; import java.util.*; /** * J. I. Marden, Analyzing and modeling rank data. Boca Raton, Florida: CRC Press, 1996. * Mingming Chen etc. A Ranking-oriented Hybrid Approach to QoS-aware Web Service Recommendation. 2015 *

* Kendall Rank Correlation Coefficient * * @author zhanghaidong */ public class KRCCSimilarity extends AbstractRecommenderSimilarity { /** * Find the common rated items by this user and that user, or the common * users have rated this item or that item. And then return the similarity. * * @param thisVector: the rated items by this user, or users that have rated this * item . * @param thatVector: the rated items by that user, or users that have rated that * item. * @return similarity */ public double getCorrelation(VectorBasedSequentialSparseVector thisVector, VectorBasedSequentialSparseVector thatVector) { if (thisVector == null || thatVector == null || thisVector.size() != thatVector.size()) { return Double.NaN; } // compute similarity List thisList = new ArrayList<>(); List thatList = new ArrayList<>(); Iterator thisIterator = thisVector.iterator(); Iterator thatIterator = thatVector.iterator(); while (thatIterator.hasNext()) { Vector.VectorEntry thatVectorEntry = thatIterator.next(); int thatIndex = thatVectorEntry.index(); thatList.add(thatVectorEntry.get()); if (thisIterator.hasNext()) { while (thisIterator.hasNext()) { Vector.VectorEntry thisVectorEntry = thatIterator.next(); int thisIndex = thisVectorEntry.index(); if (thisIndex == thatIndex) { thisList.add(thisVectorEntry.get()); break; } else if (thisIndex > thatIndex) { thisList.add(0.0D); break; } } } else { thisList.add(0.0D); } } return getSimilarity(thisList, thatList); } /** * Calculate the similarity between thisList and thatList. * * @param thisList this list * @param thatList that list * @return similarity */ protected double getSimilarity(List thisList, List thatList) { if (thisList == null || thatList == null || thisList.size() < 2 || thatList.size() < 2) { return Double.NaN; } Set commonIndices = new HashSet(); for (int i = 0; i < thisList.size(); i++) { if (thisList.get(i).doubleValue() > 0.0 && thatList.get(i).doubleValue() > 0.0) { commonIndices.add(i); } } int numCommonIndices = commonIndices.size(); if (numCommonIndices < 2) { return Double.NaN; } List commonIndexList = new ArrayList(commonIndices); double sum = 0.0; for (int i = 0; i < commonIndexList.size(); i++) { for (int j = i + 1; j < commonIndexList.size(); j++) { double thisDiff = thisList.get(commonIndexList.get(i)).doubleValue() - thisList.get(commonIndexList.get(j)).doubleValue(); double thatDiff = thatList.get(commonIndexList.get(i)).doubleValue() - thatList.get(commonIndexList.get(j)).doubleValue(); if (thisDiff * thatDiff < 0.0) { sum += 1.0; } } } return 1.0 - 4.0 * sum / (numCommonIndices * (numCommonIndices - 1)); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy