Skip to content

Commit

Permalink
Adding R example 5 and 6, adding notices to jar dist, finalising pyth…
Browse files Browse the repository at this point in the history
…on example 6 using file reading utilities; adding extra printout to Example 4 in Java
  • Loading branch information
jlizier committed Aug 22, 2014
1 parent 720d3f1 commit 643d76f
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 22 deletions.
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
<fileset file="license-gplv3.txt"/>
<fileset file="readme.txt"/>
<fileset file="${versionfile}"/>
<zipfileset dir="notices" includes="**/*.*,**/*" prefix="notices"/>
</zip>
</target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package infodynamics.demos;

import infodynamics.utils.MatrixUtils;
import infodynamics.utils.RandomGenerator;
import infodynamics.measures.continuous.kraskov.TransferEntropyCalculatorKraskov;

Expand Down Expand Up @@ -78,7 +79,9 @@ public static void main(String[] args) throws Exception {

// We can also compute the local TE values for the time-series samples here:
// (See more about utility of local TE in the CA demos)
@SuppressWarnings("unused")
double[] localTE = teCalc.computeLocalOfPreviousObservations();
System.out.printf("Notice that the mean of locals, %.4f nats," +
" equals the previous result\n",
MatrixUtils.sum(localTE)/(double)(numObservations-1));
}
}
7 changes: 3 additions & 4 deletions demos/octave/example6DynamicCallingMutualInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
% one of three concrete implementations (kernel estimator, Kraskov estimator or
% linear-Gaussian estimator) by dynamically supplying the class name of
% the concrete implementation.
%
% This is the Octave/Matlab equivalent to the demos/java/lateBindingDemo

% Change location of jar to match yours:
javaaddpath('../../infodynamics.jar');

%---------------------
% 1. Properties for the calculation (these are dynamically changeable):
% 1. Properties for the calculation (these are dynamically changeable, you could
% load them in from another properties file):
% The name of the data file (relative to this directory)
datafile = '../data/4ColsPairedNoisyDependence-1.txt';
% List of column numbers for variables 1 and 2:
Expand All @@ -43,7 +42,7 @@
% infodynamics.measures.continuous.MutualInfoCalculatorMultiVariate
% which we wish to use for the calculation.
% Note that one could use any of the following calculators (try them all!):
% implementingClass = 'infodynamics.measures.continuous.kraskov.MutualInfoCalculatorMultiVariateKraskov1'; % MI([1,2], [3,4]) = 0.35507
% implementingClass = 'infodynamics.measures.continuous.kraskov.MutualInfoCalculatorMultiVariateKraskov1'; % MI([1,2], [3,4]) = 0.36353
% implementingClass = 'infodynamics.measures.continuous.kernel.MutualInfoCalculatorMultiVariateKernel';
% implementingClass = 'infodynamics.measures.continuous.gaussian.MutualInfoCalculatorMultiVariateGaussian';
implementingClass = 'infodynamics.measures.continuous.kraskov.MutualInfoCalculatorMultiVariateKraskov1';
Expand Down
12 changes: 3 additions & 9 deletions demos/python/example6DynamicCallingMutualInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
# one of three concrete implementations (kernel estimator, Kraskov estimator or
# linear-Gaussian estimator) by dynamically supplying the class name of
# the concrete implementation.
#
# This is the Python equivalent to the demos/java/lateBindingDemo

from jpype import *
import random
import string
import numpy
import readFloatsFile

# Change location of jar to match yours:
jarLocation = "../../infodynamics.jar"
Expand All @@ -56,15 +55,10 @@
implementingClass = "infodynamics.measures.continuous.kraskov.MutualInfoCalculatorMultiVariateKraskov1"

#---------------------
# 2. Load in the data (space separate numbers, one time step per line, each column is a variable)
f = open(datafile)
data = []
for line in f:
data.append([float(x) for x in line.split()])

# 2. Load in the data
data = readFloatsFile.readFloatsFile(datafile)
# As numpy array:
A = numpy.array(data)

# Pull out the columns from the data set which correspond to each of variable 1 and 2:
variable1 = A[:,variable1Columns]
variable2 = A[:,variable2Columns]
Expand Down
27 changes: 27 additions & 0 deletions demos/python/readFloatsFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
##
## Java Information Dynamics Toolkit (JIDT)
## Copyright (C) 2012, Joseph T. Lizier
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##

def readFloatsFile(filename):
"Read a 2D array of floats from a given file"
with open(filename) as f:
# Space separate numbers, one time step per line, each column is a variable
array = []
for line in f: # read all lines
array.append([float(x) for x in line.split()])
return array

