Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/book2/12/rjmcmc_rbf/radialUpdate.m
1193 views
1
function [k,mu,M,match,aUpdate,rUpdate] = radialUpdate(match,aUpdate,rUpdate,k,mu,M,delta,x,y,hyper,t,bFunction,walkInt,walk);
2
% PURPOSE : Performs the update move of the reversible jump MCMC algorithm.
3
% INPUTS : - match: Number of times a basis function already exists (probability zero in theory).
4
% For completeness sake, I don't allow for duplicate basis functions here.
5
% - aUpdate: Number of times the update move has been accepted.
6
% - rUpdate: Number of times the update move has been rejected.
7
% - k : Number of basis functions.
8
% - mu : Basis functions centres.
9
% - M : Regressors matrix.
10
% - delta : Signal to noise ratio.
11
% - x : Input data.
12
% - y : Target data.
13
% - hyper: hyperparameters.
14
% - t : Current time step.
15
% - bFunction: Type of basis function.
16
% - walk, walkInt: Parameters defining the compact set from which mu is sampled.
17
18
% AUTHOR : Nando de Freitas - Thanks for the acknowledgement :-)
19
% DATE : 21-01-99
20
21
if nargin < 14, error('Not enough input arguments.'); end
22
[N,d] = size(x); % N = number of data, d = dimension of x.
23
[N,c] = size(y); % c = dimension of y, i.e. number of outputs.
24
insideUpdate=1;
25
uU=rand(1);
26
27
% INITIALISE H AND P MATRICES:
28
% ===========================
29
invH=zeros(k(t)+1+d,k(t)+1+d,c);
30
P=zeros(N,N,c);
31
invHproposal=zeros(k(t)+1+d,k(t)+1+d,c);
32
Pproposal=zeros(N,N,c);
33
proposal=zeros(1,d);
34
35
% UPDATE EACH CENTRE:
36
% ==================
37
if k(t)==0
38
mu{t+1}=mu{t};
39
end;
40
for basis=1:k(t),
41
for i=1:c,
42
invH(:,:,i) = (M'*M + (1/delta(t,i))*eye(k(t)+1+d));
43
P(:,:,i) = eye(N) - M*inv(invH(:,:,i))*M';
44
end;
45
46
% PROPOSE A BASIS FUNCTION TO BE UPDATED:
47
% ======================================
48
for i=1:d,
49
proposal(1,i)= (min(x(:,i))-walk(i)) + ((max(x(:,i))+walk(i))-(min(x(:,i))-walk(i)))*rand(1,1);
50
end;
51
52
% CHECK IF THE PROPOSED CENTRE ALREADY EXISTS:
53
% ===========================================
54
match1=0;
55
notEnded=1;
56
i=1;
57
while ((match1==0)&(notEnded==1)),
58
if (mu{t}(i,:)==proposal),
59
match1=1;
60
elseif (i<k(t)),
61
i=i+1;
62
else
63
notEnded=0;
64
end;
65
end;
66
match2=0;
67
notEnded=1;
68
i=1;
69
if basis>1,
70
match2=0;
71
notEnded=1;
72
i=1;
73
while ((match2==0)&(notEnded==1)),
74
if (mu{t+1}(i,:)==proposal),
75
match2=1;
76
elseif (i<basis-1),
77
i=i+1;
78
else
79
notEnded=0;
80
end;
81
end;
82
end;
83
if (match1>0),
84
match=match+1;
85
mu{t+1}(basis,:)=mu{t}(basis,:);
86
elseif (match2>0),
87
match=match+1;
88
mu{t+1}(basis,:)=mu{t}(basis,:);
89
else
90
% IF IT DOESN'T EXIST, PERFORM AN UPDATE MOVE:
91
% ===========================================
92
Mproposal = M;
93
Mproposal(:,d+1+basis) = feval(bFunction,proposal,x);
94
for i=1:c,
95
invHproposal(:,:,i) = (Mproposal'*Mproposal + inv(delta(t,i))*eye(k(t)+1+d));
96
Pproposal(:,:,i) = eye(N) - Mproposal*inv(invHproposal(:,:,i))*Mproposal';
97
end;
98
ratio = 1;
99
for i=1:c,
100
ratio = ratio*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));
101
end;
102
acceptance = min(1,ratio);
103
if (uU<acceptance),
104
mu{t+1}(basis,:) = proposal;
105
M=Mproposal;
106
aUpdate=aUpdate+1;
107
else
108
mu{t+1}(basis,:) = mu{t}(basis,:);
109
rUpdate=rUpdate+1;
110
M=M;
111
end;
112
end;
113
end;
114
k(t+1) = k(t); % Don't change dimension.
115
M = M; % Return the last value of M.
116
117
118
119
120
121
122
123
124
125
126
127
128