be.hogent.tarsos.util
Class KernelDensityEstimate

java.lang.Object
  extended by be.hogent.tarsos.util.KernelDensityEstimate

public class KernelDensityEstimate
extends java.lang.Object


Nested Class Summary
static class KernelDensityEstimate.Cosine
           
static class KernelDensityEstimate.GaussianKernel
          A Gaussian kernel function.
static interface KernelDensityEstimate.KDECorrelation
           
static interface KernelDensityEstimate.Kernel
          Defines a kernel.
static class KernelDensityEstimate.Overlap
           
static class KernelDensityEstimate.RectangularKernel
          A rectangular kernel function.
 
Field Summary
protected  double[] accumulator
           
protected  KernelDensityEstimate.Kernel kernel
           
 
Constructor Summary
KernelDensityEstimate(KernelDensityEstimate.Kernel kernel, double[] accumulator)
           
KernelDensityEstimate(KernelDensityEstimate.Kernel kernel, int size)
           
 
Method Summary
 void add(double value)
          Add the kernel to an accumulator for each value.
 void add(KernelDensityEstimate other)
          Adds a KDE to this accumulator
 void clear()
          Clears the data in the accumulator.
 double correlation(KernelDensityEstimate other, int positionsToShiftOther)
           Calculate a correlation with another KernelDensityEstimate.
 double[] getEstimate()
          Returns the current estimate.
 double getMaxElement()
           
 double getSumFreq()
          Returns the sum of all estimates in the accumulator.
 double getValue(int index)
          Return the value for the accumulator at a certain index.
 KernelDensityEstimate map(int size)
          Map the kernel density estimate to another size.
 void max(KernelDensityEstimate other)
          Takes the maximum of the value in the accumulator for two kde's.
 void normalize()
          Sets the maximum value in accumulator to 1.0
 double optimalCorrelation(KernelDensityEstimate.KDECorrelation correlationMeasure, KernelDensityEstimate other)
          Calculates the optimal correlation between two Kernel Density Estimates by shifting and searching for optimal correlation.
 double optimalCorrelation(KernelDensityEstimate other)
          Calculates the optimal correlation between two Kernel Density Estimates by shifting and searching for optimal correlation.
 void pdfify()
          Sets the area under the curve to 1.0.
 void remove(double value)
          Remove a value from the kde, removes a kernel at the specified position.
 void shift(int shift)
          Shift the accumulator x positions.
 int shiftForOptimalCorrelation(KernelDensityEstimate.KDECorrelation correlationMeasure, KernelDensityEstimate other)
          Calculates how much the other KernelDensityEstimate needs to be shifted for optimal correlation.
 int shiftForOptimalCorrelation(KernelDensityEstimate other)
          Calculates how much the other KernelDensityEstimate needs to be shifted for optimal correlation.
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

accumulator

protected final double[] accumulator

kernel

protected final KernelDensityEstimate.Kernel kernel
Constructor Detail

KernelDensityEstimate

public KernelDensityEstimate(KernelDensityEstimate.Kernel kernel,
                             int size)

KernelDensityEstimate

public KernelDensityEstimate(KernelDensityEstimate.Kernel kernel,
                             double[] accumulator)
Method Detail

add

public void add(double value)
Add the kernel to an accumulator for each value. When a kernel with a width of 7 is added at 1 cents it has influence on the bins from 1200 - 7 * 10 + 1 to 1 + 7 * 10 so from 1131 to 71. To make the modulo calculation easy 1200 is added to each value: -69 % 1200 is -69, (-69 + 1200) % 1200 is the expected 1131. If you know what I mean. This algorithm computes O(width * n) sums with n the number of annotations and width the number of bins affected, rather efficient.

Parameters:
value - The value to add.

remove

public void remove(double value)
Remove a value from the kde, removes a kernel at the specified position.

Parameters:
value - The value to remove.

shift

public void shift(int shift)
Shift the accumulator x positions.

