%sequential Test
clear
close all

alp=0.01;
T=1.877:.00001:1.879;
n=20;
aux=gamcdf(T,n/2,2/n);
[val,pos]=min(abs((1-alp)-aux));
gamm=gamcdf(T(pos),n/2,8/n);

B=(1-gamm)/alp;
A=gamm/(1-alp);


% figure(1);
% H=plot(1:200,log(B)*ones(1,200),'k',1:200,log(A)*ones(1,200),'k','linewidth',2);
% hold on

for i=1:1000
    p=round(rand);
    pp=0.5;
    hacer=1;
    k=1;
    L=1;
    LL=1;
    while(hacer)
        x=(1+p)*randn;
        L=L*exp(-x.^2/2/4)/2/exp(-x.^2/2);
        LL=[LL L];
        plot(0:k,log(LL),'--.',0:k,log(B)*ones(1,k+1),'k',0:k,log(A)*ones(1,k+1),'k','linewidth',2,'markersize',20)
        if(i<10)
            pause
        else
            drawnow
        end
        if(L<=A)
            hacer=0;
            if(p==1)
                plot(0:k,log(LL),'r--.',0:k,log(B)*ones(1,k+1),'k',0:k,log(A)*ones(1,k+1),'k','linewidth',2,'markersize',20)
                pause
            else
                plot(0:k,log(LL),'g--.',0:k,log(B)*ones(1,k+1),'k',0:k,log(A)*ones(1,k+1),'k','linewidth',2,'markersize',20)
                if(k>15)
                    pause
                else
                    if(i<10)
                        pause
                    else
                        drawnow
                    end
                end
            end
        elseif(L>=B)
            hacer=0;
            if(p==0)
                plot(0:k,log(LL),'r--.',0:k,log(B)*ones(1,k+1),'k',0:k,log(A)*ones(1,k+1),'k','linewidth',2,'markersize',20)
                pause
            else
                plot(0:k,log(LL),'g--.',0:k,log(B)*ones(1,k+1),'k',0:k,log(A)*ones(1,k+1),'k','linewidth',2,'markersize',20)
                if(k>15)
                    pause
                else
                    drawnow
                    if(i<10)
                        pause
                    else
                        drawnow
                    end
                end
            end
        else
            k=k+1;
        end
    end
end
hold off
% axis([1 150 0 100])