dX = zeros(D,E,D+2,E); Z = zeros(n,D);
bdX = zeros(n,E,D); kdX = zeros(n,E,D+1);

% centralize training inputs
inp = bsxfun(@minus,input,m');

% 2) compute predicted mean and input-output covariance
for i = 1:E
  % first some useful intermediate terms
  K2 = K(:,:,i)+exp(2*X(D+2,i))*eye(n);                         % K + sigma^2*I
  inp2 = bsxfun(@rdivide,input,exp(X(1:D,i)'));
  ii = bsxfun(@rdivide,input,exp(2*X(1:D,i)'));
  R = s+diag(exp(2*X(1:D,i)));
  L = diag(exp(-X(1:D,i)));
  B = L*s*L+eye(D); iR = 