-
Notifications
You must be signed in to change notification settings - Fork 0
/
FitLFPRetinoModel_LM.m
271 lines (223 loc) · 11 KB
/
FitLFPRetinoModel_LM.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
function [finalParameters,fisherInfo,ninetyfiveErrors,conclusion,Deviance,chi2p] = FitLFPRetinoModel_LM(Response,xaxis,yaxis,numRepeats)
%FitLFPRetinoModel_LM.m
% Use data from LFP retinotopic mapping experiment to fit a non-linear
% model of that retinotopy (data is maximum LFP magnitude in window
% from 150 to 250msec minus minimum magnitude in window from 50 to
% 120 msec after stimulus presentation, assumes a Gaussian likelihood)
%
% in this case, maximizing the likelihood is equivalent to minimizing the
% sum of squared residuals
% Levenberg-Marquardt algorithm ... far superior to gradient ascent for
% this data (faster and more reliable)
%Created: 2017/02/16, 24 Cummington Mall, Boston
% Byron Price
%Updated: 2017/03/10
% By: Byron Price
% model has 8 parameters, defined by vector p
% data ~ N(mu,sigma),
% where mu = (p(1)*exp(-(xpos-p(2)).^2./(2*p(4)*p(4))-(ypos-p(3)).^2./(2*p(5)*p(5))-
% p(6)*(xpos-p(2))*(ypos-p(3))/(2*p(4)*p(5)))+p(7));
% and sigma = p(8)
% parameter estimates are constrained to a reasonable range of values
Bounds = [0,800;min(xaxis)-50,max(xaxis)+50;min(yaxis)-50,max(yaxis)+50;10,800;10,800;0,1000;1,1000];
numChans = size(Response,1);
conclusion = zeros(numChans,1);
Deviance = zeros(numChans,1);
chi2p = zeros(numChans,1);
numParameters = length(Bounds);
finalParameters = zeros(numChans,numParameters);
fisherInfo = zeros(numChans,numParameters,numParameters);
ninetyfiveErrors = zeros(numChans,numParameters);
maxITER = 500;
likelyTolerance = 1e-3;
gradientTolerance = 1e-5;
for zz=1:numChans
% display(sprintf('Running Data for Channel %d...',zz));
Data = Response{zz};
reps = size(Data,1);
flashPoints = Data(:,1:2);
vepMagnitude = Data(:,3);
h = ones(numParameters,1)./100;
bigParameterVec = zeros(numParameters-1,numRepeats);
bigResiduals = zeros(numRepeats,1);
% repeat gradient ascent from a number of different starting
% positions
parfor repeats = 1:numRepeats
parameterVec = zeros(numParameters-1,maxITER);
squaredResiduals = zeros(maxITER,1);
% these initialization parameters were estimated from previous data
proposal = [2.5,78;5,200;2.6,152;6.7,38.6;5.9,40.3;8.6,43.1;9.2,19];
for ii=1:numParameters-2
parameterVec(ii,1) = gamrnd(proposal(ii,1),proposal(ii,2));
end
% parameterVec(6,1) = normrnd(proposal(6,1),proposal(6,2));
parameterVec(6,1) = mean(vepMagnitude)+normrnd(0,50);
parameterVec(:,1) = max(Bounds(1:numParameters-1,1),min(parameterVec(:,1),Bounds(1:numParameters-1,2)));
[yhat] = Getyhat(reps,parameterVec(:,1),flashPoints);
squaredResiduals(1) = sum((vepMagnitude-yhat).^2);
check = 1;
iter = 1;
lambda = 100;update = ones(numParameters,1);
% for each starting position, do maxITER iterations
while abs(check) > likelyTolerance && iter < maxITER && sum(abs(update)) > gradientTolerance
[Jacobian] = GetJacobian(reps,parameterVec(:,iter),flashPoints,numParameters-1,h,yhat);
H = Jacobian'*Jacobian;
update = pinv(H+lambda.*diag(diag(H)))*Jacobian'*(vepMagnitude-yhat);
tempParams = parameterVec(:,iter)+update;
tempParams = max(Bounds(1:numParameters-1,1),min(tempParams,Bounds(1:numParameters-1,2)));
[tempYhat] = Getyhat(reps,tempParams,flashPoints);
squaredResiduals(iter+1) = sum((vepMagnitude-tempYhat).^2);
check = diff(squaredResiduals(iter:iter+1));
if check >= 0
parameterVec(:,iter+1) = parameterVec(:,iter);
lambda = min(lambda*10,1e10);
check = 1;
squaredResiduals(iter+1) = squaredResiduals(iter);
else
parameterVec(:,iter+1) = tempParams;
yhat = tempYhat;
lambda = max(lambda/10,1e-10);
end
iter = iter+1;
end
[bigResiduals(repeats),index] = min(squaredResiduals(1:iter));
bigParameterVec(:,repeats) = parameterVec(:,index);
end
[~,index] = min(bigResiduals);
finalParameters(zz,1:numParameters-1) = bigParameterVec(:,index)';
[yhat] = Getyhat(reps,finalParameters(zz,1:numParameters-1),flashPoints);
finalParameters(zz,numParameters) = std(vepMagnitude-yhat);
parameterVec = zeros(maxITER,numParameters);gradientVec = zeros(1,numParameters);
logLikelihood = zeros(maxITER,1);
parameterVec(1,:) = finalParameters(zz,:);
[logLikelihood(1)] = GetLikelihood(reps,parameterVec(1,:),vepMagnitude,flashPoints);
check = 1;iter = 1;lambda = 10;
while abs(check) > likelyTolerance && iter < maxITER*2
for jj=1:numParameters
tempParameterVec = parameterVec(iter,:);
tempParameterVec(jj) = tempParameterVec(jj)+h(jj);
[gradLikelihoodplus] = GetLikelihood(reps,tempParameterVec,vepMagnitude,flashPoints);
gradientVec(jj) = (gradLikelihoodplus-logLikelihood(iter))./h(jj);
end
tempParams = parameterVec(iter,:)+lambda*gradientVec;
tempParams = max(Bounds(:,1)',min(tempParams,Bounds(:,2)'));
tempLikelihood = GetLikelihood(reps,tempParams,vepMagnitude,flashPoints);
check = tempLikelihood-logLikelihood(iter);
if check <= 0
lambda = lambda/10;
parameterVec(iter+1,:) = parameterVec(iter,:);
logLikelihood(iter+1) = logLikelihood(iter);
else
parameterVec(iter+1,:) = tempParams;
logLikelihood(iter+1) = tempLikelihood;
end
iter = iter+1;
end
[~,index] = max(logLikelihood(1:iter));
finalParameters(zz,:) = parameterVec(index,:);
[fisherInfo(zz,:,:),ninetyfiveErrors(zz,:)] = getFisherInfo(finalParameters(zz,:),numParameters,h,reps,vepMagnitude,flashPoints);
Deviance(zz) = sum((Getyhat(reps,finalParameters(zz,:),flashPoints)-vepMagnitude).^2)/(finalParameters(zz,end).^2);
chi2p(zz) = 1-chi2cdf(Deviance(zz),reps-numParameters);
totalError = sum(ninetyfiveErrors(zz,:));
test = finalParameters(zz,:)-ninetyfiveErrors(zz,:);
test2 = finalParameters(zz,:)'-Bounds;
test2([2,3,6],:) = [];
check = sum(sum(test2==0));
if totalError > 2000 || test(1) < 0 || check > 0
conclusion(zz) = 0;
else
conclusion(zz) = 1;
end
display(zz);
display(conclusion(zz));
display(chi2p(zz));
display(finalParameters(zz,:));
display(ninetyfiveErrors(zz,:));
end
end
function [Jacobian] = GetJacobian(reps,parameterVec,flashPoints,numParameters,h,yhat)
Jacobian = zeros(reps,numParameters);
for kk=1:reps
for jj=1:numParameters
tempParams = parameterVec;tempParams(jj) = tempParams(jj)+h(jj);
mu = tempParams(1)*exp(-((flashPoints(kk,1)-tempParams(2)).^2)./(2*tempParams(4).^2)-...
((flashPoints(kk,2)-tempParams(3)).^2)./(2*tempParams(5).^2))...
+tempParams(6);
% tempParams(6)*(flashPoints(kk,1)-tempParams(2))*(flashPoints(kk,2)-tempParams(3))/(2*tempParams(4)*tempParams(5)))...
Jacobian(kk,jj) = (mu-yhat(kk))/h(jj);
end
end
end
function [yhat] = Getyhat(reps,parameterVec,flashPoints)
yhat = zeros(reps,1);
for kk=1:reps
yhat(kk) = parameterVec(1)*exp(-((flashPoints(kk,1)-parameterVec(2)).^2)./(2*parameterVec(4).^2)-...
((flashPoints(kk,2)-parameterVec(3)).^2)./(2*parameterVec(5).^2))...
+parameterVec(6);
% parameterVec(6)*(flashPoints(kk,1)-parameterVec(2))*(flashPoints(kk,2)-parameterVec(3))/(2*parameterVec(4)*parameterVec(5)))...
end
end
function [loglikelihood] = GetLikelihood(reps,parameterVec,vepMagnitude,flashPoints)
loglikelihood = 0;
for kk=1:reps
mu = parameterVec(1)*exp(-((flashPoints(kk,1)-parameterVec(2)).^2)./(2*parameterVec(4).^2)-...
((flashPoints(kk,2)-parameterVec(3)).^2)./(2*parameterVec(5).^2))...
+parameterVec(6);
% parameterVec(6)*(flashPoints(kk,1)-parameterVec(2))*(flashPoints(kk,2)-parameterVec(3))/(2*parameterVec(4)*parameterVec(5)))...
stdev = parameterVec(7);
loglikelihood = loglikelihood-(1/2)*log(2*pi*stdev*stdev)-(1/(2*stdev*stdev))*(vepMagnitude(kk)-mu).^2;
% summation = summation+(peakNegativity(kk)-mu).^2;
end
% loglikelihood = (-reps/2)*log(2*pi*parameterVec(6)*parameterVec(6))-...
% (1/(2*parameterVec(6)*parameterVec(6)))*summation;
end
function [fisherInfo,errors] = getFisherInfo(parameters,numParameters,h,reps,vepMagnitude,flashPoints)
fisherInfo = zeros(numParameters,numParameters);
errors = zeros(1,numParameters);
% h = h./100;
% the observed fisher information matrix
for jj=1:numParameters
for kk=jj:numParameters
firstParam = jj;secondParam = kk;
deltaX = h(firstParam);deltaY = h(secondParam);
if firstParam ~= secondParam
parameterVec = parameters;parameterVec(firstParam) = parameterVec(firstParam)+deltaX;
parameterVec(secondParam) = parameterVec(secondParam)+deltaY;
likelyplusplus = GetLikelihood(reps,parameterVec,vepMagnitude,flashPoints);
parameterVec = parameters;parameterVec(firstParam) = parameterVec(firstParam)+deltaX;
parameterVec(secondParam) = parameterVec(secondParam)-deltaY;
likelyplusminus = GetLikelihood(reps,parameterVec,vepMagnitude,flashPoints);
parameterVec = parameters;parameterVec(firstParam) = parameterVec(firstParam)-deltaX;
parameterVec(secondParam) = parameterVec(secondParam)+deltaY;
likelyminusplus = GetLikelihood(reps,parameterVec,vepMagnitude,flashPoints);
parameterVec = parameters;parameterVec(firstParam) = parameterVec(firstParam)-deltaX;
parameterVec(secondParam) = parameterVec(secondParam)-deltaY;
likelyminusminus = GetLikelihood(reps,parameterVec,vepMagnitude,flashPoints);
fisherInfo(jj,kk) = -(likelyplusplus-likelyplusminus-likelyminusplus+likelyminusminus)./(4*deltaX*deltaY);
else
likely = GetLikelihood(reps,parameters,vepMagnitude,flashPoints);
parameterVec = parameters;parameterVec(firstParam) = parameterVec(firstParam)-deltaX;
likelyminus = GetLikelihood(reps,parameterVec,vepMagnitude,flashPoints);
parameterVec = parameters;parameterVec(firstParam) = parameterVec(firstParam)+deltaX;
likelyplus = GetLikelihood(reps,parameterVec,vepMagnitude,flashPoints);
fisherInfo(jj,kk) = -(likelyminus-2*likely+likelyplus)./(deltaX*deltaX);
end
end
end
transpose = fisherInfo';
for ii=1:numParameters
for jj=1:ii-1
fisherInfo(ii,jj) = transpose(ii,jj);
end
end
inverseFisherInfo = inv(fisherInfo);
for ii=1:numParameters
errors(ii) = sqrt(inverseFisherInfo(ii,ii));
end
if isreal(errors) == 0
temp = sqrt(errors.*conj(errors));
errors = 1.96.*temp;
elseif isreal(errors) == 1
errors = 1.96.*errors;
end
end