Path: blob/master/notebooks/book2/12/rjmcmc_rbf/radialUpdate.m
1193 views
function [k,mu,M,match,aUpdate,rUpdate] = radialUpdate(match,aUpdate,rUpdate,k,mu,M,delta,x,y,hyper,t,bFunction,walkInt,walk);1% PURPOSE : Performs the update 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% - aUpdate: Number of times the update move has been accepted.5% - rUpdate: Number of times the update 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.23insideUpdate=1;24uU=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)+1+d,k(t)+1+d,c);31Pproposal=zeros(N,N,c);32proposal=zeros(1,d);3334% UPDATE EACH CENTRE:35% ==================36if k(t)==037mu{t+1}=mu{t};38end;39for basis=1:k(t),40for i=1:c,41invH(:,:,i) = (M'*M + (1/delta(t,i))*eye(k(t)+1+d));42P(:,:,i) = eye(N) - M*inv(invH(:,:,i))*M';43end;4445% PROPOSE A BASIS FUNCTION TO BE UPDATED:46% ======================================47for i=1:d,48proposal(1,i)= (min(x(:,i))-walk(i)) + ((max(x(:,i))+walk(i))-(min(x(:,i))-walk(i)))*rand(1,1);49end;5051% CHECK IF THE PROPOSED CENTRE ALREADY EXISTS:52% ===========================================53match1=0;54notEnded=1;55i=1;56while ((match1==0)&(notEnded==1)),57if (mu{t}(i,:)==proposal),58match1=1;59elseif (i<k(t)),60i=i+1;61else62notEnded=0;63end;64end;65match2=0;66notEnded=1;67i=1;68if basis>1,69match2=0;70notEnded=1;71i=1;72while ((match2==0)&(notEnded==1)),73if (mu{t+1}(i,:)==proposal),74match2=1;75elseif (i<basis-1),76i=i+1;77else78notEnded=0;79end;80end;81end;82if (match1>0),83match=match+1;84mu{t+1}(basis,:)=mu{t}(basis,:);85elseif (match2>0),86match=match+1;87mu{t+1}(basis,:)=mu{t}(basis,:);88else89% IF IT DOESN'T EXIST, PERFORM AN UPDATE MOVE:90% ===========================================91Mproposal = M;92Mproposal(:,d+1+basis) = feval(bFunction,proposal,x);93for i=1:c,94invHproposal(:,:,i) = (Mproposal'*Mproposal + inv(delta(t,i))*eye(k(t)+1+d));95Pproposal(:,:,i) = eye(N) - Mproposal*inv(invHproposal(:,:,i))*Mproposal';96end;97ratio = 1;98for i=1:c,99ratio = 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));100end;101acceptance = min(1,ratio);102if (uU<acceptance),103mu{t+1}(basis,:) = proposal;104M=Mproposal;105aUpdate=aUpdate+1;106else107mu{t+1}(basis,:) = mu{t}(basis,:);108rUpdate=rUpdate+1;109M=M;110end;111end;112end;113k(t+1) = k(t); % Don't change dimension.114M = M; % Return the last value of M.115116117118119120121122123124125126127128