forked from lawrennd/gpsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpsimMapMarginalLikeliGradient.m
130 lines (100 loc) · 3.76 KB
/
gpsimMapMarginalLikeliGradient.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
function g = gpsimMapMarginalLikeliGradient(model)
% GPSIMMAPMARGINALLIKELIGRADIENT Compute the gradients of the log marginal likelihood of a GPSIMMAP model with respect to the model parameters.
% FORMAT
% DESC computes the gradients of the log marginal 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 : gpsimMapCreate, gpsimMapLogLikelihood,
% gpsimMapGradient, gpsimMapFunctionalLogLikeGradients
%
% COPYRIGHT : Pei Gao, Magnus Rattray and Neil D. Lawrence, 2008
% SHEFFIELDML
numData = length(model.t);
ParamL = length(model.B);
if isfield(model, 'includeNoise') && model.includeNoise
noiseMat = ones(numData, 1)*model.noiseVar;
yvar = model.yvar + noiseMat;
dlogPdn = zeros(ParamL,1);
else
yvar = model.yvar;
end
if isfield(model, 'includeRepression') && model.includeRepression
dlogPdalpha = zeros(ParamL,1);
end
dlogPdB = zeros(ParamL,1);
dlogPdD = zeros(ParamL,1);
dlogPdS = zeros(ParamL,1);
if model.ngParam > 0
ngParamk = model.ngParam/model.numGenes;
dlogPdgParam= zeros(ParamL, ngParamk);
end
% C = eye(size(model.W)) + model.K*model.W;
% invC = inv(C)*model.K;
invC = model.covf;
for k = 1:model.numGenes
for i=1:numData
ind = i + (k-1)*numData;
beta_ik=1/yvar(ind);
factor = (model.ypred(model.times_index(i), k)-model.y(ind))*beta_ik;
[dxdB dxdD dxdS dxdalpha dxdgParam] = gpsimXGradient(model, i, k);
dlogPdB(k)=dlogPdB(k)-factor*dxdB;
if isfield(model, 'includeRepression') && model.includeRepression
dlogPdalpha(k) = dlogPdalpha(k)-factor*dxdalpha;
end
dlogPdD(k) = dlogPdD(k)-factor*dxdD;
dlogPdS(k) = dlogPdS(k)-factor*dxdS;
if model.ngParam > 0
dlogPdgParam(k,:)= dlogPdgParam(k,:)-factor*dxdgParam;
end
if isfield(model,'includeNoise') && model.includeNoise
dlogPdn(k) = dlogPdn(k) + sqrt(model.noiseVar(k))*factor*factor - ...
sqrt(model.noiseVar(k))*beta_ik;
end
end
% gB(k) = dlogPdB(k);
% gD(k) = dlogPdD(k);
% gS(k) = dlogPdS(k);
% ggParam(:,k) = dlogPdgParam(k,:)';
% gn(k) = dlogPdn(k);
[dWdB, dWdD, dWdS, dWdalpha, dWdgParam, dWdn] = gpsimMapWGradient(model, k);
gB(k) = -0.5*trace(invC*dWdB)+dlogPdB(k);
gD(k) = -0.5*trace(invC*dWdD)+dlogPdD(k);
gS(k) = -0.5*trace(invC*dWdS)+dlogPdS(k);
if isfield(model, 'includeRepression') && model.includeRepression
galpha(k) = -0.5*trace(invC*dWdalpha)+dlogPdalpha(k);
end
if model.ngParam > 0
for gParam_index = 1:ngParamk
ggParam(gParam_index,k) = -0.5*trace(invC*dWdgParam(:,:, ...
gParam_index))+dlogPdgParam(k, gParam_index);
end
end
if isfield(model, 'includeNoise') && model.includeNoise
gn(k) = -0.5*trace(invC*dWdn(:,:)) + dlogPdn(k);
end
end
fhandle = str2func([model.Transform 'Transform']);
g=[];
for i = 1:model.numGenes
if isfield(model,'bTransform') && isempty(model.bTransform)
g = [g gB(i)];
else
g = [g gB(i)*fhandle(model.B(i), 'gradfact')];
end
g = [g gS(i)*fhandle(model.S(i), 'gradfact') gD(i)*fhandle(model.D(i), 'gradfact')];
if isfield(model, 'includeRepression') && model.includeRepression
if isfield(model,'alphaTransform') && isempty(model.alphaTransform)
g = [g galpha(i)];
else
g = [g galpha(i)*fhandle(model.alpha(i), 'gradfact')];
end
end
if model.ngParam > 0
g = [g ggParam(:,i).*fhandle(model.gParam(:,i), 'gradfact')];
end
end
if isfield(model, 'includeNoise') && model.includeNoise
g = [g gn.*fhandle(sqrt(model.noiseVar), 'gradfact')];
end