27 changes: 27 additions & 0 deletions demos/python/readIntsFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
##
## Java Information Dynamics Toolkit (JIDT)
## Copyright (C) 2012, Joseph T. Lizier
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##

def readIntsFile(filename):
"Read a 2D array of int from a given file"
with open(filename) as f:
# Space separate numbers, one time step per line, each column is a variable
array = []
for line in f: # read all lines
array.append([int(x) for x in line.split()])
return array

7 changes: 7 additions & 0 deletions demos/r/example3TeContinuousDataKernel.r
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ cat("TE result ", result, "bits; expected to be close to ", log(1/(1-covariance
result2 <- .jcall(teCalc,"D","computeAverageLocalOfObservations")
cat("TE result ", result2, "bits; expected to be close to 0 bits for uncorrelated Gaussians but will be biased upwards\n")

# We can get insight into the bias by examining the null distribution:
nullDist <- .jcall(teCalc,"Linfodynamics/utils/EmpiricalMeasurementDistribution;",
"computeSignificance", 100L)
cat("Null distribution for unrelated source and destination",
"(i.e. the bias) has mean", .jcall(nullDist, "D", "getMeanOfDistribution"),
"bits and standard deviation", .jcall(nullDist, "D", "getStdOfDistribution"), "\n")

6 changes: 6 additions & 0 deletions demos/r/example4TeContinuousDataKraskov.r
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ cat("TE result ", result, "nats; expected to be close to ", log(1/(1-covariance
result2 <- .jcall(teCalc,"D","computeAverageLocalOfObservations")
cat("TE result ", result2, "nats; expected to be close to 0 nats for uncorrelated Gaussians\n")

# We can also compute the local TE values for the time-series samples here:
# (See more about utility of local TE in the CA demos)
localTE <- .jcall(teCalc,"[D","computeLocalOfPreviousObservations")
cat("Notice that the mean of locals", sum(localTE)/(numObservations-1),
"nats equals the above result\n")

69 changes: 69 additions & 0 deletions demos/r/example5TeBinaryMultivarTransfer.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
##
## Java Information Dynamics Toolkit (JIDT)
## Copyright (C) 2012, Joseph T. Lizier
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##

# = Example 5 - Multivariate transfer entropy on binary data =

# Multivariate transfer entropy (TE) calculation on binary data using the discrete TE calculator:

# Load the rJava library and start the JVM
library("rJava")
.jinit()

# Change location of jar to match yours:
# IMPORTANT -- If using the default below, make sure you have set the working directory
# in R (e.g. with setwd()) to the location of this file (i.e. demos/r) !!
.jaddClassPath("../../infodynamics.jar")

# Generate some random binary data.
numObservations <- 100
sourceArray<-matrix(sample(0:1,numObservations*2, replace=TRUE),numObservations,2)
sourceArray2<-matrix(sample(0:1,numObservations*2, replace=TRUE),numObservations,2)
# Destination variable takes a copy of the first bit of the source in bit 1,
# and an XOR of the two bits of the source in bit 2:
destArray <- cbind( c(0L, sourceArray[1:numObservations-1,1]), # column 1
c(0L, 1L*xor(sourceArray[1:numObservations-1,1],
sourceArray[1:numObservations-1,2]))) # column 2

# Convert the 2D arrays to Java format:
sourceArrayJava <- .jarray(sourceArray, "[I", dispatch=TRUE)
sourceArray2Java <- .jarray(sourceArray2, "[I", dispatch=TRUE)
destArrayJava <- .jarray(destArray, "[I", dispatch=TRUE)

# Create a TE calculator and run it:
teCalc<-.jnew("infodynamics/measures/discrete/TransferEntropyCalculatorDiscrete", 4L, 1L)
.jcall(teCalc,"V","initialise") # V for void return value
# We need to construct the joint values for the dest and source before we pass them in,
# and need to use the matrix conversion routine when calling from Matlab/Octave:
mUtils<-.jnew("infodynamics/utils/MatrixUtils")
.jcall(teCalc,"V","addObservations",
.jcall(mUtils,"[I","computeCombinedValues", sourceArrayJava, 2L),
.jcall(mUtils,"[I","computeCombinedValues", destArrayJava, 2L))
result<-.jcall(teCalc,"D","computeAverageLocalOfObservations")
cat("For source which the 2 bits are determined from, result should be close to 2 bits : ", result, "\n")

.jcall(teCalc,"V","initialise")
.jcall(teCalc,"V","addObservations",
.jcall(mUtils,"[I","computeCombinedValues", sourceArray2Java, 2L),
.jcall(mUtils,"[I","computeCombinedValues", destArrayJava, 2L))
result2<-.jcall(teCalc,"D","computeAverageLocalOfObservations")
cat("For random source, result should be close to 0 bits in theory: ", result2, "\n");
cat("Result for random source is inflated towards 0.3 due to finite observation length ",
.jcall(teCalc,"I","getNumObservations"), "\n",
"One can verify that the answer is consistent with that from a\n",
"random source by checking: teCalc.computeSignificance(1000); ans.pValue\n");

90 changes: 90 additions & 0 deletions demos/r/example6DynamicCallingMutualInfo.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
##
## Java Information Dynamics Toolkit (JIDT)
## Copyright (C) 2012, Joseph T. Lizier
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##

# Example 6 - Mutual information calculation with dynamic specification of calculator

# This example shows how to write R code to take advantage of the
# common interfaces defined for various information-theoretic calculators.
# Here, we use the common form of the infodynamics.measures.continuous.MutualInfoCalculatorMultiVariate
# interface (which is never named here) to write common code into which we can plug
# one of three concrete implementations (kernel estimator, Kraskov estimator or
# linear-Gaussian estimator) by dynamically supplying the class name of
# the concrete implementation.

# Load the rJava library and start the JVM
library("rJava")
.jinit()

# Change location of jar to match yours:
# IMPORTANT -- If using the default below, make sure you have set the working directory
# in R (e.g. with setwd()) to the location of this file (i.e. demos/r) !!
.jaddClassPath("../../infodynamics.jar")

#---------------------
# 1. Properties for the calculation (these are dynamically changeable, you could
# load them in from another properties file):
# The name of the data file (relative to this directory)
datafile <- "../data/4ColsPairedNoisyDependence-1.txt"
# List of column numbers for variables 1 and 2:
# (you can select any columns you wish to be contained in each variable)
variable1Columns <- c(1,2) # array indices start from 1 in R
variable2Columns <- c(3,4)
# The name of the concrete implementation of the interface
# infodynamics.measures.continuous.MutualInfoCalculatorMultiVariate
# which we wish to use for the calculation.
# Note that one could use any of the following calculators (try them all!):
# implementingClass <- "infodynamics/measures/continuous/kraskov/MutualInfoCalculatorMultiVariateKraskov1" # MI([1,2], [3,4]) = 0.36353
# implementingClass <- "infodynamics/measures/continuous/kernel/MutualInfoCalculatorMultiVariateKernel"
# implementingClass <- "infodynamics/measures/continuous/gaussian/MutualInfoCalculatorMultiVariateGaussian"
implementingClass <- "infodynamics/measures/continuous/kraskov/MutualInfoCalculatorMultiVariateKraskov1"

#---------------------
# 2. Load in the data
data <- read.csv(datafile, header=FALSE, sep="")
# Pull out the columns from the data set which correspond to each of variable 1 and 2:
variable1 <- data[, variable1Columns]
variable2 <- data[, variable2Columns]
# Extra step to extract the raw values from these data.frame objects:
variable1 <- apply(variable1, 2, function(x) as.numeric(x))
variable2 <- apply(variable2, 2, function(x) as.numeric(x))

#---------------------
# 3. Dynamically instantiate an object of the given class:
# (in fact, all java object creation in octave/matlab is dynamic - it has to be,
# since the languages are interpreted. This makes our life slightly easier at this
# point than it is in demos/java/example6LateBindingMutualInfo where we have to handle this manually)
miCalc<-.jnew(implementingClass)

#---------------------
# 4. Start using the MI calculator, paying attention to only
# call common methods defined in the interface type
# infodynamics.measures.continuous.MutualInfoCalculatorMultiVariate
# not methods only defined in a given implementation class.
# a. Initialise the calculator to use the required number of
# dimensions for each variable:
.jcall(miCalc,"V","initialise", length(variable1Columns), length(variable2Columns))
# b. Supply the observations to compute the PDFs from:
.jcall(miCalc,"V","setObservations",
.jarray(variable1, "[D", dispatch=TRUE),
.jarray(variable2, "[D", dispatch=TRUE))
# c. Make the MI calculation:
miValue <- .jcall(miCalc,"D","computeAverageLocalOfObservations")

cat("MI calculator", implementingClass, "\n computed the joint MI as ",
miValue, "\n")

Loading

0 comments on commit 643d76f

Please sign in to comment.