forked from ICB-DCM/PESTO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testGradient.m
313 lines (280 loc) · 9.53 KB
/
testGradient.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
313
function [g, g_fd_f, g_fd_b, g_fd_c] = testGradient(varargin)
% testGradient.m calculates finite difference approximations to the
% gradient to check an analytical version.
%
% backward differences: g_fd_f = (f(theta+eps*e_i) - f(theta))/eps\n
% forward differences: g_fd_b = (f(theta) - f(theta-eps*e_i))/eps\n
% central differences: g_fd_c = (f(theta+eps*e_i) - f(theta-eps*e_i))/(2*eps)\n
%
% in order to work with tensors of order n the gradient must be returned as tensor of
% order n+1 where the n+1th tensor dimension indexes the parameters with respect to which
% the differentiation was carried out
%
% USAGE:
% [...] = testGradient(theta,fun,eps,il,ig)
% [g,g_fd_f,g_fd_b,g_fd_c] = testGradient(...)
%
% Parameters:
% varargin:
% theta: parameter vector at which gradient is evaluated.
% fun: function of theta for which gradients are checked.
% eps: epsilon used for finite difference approximation of gradient (eps = 1e-4).
% il: argout index/fieldname at which function values are returned (default = 1).
% ig: argout index/fieldname at which gradient values are returned (default = 2).
%
% Return values:
% g: gradient computed by f
% g_fd_f: backward differences
% g_fd_b: forward differences
% g_fd_c: central differences
%
% History:
% * 2014/06/11 Jan Hasenauer
% * 2015/01/16 Fabian Froehlich
% * 2015/04/03 Jan Hasenauer
% * 2015/07/28 Fabian Froehlich
theta = varargin{1};
fun = varargin{2};
if nargin >= 3
eps = varargin{3};
else
eps = 1e-4;
end
if nargin >= 4
il = varargin{4};
else
il = 1;
end
if nargin >= 5
ig = varargin{5};
else
ig = 2;
end
if nargin >= 6
if(~isempty(varargin{6}))
plist = varargin{6};
else
plist = 1:length(theta);
end
else
plist = 1:length(theta);
end
if nargin >= 7
fplot = varargin{7};
else
fplot = false;
end
theta = theta(:);
if(~ischar(ig))
% Evaluation of function and gradient
if(ig<0 || round(ig)~=ig)
error('gradient argout index must be positive')
end
str_1 = '[';
ip = 1;
while true
if(~ischar(il))
if(ip==ig)
str_1 = [str_1 'g'];
elseif(ip==il)
str_1 = [str_1 'l'];
else
str_1 = [str_1 '~'];
end
if ip == max(ig,il);
eval([str_1 '] = fun(theta);']);
break;
else
str_1 = [str_1 ','];
end
ip=ip+1;
else
if(ip==ig)
str_1 = [str_1 'g'];
else
str_1 = [str_1 '~'];
end
if ip == max(ig);
eval([str_1 '] = fun(theta);']);
break;
else
str_1 = [str_1 ','];
end
ip=ip+1;
end
end
else
struct = fun(theta);
eval(['g = struct.' ig ';']);
end
sg = size(g);
if((numel(sg) == 2) && (sg(end) == 1))
g = g(plist);
else
eval(['g = g(' repmat(':,',1,numel(sg)-1) 'plist);'])
end
% Computation of finite difference gradient
g_fd_f = nan(size(g));
g_fd_b = nan(size(g));
g_fd_c = nan(size(g));
if(~ischar(il))
if(il<0)
error('function argout index must be positive')
end
if(round(il)~=il)
error('function argout index must be positive')
end
str_2 = '[';
% Evaluation of function and gradient
ip = 1;
while true
if(ip==il)
str_2 = [str_2 'l'];
else
str_2 = [str_2 '~'];
end
if ip == max(il);
break;
else
str_2 = [str_2 ','];
end
ip=ip+1;
end
else
struct = fun(theta);
eval(['l = struct.' il ';']);
end
for ip = 1:length(plist)
disp(['computing FD for parameter index ' num2str(plist(ip))])
% function evaluation
if(~ischar(il))
eval([str_2 '_i_f] = fun(theta+[zeros(plist(ip)-1,1);eps;zeros(length(theta)-plist(ip),1)]);']);
eval([str_2 '_i_b] = fun(theta-[zeros(plist(ip)-1,1);eps;zeros(length(theta)-plist(ip),1)]);']);
else
struct_i_f = fun(theta+[zeros(plist(ip)-1,1);eps;zeros(length(theta)-plist(ip),1)]);
eval(['l_i_f = struct_i_f.' il ';']);
struct_i_b = fun(theta-[zeros(plist(ip)-1,1);eps;zeros(length(theta)-plist(ip),1)]);
eval(['l_i_b = struct_i_b.' il ';']);
end
if(length(plist)==1)
% forward differences
eval(['g_fd_f(' repmat(':,',1,numel(size(g))) 'ip) = (l_i_f-l)/eps;'])
% backward differences
eval(['g_fd_b(' repmat(':,',1,numel(size(g))) 'ip) = -(l_i_b-l)/eps;'])
% central differences
eval(['g_fd_c(' repmat(':,',1,numel(size(g))) 'ip) = (l_i_f-l_i_b)/(2*eps);'])
elseif(sg(end)==1)
eval(['g_fd_f(' repmat(':,',1,numel(size(g))-2) 'ip) = (l_i_f-l)/eps;'])
% backward differences
eval(['g_fd_b(' repmat(':,',1,numel(size(g))-2) 'ip) = -(l_i_b-l)/eps;'])
% central differences
eval(['g_fd_c(' repmat(':,',1,numel(size(g))-2) 'ip) = (l_i_f-l_i_b)/(2*eps);'])
else
% forward differences
eval(['g_fd_f(' repmat(':,',1,numel(size(g))-1) 'ip) = (l_i_f-l)/eps;'])
% backward differences
eval(['g_fd_b(' repmat(':,',1,numel(size(g))-1) 'ip) = -(l_i_b-l)/eps;'])
% central differences
eval(['g_fd_c(' repmat(':,',1,numel(size(g))-1) 'ip) = (l_i_f-l_i_b)/(2*eps);'])
end
end
if(fplot)
figure
subplot(2,3,1)
error_plot(g_fd_f(:),g_fd_b(:),g_fd_c(:))
legend('FDf','FDb','Location','NorthWest')
ylabel('FDc')
xlabel('derivative value')
title('if points do not lie on diagonal, change step-size')
subplot(2,3,2)
error_plot(g_fd_f(:)-g_fd_c(:),g_fd_b(:)-g_fd_c(:),g(:)-g_fd_c(:))
legend('FDf','FDb','Location','NorthWest')
xlabel('difference to FDc')
title('if red and blue dots do not agree, change step-size')
subplot(2,3,3)
error_plot(g(:),g_fd_c(:),g(:)-g_fd_c(:))
title('if red dots lie above diagonal, check gradient implementation')
subplot(2,3,4)
ratio_plot(g_fd_f(:),g_fd_c(:),g_fd_f(:)./g_fd_c(:),g_fd_f(:)-g_fd_c(:))
legend('FDf','FDc','Location','NorthWest')
ylabel('ratio FDf/FDc')
xlabel('derivative value')
title('if points do not lie on horizontal line change step-size')
subplot(2,3,5)
ratio_plot(g_fd_f(:)-g_fd_c(:),g_fd_b(:)-g_fd_c(:),g(:)./g_fd_c(:),g_fd_f(:)-g_fd_c(:))
legend('FDf','FDb','Location','NorthWest')
xlabel('difference to FDc')
title('if red and blue dots do not agree, change step-size')
subplot(2,3,6)
ratio_plot(g(:),g_fd_c(:),g(:)./g_fd_c(:),g_fd_f(:)-g_fd_c(:))
title('if red dots do not lie on horizontal, check gradient implementation')
end
end
function error_plot(g1,g2,ee)
% Plots the differences between gradient and finite differences
%
% Parameters:
% g1: Gradient
% g2: Finite differences
% ee:
scatter(abs(g1),abs(ee),'rx')
hold on
scatter(abs(g2),abs(ee),'bo')
set(gca,'YScale','log')
set(gca,'XScale','log')
e = [abs(ee);abs(g1);abs(g2)];
mine = min(e(e>0))*0.5;
maxe = max(e(e>0))*2;
if(isempty(mine))
mine = 1e-1;
maxe = 1e0;
end
xlim([mine,maxe])
ylim([mine,maxe])
if(isempty(mine))
mine = 1e-1;
maxe = 1e0;
end
plot([mine,maxe],[mine,maxe],'k:');
legend('Gradient','FDc','Location','NorthWest')
xlabel('derivative value')
ylabel('difference |Gradient-FDc|')
axis square
box on
end
function ratio_plot(g1,g2,rr,ee)
% Plots the differences between gradient and finite differences
%
% Parameters:
% g1: Gradient
% g2: Finite differences
% rr:
% ee:
scatter(abs(g1),abs(rr),'rx')
hold on
scatter(abs(g2),abs(rr),'bo')
set(gca,'YScale','log')
set(gca,'XScale','log')
e = [abs(ee);abs(g1);abs(g2)];
mine = min(e(e>0))*0.5;
maxe = max(e(e>0))*2;
if(isempty(mine))
mine = 1e-1;
maxe = 1e0;
end
r = [abs(rr)];
minr = min(r(r>0))*0.5;
maxr = max(r(r>0))*2;
xlim([mine,maxe])
try
ylim([minr,maxr])
catch
ylim([1e-1,1e1])
end
plot([mine,maxe],[1,1],'k:');
legend('Gradient','FDc','Location','SouthEast')
xlabel('derivative value')
ylabel('ratio Gradient/FDc')
axis square
box on
end