clear all; clc;
a=8;
lambda = 1;n=10000;
t=lambda -(1/a);
mt= lambda/(lambda-t);
u=rand(1,n);
x1=-log(u);
x = -a*log(u);
%classical estimate
tic
for i=1:length(x1)
    flag=0;
    if x1(i)>=a
      flag=flag+1;
    end
    if flag==1
        gx1(i)=1;
    else
        gx1(i)= 0;
    end
    thetahat(i)=(1/i)*sum(gx1);
end
toc
figure(1)
plot(thetahat)
xlabel('n');
ylabel('thetahat')

 
%tilted sampling
tic
for i=1:length(x)
    flag=0;
    if x(i)>=a
        flag=flag+1;
    end
    if flag==1
        gx(i)=exp(-t*x(i));
    else
        gx(i)= 0;
    end
    thetatilda(i)=(mt/i)*sum(gx);
end
toc;
figure(2)
plot(thetatilda)
xlabel('n');
ylabel('thetatilda')

thetahat(i)
thetatilda(i)
