From 720d3f1e1ab51774279ba876df2ff30b3e389bc0 Mon Sep 17 00:00:00 2001 From: "joseph.lizier" Date: Thu, 21 Aug 2014 13:15:45 +0000 Subject: [PATCH] Adding R example 4, and correcting random normal generation in example 3 --- .../octave/example4TeContinuousDataKraskov.m | 2 +- demos/r/example3TeContinuousDataKernel.r | 7 ++- demos/r/example4TeContinuousDataKraskov.r | 58 +++++++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100755 demos/r/example4TeContinuousDataKraskov.r diff --git a/demos/octave/example4TeContinuousDataKraskov.m b/demos/octave/example4TeContinuousDataKraskov.m index 97e987f..c8af0c9 100755 --- a/demos/octave/example4TeContinuousDataKraskov.m +++ b/demos/octave/example4TeContinuousDataKraskov.m @@ -31,8 +31,8 @@ destArray = [0; covariance*sourceArray(1:numObservations-1) + (1-covariance)*ran sourceArray2=randn(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.initialise(1); % Use history length 1 (Schreiber k=1) % Perform calculation with correlated source: teCalc.setObservations(sourceArray, destArray); result = teCalc.computeAverageLocalOfObservations(); diff --git a/demos/r/example3TeContinuousDataKernel.r b/demos/r/example3TeContinuousDataKernel.r index 0f4de22..c62ebfd 100755 --- a/demos/r/example3TeContinuousDataKernel.r +++ b/demos/r/example3TeContinuousDataKernel.r @@ -32,9 +32,10 @@ library("rJava") # Generate some random normalised data. numObservations<-1000 covariance<-0.4 -sourceArray<-runif(numObservations, 0, 1) -destArray = c(0, covariance*sourceArray[1:numObservations-1] + (1-covariance)*runif(numObservations-1, 0, 1)) -sourceArray2<-runif(numObservations, 0, 1) # Uncorrelated source +sourceArray<-rnorm(numObservations) +destArray = c(0, covariance*sourceArray[1:numObservations-1] + (1-covariance)*rnorm(numObservations-1, 0, 1)) +sourceArray2<-rnorm(numObservations) # Uncorrelated source + # Create a TE calculator and run it: teCalc<-.jnew("infodynamics/measures/continuous/kernel/TransferEntropyCalculatorKernel") .jcall(teCalc,"V","setProperty", "NORMALISE", "true") # Normalise the individual variables diff --git a/demos/r/example4TeContinuousDataKraskov.r b/demos/r/example4TeContinuousDataKraskov.r new file mode 100755 index 0000000..6bb9407 --- /dev/null +++ b/demos/r/example4TeContinuousDataKraskov.r @@ -0,0 +1,58 @@ +## +## 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 . +## + +# = Example 4 - Transfer entropy on continuous data using Kraskov estimators = + +# Simple transfer entropy (TE) calculation on continuous-valued data using the Kraskov-estimator 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 normalised data. +numObservations<-1000 +covariance<-0.4 +sourceArray<-rnorm(numObservations) +destArray = c(0, covariance*sourceArray[1:numObservations-1] + (1-covariance)*rnorm(numObservations-1, 0, 1)) +sourceArray2<-rnorm(numObservations) # Uncorrelated source + +# Create a TE calculator: +teCalc<-.jnew("infodynamics/measures/continuous/kraskov/TransferEntropyCalculatorKraskov") +.jcall(teCalc,"V","setProperty", "k", "4") # Use Kraskov parameter K=4 for 4 nearest points + +# Perform calculation with correlated source: +.jcall(teCalc,"V","initialise", 1L) # Use history length 1 (Schreiber k=1) +.jcall(teCalc,"V","setObservations", sourceArray, destArray) +result <- .jcall(teCalc,"D","computeAverageLocalOfObservations") +# Note that the calculation is a random variable (because the generated +# 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. +cat("TE result ", result, "nats; expected to be close to ", log(1/(1-covariance^2)), " nats for these correlated Gaussians\n") + +# Perform calculation with uncorrelated source: +.jcall(teCalc,"V","initialise") # Initialise leaving the parameters the same +.jcall(teCalc,"V","setObservations", sourceArray2, destArray) +result2 <- .jcall(teCalc,"D","computeAverageLocalOfObservations") +cat("TE result ", result2, "nats; expected to be close to 0 nats for uncorrelated Gaussians\n") +