Refactoring discrete TE calculator to take source as first argument before destination (to match continuous calculators)

This commit is contained in:
joseph.lizier 2014-08-05 12:44:05 +00:00
parent 39053afe49
commit 8b0de7e9fa
4 changed files with 66 additions and 69 deletions

View File

@ -234,7 +234,7 @@ public class TransferEntropyCalculatorSymbolic
// Add these observations in, and keep them locally
destSymbolsVector.add(destSymbols);
sourceSymbolsVector.add(sourceSymbols);
teCalc.addObservations(destSymbols, sourceSymbols);
teCalc.addObservations(sourceSymbols, destSymbols);
if (destination.length - maxEmbeddingLength != destSymbols.length - 1) {
throw new RuntimeException(
@ -293,7 +293,7 @@ public class TransferEntropyCalculatorSymbolic
Iterator<int[]> iterator = destSymbolsVector.iterator();
for (int[] sourceSymbols : sourceSymbolsVector) {
int[] destSymbols = iterator.next();
double[] theseLocals = teCalc.computeLocalFromPreviousObservations(destSymbols, sourceSymbols);
double[] theseLocals = teCalc.computeLocalFromPreviousObservations(sourceSymbols, destSymbols);
System.arraycopy(theseLocals, 1, locals, currentIndexInLocals, theseLocals.length - 1);
currentIndexInLocals += theseLocals.length - 1;
}

View File

@ -21,18 +21,17 @@ public interface ChannelCalculator {
* Add observations for the source and destination
*
* @param states
* @param destIndex
* @param sourceIndex
* @param destIndex
*/
public void addObservations(int states[][], int destIndex, int sourceIndex);
public void addObservations(int states[][], int sourceIndex, int destIndex);
/**
* Add observations for the source and destination
*
* @param dest
* @param source
* @param dest
*/
public void addObservations(int[] dest, int[] source);
public void addObservations(int[] source, int[] dest);
/**
* Compute the value of the measure

View File

@ -50,7 +50,7 @@ public class SeparableInfoCalculatorByAddition extends SeparableInfoCalculator {
aiCalc.addObservations(states);
int[] cleanedSourcesAbsolute = cleanAbsoluteSources(sourcesAbsolute, destCol);
for (int i = 0; i < numSources; i++) {
ateCalcs[i].addObservations(states, destCol, cleanedSourcesAbsolute[i]);
ateCalcs[i].addObservations(states, cleanedSourcesAbsolute[i], destCol);
}
}
@ -68,8 +68,8 @@ public class SeparableInfoCalculatorByAddition extends SeparableInfoCalculator {
aiCalc.addObservations(states, destAgentRow, destAgentColumn);
int[][] cleanedSourcesAbsolute = cleanAbsoluteSources(sourcesAbsolute, destAgentRow, destAgentColumn);
for (int i = 0; i < numSources; i++) {
ateCalcs[i].addObservations(states, destAgentRow, destAgentColumn,
cleanedSourcesAbsolute[i][ROW_INDEX], cleanedSourcesAbsolute[i][COLUMN_INDEX]);
ateCalcs[i].addObservations(states, cleanedSourcesAbsolute[i][ROW_INDEX], cleanedSourcesAbsolute[i][COLUMN_INDEX],
destAgentRow, destAgentColumn);
}
}
@ -105,7 +105,7 @@ public class SeparableInfoCalculatorByAddition extends SeparableInfoCalculator {
int[] cleanedSourcesAbsolute = cleanAbsoluteSources(sourcesAbsolute, destCol);
double[] temp;
for (int i = 0; i < numSources; i++) {
temp = ateCalcs[i].computeLocalFromPreviousObservations(states, destCol, cleanedSourcesAbsolute[i]);
temp = ateCalcs[i].computeLocalFromPreviousObservations(states, cleanedSourcesAbsolute[i], destCol);
if (computeMultiInfoCoherence) {
// Keep a link to this apparent transfer
localComponents[1 + numSources] = temp;
@ -172,9 +172,9 @@ public class SeparableInfoCalculatorByAddition extends SeparableInfoCalculator {
double[] temp;
for (int i = 0; i < numSources; i++) {
temp = ateCalcs[i].computeLocalFromPreviousObservations(
states, destAgentRow, destAgentColumn,
cleanedSourcesAbsolute[i][ROW_INDEX],
cleanedSourcesAbsolute[i][COLUMN_INDEX]);
states, cleanedSourcesAbsolute[i][ROW_INDEX], cleanedSourcesAbsolute[i][COLUMN_INDEX],
destAgentRow,
destAgentColumn);
if (computeMultiInfoCoherence) {
// Keep a link to this apparent transfer
localComponents[1 + numSources] = temp;
@ -352,7 +352,7 @@ public class SeparableInfoCalculatorByAddition extends SeparableInfoCalculator {
double[] temp;
for (int i = 0; i < numSources; i++) {
temp = ateCalc.computeLocal(
states, destCol, cleanedOffsets[i]);
states, cleanedOffsets[i], destCol);
if (computeMultiInfoCoherence) {
// Keep a link to this apparent transfer
localComponents[1 + numSources] = temp;
@ -387,9 +387,9 @@ public class SeparableInfoCalculatorByAddition extends SeparableInfoCalculator {
ateCalc.setPeriodicBoundaryConditions(periodicBoundaryConditions);
double[] temp;
for (int i = 0; i < numSources; i++) {
temp = ateCalc.computeLocal(states, destAgentRow, destAgentColumn,
cleanedSourcesAbsolute[i][ROW_INDEX],
cleanedSourcesAbsolute[i][COLUMN_INDEX]);
temp = ateCalc.computeLocal(states, cleanedSourcesAbsolute[i][ROW_INDEX], cleanedSourcesAbsolute[i][COLUMN_INDEX],
destAgentRow,
destAgentColumn);
if (computeMultiInfoCoherence) {
// Keep a link to this apparent transfer
localComponents[1 + numSources] = temp;
@ -419,7 +419,7 @@ public class SeparableInfoCalculatorByAddition extends SeparableInfoCalculator {
TransferEntropyCalculator.newInstance(base, k);
ateCalc.setPeriodicBoundaryConditions(periodicBoundaryConditions);
for (int i = 0; i < numSources; i++) {
average += ateCalc.computeAverageLocal(states, destCol, cleanedSourcesAbsolute[i]);
average += ateCalc.computeAverageLocal(states, cleanedSourcesAbsolute[i], destCol);
}
localStatsValid = false;
return average;

View File

@ -206,11 +206,10 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
/**
* Add observations for a single source-destination pair
* to our estimates of the pdfs.
*
* @param dest destination time series
* @param source source timte series
* @param dest destination time series
*/
public void addObservations(int[] dest, int[] source) {
public void addObservations(int[] source, int[] dest) {
int rows = dest.length;
// increment the count of observations:
observations += (rows - startObservationTime);
@ -253,14 +252,13 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
/**
* Add observations for a single source-destination pair
* to our estimates of the pdfs.
*
* @param dest destination time series
* @param source source timte series
* @param dest destination time series
* @param valid time series of whether the signals
* at the given time should be considered valid
* and added to our PDFs
*/
public void addObservations(int[] dest, int[] source, boolean[] valid) {
public void addObservations(int[] source, int[] dest, boolean[] valid) {
int rows = dest.length;
// Initialise and store the current previous value
@ -336,14 +334,13 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* Start and end time are the (inclusive) indices within which to add the observations.
* The start time is from the earliest of the k historical values of the destination (inclusive),
* the end time is the last destination time point to add in.
*
* @param dest
* @param source
* @param dest
* @param startTime
* @param endTime
*
*/
public void addObservations(int[] dest, int[] source, int startTime, int endTime) {
public void addObservations(int[] source, int[] dest, int startTime, int endTime) {
// increment the count of observations:
observations += (endTime - startTime) - startObservationTime + 1;
@ -389,7 +386,7 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
*
* @param states 1st index is time, 2nd index is agent number
* @param j - number of columns to compute transfer entropy across
* (i.e. src i-j, dest i: transfer is j cells to the right)
* (i.e. src is column i-j, dest is column i: transfer is j cells to the right)
*/
public void addObservations(int states[][], int j) {
int timeSteps = states.length;
@ -587,10 +584,10 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* for computing active info for heterogeneous agents.
*
* @param states 1st index is time, 2nd index is agent number
* @param destIndex destination agent index
* @param sourceIndex source agent index
* @param destIndex destination agent index
*/
public void addObservations(int states[][], int destIndex, int sourceIndex) {
public void addObservations(int states[][], int sourceIndex, int destIndex) {
int rows = states.length;
// increment the count of observations:
observations += (rows - startObservationTime);
@ -637,13 +634,13 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* for computing active info for heterogeneous agents.
*
* @param states 1st index is time, 2nd and 3rd index give the 2D agent number
* @param destRowIndex destination agent row index
* @param destColumnIndex destination agent column index
* @param sourceRowIndex source agent row index
* @param sourceColumnIndex source agent column index
* @param destRowIndex destination agent row index
* @param destColumnIndex destination agent column index
*/
public void addObservations(int states[][][], int destRowIndex, int destColumnIndex,
int sourceRowIndex, int sourceColumnIndex) {
public void addObservations(int states[][][], int sourceRowIndex, int sourceColumnIndex,
int destRowIndex, int destColumnIndex) {
int timeSteps = states.length;
// increment the count of observations:
observations += (timeSteps - startObservationTime);
@ -1015,14 +1012,14 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
/**
* Computes local transfer entropy for the given values
*
* @see {@link #getPastCount(int)} for how the joint values representing the past are calculated.
* @param sourceCurrent
* @param destNext
* @param destPast
* @param sourceCurrent
*
* @see {@link #getPastCount(int)} for how the joint values representing the past are calculated.
* @return
*/
public double computeLocalFromPreviousObservations(int destNext, int destPast, int sourceCurrent){
public double computeLocalFromPreviousObservations(int sourceCurrent, int destNext, int destPast){
double logTerm = ((double) sourceNextPastCount[sourceCurrent][destNext][destPast] / (double) sourcePastCount[sourceCurrent][destPast]) /
((double) nextPastCount[destNext][destPast] / (double) pastCount[destPast]);
@ -1034,11 +1031,11 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* states, using pdfs built up from observations previously
* sent in via the addObservations method.
* This method to be used for homogeneous agents only
*
* @param states 1st index is time, 2nd index is agent number
*
* @return
*/
public double[] computeLocalFromPreviousObservations(int destStates[], int sourceStates[]){
public double[] computeLocalFromPreviousObservations(int sourceStates[], int destStates[]){
int timeSteps = destStates.length;
// Allocate for all rows even though we'll leave the first ones as zeros
@ -1095,6 +1092,7 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* This method to be used for homogeneous agents only
*
* @param states 1st index is time, 2nd index is agent number
* @param j columns across which to compute TE
* @return
*/
public double[][] computeLocalFromPreviousObservations(int states[][], int j){
@ -1313,7 +1311,7 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* @param states 1st index is time, 2nd index is agent number
* @return
*/
public double[] computeLocalFromPreviousObservations(int states[][], int destCol, int sourceCol){
public double[] computeLocalFromPreviousObservations(int states[][], int sourceCol, int destCol){
int rows = states.length;
// int columns = states[0].length;
@ -1372,14 +1370,14 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* This method is suitable for heterogeneous agents
*
* @param states 1st index is time, 2nd and 3rd index give the 2D agent number
* @param destRowIndex destination agent row index
* @param destColumnIndex destination agent column index
* @param sourceRowIndex source agent row index
* @param sourceColumnIndex source agent column index
* @param destRowIndex destination agent row index
* @param destColumnIndex destination agent column index
* @return
*/
public double[] computeLocalFromPreviousObservations(int states[][][],
int destRowIndex, int destColumnIndex, int sourceRowIndex, int sourceColumnIndex){
int sourceRowIndex, int sourceColumnIndex, int destRowIndex, int destColumnIndex){
int timeSteps = states.length;
// int columns = states[0].length;
@ -1436,16 +1434,16 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* compute local transfer entropy between two time series
* Return a time series of local values.
* First history rows are zeros
*
* @param destStates time series of destination states
* @param sourceStates time series of source states
* @param destStates time series of destination states
*
* @return
*/
public double[] computeLocal(int destStates[], int sourceStates[]) {
public double[] computeLocal(int sourceStates[], int destStates[]) {
initialise();
addObservations(destStates, sourceStates);
return computeLocalFromPreviousObservations(destStates, sourceStates);
addObservations(sourceStates, destStates);
return computeLocalFromPreviousObservations(sourceStates, destStates);
}
/**
@ -1535,15 +1533,15 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* This method suitable for heterogeneous agents
*
* @param states - 2D array of states
* @param destCol - column index for the destination agent
* @param sourceCol - column index for the source agent
* @param destCol - column index for the destination agent
* @return
*/
public double[] computeLocal(int states[][], int destCol, int sourceCol) {
public double[] computeLocal(int states[][], int sourceCol, int destCol) {
initialise();
addObservations(states, destCol, sourceCol);
return computeLocalFromPreviousObservations(states, destCol, sourceCol);
addObservations(states, sourceCol, destCol);
return computeLocalFromPreviousObservations(states, sourceCol, destCol);
}
/**
@ -1555,19 +1553,19 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* This method suitable for heterogeneous agents
*
* @param states 1st index is time, 2nd and 3rd index give the 2D agent number
* @param destRowIndex destination agent row index
* @param destColumnIndex destination agent column index
* @param sourceRowIndex source agent row index
* @param sourceColumnIndex source agent column index
* @param destRowIndex destination agent row index
* @param destColumnIndex destination agent column index
* @return
*/
public double[] computeLocal(int states[][][], int destRowIndex, int destColumnIndex,
int sourceRowIndex, int sourceColumnIndex) {
public double[] computeLocal(int states[][][], int sourceRowIndex, int sourceColumnIndex,
int destRowIndex, int destColumnIndex) {
initialise();
addObservations(states, destRowIndex, destColumnIndex, sourceRowIndex, sourceColumnIndex);
return computeLocalFromPreviousObservations(states, destRowIndex, destColumnIndex,
sourceRowIndex, sourceColumnIndex);
addObservations(states, sourceRowIndex, sourceColumnIndex, destRowIndex, destColumnIndex);
return computeLocalFromPreviousObservations(states, sourceRowIndex, sourceColumnIndex,
destRowIndex, destColumnIndex);
}
/**
@ -1578,14 +1576,14 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* This method suitable for heterogeneous agents.
*
* @param states - 2D array of states
* @param destCol - column index for the destination agent
* @param sourceCol - column index for the source agent
* @param destCol - column index for the destination agent
* @return
*/
public double computeAverageLocal(int states[][], int destCol, int sourceCol) {
public double computeAverageLocal(int states[][], int sourceCol, int destCol) {
initialise();
addObservations(states, destCol, sourceCol);
addObservations(states, sourceCol, destCol);
return computeAverageLocalOfObservations();
}
@ -1597,17 +1595,17 @@ public class TransferEntropyCalculator extends ContextOfPastMeasureCalculator
* This method suitable for heterogeneous agents
*
* @param states 1st index is time, 2nd and 3rd index give the 2D agent number
* @param destRowIndex destination agent row index
* @param destColumnIndex destination agent column index
* @param sourceRowIndex source agent row index
* @param sourceColumnIndex source agent column index
* @param destRowIndex destination agent row index
* @param destColumnIndex destination agent column index
* @return
*/
public double computeAverageLocal(int states[][][], int destRowIndex, int destColumnIndex,
int sourceRowIndex, int sourceColumnIndex) {
public double computeAverageLocal(int states[][][], int sourceRowIndex, int sourceColumnIndex,
int destRowIndex, int destColumnIndex) {
initialise();
addObservations(states, destRowIndex, destColumnIndex, sourceRowIndex, sourceColumnIndex);
addObservations(states, sourceRowIndex, sourceColumnIndex, destRowIndex, destColumnIndex);
return computeAverageLocalOfObservations();
}