% Aufgabe 8
% Bastian Groß
% Universität Trier
% 13. Januar 2014
% Monte Carlo Methode zur Berechnung des Black-Scholes-Optionspreises

clc; clear all;
fid = fopen('Programmieraufgabe_8.txt','w');
fprintf(fid,' sigma    Verfahren      error              \n');
fprintf(fid,'-----------------------------------------------------------------------\n');


% Parameter
for sigma = [0.01, 0.3, 0.5, 1, 1.5];
r     = 0.2;
T     = 1;
S0    = 100;
B     = 90;

Call = blsprice(S0, B, r, T, sigma);

N = 100;
h = T/N;
t = [0:h:T];


for m = 1:4
        M = 2^(m^2);
        Y_euler = S0*ones(1,M);
        Y_milst = S0*ones(1,M);
        for n = 1:N
            dW = sqrt(h)*randn(1,M);
            Y_euler = Y_euler + r*Y_euler*h + sigma*Y_euler.*dW;
            Y_milst = Y_milst + r*Y_milst*h + sigma*Y_milst.*dW + 0.5*sigma^2*Y_milst.*(dW.^2 - h);
        end
        C_euler(m) = exp(-r*T)*mean(max(Y_euler-B,0)); 
        C_milst(m) = exp(-r*T)*mean(max(Y_milst-B,0));
        error(m)   = abs(Call-C_euler(m))*sqrt(M);
end

MC_error_euler=abs(Call-C_euler);
MC_error_milst=abs(Call-C_milst);


fprintf(fid,' %2.3f  Euler-Maruyama  %7.4f   %7.4f   %7.4f   %7.4f  \n',[sigma, MC_error_euler]);
fprintf(fid,' %2.3f  Milstein        %7.4f   %7.4f   %7.4f   %7.4f  \n',[sigma, MC_error_milst]);


end
fclose(fid);