% price a deep out of the money call option using importance sampling 
% look at E[ exp(-rT) (S(T)-K)_+ exp(mu^2/2 - mu Z) ] 
% where Z ~ N( mu, 1) 
% below we write "mu + Z" instead of Z ... and plot the answer and the
% error for different values of mu 

S0 = 1;
K = 1.4;
sigma = 0.1;
r = 0.02;
T = 2; 
mu = [0:0.02:5] ;
a = zeros(2,length(mu)); 

M = 50000; 
Z = randn(M,1); 

for i=1:length(mu)
   ST = S0 * exp( (r-sigma^2/2)*T + sigma*sqrt(T)*(mu(i)+Z));
   C = exp(-r*T+mu(i)^2/2)*(ST-K).*(ST>K).*exp(-mu(i)*(mu(i)+Z)) ;
   a(1,i)=100*mean(C);
   a(2,i)=100*std(C)/sqrt(M);
end
   
figure(1)
plot(mu,a(1,:))   
figure(2)
plot(mu,a(2,:))   