

function y = rndtruncRob95pos(mu,s2,a)

% This function draws random numbers from a left truncated normal
% distribution (i.e., simulates from a right tail when (a > mu)) using the 
% accept-reject method of Robert (1995).  To generate from left-tail use y = 2*mu -
% rndtruncRob95pos(mu,s2,2*mu-b).
% rndtruncRob95: random truncated normal Robert (1995) from the positive
% part.

% OUTPUT
%--------------------------------------------------------------------------
% y     : draw from a left truncated normal distribution using the 
%         accept-reject method of Robert (1995) 
%
% INPUT
%--------------------------------------------------------------------------
% mu    : mean of the untruncated normal distribution
% s2    : variance of the untruncated normal distribution
% a     : left truncation point
% b     : right truncation point
%--------------------------------------------------------------------------

sigma   = sqrt(s2);                     % standard deviation
aStd    = (a-mu)/sigma;                 % standardize left-truncation point "a"
alstar  = (aStd +sqrt(aStd^2 + 4))/2;   % alstar: a left standardized optimal scale factor

accept = 0;
while accept < 1
      z      = exprnd(1/alstar,1,1) + aStd;    % z ~ Exp(alstar,aStd)
      rho    = - ((z-alstar)^2)/2;
      accept = log(rand(1,1)) < rho;
end

y = mu + sigma*z;                           % back to N(mu,s2)


end


