-
Notifications
You must be signed in to change notification settings - Fork 4
/
weights.m
executable file
·56 lines (45 loc) · 1.42 KB
/
weights.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
function [w]=weights(gp,KSinv_NS_KSinv)
NSamples=numel(gp.hypersamples);
if nargin<2
KSinv_NS_KSinv=gp.KSinv_NS_KSinv;
end
if isfield(gp, 'grad_hyperparams')
derivs = gp.grad_hyperparams;
elseif isfield(gp, 'use_derivatives')
derivs = gp.use_derivatives;
elseif isfield(gp, 'derivs_cov')
derivs = gp.derivs_cov;
elseif isfield(gp, 'derivs_mean')
derivs = gp.derivs_mean;
elseif ~isfield(gp, 'use_derivatives') && isfield(gp, 'covfn') && (nargin(gp.covfn)~=1)
derivs=true;
elseif isfield(gp.hypersamples,'glogL') && ...
~isempty(gp.hypersamples(1).glogL)
derivs=true;
else
% we can't determine the gradient of the covariance wrt hyperparams
derivs=false;
end
if isfield(gp,'active_hp_inds')
active_hp_inds = gp.active_hp_inds;
end
logLvec=[gp.hypersamples(:).logL];
logLvec = logLvec(:);
logLvec=(logLvec-max(logLvec));
Lvec=exp(logLvec);
if derivs
[glogLcell{1:NSamples}]=gp.hypersamples(:).glogL; % actually glogl is a cell itself
glogLmat=cell2mat(cat(2,glogLcell{:}))';
glogLmat = glogLmat(:,active_hp_inds);
% multiply by likelihood to get derivative of L from derivative of logL
gLmat=fliplr(repmat(Lvec,1,size(glogLmat,2)).*glogLmat);
%fliplr because derivs are actually in reverse order
Lvec=[Lvec;gLmat(:)];
end
if iscell(KSinv_NS_KSinv)
w = kronmult(KSinv_NS_KSinv, Lvec);
else
w = KSinv_NS_KSinv*Lvec;
end
w = max(w, 0);
w = w / sum(w);