-
Notifications
You must be signed in to change notification settings - Fork 1
/
KL_Rsquare_saberi_clean.m
272 lines (237 loc) · 11.7 KB
/
KL_Rsquare_saberi_clean.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
% This file computes some experiments and figures of Dehaene, Coen-Cagli,
% Pouget 2019
% This file computes KL divergence and R-square figures for Saberi OT data
%
% IMPORTANT: to run this file, please add to path (select both -> right click -> add to path -> selected folders and subfolders)
% the following two folders
% - maxent
% - lbfgs
clear all
FS=20; % Fontsize for figures
targetVar = true % If true, the uncertainty is defined as the posterior variance
% If false, the uncertainty is defined as the posterior
% entropy instead (as in the supplementary information)
load('saberi_data.mat')
%NBC = 4; % Number of BC levels
Kf = 1;
% We pre-allocate key quantities for memory storage
KLneulin = zeros(NBC,1);
KLneunlin = KLneulin;
seKLneulin = zeros(NBC,2);
seKLneunlin = seKLneulin;
nboot=100;
Rsquare_Rlin = zeros(NBC,nboot);
Rsquare_Rnlin = Rsquare_Rlin;
Rsquare_RnlinProd = Rsquare_Rlin;
Rsquare_gRnlin = Rsquare_Rlin;
Rsquare_widthRnlin = Rsquare_Rlin;
Rsquare_gwidthRnlin = Rsquare_Rlin;
Rsquare_decodeRlin = Rsquare_Rlin;
Rsquare_decodeRnlin = Rsquare_Rlin;
%%%%%
% Main computational loop
% For every BC level, we compute a bunch of things
% loop over BC level
for s=1:NBC
load('saberi_data.mat')
Kf = 1;
Rnlin = Rlin;
BAllnlin = BAlllin;
itdSubset = 5:7; % subselect the ITDs
KitdSubset = size(itdSubset,2);
LL = LL(:,:,itdSubset,:); % log-likelihood
LLneulin = LL; % pre-allocate memory
LLneunlin = LL; % pre-allocate memory
Rlin = Rlin (s,:,itdSubset,:);
Rnlin = Rnlin(s,:,itdSubset,:);
% approximate decoding of the neuronal activity
% the weights are stored into the BAlllin variable and have been
% computed earlier
X = reshape(Rlin,[Kf*Kt1 KitdSubset*N])'; % removed inddt2
LLneulin(s,:,:,:) = reshape( energy(BAlllin,X)', [Kt1, KitdSubset, N]);
X = reshape(Rnlin,[Kf*Kt1 KitdSubset*N])'; % removed inddt2
LLneunlin(s,:,:,:) = reshape( energy(BAllnlin,X)', [Kt1, KitdSubset, N]);
% extract test set
% train set was used to construct the decoding weights earlier
LL = squeeze(LL(s,:,:,indtest));
LLneulin = squeeze(LLneulin (s,:,:,indtest));
LLneunlin = squeeze(LLneunlin(s,:,:,indtest));
Rlin = Rlin(:,:,:,indtest);
Rnlin = Rnlin(:,:,:,indtest);
% select all trials
indtri = 1:size(indtest,2);
NT = size(indtri,2);
%%% compute full posterior (w unif prior), per trial
tmp = nanmax(LL(:,indtri),[],1); % avoid infinities in the likelihood function
LL_norm = bsxfun(@minus,LL(:,indtri),tmp);
tmp = nanmax(LLneulin(:,indtri),[],1); % avoid infinities in the likelihood function
LLneulin_norm = bsxfun(@minus,LLneulin(:,indtri),tmp);
tmp = nanmax(LLneunlin(:,indtri),[],1); % avoid infinities in the likelihood function
LLneunlin_norm = bsxfun(@minus,LLneunlin(:,indtri),tmp);
% normalized posterior distributions
L_norm = bsxfun(@rdivide,exp(LL_norm),nansum(exp(LL_norm),1)); % compute likelihood
Lneulin_norm = bsxfun(@rdivide,exp(LLneulin_norm),nansum(exp(LLneulin_norm),1));
Lneunlin_norm = bsxfun(@rdivide,exp(LLneunlin_norm),nansum(exp(LLneunlin_norm),1));
%%% compute KL divergence between true and reconstructed posterior
NDT2 = size(L_norm,1);
doeps=1;
tDenom = diag(L_norm'*(log(L_norm+eps*doeps)-log(ones(NDT2,NT)/NDT2)));
tKLneulin = diag(L_norm'*(log(L_norm+eps*doeps)-log(Lneulin_norm+eps*doeps))) ./ tDenom;
tKLneunlin = diag(L_norm'*(log(L_norm+eps*doeps)-log(Lneunlin_norm+eps*doeps))) ./ tDenom;
KLneulin(s) = nanmedian(tKLneulin);
KLneunlin(s) = nanmedian(tKLneunlin);
nboot1=1000;
seKLneulin(s,:) = bootci(nboot1,{@nanmedian,tKLneulin},'type','bca');
seKLneunlin(s,:) = bootci(nboot1,{@nanmedian,tKLneunlin},'type','bca');
%%% compute posterior width
x=deltaTrue(1:end);
muL = x*L_norm;
varL = sum(L_norm.*(repmat(x',1,size(L_norm,2))-repmat(muL,numel(x),1)).^2);
ivarL = 1./varL;
muL = x*Lneulin_norm;
varL = sum(Lneulin_norm.*(repmat(x',1,size(Lneulin_norm,2))-repmat(muL,numel(x),1)).^2);
ivarLneulin = 1./varL;
muL = x*Lneunlin_norm;
varL = sum(Lneunlin_norm.*(repmat(x',1,size(Lneunlin_norm,2))-repmat(muL,numel(x),1)).^2);
ivarLneunlin = 1./varL;
%%% compute entropy
entropyL = - diag( L_norm' * log(L_norm) ); % the diagonal of U^T U corresponds to a sum
entropyLneulin = - diag( Lneulin_norm' * log(Lneulin_norm) );
entropyLneunlin = - diag( Lneunlin_norm' * log(Lneunlin_norm) );
%%% extract features from population activity (width, gain) to correlate with uncertainty; also, single neuron tuning curve width and gain
tRlin = reshape( Rlin (:,:,:,indtri), [Kf, Kt1, KitdSubset * NT] ); % removed inddt2
tRnlin = reshape( Rnlin(:,:,:,indtri), [Kf, Kt1, KitdSubset * NT] ); % removed inddt2
muRlin = zeros(Kf,KitdSubset*NT);
widthRlin = zeros(Kf,KitdSubset*NT);
muRnlin = zeros(Kf,KitdSubset*NT);
widthRnlin = zeros(Kf,KitdSubset*NT);
gwidthRnlin = zeros(Kf,KitdSubset*NT);
gRnlin = zeros(Kf,KitdSubset*NT);
for f=1:Kf
epsilon = 0.01;
%%% population, per trial
tmp = squeeze(tRlin(f,:,:) + epsilon);
tmp = bsxfun(@minus,tmp,nanmin(tmp,[],1));
tmpnorm = bsxfun(@rdivide,tmp,nansum(tmp,1));
muRlin(f,:) = x*tmpnorm;
widthRlin(f,:) = sum(tmpnorm.*bsxfun(@minus,x',muRlin(f,:)).^2);
tmp = squeeze(tRnlin(f,:,:) + epsilon);
gRnlin(f,:) = nansum(tmp);
tmpnorm = bsxfun(@rdivide,tmp,nansum(tmp,1));
muRnlin(f,:) = x*tmpnorm;
widthRnlin(f,:) = sum(tmpnorm.*bsxfun(@minus,x',muRnlin(f,:)).^2);
gwidthRnlin(f,:) = gRnlin(f,:)./widthRnlin(f,:);
end
%%% population, per trial
tmp = squeeze(sum(tRlin,1));
tmp = bsxfun(@minus,tmp,nanmin(tmp,[],1));
tmpnorm = bsxfun(@rdivide,tmp,nansum(tmp,1));
mutmp = x*tmpnorm;
widthRlinSum = sum(tmpnorm.*bsxfun(@minus,x',mutmp).^2);
tmp = squeeze(sum(tRnlin,1));
gRnlinSum = sum(tmp);
tmp = bsxfun(@minus,tmp,nanmin(tmp,[],1));
tmpnorm = bsxfun(@rdivide,tmp,nansum(tmp,1));
mutmp = x*tmpnorm;
widthRnlinSum = sum(tmpnorm.*bsxfun(@minus,x',mutmp).^2);
%%% Define the target of the regression
if targetVar
%%% Target log of certainty
ivarL = log(ivarL);
ivarLneulin = log(ivarLneulin);
ivarLneunlin = log(ivarLneunlin);
else
%%% Target the log entropy
ivarL = log(entropyL)';
ivarLneulin = log(entropyLneulin)';
ivarLneunlin = log(entropyLneunlin)';
end
%%% take log of inverse of population width
widthRlin = log(1./widthRlin);
widthRnlin = log(1./widthRnlin);
%%% take log of population gain
gRnlin = log(gRnlin);
gwidthRnlin = log(gwidthRnlin);
%%% compute R-square of true certainty vs reconstruction from poulation features (ENCODING)
NTT = NT;
NTtr = round(4*NTT/5);
NTte = numel(NTtr+1:NTT);
for k=1:nboot
indperm = randperm(NT);
indtr = indperm(1:NTtr);
indte = indperm((1+NTtr):end);
tmp=reshape(tRlin(:,:,indtr),Kf*Kt1,NTtr); %*** LINEAR
tmp1=reshape(tRlin(:,:,indte),Kf*Kt1,NTte);
B = regress(ivarL(indtr)',[tmp' ones(NTtr,1)]);
Rsquare_Rlin(s,k) = (1-sum((ivarL(indte)'-[tmp1' ones(NTte,1)]*B).^2)/sum((ivarL(indte)-mean(ivarL(indte))).^2));
tmp=reshape(tRnlin(:,:,indtr),Kf*Kt1,NTtr); %*** LRTSUM
tmp1=reshape(tRnlin(:,:,indte),Kf*Kt1,NTte);
B = regress(ivarL(indtr)',[tmp' ones(NTtr,1)]);
Rsquare_Rnlin(s,k) = (1-sum((ivarL(indte)'-[tmp1' ones(NTte,1)]*B).^2)/sum((ivarL(indte)-mean(ivarL(indte))).^2));
tmp1=reshape(tRnlin(:,:,indtr),Kf*Kt1,NTtr); %*** LRTSUM, product of activities
Kneu = Kf*Kt1;
Kneusq = (Kneu^2+Kneu)/2;
tmp = NaN(Kneusq,NTtr);
for nt = 1:NTtr
tmpmat = tmp1(:,nt)*tmp1(:,nt)';
tmp(:,nt) = tmpmat(logical(tril(ones(Kneu))));
end
tmp1=reshape(tRnlin(:,:,indte),Kf*Kt1,NTte);
testRnlinProd = NaN(Kneusq,NTte);
for nt = 1:NTte
tmpmat = tmp1(:,nt)*tmp1(:,nt)';
testRnlinProd(:,nt) = tmpmat(logical(tril(ones(Kneu))));
end
if NTtr<=Kneusq
indsub = randperm(Kneusq);
tmp = tmp(indsub(1:round(NTtr/2)),:);
testRnlinProd = testRnlinProd(indsub(1:round(NTtr/2)),:);
end
B = regress(ivarL(indtr)',[tmp' ones(NTtr,1)]);
Rsquare_RnlinProd(s,k) = (1-sum((ivarL(indte)'-[testRnlinProd' ones(NTte,1)]*B).^2)/sum((ivarL(indte)-mean(ivarL(indte))).^2));
tmp=gRnlin(:,indtr);
tmp1=gRnlin(:,indte);
B = regress(ivarL(indtr)',[tmp' ones(NTtr,1)]);
Rsquare_gRnlin(s,k) = (1-sum((ivarL(indte)'-[tmp1' ones(NTte,1)]*B).^2)/sum((ivarL(indte)-mean(ivarL(indte))).^2));
tmp=widthRnlin(:,indtr);
tmp1=widthRnlin(:,indte);
B = regress(ivarL(indtr)',[tmp' ones(NTtr,1)]);
Rsquare_widthRnlin(s,k) = (1-sum((ivarL(indte)'-[tmp1' ones(NTte,1)]*B).^2)/sum((ivarL(indte)-mean(ivarL(indte))).^2));
tmp=gwidthRnlin(:,indtr);
tmp1=gwidthRnlin(:,indte);
B = regress(ivarL(indtr)',[tmp' ones(NTtr,1)]);
Rsquare_gwidthRnlin(s,k) = (1-sum((ivarL(indte)'-[tmp1' ones(NTte,1)]*B).^2)/sum((ivarL(indte)-mean(ivarL(indte))).^2));
Rsquare_decodeRlin(s,k) = (1-sum((ivarL(indte)-ivarLneulin(indte)).^2)/sum((ivarL(indte)-mean(ivarL(indte))).^2));
Rsquare_decodeRnlin(s,k) = (1-sum((ivarL(indte)-ivarLneunlin(indte)).^2)/sum((ivarL(indte)-mean(ivarL(indte))).^2));
end
end
%%%%%
% All computations are complete: now we plot the figures
%myerrorbar = @(x,y,z,a,b) errorbar(x,y,y - z(:,1),z(:,2)-y);
%%% plot KL vs BC
figure;
hold on; axis square;
myerrorbar(BinCorLabel',KLneulin,seKLneulin,[.9 .9 .9],1);
plot(BinCorLabel',KLneulin,'--k');
plot(BinCorLabel',KLneunlin,'-k');
xlabel('BC','FontSize',FS)
ylabel('KL','FontSize',FS)
set(gca,'YLim',[0 1],'YTick',[0:.1:1],'YTickLabel',100*[0:.1:1])
%set(gca,'YLim',[0 .05],'YTick',[0:.01:0.1],'YTickLabel',100*[0:.01:0.1])
%%% plot Rsquare vs BC
figure;
alpha=0.01;
nboot2=1000;
shade=1;
hold on; axis square;
% myerrorbar(BinCorLabel,nanmedian(Rsquare_decodeRlin'),bootci(nboot2,{@nanmedian,Rsquare_decodeRlin'},'alpha',alpha)','k',shade);
myerrorbar(BinCorLabel',nanmedian(Rsquare_RnlinProd')', bootci(nboot2,{@nanmedian,Rsquare_RnlinProd'}, 'alpha',alpha)','m',shade);
myerrorbar(BinCorLabel',nanmedian(Rsquare_gwidthRnlin')', bootci(nboot2,{@nanmedian,Rsquare_gwidthRnlin'}, 'alpha',alpha)','y',shade);
myerrorbar(BinCorLabel',nanmedian(Rsquare_Rnlin')', bootci(nboot2,{@nanmedian,Rsquare_Rnlin'}, 'alpha',alpha)','b',shade);
myerrorbar(BinCorLabel',nanmedian(Rsquare_gRnlin')', bootci(nboot2,{@nanmedian,Rsquare_gRnlin'}, 'alpha',alpha)','r',shade);
myerrorbar(BinCorLabel',nanmedian(Rsquare_widthRnlin')', bootci(nboot2,{@nanmedian,Rsquare_widthRnlin'}, 'alpha',alpha)','g',shade);
myerrorbar(BinCorLabel',nanmedian(Rsquare_decodeRnlin')', bootci(nboot2,{@nanmedian,Rsquare_decodeRnlin'}, 'alpha',alpha)','k',shade);
legend({'Generalized Gain + Product (??)','Gain / Width Ratio (??)','Generalized Gain (??)','Gain (??)','Width (??)','Decoding (??)',})
xlabel('BC','FontSize',FS)
ylabel('Rsquare(certainty)')
set(gca,'YLim',[0 1], 'XLim', [0,1],'YTick',[0:.2:1])