You are on page 1of 30

金融商品設計與評價作業

110071502 曾彥寧

作業二
% bsformula.m
function bsformula(s,x,r,t,sigma)
d1 = log(s/x)+(r+sigma^2/2)*t/(sigma*sqrt(t));
d2 = d1-(sigma*sqrt(t));

c = s*normcdf(d1)-x*(exp(-r*t)*normcdf(d2))
p = x*exp(-r*t)*normcdf(-d2)-s*normcdf(-d1)

% test_bsformula
bsformula(100,98,0.02,1,0.5)

% SPV.m
function SPV(y,n,cv)
for i=1:n
ccv(i)=cv(i)/((1+y)^i);
end
PV = sum(ccv)

% test_SPV
X = SPV(3,3,[100,100,100])

作業三
%four call and put plots
x = 50;
c = 2;
p = 2;
st = 40:0.05:60;

subplot(2,2,1);
plot(st, max(st-x,0)-c);
title('long call');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);
grid on;

subplot(2,2,2);
plot(st, max(x-st,0)-p);
title('long put');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);
grid on;

subplot(2,2,3);
plot(st, c-max(st-x,0));
title('short call');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);
grid on;

subplot(2,2,4);
plot(st, p-max(x-st,0));
title('short put');
xlabel('S');
ylabel('profit');

% Plot long put and BS.m


x = 50;
c = 2;
r = 0.01;
t = 0.75;
sigma = 0.1;
st = 40:0.05:60;
[m,n]=size(st);

for i=1:n
[call(i), put(i)] = blsprice(st(i),x,r,t,sigma);
end
plot (st,max(x-st,0), '--black' );
hold on;
plot(st,put,'*blue');
title('long put and BS');
xlabel('St');
ylabel('profit');
grid on

%plot bond price and rate


list = [];
yi = 0.01:0.01:1;
for i=yi
PV = cummulativePV(i,3,[10 10 110]);
list = [list PV];
end

plot(list,yi);
title('interest rate vs bond price');
xlabel('bond price');
ylabel('interest rate');
grid on
%plot delta with time maturity
x=50;
r=0.025;
t=0.01:0.01:1;
sigma=0.5;
div=0;
s1=55;
s2=50;
s3=45;

[call_delta_in,put_delta_in]=blsdelta(s1,x,r,t,sigma);
[call_delta_at,put_delta_at]=blsdelta(s2,x,r,t,sigma);
[call_delta_out,put_delta_out]=blsdelta(s3,x,r,t,sigma);

plot(t,call_delta_in,'-red');
hold on;
plot(t,call_delta_at,'-black');
hold on;
plot(t,call_delta_out,'-green');
legend('in the money','at the money','out the money');

title('delta with time to maturity');


xlabel('time to maturity');
ylabel('delta');
grid on
% plot greeks
x=50;
r=0.05;
t=0.5;
sigma=0.2;
div=0;
s=30:120;

[call_delta,put_delta]=blsdelta(s,x,r,t,sigma);
[call_theta,put_theta]=blstheta(s,x,r,t,sigma);
[call_gamma]=blsgamma(s,x,r,t,sigma);
[call_vega]=blsvega(s,x,r,t,sigma);
[call_rho,put_rho]=blsrho(s,x,r,t,sigma);

subplot(2,3,1);
plot(s,call_delta,'-red');
title('delta vs stock price');
xlabel('stock price');
ylabel('delta');
grid on

subplot(2,3,2);
plot(s,call_theta,'-red');
title('theta vs stock price');
xlabel('stock price');
ylabel('theta');
grid on

subplot(2,3,3);
plot(s,call_gamma,'-red');
title('gamma vs stock price');
xlabel('stock price');
ylabel('gamma');
grid on

subplot(2,3,4);
plot(s,call_vega,'-red');
title('vega vs stock price');
xlabel('stock price');
ylabel('vega');
grid on

