mirror of https://github.com/jlizier/jidt
Fixed main demo scripts to be compatible with Matlab (fixed printf -> printf, rows -> size(x, 1), and not specifically converting matlab arrays to java and back)
This commit is contained in:
parent
220f4a8857
commit
b6aece6d51
|
|
@ -16,10 +16,10 @@ teCalc=javaObject('infodynamics.measures.discrete.ApparentTransferEntropyCalcula
|
|||
teCalc.initialise();
|
||||
% Since we have simple arrays of doubles, we can directly pass these in:
|
||||
teCalc.addObservations(destArray, sourceArray);
|
||||
printf("For copied source, result should be close to 1 bit : ");
|
||||
fprintf('For copied source, result should be close to 1 bit : ');
|
||||
result = teCalc.computeAverageLocalOfObservations()
|
||||
teCalc.initialise();
|
||||
teCalc.addObservations(destArray, sourceArray2);
|
||||
printf("For random source, result should be close to 0 bits: ");
|
||||
fprintf('For random source, result should be close to 0 bits: ');
|
||||
result2 = teCalc.computeAverageLocalOfObservations()
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,6 @@ teCalc=javaObject('infodynamics.measures.discrete.ApparentTransferEntropyCalcula
|
|||
teCalc.initialise();
|
||||
% Add observations of transfer across one cell to the right per time step:
|
||||
teCalc.addObservations(twoDTimeSeriesJavaInt, 1);
|
||||
printf("The result should be close to 1 bit here, since we are executing copy operations of what is effectively a random bit to each cell here: ");
|
||||
fprintf('The result should be close to 1 bit here, since we are executing copy operations of what is effectively a random bit to each cell here: ');
|
||||
result2D = teCalc.computeAverageLocalOfObservations()
|
||||
|
||||
|
|
|
|||
|
|
@ -13,17 +13,17 @@ destArray = [0; covariance*sourceArray(1:numObservations-1) + (1-covariance)*nor
|
|||
sourceArray2=normrnd(0, 1, numObservations, 1); % Uncorrelated source
|
||||
% Create a TE calculator and run it:
|
||||
teCalc=javaObject('infodynamics.measures.continuous.kernel.TransferEntropyCalculatorKernel');
|
||||
teCalc.setProperty("NORMALISE_PROP_NAME", "true"); % Normalise the individual variables
|
||||
teCalc.setProperty('NORMALISE_PROP_NAME', 'true'); % Normalise the individual variables
|
||||
teCalc.initialise(1, 0.5); % Use history length 1 (Schreiber k=1), kernel width of 0.5 normalised units
|
||||
teCalc.setObservations(sourceArray, destArray);
|
||||
% For copied source, should give something close to 1 bit:
|
||||
result = teCalc.computeAverageLocalOfObservations();
|
||||
printf("TE result %.4f bits; expected to be close to %.4f bits for these correlated Gaussians but biased upwards\n", \
|
||||
fprintf('TE result %.4f bits; expected to be close to %.4f bits for these correlated Gaussians but biased upwards\n', ...
|
||||
result, log(1/(1-covariance^2))/log(2));
|
||||
teCalc.initialise(); % Initialise leaving the parameters the same
|
||||
teCalc.setObservations(sourceArray2, destArray);
|
||||
% For random source, it should give something close to 0 bits
|
||||
result2 = teCalc.computeAverageLocalOfObservations();
|
||||
printf("TE result %.4f bits; expected to be close to 0 bits for uncorrelated Gaussians but will be biased upwards\n", \
|
||||
fprintf('TE result %.4f bits; expected to be close to 0 bits for uncorrelated Gaussians but will be biased upwards\n', ...
|
||||
result2);
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ sourceArray2=normrnd(0, 1, numObservations, 1); % Uncorrelated source
|
|||
% Create a TE calculator and run it:
|
||||
teCalc=javaObject('infodynamics.measures.continuous.kraskov.TransferEntropyCalculatorKraskov');
|
||||
teCalc.initialise(1); % Use history length 1 (Schreiber k=1)
|
||||
teCalc.setProperty("k", "4"); % Use Kraskov parameter K=4 for 4 nearest points
|
||||
teCalc.setProperty('k', '4'); % Use Kraskov parameter K=4 for 4 nearest points
|
||||
% Perform calculation with correlated source:
|
||||
teCalc.setObservations(sourceArray, destArray);
|
||||
result = teCalc.computeAverageLocalOfObservations();
|
||||
|
|
@ -22,12 +22,12 @@ result = teCalc.computeAverageLocalOfObservations();
|
|||
% data is a set of random variables) - the result will be of the order
|
||||
% of what we expect, but not exactly equal to it; in fact, there will
|
||||
% be a large variance around it.
|
||||
printf("TE result %.4f nats; expected to be close to %.4f nats for these correlated Gaussians\n", \
|
||||
fprintf('TE result %.4f nats; expected to be close to %.4f nats for these correlated Gaussians\n', ...
|
||||
result, log(1/(1-covariance^2)));
|
||||
% Perform calculation with uncorrelated source:
|
||||
teCalc.initialise(); % Initialise leaving the parameters the same
|
||||
teCalc.setObservations(sourceArray2, destArray);
|
||||
result2 = teCalc.computeAverageLocalOfObservations();
|
||||
printf("TE result %.4f nats; expected to be close to 0 nats for these uncorrelated Gaussians\n", result2);
|
||||
fprintf('TE result %.4f nats; expected to be close to 0 nats for these uncorrelated Gaussians\n', result2);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
% function octaveMatrix = javaMatrixToOctave(javaMatrix)
|
||||
%
|
||||
% Convert a java matrix (1 or 2D, double or int - but not Integer!!) to an octave matrix
|
||||
% Convert a java matrix (1 or 2D, double or int - but not Integer!!) to an octave or matlab matrix
|
||||
%
|
||||
% Octave-java doesn't seem to handle the conversion natively,
|
||||
% so we either use org.octave.Matrix (built-in) to do it, or
|
||||
|
|
@ -16,10 +16,10 @@ function octaveMatrix = javaMatrixToOctave(javaMatrix, startRow, startCol, numRo
|
|||
startCol = 1;
|
||||
end
|
||||
if (nargin < 4)
|
||||
numRows = rows(javaMatrix);
|
||||
numRows = size(javaMatrix, 1);
|
||||
end
|
||||
if (nargin < 5)
|
||||
numCols = columns(javaMatrix);
|
||||
numCols = size(javaMatrix, 2);
|
||||
end
|
||||
|
||||
if (exist ('OCTAVE_VERSION', 'builtin'))
|
||||
|
|
@ -46,11 +46,12 @@ function octaveMatrix = javaMatrixToOctave(javaMatrix, startRow, startCol, numRo
|
|||
end
|
||||
return;
|
||||
end
|
||||
% else fall through to cell by cell conversion, as per for matlab
|
||||
else
|
||||
% Else we're in matlab, in which case the native java type can be handled, so return it directly:
|
||||
octaveMatrix = javaMatrix;
|
||||
end
|
||||
|
||||
% Else, either we encountered an error in the octave resizing,
|
||||
% or we were in Matlab all along. (If there's a fast way for matlab, tell me)
|
||||
% Else, we encountered an error in the octave resizing, so fall through to element by element conversion:
|
||||
|
||||
octaveMatrix = zeros(numRows, numCols);
|
||||
for r = startRow:startRow+numRows-1
|
||||
|
|
|
|||
|
|
@ -24,16 +24,9 @@ function jDoubleArray = octaveToJavaDoubleArray(octaveArray)
|
|||
jDoubleArray(1) = octaveArray(1);
|
||||
end
|
||||
else
|
||||
% We're in matlab:
|
||||
% We're in matlab: the native matlab array can be passed to java as is:
|
||||
|
||||
% Presumably there's a quick way to do this in matlab, but since I'm not on matlab, I don't know ...
|
||||
% If someone knows or has tested something, please tell me and I'll include it here.
|
||||
% In the meantime, we copy element by element
|
||||
|
||||
jDoubleArray = javaArray('java.lang.Double', length(octaveArray));
|
||||
for r = 1:length(octaveMatrix)
|
||||
jDoubleArray(r) = octaveArray(r);
|
||||
end
|
||||
jDoubleArray = octaveArray;
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -21,21 +21,9 @@ function jDoubleMatrix = octaveToJavaDoubleMatrix(octaveMatrix)
|
|||
jDoubleMatrix(1, 1) = octaveMatrix(1);
|
||||
end
|
||||
else
|
||||
% We're in matlab:
|
||||
% We're in matlab: the native matlab 2D array can be passed to java as is:
|
||||
|
||||
% Presumably there's a quick way to do this in matlab, but since I'm not on matlab, I don't know ...
|
||||
% If someone knows or has tested something, please tell me and I'll include it here.
|
||||
% In the meantime, we copy element by element
|
||||
|
||||
jDoubleMatrix = javaArray('java.lang.Double', rows(octaveMatrix), columns(octaveMatrix));
|
||||
for r = 1:rows(octaveMatrix)
|
||||
% Slow but effective way:
|
||||
for c = 1:columns(octaveMatrix)
|
||||
jDoubleMatrix(r,c) = octaveMatrix(r,c);
|
||||
end
|
||||
% Fast way that doesn't actually work:
|
||||
% jDoubleMatrix(r,:) = octaveMatrix(r,:);
|
||||
end
|
||||
jDoubleMatrix = octaveMatrix;
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue