forked from lawrennd/gpsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpsimMapLikeGradientImplicit.m
73 lines (56 loc) · 2.05 KB
/
gpsimMapLikeGradientImplicit.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
function [g dlogPdf] = gpsimMapLikeGradientImplicit(model)
% GPSIMMAPLIKEGRADIENTIMPLICIT computes the implicit part of the gradient
% FORMAT
% ARG model : the model for which the data component of the Hessian is
% to be updated.
% RETURN g :
% RETURN dlogPdf :
%
% SEEALSO : gpsimMapCreate, gpsimMapFunctionalGradient, gpsimMapUpdatePosteriorCovariance
%
% COPYRIGHT : Pei Gao, Magnus Rattray and Neil D. Lawrence, 2008
% SHEFFIELDML
g1 = zeros(1, model.kern.nParams);
g2 = zeros(1, model.numParams-model.kern.nParams);
% covGrad = inv(eye(size(model.K))+model.K*model.W);
covGrad = model.covf*model.invK;
funcGrad = model.invK*model.f;
for ftime_index = 1:model.numMapPts
kernGrad(ftime_index, :) = kernGradient(model.kern, model.mapt, covGrad)*funcGrad(ftime_index);
% dfuncGrad(ftime_index,:) = zeros(size(g2));
dfuncGrad(ftime_index, :) = gpsimMapFunctionalLikeGrad2(model, ftime_index);
end
modelParamGrad = covGrad*model.K*dfuncGrad;
for ftime_index = 1:model.numMapPts
dWdf(:,:,ftime_index) = gpsimMapFunctionalWGradient(model, ftime_index);
dlogPdf(ftime_index) = -0.5*trace(covGrad*model.K*dWdf(:,:,ftime_index));
g1 = g1 + dlogPdf(ftime_index)*kernGrad(ftime_index, :);
g2 = g2 + dlogPdf(ftime_index)*modelParamGrad(ftime_index,:);
end
% Check if model parameters are being optimised in a transformed space
[paramvec names] = gpsimMapExtractParam(model);
if isfield(model,'bTransform') && isempty(model.bTransform)
Bindex = [];
for i = 1:length(names)
if strcmp(names{i}(1:5), 'Basal')
Bindex = [Bindex i];
end
end
paramvec(Bindex) = 0;
end
if isfield(model,'alphaTransform') && isempty(model.alphaTransform)
alphaIndex = [];
for i = 1:length(names)
if strcmp(names{i}(1:5), 'Param')
alphaIndex = [alphaIndex i];
end
end
paramvec(alphaIndex) = 0;
end
modelParams = paramvec((model.kern.nParams+1):end);
if ~isempty(model.Transform)
fhandle = str2func([model.Transform 'Transform']);
modelParams = fhandle(modelParams, 'atox');
g2 = g2.*fhandle(modelParams, 'gradfact');
end
g = [g1 g2];