org.mymedialite.correlation
Class CorrelationMatrix

java.lang.Object
  extended by org.mymedialite.datatype.SymmetricMatrix<java.lang.Float>
      extended by org.mymedialite.correlation.CorrelationMatrix
All Implemented Interfaces:
IMatrix<java.lang.Float>
Direct Known Subclasses:
BinaryDataCorrelationMatrix, RatingCorrelationMatrix

public class CorrelationMatrix
extends SymmetricMatrix<java.lang.Float>

Class for computing and storing correlations and similarities.


Field Summary
protected  int numEntities
          Number of entities, e.g.
 
Fields inherited from class org.mymedialite.datatype.SymmetricMatrix
data, dim
 
Constructor Summary
CorrelationMatrix(int numEntities)
          Creates a CorrelationMatrix object for a given number of entities.
 
Method Summary
 void addEntity(int entity_id)
          Add an entity to the CorrelationMatrix by growing it to the requested size..
static CorrelationMatrix create(int numEntities)
          Creates a correlation matrix.
 int[] getNearestNeighbors(int entity_id, int k)
          Get the k nearest neighbors of a given entity.
 IntList getPositivelyCorrelatedEntities(int entity_id)
          Get all entities that are positively correlated to an entity, sorted by correlation.
 boolean isSymmetric()
          True if the matrix is symmetric, false otherwise.
static CorrelationMatrix readCorrelationMatrix(java.io.BufferedReader reader)
          Creates a CorrelationMatrix from the lines of a StreamReader.
 double sumUp(int entity_id, java.util.Collection<java.lang.Integer> entities)
          Sum up the correlations between a given entity and the entities in a collection.
 void write(java.io.PrintWriter writer)
          Write out the correlations to a StreamWriter.
 
Methods inherited from class org.mymedialite.datatype.SymmetricMatrix
createMatrix, get, grow, init, numberOfColumns, numberOfRows, set, transpose
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numEntities

protected int numEntities
Number of entities, e.g. users or items.

Constructor Detail

CorrelationMatrix

public CorrelationMatrix(int numEntities)
Creates a CorrelationMatrix object for a given number of entities.

Parameters:
numEntities - number of entities
Method Detail

isSymmetric

public boolean isSymmetric()
Description copied from interface: IMatrix
True if the matrix is symmetric, false otherwise.

Specified by:
isSymmetric in interface IMatrix<java.lang.Float>
Overrides:
isSymmetric in class SymmetricMatrix<java.lang.Float>
Returns:
returns true if the matrix is symmetric, which is generally the case for similarity matrices

create

public static CorrelationMatrix create(int numEntities)
Creates a correlation matrix. Gives out a useful warning if there is not enough memory

Parameters:
numEntities - the number of entities
Returns:
the correlation matrix

readCorrelationMatrix

public static CorrelationMatrix readCorrelationMatrix(java.io.BufferedReader reader)
                                               throws java.io.IOException
Creates a CorrelationMatrix from the lines of a StreamReader. In the first line, we expect to be the number of entities. All the other lines have the format
   EntityID1 EntityID2 Correlation
 
where EntityID1 and EntityID2 are non-negative integers and Correlation is a floating point number.

Parameters:
reader - the StreamReader to read from
Throws:
java.io.IOException

write

public void write(java.io.PrintWriter writer)
Write out the correlations to a StreamWriter.

Parameters:
writer - A

addEntity

public void addEntity(int entity_id)
Add an entity to the CorrelationMatrix by growing it to the requested size.. Note that you still have to correctly compute and set the entity's correlation values

Parameters:
entity_id - the numerical ID of the entity

sumUp

public double sumUp(int entity_id,
                    java.util.Collection<java.lang.Integer> entities)
Sum up the correlations between a given entity and the entities in a collection.

Parameters:
entity_id - the numerical ID of the entity
entities - a collection containing the numerical IDs of the entities to compare to
Returns:
the correlation sum

getPositivelyCorrelatedEntities

public IntList getPositivelyCorrelatedEntities(int entity_id)
Get all entities that are positively correlated to an entity, sorted by correlation.

Parameters:
entity_id - the entity ID
Returns:
a sorted list of all entities that are positively correlated to entity_id

getNearestNeighbors

public int[] getNearestNeighbors(int entity_id,
                                 int k)
Get the k nearest neighbors of a given entity.

Parameters:
entity_id - the numerical ID of the entity
k - the neighborhood size
Returns:
an array containing the numerical IDs of the k nearest neighbors