clear all
close all
clc
eps=1;
n=1;
y=0.5;
lambda=3;

while(eps>10^(-8))
	X_i(n) =exponential2_RV(lambda);
	W_i(n) =(1/sqrt(2*pi))*exp(-(y-X_i(n))^2/2);
	theta_N(n) = mean(X_i'.*W_i')/mean(W_i');
	if(n==1)
		eps=1;
	else
		eps=abs(var(theta_N(1:n)')-var(theta_N(1:n-1)'));
	end;
n=n+1;
end;

figure(1)
plot(theta_N,'r','LineWidth',2)
title('Convergence of theta_N using importance sampling')
ylabel('theta_N');
xlabel('n');
axis([1 n 0 1])