-
Notifications
You must be signed in to change notification settings - Fork 73
/
activeInfoStorageHeartBreathRatesKraskov.m
executable file
·126 lines (107 loc) · 4.82 KB
/
activeInfoStorageHeartBreathRatesKraskov.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
%%
%% 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 [aisHeart, aisBreath] = activeInfoStorageHeartBreathRatesKraskov(kHistories, knn, numSurrogates)
%
% activeInfoStorageHeartBreathRatesKraskov
% Version 1.0
% Joseph Lizier
% 04/04/2014
%
% Used to explore active information storage in the heart rate / breath rate example of Schreiber --
% estimated using Kraskov-Grassberger estimation.
%
% Usually plateaus of AIS indicate that the correct embedding is found; for Kraskov estimation,
% a peak will indicate this (given that bias correction will pull down values for larger k.
%
%
% Inputs
% - kHistories - a vector of which embedded history lengths to evaluate for both variables
% - knn - a scalar specifying a single value of K nearest neighbours to evaluate AIS (Kraskov) with
% - numSurrogates - a scalar specifying the number of surrogates to evaluate AIS from null distribution
% Outputs
% - aisHeart - active information storage TE (heart -> breath) for each value of k nearest neighbours
% - aisBreath - TE (breath -> heart) for each value of k nearest neighbours
function [aisHeart, aisBreath] = activeInfoStorageHeartBreathRatesKraskov(kHistories, knn, numSurrogates)
tic;
% Add utilities to the path
addpath('..');
% Assumes the jar is two 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');
if (nargin < 3)
numSurrogates = 0;
end
data = load('../../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('AIS for heart rate and breath rate for Kraskov estimation with %d samples:\n', timeSteps);
aisCalc=javaObject('infodynamics.measures.continuous.kraskov.ActiveInfoStorageCalculatorKraskov');
for kIndex = 1:length(kHistories)
kHistory = kHistories(kIndex);
% Compute an AIS value for this embedding length for each variable:
% Perform calculation for heart
aisCalc.setProperty('k', sprintf('%d',knn));
aisCalc.setProperty('NORMALISE', 'true');
aisCalc.initialise(kHistory); % Use history length kHistory (Schreiber k)
aisCalc.setObservations(octaveToJavaDoubleArray(heart(1:timeSteps)));
aisHeart(kIndex) = aisCalc.computeAverageLocalOfObservations();
if (numSurrogates > 0)
aisHeartNullDist = aisCalc.computeSignificance(numSurrogates);
aisHeartNullMean = aisHeartNullDist.getMeanOfDistribution();
aisHeartNullStd = aisHeartNullDist.getStdOfDistribution();
end
% Perform calculation for breath
aisCalc.setProperty('k', sprintf('%d',knn));
aisCalc.setProperty('NORMALISE', 'true');
aisCalc.initialise(kHistory); % Use history length kHistory (Schreiber k)
aisCalc.setObservations(octaveToJavaDoubleArray(chestVol(1:timeSteps)));
aisBreath(kIndex) = aisCalc.computeAverageLocalOfObservations();
if (numSurrogates > 0)
aisBreathNullDist = aisCalc.computeSignificance(numSurrogates);
aisBreathNullMean = aisBreathNullDist.getMeanOfDistribution();
aisBreathNullStd = aisBreathNullDist.getStdOfDistribution();
end
fprintf('AIS(k=%d,knns=%d): h = %.3f', kHistory, knn, aisHeart(kIndex));
if (numSurrogates > 0)
fprintf(' (null = %.3f +/- %.3f)', aisHeartNullMean, aisHeartNullStd);
end
fprintf(', b = %.3f', aisBreath(kIndex));
if (numSurrogates > 0)
fprintf('(null = %.3f +/- %.3f)\n', aisBreathNullMean, aisBreathNullStd);
else
fprintf('\n');
end
end
totaltime = toc;
fprintf('Total runtime was %.1f sec\n', totaltime);
hold off;
plot(kHistories, aisHeart, 'rx', 'markersize', 10);
hold on;
plot(kHistories, aisBreath, 'bo', 'markersize', 10);
hold off;
legend(['AIS(Heart) '; 'AIS(Breath)'])
set (gca,'fontsize',26);
xlabel('embedding history k', 'FontSize', 36, 'FontWeight', 'bold');
ylabel('AIS(k)', 'FontSize', 36, 'FontWeight', 'bold');
print('heartBreathResults-kraskovAIS.eps', '-depsc');
end