L/B*L;
  t = inp*iR;
  l = exp(-sum(t.*inp,2)/2); lb = l.*beta(:,i);
  tliK = t'*bsxfun(@times,l,iK(:,:,i));
  liK = K2\l;
  tlb = bsxfun(@times,t,lb);
  
  c = exp(2*X(D+1,i))/sqrt(det(R))*exp(sum(X(1:D,i)));
  detdX = diag(bsxfun(@times,det(R)*iR',2.*exp(2.*X(1:D,i))));    % d(det R)/dX
  cdX = -0.5*c/det(R).*detdX'+ c.*ones(1,D);       % derivs w.r.t length-scales
  dldX = bsxfun(@times,l,bsxfun(@times,t,2.*exp(2*X(1:D,i)')).*t./2);
  
  M(i) = sum(lb)*c;                                            % pred