function X = xing % Perfect simulation of the time taken for a BM to hit +/- 1 starting at 0 % Zaeem Burq 23.3.06, Owen Jones 24.3.06 accepted = 0; while ~accepted X = gamrnd(1.088870, 0.810570); Y = rand*1.243707*gampdf(X, 1.088870, 0.810570); sqrt2piX3 = sqrt(2*pi*X^3); N = max([ceil(0.275*X), 3]); K = (1+2*[-N:N]); fN0 = sum((-1).^[-N:N].*K.*exp(-K.^2./(2*X)))/sqrt2piX3; N = N + 1; fN1 = fN0 + (-1)^N*((1-2*N)*exp(-(1-2*N)^2/(2*X)) ... + (1+2*N)*exp(-(1+2*N)^2/(2*X)))/sqrt2piX3; while sign((Y - fN0)*(Y - fN1)) == -1 fN0 = fN1; N = N + 1; fN1 = fN0 + (-1)^N*((1-2*N)*exp(-(1-2*N)^2/(2*X)) ... + (1+2*N)*exp(-(1+2*N)^2/(2*X)))/sqrt2piX3; end if Y <= fN1, accepted = 1; end end