

function [deltaReturn,accept] = drawdeltaRW(y,x,beta,delta0,d0,D0,tune,Dhat,p) 

% Output
%--------------------------------------------------------------------------
% deltareturn : Draw of delta using random-walk MH, a column vector
% accept      : acceptance indicator
% Input
% y         : observed dicrete values, a column vector
% x         : covariate matrix including a column of ones
% delta0    : current value of delta, a column vector
% d0        : prior mean of delta
% D0        : prior covariance matrix of delta
% tune      : tuning parameter to adjust MH acceptance rate
% Dhat      : negative inverse Hessian from maximization of log-likelihood
% p         : quantile value
%--------------------------------------------------------------------------

% Samples the transformed cut-points delta using RW-MH

k        = size(delta0,1);

% Proposed value
L        = chol(Dhat,'lower');
delta1   = delta0 + tune*L*normrnd(0,1,k,1);   % Random Walk, draw from proposal density

% Target Density, denoted by p
pNum     = - QRnegSumloglike(delta1,y,x,beta,p)...
           + log(mvnpdf(delta1,d0,D0));        % numerator of target density
           
           
pDen     = - QRnegSumloglike(delta0,y,x,beta,p)...
           + log(mvnpdf(delta0,d0,D0));        % denominator of target density         
       
% proposal denisities are not included since they cancel out.

% pNum - pDen 
%------ M-H Acceptance --------
    if log(rand) <= pNum - pDen 
        deltaReturn = delta1;
        accept      = 1;
    else
        deltaReturn = delta0;
        accept=0;
    end

end
       
       


