-
Notifications
You must be signed in to change notification settings - Fork 73
/
runHeartBreathRateKraskovMI.m
76 lines (61 loc) · 2.7 KB
/
runHeartBreathRateKraskovMI.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
%%
%% 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/>.
%%
% function [miHeartToBreath] = runHeartBreathRateKraskovMI()
%
% runHeartBreathRateKraskovMI
% Version 1.0
% Joseph Lizier
% 3/2/2015
%
% Used to explore mutual information in the heart rate / breath rate example of Schreiber --
% but estimates MI using Kraskov-Stoegbauer-Grassberger estimation.
%
% Note that the paths (to libraries, data, etc) are set assuming this code is run in
% the folder tutorial/sampleExercises/matlabOctave, relative to the main folder of the JIDT distribution.
%
%
% Outputs
% - miHeartToBreath - MI (heart ; breath)
function [miHeartToBreath] = runHeartBreathRateKraskovMI()
tic;
% Add Octave utilities to the path
addpath('../../../demos/octave/');
% Assumes the jar is three levels up - change this if this is not the case
% Octave is happy to have the path added multiple times; I'm unsure if this is true for matlab
javaaddpath('../../../infodynamics.jar');
data = load('../../../demos/data/SFI-heartRate_breathVol_bloodOx.txt');
% Restrict to the samples that Schreiber mentions:
data = data(2350:3550,:);
% Separate the data from each column:
heart = data(:,1);
chestVol = data(:,2);
bloodOx = data(:,3);
timeSteps = length(heart);
fprintf('MI for heart rate <-> breath rate for Kraskov estimation with %d samples:\n', timeSteps);
% Using a KSG estimator for MI is the least biased way to run this:
miCalc=javaObject('infodynamics.measures.continuous.kraskov.MutualInfoCalculatorMultiVariateKraskov2');
% Compute an MI value between heart and breath
miCalc.initialise(1,1); % univariate calculation
miCalc.setProperty('k', '4'); % 4 nearest neighbours for KSG estimator
miCalc.setObservations(octaveToJavaDoubleArray(heart), ...
octaveToJavaDoubleArray(chestVol));
miHeartToBreath = miCalc.computeAverageLocalOfObservations();
fprintf('MI: = %.3f nats\n', miHeartToBreath);
tElapsed = toc;
fprintf('Total runtime was %.1f sec\n', tElapsed);
end