subplot(2,3,5);
plot(s,call_rho,'-red');
title('Rho vs stock price');
xlabel('stock price');
ylabel('Rho');
grid on
作業四
%bear call spread=long high call+short low call
x1=55;
x2=50;
c1=4;
c2=6;
s=30:1:70;
xlabel('stock price');
ylabel('profit');
title('bear call spread');
plot(s,max(s-x1,0)-c1,'*');
hold on;
plot(s,c2-max(s-x2,0),'--');
hold on;
plot(s,max(s-x1,0)-c1+c2-max(s-x2,0))
hold on;
plot(s,zeros(1,41),'black')
grid on

%bear put spread=long high put+short low put


x1=53;
x2=50;
p1=5;
p2=4;
s=30:1:70;
xlabel('stock price');
ylabel('profit');
title('bear put spread');
plot(s,max(x1-s,0)-p1,'*');
hold on;
plot(s,p2-max(x2-s,0),'--');
hold on;
plot(s,max(x1-s,0)-p1+p2-max(x2-s,0));
plot(s,zeros(1,41),'black');
grid on;

%bull put spread = long low put + short high put


x1=50;
x2=53;
p1=2;
p2=4;
s=40:1:60;
xlabel('stock price');
ylabel('profit');
title('bull put spread')
plot(s,max(x1-s,0)-p1,'*');
hold on;
plot(s,-(max(x2-s,0)-p2),'--');
hold on;
plot(s,max(x1-s,0)-p1-(max(x2-s,0)-p2));
hold on;
plot(s,zeros(1,21),'black')
grid on;
% butterfly spread with arbitrage oppurtunity
clear;
x1=95;x2=100;x3=105;
c11=10;c21=6;c31=4;
r=0.02;t=0.05;sigma=0.02;
s0=80:0.1:120;
[m,n]=size(s0);

subplot(2,1,1);
plot(s0,max(s0-x1,0)+max(s0-x3,0)-2*max(s0-x2,0)-(2*c21-c11-c31));
axis([80 120 -5 10]);
title('Butterfly above profit=0')
xlabel('s0');
ylabel('profit');
grid on;

subplot(2,1,2);
plot(s0,-max(s0-x1,0)-max(s0-x3,0)+2*max(s0-x2,0)+(2*c21-c11-c31));
axis([80 120 -10 5]);
title('Butterfly under profit=0')
xlabel('s0');
ylabel('profit');
grid on;
%butterfly spread + BS=long high,low x call+short 2 middle x call
clear;
x1=95;x2=100;x3=105;
c1=8;c2=7;c3=4;
r=0.02;t1=0.01;t2=0.05;t3=0;sigma=0.2;
s0=80:0.1:120;
[m,n]=size(s0);

for i=1:n;
[c11(i),p11(i)]=blsprice(s0(i),x1,r,t1,sigma);
[c21(i),p21(i)]=blsprice(s0(i),x2,r,t1,sigma);
[c31(i),p31(i)]=blsprice(s0(i),x3,r,t1,sigma);

[c12(i),p12(i)]=blsprice(s0(i),x1,r,t2,sigma);
[c22(i),p22(i)]=blsprice(s0(i),x2,r,t2,sigma);
[c32(i),p32(i)]=blsprice(s0(i),x3,r,t2,sigma);
end;
plot(s0,c11+c31-2*c21-(2*c2-c1-c3),'blue');
hold on;
plot(s0,c12+c32-2*c22-(2*c2-c1-c3),'red');
plot(s0,max(s0-x1,0)+max(s0-x3,0)-2*max(s0-x2,0)-(2*c2-c1-c3),'green');
legend({'t=0.01','t=0.05','t=0'})
grid on;
%condor=long highest,lowest call+short middle 2 call
x1=47;
x2=49;
x3=51;
x4=53;
c1=8;
c2=6;
c3=4;
c4=3;
s=30:1:70;
xlabel('stock price');
ylabel('profit');
title('condo');
plot(s,max(s-x4,0)-c4,'*');
hold on;
plot(s,max(s-x1,0)-c1,'--');
hold on;
plot(s,c2-max(s-x2,0),'*');
hold on;
plot(s,c3-max(s-x3,0),'--');
hold on;
plot(s,max(s-x4,0)-c4+max(s-x1,0)-c1+c2-max(s-x2,0)+c3-max(s-x3,0));
grid on;
%covered put=short put+short stock
p=3;
x=100;
s=90:1:110;
xlabel('stock price');
ylabel('profit');
title('covered put');
plot(s,x-s,'*');
hold on;
plot(s,p-max(x-s,0),'--');
hold on;
plot(s,p-max(x-s,0)+x-s);
grid on;