Parameters:
shift - The number of positions the accumulator should be shifted.

getEstimate

public double[] getEstimate()
Returns the current estimate.

Returns:
The current estimate. To prevent unauthorized modification a clone of the array is returned. Please cache appropriately.

map

public KernelDensityEstimate map(int size)
Map the kernel density estimate to another size. E.g. a KDE with 4 values mapped to two is done by iterating the 4 elements and adding them on modulo 2 places. Here 1 + 4 = 5, 2 + 9 = 11
 (1 2 4 9).map(2) = (5 11) 
 

Parameters:
size - The new size for the KDE.
Returns:
A new KDE with the contents of the original mapped to the new size.

getValue

public double getValue(int index)
Return the value for the accumulator at a certain index.

Parameters:
index - The index.
Returns:
The value for the accumulator at a certain index.

size

public int size()
Returns:
The size of the accumulator.

getSumFreq

public double getSumFreq()
Returns the sum of all estimates in the accumulator.

Returns:
The total sum of all estimates.

normalize

public void normalize()
Sets the maximum value in accumulator to 1.0


getMaxElement

public double getMaxElement()
Returns:
the maximum element in the accumulator;

pdfify

public void pdfify()
Sets the area under the curve to 1.0. In essence every value is divided by getSumFreq(). As per definition of a probability density function.


clear

public void clear()
Clears the data in the accumulator.


max

public void max(KernelDensityEstimate other)
Takes the maximum of the value in the accumulator for two kde's.

Parameters:
other - The other kde of the same size.

add

public void add(KernelDensityEstimate other)
Adds a KDE to this accumulator

Parameters:
other - The other KDE of the same size.

correlation

public double correlation(KernelDensityEstimate other,
                          int positionsToShiftOther)

Calculate a correlation with another KernelDensityEstimate. The index of the other estimates are shifted by a number which can be zero (or positive or negative). Beware: the index wraps around the edges.

This and the other KernelDensityEstimate should have the same size.

Parameters:
other - The other estimate.
positionsToShiftOther - The number of positions to shift the estimate.
Returns:
A value between 0 and 1 representing how similar both estimates are. 1 means total correlation, 0 no correlation.

shiftForOptimalCorrelation

public int shiftForOptimalCorrelation(KernelDensityEstimate other)
Calculates how much the other KernelDensityEstimate needs to be shifted for optimal correlation.

Parameters:
other - The other KernelDensityEstimate.
Returns:
A number between 0 (inclusive) and the size of the KernelDensityEstimate (exclusive) which represents how much the other KernelDensityEstimate needs to be shifted for optimal correlation.

optimalCorrelation

public double optimalCorrelation(KernelDensityEstimate other)
Calculates the optimal correlation between two Kernel Density Estimates by shifting and searching for optimal correlation.

Parameters:
other - The other KernelDensityEstimate.
Returns:
A value between 0 and 1 representing how similar both estimates are. 1 means total correlation, 0 no correlation.

optimalCorrelation

public double optimalCorrelation(KernelDensityEstimate.KDECorrelation correlationMeasure,
                                 KernelDensityEstimate other)
Calculates the optimal correlation between two Kernel Density Estimates by shifting and searching for optimal correlation.

Parameters:
correlationMeasure -
other - The other KernelDensityEstimate.
Returns:
A value between 0 and 1 representing how similar both estimates are. 1 means total correlation, 0 no correlation.

shiftForOptimalCorrelation

public int shiftForOptimalCorrelation(KernelDensityEstimate.KDECorrelation correlationMeasure,
                                      KernelDensityEstimate other)
Calculates how much the other KernelDensityEstimate needs to be shifted for optimal correlation.

Parameters:
correlationMeasure -
other - The other KernelDensityEstimate.
Returns:
A number between 0 (inclusive) and the size of the KernelDensityEstimate (exclusive) which represents how much the other KernelDensityEstimate needs to be shifted for optimal correlation.