## org.apache.mahout.cf.taste.impl.similarity
Class PearsonCorrelationSimilarity

    java.lang.Object
      org.apache.mahout.cf.taste.impl.similarity.AbstractItemSimilarity
          org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity

All Implemented Interfaces:
  Refreshable, ItemSimilarity, UserSimilarity

    public final class PearsonCorrelationSimilarity

extends AbstractItemSimilarity

An implementation of the Pearson correlation. For users X and Y, the following values are calculated:

- sumX2: sum of the square of all X's preference values
- sumY2: sum of the square of all Y's preference values
- sumXY: sum of the product of X and Y's preference value for all items for which both X and Y express a preference

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 Summary
`PearsonCorrelationSimilarity(DataModel dataModel)`
`PearsonCorrelationSimilarity(DataModel dataModel, Weighting weighting)`

Method Summary
`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.

Methods inherited from class org.apache.mahout.cf.taste.impl.similarity.AbstractItemSimilarity
`allSimilarItemIDs, getDataModel`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Constructor Detail

### PearsonCorrelationSimilarity

    public PearsonCorrelationSimilarity(DataModel dataModel)
                                 throws TasteException

Throws:
  `IllegalArgumentException` - if `DataModel` does not have preference values
  `TasteException`

### PearsonCorrelationSimilarity

    public PearsonCorrelationSimilarity(DataModel dataModel,
                                        Weighting weighting)
                                 throws TasteException

Throws:
  `IllegalArgumentException` - if `DataModel` does not have preference values
  `TasteException`

Method Detail

### setPreferenceInferrer

    public final void setPreferenceInferrer(PreferenceInferrer inferrer)

  Description copied from interface: `UserSimilarity`

  Attaches a `PreferenceInferrer` to the `UserSimilarity` implementation.

  Specified by:
    `setPreferenceInferrer` in interface `UserSimilarity`
  Parameters:
    `inferrer` - `PreferenceInferrer`

### userSimilarity

    public double userSimilarity(long userID1,
                                 long userID2)
                          throws TasteException

  Description copied from interface: `UserSimilarity`

  Returns the degree of similarity, of two users, based on the their preferences.

  Specified by:
    `userSimilarity` in interface `UserSimilarity`
  Parameters:
    `userID1` - first user ID
    `userID2` - second user ID
  Returns:
    similarity between the users, in [-1,1] or `Double.NaN` similarity is unknown
  Throws:
    `NoSuchUserException` - if either user is known to be non-existent in the data
    `TasteException` - if an error occurs while accessing the data

### itemSimilarity

    public final double itemSimilarity(long itemID1,
                                       long itemID2)
                                throws TasteException

  Description copied from interface: `ItemSimilarity`

  Returns the degree of similarity, of two items, based on the preferences that users have expressed for the items.

  Specified by:
    `itemSimilarity` in interface `ItemSimilarity`
  Parameters:
    `itemID1` - first item ID
    `itemID2` - second item ID
  Returns:
    similarity between the items, in [-1,1] or `Double.NaN` similarity is unknown
  Throws:
    `NoSuchItemException` - if either item is known to be non-existent in the data
    `TasteException` - if an error occurs while accessing the data

### itemSimilarities

    public double[] itemSimilarities(long itemID1,
                                     long[] itemID2s)
                              throws TasteException

  Description copied from interface: `ItemSimilarity`

  A bulk-get version of `ItemSimilarity.itemSimilarity(long, long)`.

  Specified by:
    `itemSimilarities` in interface `ItemSimilarity`
  Parameters:
    `itemID1` - first item ID
    `itemID2s` - second item IDs to compute similarity with
  Returns:
    similarity between itemID1 and other items
  Throws:
    `NoSuchItemException` - if any item is known to be non-existent in the data
    `TasteException` - if an error occurs while accessing the data

### refresh

    public final void refresh(Collection<Refreshable> alreadyRefreshed)

  Description copied from interface: `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.

  Specified by:
    `refresh` in interface `Refreshable`
  Overrides:
    `refresh` in class `AbstractItemSimilarity`
  Parameters:
    `alreadyRefreshed` - `Refreshable`s that are known to have already been refreshed as a result of an initial call to a `Refreshable.refresh(Collection)` method on some object. This ensure that objects in a refresh dependency graph aren't refreshed twice needlessly.

### toString

    public final String toString()

  Overrides:
    `toString` in class `Object`

Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.