forked from lawrennd/gpsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpnddisimLogLikeGradients.m
455 lines (399 loc) · 15.2 KB
/
gpnddisimLogLikeGradients.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
function g = gpnddisimLogLikeGradients(model,varargin)
% GPNDDISIMLOGLIKEGRADIENTS Compute the gradients of the log likelihood of a GPNDDISIM model.
% FORMAT
% DESC computes the gradients of the log likelihood of the given
% Gaussian process for use in a single input motif protein network.
% ARG model : the model for which the log likelihood is computed.
% RETURN g : the gradients of the parameters of the model.
%
% SEEALSO : gpsimCreate, gpsimLogLikelihood, gpsimGradient
%
% COPYRIGHT : Neil D. Lawrence, 2006
%
% COPYRIGHT : Antti Honkela, 2007
%
% COPYRIGHT : Jaakko Peltonen, 2011
% GPSIM
if length(varargin)==2,
update_kernel=varargin{1};
update_mean=varargin{2};
else
update_kernel=1;
update_mean=1;
end;
covGrad = -model.invK + model.invK*model.m*model.m'*model.invK;
covGrad = 0.5*covGrad;
if (update_kernel==1),
if isfield(model, 'proteinPrior') && ~isempty(model.proteinPrior)
g = kernGradient(model.kern, model.timesCell, covGrad);
else
g = kernGradient(model.kern, model.t, covGrad);
end
% fprintf('gpdisimLogLikeGradients: g before any priors\n');
% g'
% pause
% In case we need priors in.
% Add contribution of any priors
if isfield(model, 'bprior'),
g = g + kernPriorGradient(model.kern);
end
else
g = zeros(1,model.kern.nParams);
end;
if (update_mean==1),
gmuFull = model.m'*model.invK;
% if isfield(model, 'proteinPrior') && ~isempty(model.proteinPrior)
% if model.includeNoise
% ind = model.kern.comp{1}.diagBlockDim{1} + (1:model.kern.comp{1}.diagBlockDim{2});
% gmu = zeros(size(1, model.numGenes));
% for i = 1:model.numGenes
% gmu(i) = sum(gmuFull(ind));
% ind = ind + model.kern.comp{1}.diagBlockDim{i+1};
% end
% else
% ind = model.kern.diagBlockDim{1} + (1:model.kern.diagBlockDim{2});
% gmu = zeros(size(1, model.numGenes));
% for i = 1:model.numGenes
% gmu(i) = sum(gmuFull(ind));
% ind = ind + model.kern.diagBlockDim{i+1};
% end
% end
% else
if (model.numGenes>0) && (model.use_disimstartmean==1),
%--------------------------------
% Version with NDDISIM start mean
%--------------------------------
% PART1 Compute gradient for basal transcription rates
gb=zeros(1, model.numGenes);
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{1}; end;
indStart=length(tempt)+1;
for k=1:model.numGenes,
% note that delay in the DISIM model does not affect the
% contribution of the basal transcription, thus delays are
% not applied to model.t here.
if iscell(model.t)==0, delayedt=model.t; else delayedt=model.t{k+1}; end;
% indStart=length(model.t)*k + 1;
% indEnd=indStart+length(model.t)-1;
indEnd=indStart+length(delayedt)-1;
%size(delayedt)
%indStart
%indEnd
if size(delayedt,1)>0,
gb(k)=gmuFull(indStart:indEnd)*((1-exp(-model.D(k)*delayedt))/model.D(k));
else
gb(k)=0;
end;
indStart=indStart+size(delayedt,1);
end;
% Add contribution of prior on B if it exists.
if isfield(model, 'bprior');
if model.numGenes>0,
gb = gb + priorGradient(model.bprior, model.B);
end;
end
% Multiply by factors from parameter transformations
if model.numGenes>0,
for k=1:length(gb),
gb(k) = gb(k)*doTransform(model.B(k), 'gradfact', model.bTransform(k));
end;
end;
% PART2 Compute gradient for DISIM start mean
gdisimstartmean=zeros(1, model.numGenes);
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{1}; end;
timeshift = min(tempt);
indStart=length(tempt)+1;
for k=1:model.numGenes,
% note that delay in the DISIM model does not affect the
% decay of the starting RNA concentration, thus delays are
% not applied to model.t here.
if iscell(model.t)==0, delayedt=model.t; else delayedt=model.t{k+1}; end;
%indStart=length(model.t)*k + 1;
%indEnd=indStart+length(model.t)-1;
indEnd=indStart+size(delayedt,1)-1;
if size(delayedt,1)>0,
gdisimstartmean(k)=gmuFull(indStart:indEnd)* ...
exp(-model.D(k)*(delayedt - timeshift));
else
gdisimstartmean(k)=0;
end;
indStart=indStart+size(delayedt,1);
end;
% Add contribution of prior on disimStartMean if it exists.
if isfield(model, 'disimStartMeanPrior');
if model.numGenes>0,
gdisimstartmean = gdisimstartmean + ...
priorGradient(model.disimStartMeanPrior, model.disimStartMean);
end;
end
% Multiply by factors from parameter transformations
if model.numGenes>0,
for k=1:length(gdisimstartmean),
gdisimstartmean(k) = gdisimstartmean(k)*doTransform(model.disimStartMean(k), 'gradfact', model.disimStartMeanTransform(k));
end;
end;
% PART3 Compute gradient for DISIM-level decays
gd=zeros(1, model.numGenes);
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{1}; end;
timeshift = min(tempt);
indStart=length(tempt)+1;
for k=1:model.numGenes,
% note that delay in the DISIM model does not affect the
% decay of the starting RNA concentration, or the
% contribution of the basal transcription rate, thus delays are
% not applied to model.t for those parts. However, delays do
% affect the contribution of the POL2 mean ("simMean"), so
% delays must be applied when computing the gradient with
% respect to decay for that part of the mean function.
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{k+1}; end;
delayedt=tempt-model.delay(k);
I=find(delayedt<0);
delayedt(I)=0;
indEnd=indStart+size(delayedt,1)-1;
if size(delayedt,1)>0,
gd(k)=gmuFull(indStart:indEnd)*...
( -model.B(k)/(model.D(k)*model.D(k))*(1-exp(-model.D(k)*tempt)) ...
+model.B(k)/model.D(k)*exp(-model.D(k)*tempt).*tempt ...
+model.disimStartMean(k)* ...
exp(-model.D(k)*(tempt - timeshift)).*(-tempt + timeshift) ...
-model.simMean*model.S(k)/(model.D(k)*model.D(k))*(1-exp(-model.D(k)*delayedt)) ...
+model.simMean*model.S(k)/model.D(k)*exp(-model.D(k)*delayedt).*delayedt ...
);
else
gd(k)=0;
end;
indStart=indStart+size(delayedt,1);
end;
% Apply factors from transformations, and add gradient of the
% decays to the main decay-gradient from the kernel,
if model.numGenes>0,
decayIndices = model.disimdecayindices;
for k=1:length(decayIndices),
g(decayIndices(k)) = g(decayIndices(k)) ...
+ gd(k)*doTransform(model.D(k), 'gradfact',model.disimdecaytransformation(k));
end;
end;
% PART4 Compute gradient for SIM-level mean
gsimmean=zeros(1, 1);
indStart=1;
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{1}; end;
indEnd=indStart+length(tempt)-1;
if size(tempt,1)>0,
gsimmean=gmuFull(indStart:indEnd)*(ones(indEnd-indStart+1,1));
else
gsimmean=0;
end;
indStart=indStart+length(tempt);
for k=1:model.numGenes,
% note that delay in the DISIM model affects the contribution
% of the POL2 mean ("simMean"), so delays must be applied to time
% points when computing the gradient with respect to simMean.
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{k+1}; end;
delayedt=tempt-model.delay(k);
I=find(delayedt<0);
delayedt(I)=0;
%indStart=length(model.t)*k + 1;
%indEnd=indStart+length(model.t)-1;
indEnd=indStart+size(delayedt,1)-1;
if size(delayedt,1)>0,
gsimmean=gsimmean+gmuFull(indStart:indEnd)*((1-exp(-model.D(k)*delayedt))*model.S(k)/model.D(k));
else
gsimmean=gsimmean+0;
end;
indStart=indStart+size(delayedt,1);
end;
% Add contribution of prior on simMean if it exists.
if isfield(model, 'simMeanPrior');
if model.numGenes>0,
gsimmean = gsimmean + ...
priorGradient(model.simMeanPrior, model.simMean);
end;
end
% Multiply by factors from parameter transformations
gsimmean = gsimmean*doTransform(model.simMean, 'gradfact', model.simMeanTransform);
% PART5 Compute gradient for DISIM-level variance
gdisimvar=zeros(1, model.numGenes);
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{1}; end;
indStart=length(tempt)+1;
for k=1:model.numGenes,
% note that delay in the DISIM model affects the contribution
% of the POL2 mean ("simMean"), so delays must be applied to time
% points when computing the gradient with respect to DISIM variance,
% for the part of the mean function related to simMean.
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{k+1}; end;
delayedt=tempt-model.delay(k);
I=find(delayedt<0);
delayedt(I)=0;
% indStart=length(model.t)*k + 1;
% indEnd=indStart+length(model.t)-1;
indEnd=indStart+size(delayedt,1)-1;
%size(gmuFull)
%indStart
%indEnd
%size(delayedt)
if size(delayedt,1)>0,
gdisimvar(k)=gmuFull(indStart:indEnd)*...
((1-exp(-model.D(k)*delayedt))*model.simMean/model.D(k)*0.5/model.S(k));
else
gdisimvar(k)=0;
end;
indStart=indStart+size(delayedt,1);
end;
% Multiply by factors from parameter transformations, and add gradient of the
% DISIM-variances to the main DISIM-variance gradient from the kernel
if model.numGenes>0,
disimvarIndices = model.disimvarianceindices;
for k=1:length(disimvarIndices),
g(disimvarIndices(k)) = g(disimvarIndices(k)) ...
+ gdisimvar(k)*doTransform(model.S(k)*model.S(k), 'gradfact',model.disimvariancetransformation(k));
end;
end;
% PART6 Compute gradient for DISIM-level delay
gdisimdelay=zeros(1, model.numGenes);
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{1}; end;
indStart=length(tempt)+1;
for k=1:model.numGenes,
% note that delay in the DISIM model affects the contribution
% of the POL2 mean ("simMean"), so delays must be applied to time
% points when computing the gradient with respect to DISIM variance,
% for the part of the mean function related to simMean.
if iscell(model.t)==0, tempt=model.t; else tempt=model.t{k+1}; end;
delayedt=tempt-model.delay(k);
I=find(delayedt<0);
delayedt(I)=0;
% indStart=length(model.t)*k + 1;
% indEnd=indStart+length(model.t)-1;
indEnd=indStart+size(delayedt,1)-1;
if size(delayedt,1)>0,
gdisimdelay(k)=gmuFull(indStart:indEnd)*...
(-exp(-model.D(k)*delayedt).*(delayedt>0)*model.simMean*model.S(k));
else
gdisimdelay(k)=0;
end;
indStart=indStart+size(delayedt,1);
% +(model.simMean*model.S(k)/model.D(k))*(1-exp(-model.D(k)*tempt));
end;
% gdisimdelay
% Multiply by factors from parameter transformations, and add gradient of the
% DISIM-variances to the main DISIM-variance gradient from the kernel
if model.numGenes>0,
disimdelayIndices = model.disimdelayindices;
for k=1:length(disimdelayIndices),
g(disimdelayIndices(k)) = g(disimdelayIndices(k)) ...
+ gdisimdelay(k)*doTransform(model.delay(k), 'gradfact',model.disimdelaytransformation(k));
end;
end;
else
%--------------------------------
% Version without DISIM start mean
% TODO: this version of the code does not yet take into account delays in the model!
% TODO: it also does not allow cell-format time indices, assumes RNA and POL2 have the
% observation times
%--------------------------------
gdisimstartmean=[];
numData = size(model.t, 1);
ind = 1:numData;
ind = ind + numData;
% gmu = zeros(size(1, model.numGenes));
gmu = zeros(1, model.numGenes);
for i = 1:model.numGenes
gmu(i) = sum(gmuFull(ind));
ind = ind + numData;
end
%end
if model.numGenes>0,
gb = gmu./model.D;
end;
% In case we need priors in.
% Add prior on B if it exists.
if isfield(model, 'bprior');
if model.numGenes>0,
gb = gb + priorGradient(model.bprior, model.B);
end;
end
if model.numGenes>0,
for k=1:length(gb),
gb(k) = gb(k)*doTransform(model.B(k), 'gradfact', model.bTransform(k));
end;
end;
% Account for decay in mean.
% This is a nasty hack to add the influence of the D in the mean to
% the gradient already computed for the kernel. This is all very
% clunky and sensitive to changes that take place elsewhere in the
% code ...
if model.numGenes>0,
gd = -gmu.*(model.B+model.simMean)./(model.D.*model.D);
end;
% Apply transformations for decay-gradient in mean, and add to
% main decay-gradient. Warning: only tested for 1 decay
% parameter, the indices here might be slightly
% incorrect in the general case!
if model.numGenes>0,
%decayIndices = [5];
%for i = 3:model.kern.numBlocks
% decayIndices(end+1) = decayIndices(end) + 2;
%end
decayIndices = model.disimdecayindices;
for k=1:length(decayIndices),
g(decayIndices(k)) = g(decayIndices(k)) ...
+ gd(k)*doTransform(model.D(k), 'gradfact',model.disimdecaytransformation(k));
end;
end;
% Compute gradient for SIM-level mean
gsimmean=zeros(1, 1);
indStart=1;
indEnd=indStart+length(model.t)-1;
gsimmean=gmuFull(indStart:indEnd)*(ones(indEnd-indStart+1,1));
for k=1:model.numGenes,
indStart=length(model.t)*k + 1;
indEnd=indStart+length(model.t)-1;
gsimmean=gsimmean+gmuFull(indStart:indEnd)*(ones(indEnd-indStart+1,1)*model.S(k)/model.D(k));
end;
% Add contribution of prior on simMean if it exists.
if isfield(model, 'simMeanPrior');
if model.numGenes>0,
gsimmean = gsimmean + ...
priorGradient(model.simMeanPrior, model.simMean);
end;
end
% Multiply by factors from parameter transformations
gsimmean = gsimmean*doTransform(model.simMean, 'gradfact', model.simMeanTransform);
% Compute gradient for DISIM-level variance
gdisimvar=zeros(1, model.numGenes);
for k=1:model.numGenes,
indStart=length(model.t)*k + 1;
indEnd=indStart+length(model.t)-1;
gdisimvar(k)=gmuFull(indStart:indEnd)*...
(ones(indEnd-indStart+1,1)*model.simMean/model.D(k)*0.5/model.S(k));
end;
% Multiply by factors from parameter transformations, and add gradient of the
% DISIM-variances to the main DISIM-variance gradient from the kernel
if model.numGenes>0,
disimvarIndices = model.disimvarianceindices;
for k=1:length(disimvarIndices),
g(disimvarIndices(k)) = g(disimvarIndices(k)) ...
+ gdisimvar(k)*doTransform(model.S(k)*model.S(k), 'gradfact',model.disimvariancetransformation(k));
end;
end;
end;
else
gb = zeros(1,model.numGenes);
if (model.use_disimstartmean==1),
gdisimstartmean = zeros(1,model.numGenes);
else
gdisimstartmean = [];
end;
gsimmean = 0;
end;
%fprintf(1,'Gradient after modifications from mean terms:\n');
%g
%pause
if model.numGenes>0,
g = [g gb gdisimstartmean gsimmean];
else
g = [g gsimmean];
end;
if isfield(model, 'fix')
for i = 1:length(model.fix)
g(model.fix(i).index) = 0;
end
end