Added unit tests for TransferEntropyCalculatorMultiVariateViaCondMutualInfo and derived classes for Kraskov and Gaussian estimators.

This commit is contained in:
joseph.lizier 2014-04-08 11:21:40 +00:00
parent 7a40b433b7
commit 9cad2737af
5 changed files with 199 additions and 13 deletions

View File

@ -0,0 +1,84 @@
package infodynamics.measures.continuous;
import junit.framework.TestCase;
import infodynamics.utils.MatrixUtils;
import infodynamics.utils.RandomGenerator;
public abstract class TransferEntropyAbstractTester extends TestCase {
/**
* Confirm that the local values average correctly back to the average value
*
* @param teCalc a pre-constructed TransferEntropyCalculator object
* @param timeSteps number of time steps for the random data
* @param k dest history length for the TE calculator to use
*/
public void testLocalsAverageCorrectly(TransferEntropyCalculator teCalc,
int timeSteps, int k)
throws Exception {
teCalc.initialise(k);
// generate some random data
RandomGenerator rg = new RandomGenerator();
double[] sourceData = rg.generateNormalData(timeSteps,
0, 1);
double[] destData = rg.generateNormalData(timeSteps,
0, 1);
teCalc.setObservations(sourceData, destData);
//teCalc.setDebug(true);
double te = teCalc.computeAverageLocalOfObservations();
//teCalc.setDebug(false);
double[] teLocal = teCalc.computeLocalOfPreviousObservations();
System.out.printf("Average was %.5f\n", te);
assertEquals(te, MatrixUtils.mean(teLocal, k, timeSteps-k), 0.00001);
}
/**
* Confirm that significance testing doesn't alter the average that
* would be returned.
*
* @param teCalc a pre-constructed TransferEntropyCalculator object
* @param timeSteps number of time steps for the random data
* @param k history length for the TE calculator to use
* @throws Exception
*/
public void testComputeSignificanceDoesntAlterAverage(TransferEntropyCalculator teCalc,
int timeSteps, int k) throws Exception {
teCalc.initialise(k);
// generate some random data
RandomGenerator rg = new RandomGenerator();
double[] sourceData = rg.generateNormalData(timeSteps,
0, 1);
double[] destData = rg.generateNormalData(timeSteps,
0, 1);
teCalc.setObservations(sourceData, destData);
//teCalc.setDebug(true);
double te = teCalc.computeAverageLocalOfObservations();
//teCalc.setDebug(false);
//double[] teLocal = teCalc.computeLocalOfPreviousObservations();
System.out.printf("Average was %.5f\n", te);
// Now look at statistical significance tests
int[][] newOrderings = rg.generateDistinctRandomPerturbations(
timeSteps - k, 100);
teCalc.computeSignificance(newOrderings);
// And compute the average value again to check that it's consistent:
for (int i = 0; i < 10; i++) {
double averageCheck1 = teCalc.computeAverageLocalOfObservations();
assertEquals(te, averageCheck1);
}
}
}

View File

@ -83,4 +83,43 @@ public abstract class TransferEntropyMultiVariateAbstractTester extends TestCase
}
}
/**
* Confirm that a calculation for univariate data using univariate method signatures
* matches that with multivariate signatures.
*
* @param teCalc a pre-constructed TransferEntropyCalculatorMultiVariate object
* @param timeSteps number of time steps for the random data
* @param k history length for the TE calculator to use
*/
public void testUnivariateMatchesMultivariateRoute(TransferEntropyCalculatorMultiVariate teCalc,
int timeSteps, int k)
throws Exception {
if (!(teCalc instanceof TransferEntropyCalculator)) {
throw new Exception("The given calculator does not implement univariate TE");
}
// generate some random data
RandomGenerator rg = new RandomGenerator();
double[][] sourceData = rg.generateNormalData(timeSteps, 1,
0, 1);
double[][] destData = rg.generateNormalData(timeSteps, 1,
0, 1);
// Compute via univariate signatures:
TransferEntropyCalculator teCalcUni = (TransferEntropyCalculator) teCalc;
teCalc.initialise(k, 1, 1);
teCalcUni.setObservations(MatrixUtils.selectColumn(sourceData, 0),
MatrixUtils.selectColumn(destData, 0));
double teUnivariate = teCalc.computeAverageLocalOfObservations();
// compute via multivariate signatures:
teCalc.initialise(k, 1, 1);
teCalc.setObservations(sourceData, destData);
//teCalc.setDebug(true);
double teMultivariate = teCalc.computeAverageLocalOfObservations();
//teCalc.setDebug(false);
assertEquals(teUnivariate, teMultivariate, 0.00001);
}
}

View File

@ -0,0 +1,43 @@
package infodynamics.measures.continuous.gaussian;
public class TransferEntropyMultiVariateGaussianTester
extends infodynamics.measures.continuous.TransferEntropyMultiVariateAbstractTester {
/**
* Confirm that the local values average correctly back to the average value
*
*/
public void testLocalsAverageCorrectly() throws Exception {
TransferEntropyCalculatorMultiVariateGaussian teCalc =
new TransferEntropyCalculatorMultiVariateGaussian();
super.testLocalsAverageCorrectly(teCalc, 2, 100, 1);
}
/**
* Confirm that significance testing doesn't alter the average that
* would be returned.
*
* @throws Exception
*/
public void testComputeSignificanceDoesntAlterAverage() throws Exception {
TransferEntropyCalculatorMultiVariateGaussian teCalc =
new TransferEntropyCalculatorMultiVariateGaussian();
super.testComputeSignificanceDoesntAlterAverage(teCalc, 2, 100, 1);
}
/**
* Confirm that the local values average correctly back to the average value
*
*/
public void testUnivariateSignatureMatchesMultivariate() throws Exception {
TransferEntropyCalculatorMultiVariateGaussian teCalc =
new TransferEntropyCalculatorMultiVariateGaussian();
super.testUnivariateMatchesMultivariateRoute(teCalc, 100, 1);
}
}

