Path: blob/master/notebooks/book2/12/rjmcmc_rbf/radialBirth.m
1193 views
function [k,mu,M,match,aBirth,rBirth] = radialBirth(match,aBirth,rBirth,k,mu,M,delta,x,y,hyper,t,bFunction,walkInt,walk);1% PURPOSE : Performs the birth move of the reversible jump MCMC algorithm.2% INPUTS : - match: Number of times a basis function already exists (probability zero in theory).3% For completeness sake, I don't allow for duplicate basis functions here.4% - aBirth: Number of times the birth move has been accepted.5% - rBirth: Number of times the birth move has been rejected.6% - k : Number of basis functions.7% - mu : Basis functions centres.8% - M : Regressors matrix.9% - delta : Signal to noise ratio.10% - x : Input data.11% - y : Target data.12% - hyper: hyperparameters.13% - t : Current time step.14% - bFunction: Type of basis function.15% - walk, walkInt: Parameters defining the compact set from which mu is sampled.1617% AUTHOR : Nando de Freitas - Thanks for the acknowledgement :-)18% DATE : 21-01-991920if nargin < 14, error('Not enough input arguments.'); end21[N,d] = size(x); % N = number of data, d = dimension of x.22[N,c] = size(y); % c = dimension of y, i.e. number of outputs.23insideBirth=1;24uB=rand(1);2526% INITIALISE H AND P MATRICES:27% ===========================28invH=zeros(k(t)+1+d,k(t)+1+d,c);29P=zeros(N,N,c);30invHproposal=zeros(k(t)+2+d,k(t)+2+d,c);31Pproposal=zeros(N,N,c);32for i=1:c,33invH(:,:,i) = (M'*M + (1/delta(t,i))*eye(k(t)+1+d));34P(:,:,i) = eye(N) - M*inv(invH(:,:,i))*M';35end;3637% PROPOSE A NEW BASIS FUNCTION:38% ============================39proposal=zeros(1,d);40for i=1:d,41proposal(1,i)= (min(x(:,i))-walk(i)) + ((max(x(:,i))+walk(i))-(min(x(:,i))-walk(i)))*rand(1,1);42end;4344% CHECK IF THE PROPOSED CENTRE ALREADY EXISTS:45% ===========================================46match1=0;47notEnded=1;48i=1;49if k(t)>050while ((match1==0)&(notEnded==1)),51if k(t)>052if (mu{t}(i,:)==proposal),53match1=1;54elseif (i<k(t)),55i=i+1;56else57notEnded=0;58end;59else60notEnded=0;61end;62end;63end;64if (match1>0),65match=match+1;66mu{t+1} = mu{t};67k(t+1) = k(t);68M=M;69else70% IF IT DOESN'T EXIST, PERFORM A BIRTH MOVE:71% =========================================72Mproposal = [M feval(bFunction,proposal,x)];73for i=1:c,74invHproposal(:,:,i) = (Mproposal'*Mproposal + inv(delta(t,i))*eye(k(t)+2+d));75Pproposal(:,:,i) = eye(N) - Mproposal*inv(invHproposal(:,:,i))*Mproposal';76end;77ratio= 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);78for i=2:c,79ratio= 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);80end;81acceptance = min(1,ratio);82if (uB<acceptance),83mu{t+1} = [mu{t}; proposal];84k(t+1) = k(t)+1;85M=Mproposal;86aBirth=aBirth+1;87else88mu{t+1} = mu{t};89k(t+1) = k(t);90rBirth=rBirth+1;91M=M;92end;93end;949596979899100101102103104105106107108