Skip to content

Commit

Permalink
Updating all discrete calculators (except SeparableInfo, since it's o…
Browse files Browse the repository at this point in the history
…utmoded) to have a default empty constructor, with an overloaded initialise method added that provides all relevant properties. Fixes #77, and provides a first step for #66. Also aligned PredictiveInfo and ConditionalTE calculators to use the appropriate super classes.
  • Loading branch information
jlizier committed Jul 25, 2019
1 parent d545793 commit 1dc2dc8
Show file tree
Hide file tree
Showing 15 changed files with 651 additions and 538 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ public static ActiveInformationCalculatorDiscrete newInstance(int base, int hist
return new ActiveInformationCalculatorDiscrete(base, history);
}

/**
* Construct a new instance with default base 2 and history 1
*/
public ActiveInformationCalculatorDiscrete() {
super();
}

/**
* Construct a new instance
*
Expand All @@ -104,10 +111,14 @@ public ActiveInformationCalculatorDiscrete(int base, int history) {

@Override
public void initialise() {
super.initialise();
aisComputed = false;
initialise(base, k);
}

public void initialise(int base, int history) {
super.initialise(base, history);
aisComputed = false;
}

@Override
public void addObservations(int states[]) {
int timeSteps = states.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public static EntropyCalculatorDiscrete newInstance(int blocksize, int base) {
return new BlockEntropyCalculatorDiscrete(blocksize, base);
}

/**
* Construct an instance with default blocksize 1 and base 2
*/
public BlockEntropyCalculatorDiscrete() {
this(1, 2);
}

/**
* Construct a new instance
*
Expand All @@ -92,7 +99,10 @@ public static EntropyCalculatorDiscrete newInstance(int blocksize, int base) {
public BlockEntropyCalculatorDiscrete(int blocksize, int base) {

super(base);

resetBlocksize(blocksize);
}

private void resetBlocksize(int blocksize) {
this.blocksize = blocksize;
base_power_blocksize = MathsUtils.power(base, blocksize);

Expand All @@ -102,30 +112,43 @@ public BlockEntropyCalculatorDiscrete(int blocksize, int base) {
if (blocksize > Math.log(Integer.MAX_VALUE) / log_base) {
throw new RuntimeException("Base and blocksize combination too large");
}

// Create storage for counts of observations.
// We're recreating stateCount, which was created in super()
// however the super() didn't create it for blocksize > 1
try {
stateCount = new int[MathsUtils.power(base, blocksize)];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a Runtimexception
throw new RuntimeException("Requested memory for the base " +
base + " and k=" + blocksize+ " is too large for the JVM at this time", e);
}


// Create constants for tracking stateValues
maxShiftedValue = new int[base];
for (int v = 0; v < base; v++) {
maxShiftedValue[v] = v * MathsUtils.power(base, blocksize-1);
}
}

public void initialise(int blocksize, int base) {
boolean baseOrBlocksizeChanged = (this.blocksize != blocksize) || (this.base != base);

super.initialise(base);
if (baseOrBlocksizeChanged) {
resetBlocksize(blocksize);
}

if (baseOrBlocksizeChanged || (stateCount == null)) {
// Create storage for counts of observations.
// We're recreating stateCount, which was created in super()
// however the super() didn't create it for blocksize > 1
try {
stateCount = new int[MathsUtils.power(base, blocksize)];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a Runtimexception
throw new RuntimeException("Requested memory for the base " +
base + " and k=" + blocksize+ " is too large for the JVM at this time", e);
}
} else {
// Storage for sample counts exists and needs to be reset
MatrixUtils.fill(stateCount, 0);
}
}

@Override
public void initialise(){
super.initialise();
MatrixUtils.fill(stateCount, 0);
public void initialise() {
initialise(blocksize, base);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,16 @@ public class CombinedActiveEntRateLocalResults {
}

public CombinedActiveEntRateCalculatorDiscrete() {
super();
// TODO Make this inherit from InfoMeasureCalculatorDiscrete
}

/**
* Initialise with the existing history and base
*/
public void initialise() {
initialise(k, base);
}

/**
* Initialise calculator, preparing to take observation sets in
* Should be called prior to any of the addObservations() methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ public static ConditionalMutualInformationCalculatorDiscrete newInstance(int bas
return new ConditionalMutualInformationCalculatorDiscrete(base1, base2, condBase);
}

/**
* Construct a new instance with default bases
*/
public ConditionalMutualInformationCalculatorDiscrete() {

// Create super object, just with first base defaulting to 2
this(2, 2, 2);
}

/**
* Construct a new instance
*
Expand All @@ -110,39 +119,68 @@ public ConditionalMutualInformationCalculatorDiscrete(int base1, int base2, int

// Create super object, just with first base
super(base1);

changeBases(base1, base2, condBase);
}

/**
* Common code to be called when bases are changed (does not update arrays though)
*
* @param base1
* @param base2
* @param condBase
*/
private boolean changeBases(int base1, int base2, int condBase) {
boolean basesChanged = false;
if ((this.base1 != base1) || (this.base2 != base2) || (this.condBase != condBase)) {
basesChanged = true;
}
// Store the bases
this.base1 = base1;
this.base2 = base2;
this.condBase = condBase;

// Create storage for extra counts of observations
try {
firstSecondCondCount = new int[base1][base2][condBase];
firstCondCount = new int[base1][condBase];
secondCondCount = new int[base2][condBase];
condCount = new int[condBase];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as an Exception
throw new RuntimeException("Requested memory for the MI bases (" +
base1 + ", " + base2 + ", " + condBase +
") is too large for the JVM at this time", e);
}
return basesChanged;
}

@Override
public void initialise(){
super.initialise();
initialise(base1, base2, condBase);
}

/**
* Initialise with new bases
*
* @param base1
* @param base2
* @param condBase
*/
public void initialise(int base1, int base2, int condBase){
boolean basesChanged = changeBases(base1, base2, condBase);
super.initialise(base1);

condMiComputed = false;

MatrixUtils.fill(firstSecondCondCount, 0);
MatrixUtils.fill(firstCondCount, 0);
MatrixUtils.fill(secondCondCount, 0);
MatrixUtils.fill(condCount,0);

if (basesChanged || (firstSecondCondCount == null)) {
// Create storage for extra counts of observations
try {
firstSecondCondCount = new int[base1][base2][condBase];
firstCondCount = new int[base1][condBase];
secondCondCount = new int[base2][condBase];
condCount = new int[condBase];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as an Exception
throw new RuntimeException("Requested memory for the MI bases (" +
base1 + ", " + base2 + ", " + condBase +
") is too large for the JVM at this time", e);
}
} else {
MatrixUtils.fill(firstSecondCondCount, 0);
MatrixUtils.fill(firstCondCount, 0);
MatrixUtils.fill(secondCondCount, 0);
MatrixUtils.fill(condCount,0);
}
}

/**
* Add observations for the given var1,var2,cond tuples
* of the variables
Expand Down
Loading

0 comments on commit 1dc2dc8

Please sign in to comment.