View File

@ -47,4 +47,24 @@ public class TransferEntropyMultiVariateTester
super.testComputeSignificanceDoesntAlterAverage(teCalc, 2, 100, 1);
}
/**
* Confirm that the local values average correctly back to the average value
*
*/
public void testUnivariateSignatureMatchesMultivariate() throws Exception {
TransferEntropyCalculatorMultiVariateKraskov teCalc =
new TransferEntropyCalculatorMultiVariateKraskov();
String kraskov_K = "4";
teCalc.setProperty(
TransferEntropyCalculatorMultiVariateKraskov.PROP_KRASKOV_ALG_NUM,
"1");
teCalc.setProperty(
MutualInfoCalculatorMultiVariateKraskov.PROP_K,
kraskov_K);
super.testUnivariateMatchesMultivariateRoute(teCalc, 100, 1);
}
}

View File

@ -89,12 +89,12 @@ public class TransferEntropyTester
// normalisation without taking the extra values in to account if we did it
teCalc.setProperty(ConditionalMutualInfoCalculatorMultiVariateKraskov.PROP_NORMALISE, "false");
teCalc.initialise(1, 1);
teCalc.initialise(1);
teCalc.setObservations(col0, col1);
double result = teCalc.computeAverageLocalOfObservations();
assertEquals(expectedFromTRENTOOL0to1, result, 0.000001);
teCalc.initialise(1, 1);
teCalc.initialise(1);
teCalc.setObservations(col1, col0);
result = teCalc.computeAverageLocalOfObservations();
assertEquals(expectedFromTRENTOOL1to0, result, 0.000001);
@ -150,35 +150,35 @@ public class TransferEntropyTester
teCalc.setProperty(ConditionalMutualInfoCalculatorMultiVariateKraskov.PROP_NORMALISE, "false");
System.out.printf("Kraskov TE comparison 2 to TRENTOOL - univariate random data 1 (col 0->1)");
teCalc.initialise(1, 1);
teCalc.initialise(1);
teCalc.setObservations(col0, col1);
double result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
assertEquals(expectedFromTRENTOOL0to1, result, 0.000001);
System.out.printf(" (col 1->2):");
teCalc.initialise(1, 1);
teCalc.initialise(1);
teCalc.setObservations(col1, col2);
result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
assertEquals(expectedFromTRENTOOL1to2, result, 0.000001);
System.out.printf(" (col 1->0):");
teCalc.initialise(1, 1);
teCalc.initialise(1);
teCalc.setObservations(col1, col0);
result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
assertEquals(expectedFromTRENTOOL1to0, result, 0.000001);
System.out.printf(" (col 0->2):");
teCalc.initialise(1, 1);
teCalc.initialise(1);
teCalc.setObservations(col0, col2);
result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
assertEquals(expectedFromTRENTOOL0to2, result, 0.000001);
System.out.printf(" (col 2->0):");
teCalc.initialise(1, 1);
teCalc.initialise(1);
teCalc.setObservations(col2, col0);
result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
@ -239,28 +239,28 @@ public class TransferEntropyTester
System.out.println("Kraskov Cond MI as TE - multivariate coupled data 1, k=2,l=2");
System.out.println(" (0->2)");
teCalc.initialise(2, 2);
teCalc.initialise(2, 1, 2, 1, 1);
teCalc.setObservations(col0, col2);
double result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
assertEquals(expectedFromTRENTOOL0to2, result, 0.000001);
System.out.println(" (2->0):");
teCalc.initialise(2, 2);
teCalc.initialise(2, 1, 2, 1, 1);
teCalc.setObservations(col2, col0);
result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
assertEquals(expectedFromTRENTOOL2to0, result, 0.000001);
System.out.println(" (1->3):");
teCalc.initialise(2, 2);
teCalc.initialise(2, 1, 2, 1, 1);
teCalc.setObservations(col1, col3);
result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
assertEquals(expectedFromTRENTOOL1to3, result, 0.000001);
System.out.println(" (3->1):");
teCalc.initialise(2, 2);
teCalc.initialise(2, 1, 2, 1, 1);
teCalc.setObservations(col3, col1);
result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
@ -273,7 +273,7 @@ public class TransferEntropyTester
double expectedFromTRENTOOL1to2_k1l1 = 0.0011738;
System.out.println(" (0->1) but with k=1,l=1:");
teCalc.initialise(1, 1);
teCalc.initialise(1, 1, 1, 1, 1);
teCalc.setObservations(col0, col1);
result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);
@ -281,7 +281,7 @@ public class TransferEntropyTester
// And in reverse
System.out.println(" (1->2) but with k=1,l=1:");
teCalc.initialise(1, 1);
teCalc.initialise(1, 1, 1, 1, 1);
teCalc.setObservations(col1, col2);
result = teCalc.computeAverageLocalOfObservations();
System.out.printf(" %.5f\n", result);