radialMerge.m
上传用户:fsbooksir
上传日期:2013-10-19
资源大小:14k
文件大小:3k
源码类别:

matlab例程

开发平台:

Matlab

  1. function [k,mu,M,aMerge,rMerge] = radialMerge(aMerge,rMerge,k,mu,M,delta,x,y,hyper,t,bFunction,sigStar,walkInt);
  2. if nargin < 12, error('Not enough input arguments.'); end
  3. [N,d] = size(x);      % N = number of data, d = dimension of x.
  4. [N,c] = size(y);      % c = dimension of y, i.e. number of outputs.
  5. insideMerge=1;
  6. uD=rand(1);
  7. % INITIALISE H AND P MATRICES:
  8. % ===========================
  9. invH=zeros(k(t)+1+d,k(t)+1+d,c);
  10. P=zeros(N,N,c);
  11. invHproposal=zeros(k(t)+d,k(t)+d,c);
  12. Pproposal=zeros(N,N,c);
  13. for i=1:c,
  14.   invH(:,:,i) = (M'*M + (1/delta(t,i))*eye(k(t)+1+d));
  15.   P(:,:,i) = eye(N) - M*inv(invH(:,:,i))*M';
  16. end;
  17. % PROPOSE FIRST BASIS FUNCTION:
  18. % ============================
  19. position = unidrnd(length(mu{t}(:,1)),1,1);
  20. mu1 = mu{t}(position,:);
  21. dist = zeros(k(t),1);
  22. for i=1:k(t),
  23.   if i== position,
  24.     dist(i) = inf; 
  25.   else
  26.     dist(i)=norm(mu1-mu{t}(i,:)); % Euclidean distance;
  27.   end;
  28. end; 
  29. % PROPOSE SECOND BASIS FUNCTION:
  30. % =============================
  31. position2 = find(dist == min(dist));
  32. mu2 = mu{t}(position2,:);
  33. mumg = .5*(mu1 + mu2);
  34. % extract components:
  35. proposalPos1 = position + d+1;
  36. if (proposalPos1==d+1+k(t)),
  37.   Mproposal = [M(:,1:proposalPos1-1)];     
  38. else
  39.   Mproposal = [M(:,1:proposalPos1-1) M(:,proposalPos1+1:k(t)+d+1)];      
  40. end;
  41. if position2>position,
  42.   proposalPos2 = position2 + d+1;
  43.   if (proposalPos2==d+1+k(t)),
  44.     Mproposal = [Mproposal(:,1:proposalPos2-2)];     
  45.   else
  46.     Mproposal = [Mproposal(:,1:proposalPos2-2) Mproposal(:,proposalPos2:k(t)+d)];      
  47.   end;
  48. elseif position2<position,
  49.   proposalPos2 = position2 + d+1;
  50.   Mproposal = [Mproposal(:,1:proposalPos2-1) Mproposal(:,proposalPos2+1:k(t)+d)];      
  51. else
  52.   error('Something wrong with merge move');
  53. end;
  54. % add merged component:
  55. Mproposal = [Mproposal feval(bFunction,mumg,x)]; 
  56. for i=1:c,
  57.   invHproposal(:,:,i) = (Mproposal'*Mproposal + inv(delta(t,i))*eye(k(t)+d)); 
  58.   Pproposal(:,:,i) = eye(N) - Mproposal*inv(invHproposal(:,:,i))*Mproposal'; 
  59. end;
  60. % PERFORM A MERGE MOVE:
  61. % ====================
  62. Jacobian = inv(sigStar);
  63. ratio= Jacobian* prod(walkInt) * k(t) * inv(k(t)-1) * 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);  
  64. for i=2:c,
  65.   ratio= ratio * 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); 
  66. end;
  67. acceptance = min(1,ratio);  
  68. if min(dist)<2*sigStar
  69.   acceptance = 0;   % To ensure reversibility.
  70. end;
  71. % METROPOLIS-HASTINGS STEP:
  72. % ========================
  73. if (uD<acceptance),
  74.   previousMu = mu{t};
  75.   if (proposalPos1==(1+d+1)),
  76.     muTrunc = [previousMu(2:k(t),:)]; 
  77.   elseif (proposalPos1==(1+d+k(t))),
  78.     muTrunc = [previousMu(1:k(t)-1,:)];
  79.   else
  80.     muTrunc = [previousMu(1:proposalPos1-1-d-1,:); previousMu(proposalPos1-d-1+1:k(t),:)];
  81.   end;
  82.   if position2>position,
  83.     if (proposalPos2==(1+d+k(t))),
  84.       muTrunc = [muTrunc(1:k(t)-2,:)];
  85.     else
  86.       muTrunc = [muTrunc(1:proposalPos2-1-d-2,:); muTrunc(proposalPos2-d-1:k(t)-1,:)];
  87.     end;
  88.   elseif position2<position,
  89.     if (proposalPos2==(1+d+1)),
  90.       muTrunc = [muTrunc(2:k(t)-1,:)]; 
  91.     else
  92.       muTrunc = [muTrunc(1:proposalPos2-1-d-1,:); muTrunc(proposalPos2-d-1+1:k(t)-1,:)];
  93.     end;
  94.   else
  95.     error('Something wrong with merge move');
  96.   end;
  97.   mu{t+1} = [muTrunc; mumg];
  98.   k(t+1) = k(t)-1;
  99.   M=Mproposal;
  100.   aMerge=aMerge+1;
  101. else
  102.   mu{t+1} = mu{t};
  103.   k(t+1) = k(t);
  104.   rMerge=rMerge+1;
  105.   M=M;
  106. end;