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.GaussianKernel
          A Gaussian kernel function.
static interface KernelDensityEstimate.Kernel
          Defines a kernel.
static class KernelDensityEstimate.RectangularKernel
          A rectangular kernel function.
 
Constructor Summary
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
 double correlation(KernelDensityEstimate other, int positionsToShiftOther)
           Calculate a correlation with another KernelDensityEstimate.
 double[] getEstimate()
          Returns the current estimate.
 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.
 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
 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 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
 

Constructor Detail

KernelDensityEstimate

public KernelDensityEstimate(KernelDensityEstimate.Kernel kernel,
                             int size)
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.

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


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.