%diagnal spread = long low x not yet expired call (c high) + short high x expired
%call(c low)
x1=95;
x2=105;
c1=5;
c2=4;
r=0.03;
t=0.5;
sigma=0.3;
s0=80:1:110;
[m,n]=size(s0);

for i=1:n;
[c(i),p(i)]=blsprice(s0(i),x2,r,t,sigma);
end;
plot(s0,c-c2,'--');
hold on;
plot(s0,c1-max(s0-x1,0),'o');
plot(s0,c-c2+c1-max(s0-x1,0));
grid on;
title('diagnal spread');
xlabel('s0');
ylabel('profit');

%protective call = long call + short stock


c=2;
x=100;
s=90:1:110;
plot(s,max(s-x,0)-c,'.');
hold on;
plot(s,-(s-x),'--');
hold on;
plot(s,max(s-x,0)-c-(s-x));
grid on;
xlabel('profit');
ylabel('stock price');
title('protective call');
plot(s,0)

作業五
%butterfly spread + BS=long high,low x call+short 2 middle x call
clear;
x1=95;x2=100;x3=105;
c1=8;c2=7;c3=4;
r=0.02;t1=0.01;t2=0.05;t3=0;sigma=0.2;
s0=80:0.1:120;
[m,n]=size(s0);

for i=1:n;
[c11(i),p11(i)]=blsprice(s0(i),x1,r,t1,sigma);
[c21(i),p21(i)]=blsprice(s0(i),x2,r,t1,sigma);
[c31(i),p31(i)]=blsprice(s0(i),x3,r,t1,sigma);

[c12(i),p12(i)]=blsprice(s0(i),x1,r,t2,sigma);
[c22(i),p22(i)]=blsprice(s0(i),x2,r,t2,sigma);
[c32(i),p32(i)]=blsprice(s0(i),x3,r,t2,sigma);
end;
plot(s0,c11+c31-2*c21-(2*c2-c1-c3),'blue');
hold on;
plot(s0,c12+c32-2*c22-(2*c2-c1-c3),'red');
plot(s0,max(s0-x1,0)+max(s0-x3,0)-2*max(s0-x2,0)-(2*c2-c1-c3),'green');
legend({'t=0.01','t=0.05','t=0'})
grid on;

% CMBO=BXM+PUT
clear;
s=40:1:60;
k1=50;
c1=3;
rf=0.02;
t=0.5
% BXM=covered call=short call+long stock
plot(s,c1-max(s-k1,0)+s-k1,'--');
hold on
% PUT=long rf+short put
p1=2;
plot(s,p1-max(k1-s,0)+k1-k1*exp(-rf*t),'--');
hold on
plot(s,c1-max(s-k1,0)+s-k1+p1-max(k1-s,0)+k1-k1*exp(-rf*t));
xlabel('stock price');
ylabel('profit');
title('CMBO');
legend('BXM','PUT','CMBO');
grid on;
% CMBO=Stock index+Strangle
clear;
s=40:1:60;
k1=50;
c1=3;
rf=0.02;
t=0.5;
% stock index
plot(s,s-k1,'--');
hold on
% strangle=short call +short put
p1=2;
plot(s,c1-max(s-k1,0)+p1-max(k1-s,0),'--');
hold on
plot(s,c1-max(s-k1,0)+p1-max(k1-s,0)+s-k1);
xlabel('stock price');
ylabel('profit');
title('CMBO');
legend('BXM','PUT','CMBO');
grid on;
作業六
% LatticeEurPutDivP
function [price,lattice]=LatticeEurPutDivP(s0,x,r,t,sigma,n,div,tau)
deltaT=t/n;
u=exp(sigma*sqrt(deltaT));
d=1/u;
p=(exp(r*deltaT)-d)/(u-d);

