public final class PearsonCorrelationSimilarity extends AbstractItemSimilarity
An implementation of the Pearson correlation. For users X and Y, the following values are calculated:
The correlation is then:
sumXY / sqrt(sumX2 * sumY2)
Note that this correlation "centers" its data, shifts the user's preference values so that each of their means is 0. This is necessary to achieve expected behavior on all data sets.
This correlation implementation is equivalent to the cosine similarity since the data it receives is assumed to be centered -- mean is 0. The correlation may be interpreted as the cosine of the angle between the two vectors defined by the users' preference values.
For cosine similarity on uncentered data, see UncenteredCosineSimilarity
.
Constructor and Description |
---|
PearsonCorrelationSimilarity(DataModel dataModel) |
PearsonCorrelationSimilarity(DataModel dataModel,
Weighting weighting) |
Modifier and Type | Method and Description |
---|---|
double[] |
itemSimilarities(long itemID1,
long[] itemID2s)
A bulk-get version of
ItemSimilarity.itemSimilarity(long, long) . |
double |
itemSimilarity(long itemID1,
long itemID2)
Returns the degree of similarity, of two items, based on the preferences that users have expressed for
the items.
|
void |
refresh(Collection<Refreshable> alreadyRefreshed)
Triggers "refresh" -- whatever that means -- of the implementation.
|
void |
setPreferenceInferrer(PreferenceInferrer inferrer)
Attaches a
PreferenceInferrer to the UserSimilarity implementation. |
String |
toString() |
double |
userSimilarity(long userID1,
long userID2)
Returns the degree of similarity, of two users, based on the their preferences.
|
allSimilarItemIDs, getDataModel
public PearsonCorrelationSimilarity(DataModel dataModel) throws TasteException
IllegalArgumentException
- if DataModel
does not have preference valuesTasteException
public PearsonCorrelationSimilarity(DataModel dataModel, Weighting weighting) throws TasteException
IllegalArgumentException
- if DataModel
does not have preference valuesTasteException
public final void setPreferenceInferrer(PreferenceInferrer inferrer)
UserSimilarity
Attaches a PreferenceInferrer
to the UserSimilarity
implementation.
setPreferenceInferrer
in interface UserSimilarity
inferrer
- PreferenceInferrer
public double userSimilarity(long userID1, long userID2) throws TasteException
UserSimilarity
Returns the degree of similarity, of two users, based on the their preferences.
userSimilarity
in interface UserSimilarity
userID1
- first user IDuserID2
- second user IDDouble.NaN
similarity is unknownNoSuchUserException
- if either user is known to be non-existent in the dataTasteException
- if an error occurs while accessing the datapublic final double itemSimilarity(long itemID1, long itemID2) throws TasteException
ItemSimilarity
Returns the degree of similarity, of two items, based on the preferences that users have expressed for the items.
itemSimilarity
in interface ItemSimilarity
itemID1
- first item IDitemID2
- second item IDDouble.NaN
similarity is unknownNoSuchItemException
- if either item is known to be non-existent in the dataTasteException
- if an error occurs while accessing the datapublic double[] itemSimilarities(long itemID1, long[] itemID2s) throws TasteException
ItemSimilarity
A bulk-get version of ItemSimilarity.itemSimilarity(long, long)
.
itemSimilarities
in interface ItemSimilarity
itemID1
- first item IDitemID2s
- second item IDs to compute similarity withNoSuchItemException
- if any item is known to be non-existent in the dataTasteException
- if an error occurs while accessing the datapublic final void refresh(Collection<Refreshable> alreadyRefreshed)
Refreshable
Triggers "refresh" -- whatever that means -- of the implementation. The general contract is that any
Refreshable
should always leave itself in a consistent, operational state, and that the refresh
atomically updates internal state from old to new.
refresh
in interface Refreshable
refresh
in class AbstractItemSimilarity
alreadyRefreshed
- Refreshable
s that are known to have already been
refreshed as a result of an initial call to a {#refresh(Collection)} method on some
object. This ensure that objects in a refresh dependency graph aren't refreshed twice
needlessly.Copyright © 2008–2017 The Apache Software Foundation. All rights reserved.