Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-engineered Discrete Measure Calculation #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
* @author Joseph Lizier (<a href="joseph.lizier at gmail.com">email</a>,
* <a href="http://lizier.me/joseph/">www</a>)
*/
public class ActiveInformationCalculatorDiscrete extends SingleAgentMeasureDiscreteInContextOfPastCalculator
public class ActiveInformationCalculatorDiscrete extends UnivariateMeasureDiscreteInContextOfPastCalculator
implements EmpiricalNullDistributionComputer, AnalyticNullDistributionComputer {

protected boolean aisComputed = false;
Expand Down Expand Up @@ -111,7 +111,7 @@ public ActiveInformationCalculatorDiscrete(int base, int history) {

@Override
public void initialise() {
initialise(base, k);
initialise(alphabetSize, k);
}

public void initialise(int base, int history) {
Expand All @@ -128,7 +128,7 @@ public void addObservations(int states[]) {
// Initialise and store the current previous value for each column
int prevVal = 0;
for (int p = 0; p < k; p++) {
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[p];
}

Expand All @@ -142,7 +142,7 @@ public void addObservations(int states[]) {
nextCount[nextVal]++;
// Update the previous value:
prevVal -= maxShiftedValue[states[t-k]];
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[t];
}
}
Expand All @@ -159,7 +159,7 @@ public void addObservations(int states[][]) {
for (int c = 0; c < columns; c++) {
prevVal[c] = 0;
for (int p = 0; p < k; p++) {
prevVal[c] *= base;
prevVal[c] *= alphabetSize;
prevVal[c] += states[p][c];
}
}
Expand All @@ -176,13 +176,12 @@ public void addObservations(int states[][]) {
nextCount[nextVal]++;
// Update the previous value:
prevVal[c] -= maxShiftedValue[states[r-k][c]];
prevVal[c] *= base;
prevVal[c] *= alphabetSize;
prevVal[c] += states[r][c];
}
}
}

@Override
public void addObservations(int states[][][]) {
int timeSteps = states.length;
if (timeSteps == 0) {
Expand All @@ -202,7 +201,7 @@ public void addObservations(int states[][][]) {
for (int c = 0; c < agentColumns; c++) {
prevVal[r][c] = 0;
for (int p = 0; p < k; p++) {
prevVal[r][c] *= base;
prevVal[r][c] *= alphabetSize;
prevVal[r][c] += states[p][r][c];
}
}
Expand All @@ -221,14 +220,13 @@ public void addObservations(int states[][][]) {
nextCount[nextVal]++;
// Update the previous value:
prevVal[r][c] -= maxShiftedValue[states[t-k][r][c]];
prevVal[r][c] *= base;
prevVal[r][c] *= alphabetSize;
prevVal[r][c] += states[t][r][c];
}
}
}
}

@Override
public void addObservations(int states[][], int col) {
int rows = states.length;
// increment the count of observations:
Expand All @@ -238,7 +236,7 @@ public void addObservations(int states[][], int col) {
int prevVal = 0;
prevVal = 0;
for (int p = 0; p < k; p++) {
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[p][col];
}

Expand All @@ -253,12 +251,11 @@ public void addObservations(int states[][], int col) {
nextCount[nextVal]++;
// Update the previous value:
prevVal -= maxShiftedValue[states[r-k][col]];
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[r][col];
}
}

@Override
public void addObservations(int states[][][], int agentIndex1, int agentIndex2) {
int timeSteps = states.length;
// increment the count of observations:
Expand All @@ -268,7 +265,7 @@ public void addObservations(int states[][][], int agentIndex1, int agentIndex2)
int prevVal = 0;
prevVal = 0;
for (int p = 0; p < k; p++) {
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[p][agentIndex1][agentIndex2];
}

Expand All @@ -283,7 +280,7 @@ public void addObservations(int states[][][], int agentIndex1, int agentIndex2)
nextCount[nextVal]++;
// Update the previous value:
prevVal -= maxShiftedValue[states[t-k][agentIndex1][agentIndex2]];
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[t][agentIndex1][agentIndex2];
}
}
Expand All @@ -295,7 +292,7 @@ public double computeAverageLocalOfObservations() {

max = 0;
min = 0;
for (int nextVal = 0; nextVal < base; nextVal++) {
for (int nextVal = 0; nextVal < alphabetSize; nextVal++) {
// compute p_next
double p_next = (double) nextCount[nextVal] / (double) observations;
for (int prevVal = 0; prevVal < base_power_k; prevVal++) {
Expand Down Expand Up @@ -335,7 +332,7 @@ public double computeAverageLocalEntropyRateOfObservations() {
double entRate = 0.0;
double entRateCont = 0.0;

for (int nextVal = 0; nextVal < base; nextVal++) {
for (int nextVal = 0; nextVal < alphabetSize; nextVal++) {
for (int prevVal = 0; prevVal < base_power_k; prevVal++) {
// compute p_prev
double p_prev = (double) pastCount[prevVal] / (double) observations;
Expand Down Expand Up @@ -389,7 +386,7 @@ public double[] computeLocalFromPreviousObservations(int states[]){
// Initialise and store the current previous value for each column
int prevVal = 0;
for (int p = 0; p < k; p++) {
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[p];
}

Expand All @@ -414,7 +411,7 @@ public double[] computeLocalFromPreviousObservations(int states[]){
}
// Update the previous value:
prevVal -= maxShiftedValue[states[t-k]];
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[t];
}
average = average/(double) (timeSteps - k);
Expand All @@ -423,7 +420,6 @@ public double[] computeLocalFromPreviousObservations(int states[]){

}

@Override
public double[][] computeLocalFromPreviousObservations(int states[][]){
int rows = states.length;
int columns = states[0].length;
Expand All @@ -439,7 +435,7 @@ public double[][] computeLocalFromPreviousObservations(int states[][]){
for (int c = 0; c < columns; c++) {
prevVal[c] = 0;
for (int p = 0; p < k; p++) {
prevVal[c] *= base;
prevVal[c] *= alphabetSize;
prevVal[c] += states[p][c];
}
}
Expand All @@ -465,7 +461,7 @@ public double[][] computeLocalFromPreviousObservations(int states[][]){
}
// Update the previous value:
prevVal[c] -= maxShiftedValue[states[r-k][c]];
prevVal[c] *= base;
prevVal[c] *= alphabetSize;
prevVal[c] += states[r][c];
}
}
Expand All @@ -475,7 +471,7 @@ public double[][] computeLocalFromPreviousObservations(int states[][]){

}

@Override

public double[][][] computeLocalFromPreviousObservations(int states[][][]){
int timeSteps = states.length;
int agentRows = states[0].length;
Expand All @@ -493,7 +489,7 @@ public double[][][] computeLocalFromPreviousObservations(int states[][][]){
for (int c = 0; c < agentColumns; c++) {
prevVal[r][c] = 0;
for (int p = 0; p < k; p++) {
prevVal[r][c] *= base;
prevVal[r][c] *= alphabetSize;
prevVal[r][c] += states[p][r][c];
}
}
Expand Down Expand Up @@ -521,7 +517,7 @@ public double[][][] computeLocalFromPreviousObservations(int states[][][]){
}
// Update the previous value:
prevVal[r][c] -= maxShiftedValue[states[t-k][r][c]];
prevVal[r][c] *= base;
prevVal[r][c] *= alphabetSize;
prevVal[r][c] += states[t][r][c];
}
}
Expand All @@ -531,7 +527,7 @@ public double[][][] computeLocalFromPreviousObservations(int states[][][]){
return localActive;
}

@Override

public double[] computeLocalFromPreviousObservations(int states[][], int col){
int rows = states.length;
//int columns = states[0].length;
Expand All @@ -546,7 +542,7 @@ public double[] computeLocalFromPreviousObservations(int states[][], int col){
int prevVal = 0;
prevVal = 0;
for (int p = 0; p < k; p++) {
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[p][col];
}
int nextVal;
Expand All @@ -570,7 +566,7 @@ public double[] computeLocalFromPreviousObservations(int states[][], int col){
}
// Update the previous value:
prevVal -= maxShiftedValue[states[r-k][col]];
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[r][col];
}
average = average/(double) (rows - k);
Expand All @@ -579,7 +575,7 @@ public double[] computeLocalFromPreviousObservations(int states[][], int col){

}

@Override

public double[] computeLocalFromPreviousObservations(int states[][][],
int agentIndex1, int agentIndex2){
int timeSteps = states.length;
Expand All @@ -595,7 +591,7 @@ public double[] computeLocalFromPreviousObservations(int states[][][],
int prevVal = 0;
prevVal = 0;
for (int p = 0; p < k; p++) {
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[p][agentIndex1][agentIndex2];
}
int nextVal;
Expand All @@ -619,7 +615,7 @@ public double[] computeLocalFromPreviousObservations(int states[][][],
}
// Update the previous value:
prevVal -= maxShiftedValue[states[t-k][agentIndex1][agentIndex2]];
prevVal *= base;
prevVal *= alphabetSize;
prevVal += states[t][agentIndex1][agentIndex2];
}
average = average/(double) (timeSteps - k);
Expand Down Expand Up @@ -709,14 +705,14 @@ public EmpiricalMeasurementDistribution computeSignificance(int[][] newOrderings
MatrixUtils.fill(prevValues, prevVal, t_prev, numberOfSamplesPrev);
t_prev += numberOfSamplesPrev;
}
for (int nextVal = 0; nextVal < base; nextVal++) {
for (int nextVal = 0; nextVal < alphabetSize; nextVal++) {
int numberOfSamplesNext = nextCount[nextVal];
MatrixUtils.fill(nextValues, nextVal, t_next, numberOfSamplesNext);
t_next += numberOfSamplesNext;
}

ActiveInformationCalculatorDiscrete ais2;
ais2 = new ActiveInformationCalculatorDiscrete(base, k);
ais2 = new ActiveInformationCalculatorDiscrete(alphabetSize, k);
ais2.initialise();
ais2.observations = observations;
ais2.pastCount = pastCount;
Expand Down Expand Up @@ -754,7 +750,7 @@ public AnalyticMeasurementDistribution computeSignificance()
}
return new ChiSquareMeasurementDistribution(average,
observations,
(base - 1) * (base_power_k - 1));
(alphabetSize - 1) * (base_power_k - 1));
}

/**
Expand All @@ -767,7 +763,7 @@ public void writePdfs() {
double miCont = 0.0;

System.out.println("nextVal p(next) prevVal p(prev) p(joint) logTerm localVal");
for (int nextVal = 0; nextVal < base; nextVal++) {
for (int nextVal = 0; nextVal < alphabetSize; nextVal++) {
// compute p_next
double p_next = (double) nextCount[nextVal] / (double) observations;
for (int prevVal = 0; prevVal < base_power_k; prevVal++) {
Expand Down Expand Up @@ -811,7 +807,7 @@ public void writePdfs() {
public int computePastValue(int[] x, int t) {
int pastVal = 0;
for (int p = 0; p < k; p++) {
pastVal *= base;
pastVal *= alphabetSize;
pastVal += x[t - k + 1 + p];
}
return pastVal;
Expand All @@ -835,7 +831,7 @@ public int computePastValue(int[] x, int t) {
public int computePastValue(int[][] data, int column, int t) {
int pastVal = 0;
for (int p = 0; p < k; p++) {
pastVal *= base;
pastVal *= alphabetSize;
pastVal += data[t - k + 1 + p][column];
}
return pastVal;
Expand Down Expand Up @@ -863,9 +859,27 @@ public int computePastValue(int[][][] data, int agentRow,
int agentColumn, int t) {
int pastVal = 0;
for (int p = 0; p < k; p++) {
pastVal *= base;
pastVal *= alphabetSize;
pastVal += data[t - k + 1 + p][agentRow][agentColumn];
}
return pastVal;
}

@Override
public void setObservations(Object[] observations) throws Exception {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'setObservations'");
}

@Override
public void startAddObservations() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'startAddObservations'");
}

@Override
public void finaliseAddObservations() throws Exception {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'finaliseAddObservations'");
}
}
Loading