Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/book2/12/rjmcmc_rbf/radialBirth.m
1193 views
1
function [k,mu,M,match,aBirth,rBirth] = radialBirth(match,aBirth,rBirth,k,mu,M,delta,x,y,hyper,t,bFunction,walkInt,walk);
2
% PURPOSE : Performs the birth 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
% - aBirth: Number of times the birth move has been accepted.
6
% - rBirth: Number of times the birth 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
insideBirth=1;
25
uB=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)+2+d,k(t)+2+d,c);
32
Pproposal=zeros(N,N,c);
33
for i=1:c,
34
invH(:,:,i) = (M'*M + (1/delta(t,i))*eye(k(t)+1+d));
35
P(:,:,i) = eye(N) - M*inv(invH(:,:,i))*M';
36
end;
37
38
% PROPOSE A NEW BASIS FUNCTION:
39
% ============================
40
proposal=zeros(1,d);
41
for i=1:d,
42
proposal(1,i)= (min(x(:,i))-walk(i)) + ((max(x(:,i))+walk(i))-(min(x(:,i))-walk(i)))*rand(1,1);
43
end;
44
45
% CHECK IF THE PROPOSED CENTRE ALREADY EXISTS:
46
% ===========================================
47
match1=0;
48
notEnded=1;
49
i=1;
50
if k(t)>0
51
while ((match1==0)&(notEnded==1)),
52
if k(t)>0
53
if (mu{t}(i,:)==proposal),
54
match1=1;
55
elseif (i<k(t)),
56
i=i+1;
57
else
58
notEnded=0;
59
end;
60
else
61
notEnded=0;
62
end;
63
end;
64
end;
65
if (match1>0),
66
match=match+1;
67
mu{t+1} = mu{t};
68
k(t+1) = k(t);
69
M=M;
70
else
71
% IF IT DOESN'T EXIST, PERFORM A BIRTH MOVE:
72
% =========================================
73
Mproposal = [M feval(bFunction,proposal,x)];
74
for i=1:c,
75
invHproposal(:,:,i) = (Mproposal'*Mproposal + inv(delta(t,i))*eye(k(t)+2+d));
76
Pproposal(:,:,i) = eye(N) - Mproposal*inv(invHproposal(:,:,i))*Mproposal';
77
end;
78
ratio= inv(k(t)+1) * inv(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);
79
for i=2:c,
80
ratio= ratio * inv(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);
81
end;
82
acceptance = min(1,ratio);
83
if (uB<acceptance),
84
mu{t+1} = [mu{t}; proposal];
85
k(t+1) = k(t)+1;
86
M=Mproposal;
87
aBirth=aBirth+1;
88
else
89
mu{t+1} = mu{t};
90
k(t+1) = k(t);
91
rBirth=rBirth+1;
92
M=M;
93
end;
94
end;
95
96
97
98
99
100
101
102
103
104
105
106
107
108