com.planetj.taste.impl.correlation
Class PearsonCorrelation

java.lang.Object
  extended by com.planetj.taste.impl.correlation.PearsonCorrelation
All Implemented Interfaces:
Refreshable, ItemCorrelation, UserCorrelation

public final class PearsonCorrelation
extends java.lang.Object
implements UserCorrelation, ItemCorrelation

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

The correlation is then:

sumXY / sqrt(sumX2 * sumY2)

where size is the number of Items in the DataModel.

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 measure correlation 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.

Author:
Sean Owen

Constructor Summary
PearsonCorrelation(DataModel dataModel)
          Creates a normal (unweighted) PearsonCorrelation.
PearsonCorrelation(DataModel dataModel, boolean weighted)
          Creates a weighted PearsonCorrelation.
 
Method Summary
 CorrelationTransform<?> getCorrelationTransform()
           
 PreferenceTransform2 getPrefTransform()
           
 double itemCorrelation(Item item1, Item item2)
          

Returns the "itemCorrelation", or degree of similarity, of two Items, based on the preferences that Users have expressed for the items.

 void refresh()
          

Triggers "refresh" -- whatever that means -- of the implementation.

 void setCorrelationTransform(CorrelationTransform<java.lang.Object> correlationTransform)
           
 void setPreferenceInferrer(PreferenceInferrer inferrer)
          

Attaches a PreferenceInferrer to the UserCorrelation implementation.

 void setPrefTransform(PreferenceTransform2 prefTransform)
           
 java.lang.String toString()
           
 double userCorrelation(User user1, User user2)
          

Returns the "itemCorrelation", or degree of similarity, of two Users, based on the their preferences.

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

Constructor Detail

PearsonCorrelation

public PearsonCorrelation(@Nullable
                          DataModel dataModel)

Creates a normal (unweighted) PearsonCorrelation.

Parameters:
dataModel -

PearsonCorrelation

public PearsonCorrelation(@Nullable
                          DataModel dataModel,
                          boolean weighted)

Creates a weighted PearsonCorrelation.

Parameters:
dataModel -
weighted -
Since:
1.5.4
Method Detail

setPreferenceInferrer

public void setPreferenceInferrer(@Nullable
                                  PreferenceInferrer inferrer)

Attaches a PreferenceInferrer to the UserCorrelation implementation.

Specified by:
setPreferenceInferrer in interface UserCorrelation
Parameters:
inferrer - PreferenceInferrer

getPrefTransform

@Nullable
public PreferenceTransform2 getPrefTransform()

setPrefTransform

public void setPrefTransform(@Nullable
                             PreferenceTransform2 prefTransform)

getCorrelationTransform

@Nullable
public CorrelationTransform<?> getCorrelationTransform()

setCorrelationTransform

public void setCorrelationTransform(@Nullable
                                    CorrelationTransform<java.lang.Object> correlationTransform)

userCorrelation

public double userCorrelation(@Nullable
                              User user1,
                              @Nullable
                              User user2)
                       throws TasteException

Returns the "itemCorrelation", or degree of similarity, of two Users, based on the their preferences.

Specified by:
userCorrelation in interface UserCorrelation
Parameters:
user1 - first user
user2 - second user
Returns:
itemCorrelation between the two users
Throws:
TasteException - if an error occurs while accessing the data

itemCorrelation

public double itemCorrelation(@Nullable
                              Item item1,
                              @Nullable
                              Item item2)
                       throws TasteException

Returns the "itemCorrelation", or degree of similarity, of two Items, based on the preferences that Users have expressed for the items.

Specified by:
itemCorrelation in interface ItemCorrelation
Parameters:
item1 - first item
item2 - second item
Returns:
itemCorrelation between the Items
Throws:
TasteException - if an error occurs while accessing the data

refresh

public void refresh()

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

toString

@NotNull
public java.lang.String toString()
Overrides:
toString in class java.lang.Object