-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmwmkld.m
313 lines (267 loc) · 11.1 KB
/
mwmkld.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
function DATA = mwmkld(DATA,platforms,varargin);
% function DATA{} = mwmkld(DATA{},PLATFORMS,varargin);
%
% Estimates spatial Kullback-Leibler divergence values between animals' search paths and the
% distribution of a set of platforms. The "true" distribution (i.e. the platform distribution) is
% estimated using a Gaussian kernel density estimation technique (see below). PLATFORMS can be a
% cell array, in which case the KLD is calculated for each set of platforms in the cell array.
%
% IMPORTANT: This function can only be run *after* mwmpdf.m (see help mwmpdf).
%
% The function utilizes Alexander Ihler's KDE toolbox for Matlab, which can be found at
% http://www.ics.uci.edu/~ihler/code/kde.html. If a bandwidth for the kernels is not specified then
% the KDE's optimal bandwidth calculation tool is used with the default spherical, uniform
% likelihood cross-validation search method. See 'help ksize' for more information.
%
% Note that the KDEs for the platforms as well as their estimated PDF are also stored, along with
% the actual divergence value, in the sub-structure KDE. All of the results are stored in the
% structure KLD. If multiple platform sets are given then KLD is a cell array, one structure for
% each set.
%
% Optional parameter/value inputs to the function are as follows:
%
% - 'bandwidth' : Scalar valued spatial size of kernels for the platform KDE. Default = optimal
% estimation.
%
% - 'logbase': Which logarithmic base to use for the calculation (can be 2, e, or 10). Default = e (nats).
%
% - 'weights': A vector of weights for each platforms contribution to the KDE. Default = 1 for all.
%
% - 'means' : A cell array of vectors that can be used to calculate the KLD using the mean of
% multiple trials' pdfs. For example, {[1 2],[4 5]} would lead to KLD being calculated
% for pdfs averaged over the first and second, then the fourth and fifth trials. These
% KLD values are stored in the sub-vector mkld in the order they were requested.
%
% - 'zones' : A matrix set of zones for which the probability of each zone will be calculated for each
% set of platform pdfs. The zones should be an M x 3 matrix where M is the number of zones,
% the first column is X locations, the second column is Y locations and the third column
% is the radius of each zone.
%
%--------------------------------------------------------------------------------
%
% 02/2013, Frankland Lab (www.franklandlab.com)
%
% Author: Blake Richards
% Contact: [email protected]
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright 2013 Blake Richards ([email protected])
%
% This file is part of the MWM Matlab Toolbox.
%
% The MWM Toolbox is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% The MWM Toolbox 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 Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public License
% along with the MWM Toolbox (in the file COPYING.LESSER). If not,
% see <http://www.gnu.org/licenses/>.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PARSE THE INPUT
% check the DATA argument
if ~isa(DATA,'cell')
error('DATA should be a cell array');
end
% check the platforms argument
if ~(isa(platforms,'numeric') && size(platforms,2) == 3) && ~isa(platforms,'cell')
error('PLATFORMS should be a K x 3 matrix or a cell array');
end
% define the default optional arguments
optargs = struct('bandwidth',-1,...
'logbase',exp(1),...
'weights',ones(1,size(platforms,1)),...
'means',{{}},...
'zones',[0,0,0]);
% get the optional argument names
optnames = fieldnames(optargs);
% get the number of optional arguments
nargs = length(varargin)/2;
% make sure the property/value pairs are input in pairs
if round(length(varargin)/2) ~= nargs
error('Expecting propertyName/propertyValue pairs after platforms');
end
% step through the optional arguments, check them, and store them
for pair = reshape(varargin,2,[])
% make it case insensitive
inpname = lower(pair{1});
% check whether the name matches a known option
if any(strmatch(inpname,optnames))
switch inpname
case 'bandwidth'
if isa(pair{2},'numeric')
optargs.(inpname) = pair{2};
else
error('bandwidth must be a scalar value');
end
case 'logbase'
if isa(pair{2},'numeric') && ismember(pair{2},[2 exp(1) 10])
optargs.(inpname) = pair{2};
else
error('logbase must be 2, e or 10');
end
case 'weights'
if isa(pair{2},'numeric') && length(pair{2}) == size(platforms,1)
optargs.(inpname) = pair{2};
else
error('weights must be a vector with length = size(PLATFORMS,1)');
end
case 'means'
if isa(pair{2},'cell')
optargs.(inpname) = pair{2};
else
error('means must be a cell array');
end
case 'zones'
if isa(pair{2},'numeric') && size(pair{2},2) == 3
optargs.(inpname) = pair{2};
else
error('zones must be an M x 3 matrix');
end
end
else
error('%s is not a recognized parameter name',inpname);
end
end
% fix weights if necessary
if isa(platforms,'cell') && optargs.weights == ones(size(platforms,1))
optargs.weights = [];
end
% for each data set in the structure
for ff = 1:length(DATA)
% get the points we're going to sample
XX = DATA{ff}.PDF.x;
YY = DATA{ff}.PDF.y;
outofpool = isnan(XX);
X = XX; X(outofpool) = 0;
Y = YY; Y(outofpool) = 0;
% check whether we have multiple platform sets
if isa(platforms,'cell')
% for each set of platforms...
for pp = 1:length(platforms)
% initialize the KLD{pp} structure
DATA{ff}.KLD{pp}.kld = zeros(size(DATA{ff}.PDF.p));
DATA{ff}.KLD{pp}.p = zeros(size(DATA{ff}.PDF.p,1),size(DATA{ff}.PDF.p,2));
% get the KDE
if optargs.bandwidth == -1
% create a KDE with an arbitrary bandwidth
tmpkde = kde(platforms{pp}(:,1:2)', [1; 1],optargs.weights);
% calculate the optimal bandwidth
DATA{ff}.KLD{pp}.kde = ksize(tmpkde);
else
% just use the specified bandwidth
DATA{ff}.KLD{pp}.kde = kde(platforms{pp}(:,1:2)', [optargs.bandwidth;optargs.bandwidth],optargs.weights);
end
% use the KDE to estimate the PDF of the platforms across the pool
P = reshape(evaluate(DATA{ff}.KLD{pp}.kde,[X(:)';Y(:)']),size(DATA{ff}.PDF.p,1),size(DATA{ff}.PDF.p,1));
% limit the data to the actual area of the pool
P(outofpool) = nan;
% renormalize P
P = P./nansum(P(:)*DATA{ff}.PDF.res^2);
% store the results
DATA{ff}.KLD{pp}.p = P;
% calculate the zone proabilities if requested
if any(optargs.zones)
DATA{ff}.KLD{pp}.zones = zeros(size(optargs.zones,1),1);
for zz = 1:size(optargs.zones,1)
DATA{ff}.KLD{pp}.zones(zz) = nansum(nansum(DATA{ff}.KLD{pp}.p.*(sqrt((optargs.zones(zz,1)-XX).^2 + (optargs.zones(zz,2)-YY).^2) < optargs.zones(zz,3))));
end
end
% for each trial, calcualte the KLD{pp}
DATA{ff}.KLD{pp}.kld = zeros(DATA{ff}.ntrials,1);
for tt = 1:DATA{ff}.ntrials
switch optargs.logbase
case 2
DATA{ff}.KLD{pp}.kld(tt) = nansum(nansum(DATA{ff}.KLD{pp}.p.*log2(DATA{ff}.KLD{pp}.p./(DATA{ff}.PDF.p(:,:,tt)+exp(-100)))));
case exp(1)
DATA{ff}.KLD{pp}.kld(tt) = nansum(nansum(DATA{ff}.KLD{pp}.p.*log(DATA{ff}.KLD{pp}.p./(DATA{ff}.PDF.p(:,:,tt)+exp(-100)))));
case 10
DATA{ff}.KLD{pp}.kld(tt) = nansum(nansum(DATA{ff}.KLD{pp}.p.*log10(DATA{ff}.KLD{pp}.p./(DATA{ff}.PDF.p(:,:,tt)+exp(-100)))));
end
end
% for each set of means, calcualte the KLD{pp}
DATA{ff}.KLD{pp}.mkld = zeros(length(optargs.means),1);
for mm = 1:length(optargs.means)
% get the mean pdf for these trials
allP = zeros(size(DATA{ff}.PDF.p,1),size(DATA{ff}.PDF.p,2),length(optargs.means));
for tt = 1:min(length(optargs.means{mm}),size(DATA{ff}.PDF.p,3))
allP(:,:,tt) = DATA{ff}.PDF.p(:,:,optargs.means{mm}(tt));
end
P = nanmean(allP,3);
switch optargs.logbase
case 2
DATA{ff}.KLD{pp}.mkld(mm) = nansum(nansum(DATA{ff}.KLD{pp}.p.*log2(DATA{ff}.KLD{pp}.p./(P+exp(-100)))));
case exp(1)
DATA{ff}.KLD{pp}.mkld(mm) = nansum(nansum(DATA{ff}.KLD{pp}.p.*log(DATA{ff}.KLD{pp}.p./(P+exp(-100)))));
case 10
DATA{ff}.KLD{pp}.mkld(mm) = nansum(nansum(DATA{ff}.KLD{pp}.p.*log10(DATA{ff}.KLD{pp}.p./(P+exp(-100)))));
end
end
end
else
% initialize the KLD structure
DATA{ff}.KLD.kld = zeros(size(DATA{ff}.PDF.p));
DATA{ff}.KLD.p = zeros(size(DATA{ff}.PDF.p,1),size(DATA{ff}.PDF.p,2));
% get the KDE
if optargs.bandwidth == -1
% create a KDE with an arbitrary bandwidth
tmpkde = kde(platforms(:,1:2)', [1; 1],optargs.weights);
% calculate the optimal bandwidth
DATA{ff}.KLD.kde = ksize(tmpkde);
else
% just use the specified bandwidth
DATA{ff}.KLD.kde = kde(platforms(:,1:2)', [optargs.bandwidth;optargs.bandwidth],optargs.weights);
end
% use the KDE to estimate the PDF of the platforms across the pool
P = reshape(evaluate(DATA{ff}.KLD.kde,[X(:)';Y(:)']),size(DATA{ff}.PDF.p,1),size(DATA{ff}.PDF.p,1));
% limit the data to the actual area of the pool
P(outofpool) = nan;
% renormalize P
P = P./nansum(P(:)*DATA{ff}.PDF.res^2);
% store the results
DATA{ff}.KLD.p = P;
% calculate the zone proabilities if requested
if any(optargs.zones)
DATA{ff}.KLD.zones = zeros(size(optargs.zones,1),1);
for zz = 1:size(optargs.zones,1)
DATA{ff}.KLD.zones(zz) = nansum(nansum(DATA{ff}.KLD.p.*(sqrt((optargs.zones(zz,1)-XX).^2 + (optargs.zones(zz,2)-YY).^2) < optargs.zones(zz,3))));
end
end
% for each trial, calcualte the KLD
DATA{ff}.KLD.kld = zeros(DATA{ff}.ntrials,1);
for tt = 1:DATA{ff}.ntrials
switch optargs.logbase
case 2
DATA{ff}.KLD.kld(tt) = nansum(nansum(DATA{ff}.KLD.p.*log2(DATA{ff}.KLD.p./(DATA{ff}.PDF.p(:,:,tt)+exp(-100)))));
case exp(1)
DATA{ff}.KLD.kld(tt) = nansum(nansum(DATA{ff}.KLD.p.*log(DATA{ff}.KLD.p./(DATA{ff}.PDF.p(:,:,tt)+exp(-100)))));
case 10
DATA{ff}.KLD.kld(tt) = nansum(nansum(DATA{ff}.KLD.p.*log10(DATA{ff}.KLD.p./(DATA{ff}.PDF.p(:,:,tt)+exp(-100)))));
end
end
% for each set of means, calcualte the KLD
DATA{ff}.KLD.mkld = zeros(length(optargs.means),1);
for mm = 1:length(optargs.means)
% get the mean pdf for these trials
allP = zeros(size(DATA{ff}.PDF.p,1),size(DATA{ff}.PDF.p,2),length(optargs.means));
for tt = 1:length(optargs.means{mm})
allP(:,:,tt) = DATA{ff}.PDF.p(:,:,optargs.means{mm}(tt));
end
P = nanmean(allP,3);
switch optargs.logbase
case 2
DATA{ff}.KLD.mkld(mm) = nansum(nansum(DATA{ff}.KLD.p.*log2(DATA{ff}.KLD.p./(P+exp(-100)))));
case exp(1)
DATA{ff}.KLD.mkld(mm) = nansum(nansum(DATA{ff}.KLD.p.*log(DATA{ff}.KLD.p./(P+exp(-100)))));
case 10
DATA{ff}.KLD.mkld(mm) = nansum(nansum(DATA{ff}.KLD.p.*log10(DATA{ff}.KLD.p./(P+exp(-100)))));
end
end
end
end