Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/book2/12/rjmcmc_rbf/radialDeath.m
1193 views
1
function [k,mu,M,aDeath,rDeath] = radialDeath(aDeath,rDeath,k,mu,M,delta,x,y,hyper,t,nabla);
2
% PURPOSE : Performs the death move of the reversible jump MCMC algorithm.
3
% INPUTS : - aDeath: Number of times the death move has been accepted.
4
% - rDeath: Number of times the death move has been rejected.
5
% - k : Number of basis functions.
6
% - mu : Basis functions centres.
7
% - M : Regressors matrix.
8
% - delta : Signal to noise ratio.
9
% - x : Input data.
10
% - y : Target data.
11
% - hyper: hyperparameters.
12
% - t : Current time step.
13
% - nabla: Hyperparameter for k.
14
15
% AUTHOR : Nando de Freitas - Thanks for the acknowledgement :-)
16
% DATE : 21-01-99
17
18
if nargin < 11, error('Not enough input arguments.'); end
19
[N,d] = size(x); % N = number of data, d = dimension of x.
20
[N,c] = size(y); % c = dimension of y, i.e. number of outputs.
21
insideDeath=1;
22
uD=rand(1);
23
24
% INITIALISE H AND P MATRICES:
25
% ===========================
26
invH=zeros(k(t)+1+d,k(t)+1+d,c);
27
P=zeros(N,N,c);
28
invHproposal=zeros(k(t)+d,k(t)+d,c);
29
Pproposal=zeros(N,N,c);
30
for i=1:c,
31
invH(:,:,i) = (M'*M + (1/delta(t,i))*eye(k(t)+1+d));
32
P(:,:,i) = eye(N) - M*inv(invH(:,:,i))*M';
33
end;
34
35
% CHOOSE UNIFORMLY A BASIS FUNCTION TO BE DELETED:
36
% ===============================================
37
proposalPos= d+1+unidrnd(length(mu{t}(:,1)),1,1);
38
if (proposalPos==d+1+k(t)),
39
Mproposal = [M(:,1:proposalPos-1)];
40
else
41
Mproposal = [M(:,1:proposalPos-1) M(:,proposalPos+1:k(t)+d+1)];
42
end;
43
for i=1:c,
44
invHproposal(:,:,i) = (Mproposal'*Mproposal + inv(delta(t,i))*eye(k(t)+d));
45
Pproposal(:,:,i) = eye(N) - Mproposal*inv(invHproposal(:,:,i))*Mproposal';
46
end;
47
48
% PERFORM A DEATH MOVE:
49
% ====================
50
ratio= k(t) * sqrt(delta(t,1)) * sqrt((det(invH(:,:,1)))/(det(invHproposal(:,:,1)))) * ((hyper.gamma+y(:,1)'*P(:,:,1)*y(:,1))/(hyper.gamma + y(:,1)'*Pproposal(:,:,1)*y(:,1)))^((hyper.v+N)/2);
51
for i=2:c,
52
ratio= ratio * sqrt(delta(t,i)) * sqrt((det(invH(:,:,i)))/(det(invHproposal(:,:,i)))) * ((hyper.gamma+y(:,i)'*P(:,:,i)*y(:,i))/(hyper.gamma + y(:,i)'*Pproposal(:,:,i)*y(:,i)))^((hyper.v+N)/2);
53
end;
54
acceptance = min(1,ratio);
55
if (uD<acceptance),
56
previousMu = mu{t};
57
if (proposalPos==(1+d+1)),
58
mu{t+1} = [previousMu(2:k(t),:)];
59
elseif (proposalPos==(1+d+k(t))),
60
mu{t+1} = [previousMu(1:k(t)-1,:)];
61
else
62
mu{t+1} = [previousMu(1:proposalPos-1-d-1,:); previousMu(proposalPos-d-1+1:k(t),:)];
63
end;
64
k(t+1) = k(t)-1;
65
M=Mproposal;
66
aDeath=aDeath+1;
67
else
68
mu{t+1} = mu{t};
69
k(t+1) = k(t);
70
rDeath=rDeath+1;
71
M=M;
72
end;
73
74
75
76
77
78
79
80
81
82
83
84