lattice=zeros(n+1,n+1);
for i=0:n
lattice(n+1,i+1)=max(0,x-s0*(u^i)*(d^(n-i))*(1-div));
end
for i=n-1:-1:tau
for j=0:i
lattice(i+1,j+1)=exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1));
end
end
for i=tau-1:-1:0
for j=0:i
lattice(i+1,j+1)=exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1));
end
end
price=lattice(1,1);
% LatticeEurCallivP
function [price,lattice]=LatticeEurCallDivP(s0,x,r,t,sigma,n,div,tau)
deltaT=t/n;
u=exp(sigma*sqrt(deltaT));
d=1/u;
p=(exp(r*deltaT)-d)/(u-d);

lattice=zeros(n+1,n+1);
for i=0:n
lattice(n+1,i+1)=max(0,s0*(u^i)*(d^(n-i))*(1-div)-x);
end
for i=n-1:-1:tau
for j=0:i
lattice(i+1,j+1)=exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1));
end
end
for i=tau-1:-1:0
for j=0:i
lattice(i+1,j+1)=exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1));
end
end
price=lattice(1,1);
% LatticeAmCallivP
function [price,lattice]=LatticeAmCallDivP(s0,x,r,t,sigma,n,div,tau)
deltaT=t/n;
u=exp(sigma*sqrt(deltaT));
d=1/u;
p=(exp(r*deltaT)-d)/(u-d);

lattice=zeros(n+1,n+1);
for i=0:n
lattice(n+1,i+1)=max(0,s0*(u^i)*(d^(n-i))*(1-div)-x);
end
for i=n-1:-1:tau
for j=0:i
lattice(i+1,j+1)=max(s0*(u^j)*(d^(i-j))*(1-div)-x, ...
exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end
for i=tau-1:-1:0
for j=0:i
lattice(i+1,j+1)=max(s0*u^j*d^(i-j)-x, ...
exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end
price=lattice(1,1);
%LatticeEurCallDivD_vs_LatticeAmCallDivD
price_eur=LatticeEurCallDivD(50,50,0.02,1,0.2,10,5,3);
price_am=LatticeAmCallDivD(50,50,0.02,1,0.2,10,5,3);
price_eur
price_am

% LatticeEurPutDivD
function [price,lattice]=LatticeEurPutDivD(s0,x,r,t,sigma,n,div,tau)
deltaT=t/n;
u=exp(sigma*sqrt(deltaT));
d=1/u;
p=(exp(r*deltaT)-d)/(u-d);
s0a=s0-div*exp(-r*tau*deltaT);

lattice=zeros(n+1,n+1);
for i=0:n
lattice(n+1,i+1)=max(0,x-s0a*(u^i)*(d^(n-i)));
end
for i=n-1:-1:tau
for j=0:i
lattice(i+1,j+1)=exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1));
end
end
for i=tau-1:-1:0
for j=0:i
lattice(i+1,j+1)=exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1));
end
end
price=lattice(1,1);

%LatticeEurCallDivD
function [price,lattice]=LatticeEurCallDivD(s0,x,r,t,sigma,n,div,tau)
deltaT=t/n;
u=exp(sigma*sqrt(deltaT));
d=1/u;
p=(exp(r*deltaT)-d)/(u-d);
s0a=s0-div*exp(-r*tau*deltaT);

lattice=zeros(n+1,n+1);
for i=0:n
lattice(n+1,i+1)=max(0,s0a*(u^i)*(d^(n-i))-x);
end
for i=n-1:-1:tau
for j=0:i
lattice(i+1,j+1)=exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1));
end
end
for i=tau-1:-1:0
for j=0:i
lattice(i+1,j+1)=exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1));
end
end
price=lattice(1,1);
% LatticeAmCallDivD
function [price,lattice]=LatticeAmCallDivD(s0,x,r,t,sigma,n,div,tau)
deltaT=t/n;
u=exp(sigma*sqrt(deltaT));
d=1/u;
p=(exp(r*deltaT)-d)/(u-d);
s0a=s0-div*exp(-r*tau*deltaT);

