Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/book2/12/rjmcmc_rbf/radialRW.m
1193 views
1
function [k,mu,M,match,aRW,rRW] = radialRW(match,aRW,rRW,k,mu,M,delta,x,y,hyper,t,bFunction,sWalk,walk);
2
% PURPOSE : Performs the random walk 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
% - aRW: Number of times the random walk move has been accepted.
6
% - rRW: Number of times the random walk 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: Parameter defining the compact set from which mu is sampled.
17
% - sWalk: Random walk variance.
18
19
% AUTHOR : Nando de Freitas - Thanks for the acknowledgement :-)
20
% DATE : 21-01-99
21
22
if nargin < 14, error('Not enough input arguments.'); end
23
[N,d] = size(x); % N = number of data, d = dimension of x.
24
[N,c] = size(y); % c = dimension of y, i.e. number of outputs.
25
insideRW=1;
26
uU=rand(1);
27
28
% INITIALISE H AND P MATRICES:
29
% ===========================
30
invH=zeros(k(t)+1+d,k(t)+1+d,c);
31
P=zeros(N,N,c);
32
invHproposal=zeros(k(t)+1+d,k(t)+1+d,c);
33
Pproposal=zeros(N,N,c);
34
35
% UPDATE EACH CENTRE:
36
% ==================
37
for basis=1:k(t),
38
for i=1:c,
39
invH(:,:,i) = (M'*M + (1/delta(t,i))*eye(k(t)+1+d));
40
P(:,:,i) = eye(N) - M*inv(invH(:,:,i))*M';
41
end;
42
43
% PROPOSE AND CONSTRAIN RANDOM WALK:
44
% =================================
45
proposal = mu{t}(basis,:) + sqrt(sWalk)*randn(size(mu{t}(basis,:)));
46
for i=1:d,
47
proposal(:,i) = min(proposal(:,i),max(x(:,i))+walk(i));
48
proposal(:,i) = max(proposal(:,i),min(x(:,i))-walk(i));
49
end;
50
51
% CHECK IF THE PROPOSED CENTRE ALREADY EXISTS:
52
% ===========================================
53
match1=0;
54
notEnded=1;
55
i=1;
56
while ((match1==0)&(notEnded==1)),
57
if (mu{t}(i,:)==proposal),
58
match1=1;
59
elseif (i<k(t)),
60
i=i+1;
61
else
62
notEnded=0;
63
end;
64
end;
65
match2=0;
66
notEnded=1;
67
i=1;
68
if basis>1,
69
match2=0;
70
notEnded=1;
71
i=1;
72
while ((match2==0)&(notEnded==1)),
73
if (mu{t+1}(i,:)==proposal),
74
match2=1;
75
elseif (i<basis-1),
76
i=i+1;
77
else
78
notEnded=0;
79
end;
80
end;
81
end;
82
if (match1>0),
83
match=match+1;
84
mu{t+1}(basis,:)=mu{t}(basis,:);
85
elseif (match2>0),
86
match=match+1;
87
mu{t+1}(basis,:)=mu{t}(basis,:);
88
else
89
% IF IT DOESN'T EXIST, PERFORM AN UPDATE MOVE:
90
% ===========================================
91
Mproposal = M;
92
Mproposal(:,d+1+basis) = feval(bFunction,proposal,x);
93
for i=1:c,
94
invHproposal(:,:,i) = (Mproposal'*Mproposal + inv(delta(t,i))*eye(k(t)+1+d));
95
Pproposal(:,:,i) = eye(N) - Mproposal*inv(invHproposal(:,:,i))*Mproposal';
96
end;
97
ratio = 1;
98
for i=1:c,
99
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));
100
end;
101
acceptance = min(1,ratio);
102
if (uU<acceptance),
103
mu{t+1}(basis,:) = proposal;
104
M=Mproposal;
105
aRW=aRW+1;
106
else
107
mu{t+1}(basis,:) = mu{t}(basis,:);
108
rRW=rRW+1;
109
M=M;
110
end;
111
end;
112
end;
113
k(t+1) = k(t); % Don't change dimension.
114
M = M; % Return the last value of M.
115
116
117
118
119
120
121
122
123
124
125
126
127