% BLACK-SCHOLES SIMULATION

dt=0.001; %step length

%%%% Parameters
T=10; 
sigma=0.3; 
mu=0.25; 
S0=1; 
r=0.15;
beta0=1;
K=5; 


%T=5; sigma=0.2; r=0.04; K=1;


t=[0:dt:T];

clf reset;
h = axes('Position',[0 0 1 1],'Visible','off');
axes('Position',[.25 .1 .7 .8]);
grid on;
hold on;

nt=size(t);
nt=nt(2)-1;
Bmt = cumsum(sigma*sqrt(dt)*randn(1,nt));
Bmt =[0,Bmt];
St = (mu-0.5.*(sigma^2))*t + Bmt;
St=S0*exp(St);
plot(t,St,'Color',[0 0 1]);
hold on; 
betat=beta0*exp(r*t);
plot(t,betat,'Color',[0 1 1]);
hold on;

tau=T-t;
tau(nt+1)=eps;

arg1=log(St/K)+(r+0.5*sigma^2)*tau;
arg2=arg1-sigma^2*tau;
arg1=arg1./(sigma*sqrt(tau));
arg2=arg2./(sigma*sqrt(tau));
At=normcdf(arg1);
Bt=-K*exp(-r*tau).*normcdf(arg2)./betat;

plot(t,At,'Color',[1 0 0]);
hold on;
plot(t,Bt,'Color',[1 0.5 0]);
hold on;

Vt=At.*St+Bt.*betat;

fprintf('K=%2.4f, r=%2.4f, sigma=%2.4f, T=%2.4f \n', K, r, sigma, T);
fprintf('Option value=%2.8f\n',Vt(1));

plot(t, Vt,'Color',[0 0 0]);

title('Replicating portfolio for the call option');
legend('S_t', '\beta_t','a_t','b_t', 'V_t','Location','northwest');
str=sprintf('K=%2.4f\n r=%2.4f', K, r);
str=strvcat(str, sprintf('\\mu=%2.4f', mu));
str=strvcat(str, sprintf('\\sigma=%2.4f', sigma));
str=strvcat(str, sprintf('V_0=%2.4f', Vt(1)));
set(gcf,'CurrentAxes',h);
text(0.025,.6,str);

hold off;



%axis equal;