lattice=zeros(n+1,n+1);
for i=0:n
lattice(n+1,i+1)=max(0,s0a*(u^i)*(d^(n-i))-x);
end
for i=n-1:-1:tau
for j=0:i
lattice(i+1,j+1)=max(s0a*u^j*d^(i-j)-x, ...
exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end
for i=tau-1:-1:0
for j=0:i
lattice(i+1,j+1)=max(s0a*u^j*d^(i-j)-x+div*exp(-r*(tau-i)*deltaT), ...
exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end
price=lattice(1,1);
%LatticeEurCallDivD_vs_LatticeAmCallDivD
price_eur=LatticeEurCallDivD(50,50,0.02,1,0.2,10,5,3);
price_am=LatticeAmCallDivD(50,50,0.02,1,0.2,10,5,3);
price_eur
price_am

作業九
% TriEurPut
function [price,lattice]=TriEurPut(s0,x,r,t,sigma,n,lambda)
deltaT=t/n;
u=exp(lambda*sigma*sqrt(deltaT));
d=1/u;
pu=1/(2*lambda^2)+(r-(sigma^2/2))*sqrt(deltaT)/(2*lambda*sigma);
pm=1-1/(lambda^2);
pd=1-pu-pm;
lattice=zeros(n+1,2*n+1);
for j=1:n+1
lattice(n+1,j)=max(0,x-s0*(d^(n-j+1)));
end
for j=n+2:2*n+1
lattice(n+1,j)=max(0,x-s0*(u^(j-n-1)));
end
for i=n-1:-1:0
for j=1:2*i+1
lattice(i+1,j)=exp(-r*deltaT)*(pd*lattice(i+2,j)+pm*lattice(i+2,j+1)+pd*lattice(i+2,j+2));
end
end
price=lattice(1,1);

% Price_Tri
[price,lattice]=TriEurPut(100,100,0.02,1,0.5,3,1)

% TriAmPut
function [price,lattice]=TriAmPut(s0,x,r,t,sigma,n,lambda)
deltaT=t/n;
u=exp(lambda*sigma*sqrt(deltaT));
d=1/u;
pu=1/(2*lambda^2)+(r-(sigma^2/2))*sqrt(deltaT)/(2*lambda*sigma);
pm=1-1/(lambda^2);
pd=1-pu-pm;
lattice=zeros(n+1,2*n+1);
for j=1:n+1
lattice(n+1,j)=max(0,x-s0*(d^(n-j+1)));
end
for j=n+2:2*n+1
lattice(n+1,j)=max(0,x-s0*(u^(j-n-1)));
end
for i=n-1:-1:0
for j=1:i+1
lattice(i+1,j)=max(x-s0*(d^(i-j+1)),exp(-
r*deltaT)*(pd*lattice(i+2,j)+pm*lattice(i+2,j+1)+pd*lattice(i+2,j+2)));
end
for k=i+2:(2*i+1)
lattice(i+1,k)=max(x-s0*(u^(k-i-1)),exp(-
r*deltaT)*(pd*lattice(i+2,k)+pm*lattice(i+2,k+1)+pd*lattice(i+2,k+2)));
end
end
price=lattice(1,1);
% Price_tri
[price,lattice]=TriAmPut(100,100,0.02,1,0.5,3,1)

作業十
% european put
function [price,ci] = MCEurPut(s0,x,r,t,sigma,step)
nut=(r-0.5*sigma^2)*t;
sit=sigma*sqrt(t);
discpayoff=exp(-r*t)*max(0,x-s0*exp(nut+sit*randn(step,1)));
[price,pricesig,ci]=normfit(discpayoff);
% Price_eur
[price,ci] = MCEurPut(50,50,0.03,0.5,0.2,500)

% cash-or-nothing call
function [price,ci]=MCcncall(s0,x,cash,r,t,sigma,run)
% geometric brownian motion
nut=(r-0.5*sigma^2)*t;
sit=sigma*sqrt(t);

stockprice=s0*exp(nut+sit*randn(run,1));
clip=find(stockprice>x);
stockprice(clip)=cash;

payoff=exp(-r*t)*max(0,stockprice-x);
[price,varprice,ci]=normfit(payoff);
% Price_CN
[price,ci]=MCcncall(100,105,110,0.02,0.6,0.2,5000)

作業十三
%Sallie Mae
function [price,lattice]=SallieMae(s0,x1,x2,x3,x4,r,sigma,m,ret)
t=5;
n=5*m;
deltaT=t/n;
u=exp(sigma*sqrt(deltaT));
d=1/u;
p=(exp(r*deltaT)-d)/(u-d);

lattice=zeros(n+1,n+1);

for j=0:n
lattice(n+1,j+1)=max(ret,50*(1-(x4^-1)*s0*u^j*d^(n-j)));
end

for i=n-1:-1:4/5*n
for j=0:i
lattice(i+1,j+1)=max(50*(1-(x4^-1)*s0*u^j*d^(i-j)),exp(-r*deltaT)*( ...
p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end

for i=4/5*n-1:-1:3/5*n
for j=0:i
lattice(i+1,j+1)=max(50*(1-(x3^-1)*s0*u^j*d^(i-j)),exp(-r*deltaT)*( ...
p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end
for i=3/5*n-1:-1:2/5*n
for j=0:i
lattice(i+1,j+1)=max(50*(1-(x2^-1)*s0*u^j*d^(i-j)),exp(-r*deltaT)*( ...
p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end

for i=2/5*n-1:-1:1/5*n
for j=0:i
lattice(i+1,j+1)=max(50*(1-(x1^-1)*s0*u^j*d^(i-j)),exp(-r*deltaT)*( ...
p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end

for i=1/5*n-1:-1:0
for j=0:i
lattice(i+1,j+1)=max(0,exp(-r*deltaT)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end

price=lattice(1,1);
% Price_Sallimae
[price,lattice]=SallieMae(1/135.058,1/131.75,1/129.5,1/127,1/124.5,0.02,0.1,2,9.25)

%MCintegration
rand('seed',0);
N=10000;
meanexp=zeros(1,N);
for i=(1:N)
meanexp(i)=mean(2*exp(2*rand(1,i)));
end
plot(1:N,meanexp);

作業十四
% Rand + Box_muller + European Put
function price = RandBoxMullerEurPut(s0,x,r,t,sigma,n)
nut = (r-0.5*sigma^2)*t;
sit = sigma*sqrt(t);
R = sqrt(-2*log(rand(n/2,1)));
Theta = rand(n/2,1);
Norm1 = R.*cos(2*pi*Theta);
Norm2 = R.*sin(2*pi*Theta);
norm = [Norm1 ; Norm2];
discpayoff = exp(-r*t)*max(0,x-s0*exp(nut-sit*norm));
price=mean(discpayoff);
% Comp_Rand_BS
[c,p]=blsprice(50,52,0.02,1,0.2);
price= RandBoxMullerEurPut(50,52,0.02,1,0.2,1000);
p
price

% Halton + Box_muller + European Put


function price = HaltonBoxMullerEurPut(s0,x,r,t,sigma,n,base1,base2)
nut = (r-0.5*sigma^2)*t;
sit = sigma*sqrt(t);
h1 = GetHalton(ceil(n/2),base1);
h2 = GetHalton(ceil(n/2),base2);
R = sqrt(-2*log(h1));
Norm1 = R.*cos(2*pi*h2);
Norm2 = R.*sin(2*pi*h2);
norm = [Norm1 ; Norm2];
discpayoff = exp(-r*t)*max(0,x-s0*exp(nut-sit*norm));
price=mean(discpayoff);
% Comp_Hal_BS
[c,p]=blsprice(50,52,0.02,1,0.2);
price= HaltonBoxMullerEurPut (50,52,0.02,1,0.2,1000,2,7);
p
price

% Rand + cdf + European Put


function price = RandCdfEurPut(s0,x,r,t,sigma,n)
nut = (r-0.5*sigma^2)*t;
sit = sigma*sqrt(t);
norm = norminv(rand(n,1));
discpayoff = exp(-r*t)*max(0,x-s0*exp(nut-sit*norm));
price=mean(discpayoff);
%Comp_Rand_BS
[c,p]=blsprice(50,52,0.02,1,0.2);
price= RandCdfEurPut (50,52,0.02,1,0.2,1000);
p
price

% Halton + CDF + European Put


function price = HaltonCdfEurPut(s0,x,r,t,sigma,n,base1)
nut = (r-0.5*sigma^2)*t;
sit = sigma*sqrt(t);
h1 = GetHalton(ceil(n),base1);
norm = norminv(h1);
discpayoff = exp(-r*t)*max(0,x-s0*exp(nut-sit*norm));
price=mean(discpayoff);
%Comp_Hal_Bs
[c,p]=blsprice(50,52,0.02,1,0.2);
price= HaltonCdfEurPut (50,52,0.02,1,0.2,1000,2);
p
price

作業十五
% Up and Out put Monte Carlo simulation
function [price,ci,ncroseed]=UOPutMC(s0,x,r,t,sigma,sb,nstep,nrep)
payoff=zeros(nrep,1);
ncroseed=0;
for i=1:nrep
path=AssetPath1(s0,r,sigma,t,nstep,1);
crossed=any(path>=sb);
if crossed==0
payoff(i)=max(0,x-path(nstep+1));
else
payoff(i)=0;
ncroseed=ncroseed+1;
end
end
[price,aux,ci]=normfit(exp(-t*t)*payoff);
% Price
[price,ci,ncroseed]=UOPutMC(110,110,0.05,2,0.4,120,1000,100)
作業十六
% EuCallExpl1.m
function price = EuCallExpl1(S0,X,r,T,sigma,Smax,dS,dt)
M = round(Smax/dS);
dS = Smax/M;
N = round(T/dt);
dt = T/N;
matval = zeros(M+1,N+1);
vetS = linspace(0,Smax,M+1)';
vetj = 0:N;
veti = 0:M;
matval(:,N+1) = max(vetS-X,0);
matval(1,:) = 0;
matval(M+1,:) = Smax-X*exp(-r*dt*(N-vetj));
a = 0.5*dt*(sigma^2*veti - r).*veti;
b = 1- dt*(sigma^2*veti.^2 + r);
c = 0.5*dt*(sigma^2*veti + r).*veti;
for j=N:-1:1
for i=2:M
matval(i,j) = a(i)*matval(i-1,j+1) + b(i)*matval(i,j+1)+ ...
c(i)*matval(i+1,j+1);
end
end
idown = floor(S0/dS);
iup = ceil(S0/dS);
if idown == iup
price = matval(idown+1,1);
else
price = matval(idown+1,1) + ...
(S0 -(idown+1)*dS)*(matval(iup+1,1) - matval(iup,1))/dS;
end
% CompBlsExpl.m Compare blsprice and BlsExpl
S0=50;
X=50;
r=0.2;
T=11/12;
sigma1=0.5;
sigma2=0.3;
Smax=100;
dS1=2;
dS2=1.5;
dS3=1;
dT=5/1200;

[c1,p04]=blsprice(S0,X,r,T,sigma1);
Exps04=EuCallExpl1(S0,X,r,T,sigma1,Smax,dS1,dT);
[c2,p03]=blsprice(S0,X,r,T,sigma2);
Exps03dS2=EuCallExpl1(S0,X,r,T,sigma2,Smax,dS1,dT);
ExpdS15=EuCallExpl1(S0,X,r,T,sigma2,Smax,dS2,dT);
ExpdS1=EuCallExpl1(S0,X,r,T,sigma2,Smax,dS3,dT);

p04
Exps04
p03
Exps03dS2
ExpdS15
ExpdS1

You might also like