You are on page 1of 15

金融商品設計與評價程式作業

110071506 計財碩一 林競妍

作業 2
% bs
S=100;
X=100;
r=0.02;
T=1;
sigma=0.2;
d1=(log(S/X)+(r+sigma^2/2)*T)/(sigma*sqrt(T))
d2=d1-(sigma*sqrt(T))

C=S*normcdf(X,d1,sigma)-X*(exp(-r*T))*(normcdf(X,d2,sigma));
P=X*(exp(-r*T))*normcdf(-d2)-S*normcdf(-d1);
disp(C);
disp(P);
%w2_q1
y=0.1;
n=3;
c=100;
PV=0;
d=1+y;
for i=1:n
PV=PV+c*y/d;
d=d*(1+y);
end
PV=PV+c*(1+y)/d;
disp(PV);

作業 3
%PlotlongputBS
clc;
clear;

x=50;
p=2;
r=0.1;
t=0.5;
sig=0.2;
St=40:0.05:60;
[m,n]=size(St);
for i=1:n
[call(i),put(i)]=blsprice(St(i),x,r,t,sig);
end
plot(St,max(x-St,0),'--black');
hold on;
plot(St,put,'*blue');
title('long put');
xlabel('S');
ylabel('profit');
axis([40 60 0 10]);

%Plotoptions
clc;
x=50; c=2;p=2;

%draw long call


St=40:0.05:60;
subplot(2,2,1);
%draw the relationship nestween the stock price and the profit
plot(St,max(St-x,0)-c);
title('long call');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);

%draw short call


subplot(2,2,2);
plot(St,c-max(St-x,0));
title('short call');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);
%draw long put
subplot(2,2,3);
plot(St,max(x-St,0)-p);
title('long put');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);

%draw short put


subplot(2,2,4);
plot(St,p-max(x-St,0));
title('short put');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);

%HW1
% Plot the variation of theta with stock price for a call option
clc;
x=50;
r=0.05;
t=0.5;
sig=0.2;
div=0;
s=30:120;

[call_theta,put_theta]=blstheta(s,x,r,t,sig,div)

figure
plot(s,call_theta,'-')
title('Theta vs. s')
xlabel('s')
ylabel('Theta')
legend('Theta')

%HW1
% Plot the variation of theta with stock price for a call option
clc;
x=50;
r=0.05;
t=0.5;
sig=0.2;
div=0;
s=30:120;

[call_gamma]=blsgamma(s,x,r,t,sig,div)

figure
plot(s,call_gamma,'-')
title('gamma vs. s')
xlabel('s')
ylabel('gamma')
legend('gamma')

%HW2
% Plot the patterns for variation of delta with time to maturity for a call
% option
clc;
x=50;
r=0.05;
s=50;
sig=0.2;
div=0;
t=0.01:0.1:10;

[call_delta]=blsdelta(s,x,r,t,sig,div)

figure
plot(t,call_delta,'-')
title('delta vs. t')
xlabel('t')
ylabel('vega')

作業 4
%Covered put
clear;
clc;
p=2;
x=100;
s0=92:1:112;
plot(s0,100-s0,'--');%sell stock
hold on;
plot(s0,-(max(x-s0,0))+p,'.');%sell put
plot(s0,100-s0-(max(x-s0,0))+p); %covered put
grid on;
title('covered put');
xlabel('s0');
ylabel('profit');
%protective call

clear;
clc;

c=2;
x=100;
s0=90:1:110;
plot(s0,100-s0,'--');%sell stock
hold on;
plot(s0,max(s0-x,0)-c,'.');%buy call
plot(s0,100-s0+max(s0-x,0)-c);%protective call
grid on;
title('protective call');
xlabel('s0');
ylabel('payoff');
%Bear Spread(call)
clear; clc;
%Buy Call with higher strike price(cheaper),and sell Call with lower strike
%price(more expensive)
c1=1;%buy call 1
c2=3;%sell call 2
x1=105;
x2=100;
s0=90:1:120;
plot(s0,max(0,s0-x1)-c1,'--');%buy call 1
hold on;
plot(s0,-(max(0,s0-x2)-c2),'.');%sell call 2
plot(s0,max(0,s0-x1)-c1-(max(0,s0-x2)-c2));

%Bear Spread(put)
clear; clc;
%Buy Put with higher strike price(more expensive),and sell Put with lower strike
%price(cheaper)
p1=3;%buy put 1
p2=1;%sell put 2
x1=105;
x2=100;

s0=90:1:120;
plot(s0,max(0,x1-s0)-p1,'--');%buy put 1
hold on;
plot(s0,-(max(0,x2-s0)-p2),'.');%sell put 2
plot(s0,max(0,x1-s0)-p1-(max(0,x2-s0)-p2));

%Put Bull Spread


%buy the put option with lower strike proce(cheaper)
clear; clc;

p1=3;%buy p1
p2=1;%sell p2
x1=105;
x2=100;
s0=95:1:120;
plot(s0,max(s0-x1,0)-p1,'--');%buy p1
hold on;
plot(s0,-(max(s0-x2,0)-p2),'.');%sell p2
plot(s0,max(s0-x1,0)-p1-(max(s0-x2,0)-p2));
clear;
clc;
%condor
%buy OTM call,put; sell closer the money call,put

c1=4;%sell closer money call


c2=1;%buy OTM call
p1=3;%sell closer money put
p2=1;%buy OTM put

xc1=99;xc2=105;xp1=100;xp2=95;

s0=90:1:110;
plot(s0,max(s0-xc2,0)-c2,'--');%buy OTM call
hold on;
plot(s0,-(max(s0-xc1,0)-c1),'--');%sell closer money call
hold on;
plot(s0,max(xp2-s0,0)-p2,'*');%buy OTM put
hold on;
plot(s0,-(max(xp1-s0,0)-p1),'*');%sell closer money put

plot(s0,max(s0-xc2,0)-c2-(max(s0-xc1,0)-c1)+max(xp2-s0,0)-p2-(max(xp1-s0,0)-
p1));

grid on;
title('Condor');
xlabel('S0');
ylabel('profit');
作業 5
%CMBO-->sell strangle+ buy stock

clear;clc;

r=0.02;
kc=105;
kp=95;
c=4;
p=5;
s0=90:1:110;
plot(s0,100-s0,'--');%stock
hold on;grid on;
plot(s0,-max(s0-kc,0)+c-max(kp-s0,0)+p,'.');%sell strangle
plot(s0,100-s0-max(s0-kc,0)+c-max(kp-s0,0)+p)

xlabel('security price');
ylabel('profit');
%CMBO-->bxm+Put
clear;clc;
%I am so confused.
% Q:What is the xlabel and the ylabel?
%I suppose xlabel is security price and ylabel is profit

%CMBO:
%BXM:buy stock and sell at the money call option

r=0.02;
kc=102;
k=100;
c=3;
p=5;

s=90:1:110;
%plot(s,-max(s-k,0)+c); sell call
%hold on;

plot(s,(s-k)-max(s-kc,0)+c,'--');%construct bxm
hold on;
plot(s,-max(k-s,0)+p,'--');%construct sell put
plot(s,(s-k)+max(s-kc,0)+c-max(k-s,0)+p);

grid on;
xlabel('security price');
ylabel('profit');
%HW_Butterfly
%buy_X1 + buy_X3 + sell_X2*2
clear;clc;
x1=95;x2=100;x3=105;
c1=5;c2=3;c3=1;
r=0.02;t1=0.05;t2=0.5;sigma=0.2;
s0=90:1:115;
[m,n]=size(s0);
for i=1:n
[c11(i),p1(i)]=blsprice(s0(i),x1,r,t1,sigma);
[c21(i),p2(i)]=blsprice(s0(i),x2,r,t1,sigma);
[c31(i),p2(i)]=blsprice(s0(i),x3,r,t1,sigma);
[c12(i),p1(i)]=blsprice(s0(i),x1,r,t2,sigma);
[c22(i),p2(i)]=blsprice(s0(i),x2,r,t2,sigma);
[c32(i),p2(i)]=blsprice(s0(i),x3,r,t2,sigma);
end

plot(s0,max(s0-x1,0)+max(s0-x3,0)-max(s0-x2,0)*2-c1-c3+c2*2);%t=0
hold on;

plot(s0,c11+c31-c21*2-c1+c2*2-c3,'--');%t=0.05
plot(s0,c12+c32-c22*2-c1+c2*2-c3,'.');%t=0.5
grid on;

xlabel('butterfly spraed');
xlabel('s0');
ylabel('profit');
legend({'t=0,t=0.05,t=0.5'},'Location','NorthEast');

作業 6
%HW1_CompLatticeBLSPut
clear;clc;
s0=50;x=50;r=0.1;sigma=0.4;t=5/12;n=50;
[BlsC,BlsP]=blsprice(s0,x,r,t,sigma);
LatticeP=zeros(1,n);
for i=1:n
LatticeP(i)=HW1_LatticeEurPut(s0,x,r,t,sigma,i);
end
plot(1:n,ones(1,n)*BlsP);
hold on;
plot(1:n,LatticeP);
function [price,lattice]=HW1_LatticeEurPut(s0,x,r,t,sigma,n)
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(0,x-s0*(u^j)*(d^(n-j)));
end
for i=n-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);
end
%command: [price,lattice]=HW_LatticeEurPut(100,100,0.02,1,0.2,10)
%HW2 LatticeAmCallvsBLS

function[price,lattice]=HW2_LatticeAmCallvsBLS(s0,x,r,t,sigma,n)

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(0,s0*(u^j)*(d^(n-j))-x);
end
for i=n-1:-1:0
for j=0:i
lattice(i+1,j+1)=max(s0*(u^j)*(d^(n-j))-x,exp(-
r*deltat)*(p*lattice(i+2,j+2)+(1-p)*lattice(i+2,j+1)));
end
end
price=lattice(1,1);

%Command Window:
[price,lattice]=HW2_LatticeAmCallvsBLS(50,50,0.1,5/12,0.4,5)

%run the following commands and compare the answers


%<1> [stockpr,optpr]=binprice(50,50,0.1,5/12,1/12,0.4,0);
%<2> optpr(1,1);

End

作業 7
function[price,lattice]=HW4_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 j=1:N+1
lattice(N+1,j)=max(0,S0*(u^j)*(d^(N+1-j))*(1-div)-X)
end
for i=N-1:-1:tau
for j=0:i
lattice(i+1,j+1)=max((S0*(u^j)*(d^(N-j))*(1-div)-X),exp(-
r*deltaT)*(p*lattice(i+2,j+1)+(1-p)*lattice(i+2,j+2)));
end
end
for i=tau-1:-1:0
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
price=lattice(1,1);
作業 9
function[price,lattice]=HW6_TriEurPut(S0,X,r,T,sigma,N,lamda)
deltaT=T/N
u=exp(lamda*sigma*sqrt(deltaT))
d=1/u
pu=1/(2*lamda^2)+(r-(sigma^2/2))*sqrt(deltaT)/(2*lamda*sigma)
pm=1-1/(lamda^2)
pd=1-pu-pm
lattice=zeros(N+1,2*N+1)
for j=1:N+1
laattice(N+1,j)=-max(0,S0*(d^(N-j+1))-X)
end
for j=N+2:2*N+1
lattice(N+1,j)=-max(0,S0*(u^(j-N-1))-X)
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)+pu*lattice(i+2,j+2))
end
end

作業 10
% BlsMC
function[Price,Cl]=HW_PUT_BlsMC(S0,X,r,T,sigma,Nrep1)
nuT=(r-0.5*sigma^2)*T;
siT=sigma*sqrt(T);
DiscPayoff=exp(-r*T)*max(0,X-S0*exp(nuT+siT*randn(Nrep1,1)));
[Price,VarPrice,Cl]=normfit(DiscPayoff);
%BlsMC(50,52,0.1,5/12,0.4,1000)

% european put
function [price,ci] = MCEurPut(s0,x,r,t,sigma,step)
nnn=(r-0.5*sigma^2)*t;
sit=sigma*sqrt(t);
discpayoff=exp(-r*t)*max(0,x-s0*exp(nnn+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
nnn=(r-0.5*sigma^2)*t;
sit=sigma*sqrt(t);
stockprice=s0*exp(nnn+sit*randn(run,1));
clip=find(stockprice>x);
stockprice(clip)=cash;
payoff=exp(-r*t)*max(0,stockprice-x);
[price,varprice,ci]=normfit(payoff);

作業 13

%MCIntegration

clear;
clc;
rand('seed',0);
N=1000;
meanexp=zeros(1,N);
for i=(1:N)
meanexp(i)=2*mean(exp(rand(1,i)));
end
exp=exp(2)-1
exp10=meanexp(10)
exp100=meanexp(100)
exp1000=meanexp(1000)
plot(1:N,meanexp)

作業 14
% Rand Boxmuller EP
function price = RBMEP(s0,x,r,t,sig,n)
R = sqrt(-2log(rand(n/2,1)));
k = (r-0.5*sig^2)*t;
S = sig*sqrt(t);
Th = rand(n/2,1);
N1 = R.*cos(2*pi*Th);
N2 = R.*sin(2*pi*Th);
N = [N1 ; N2];
P = exp(-r*t)*max(0,x-s0*exp(k-S*N));
price=mean(P);
[c,p]=blsprice(50,52,0.02,1,0.2);
price= RBMEP(50,52,0.01,1,0.2,1000);

% RandCdf EP
function price = RCdfEP(s0,x,r,t,sig,n)
k = (r-0.5*sig^2)*t;
S = sig*sqrt(t);
N = norminv(rand(n,1));
P = exp(-r*t)*max(0,x-s0*exp(k-S*N));
price=mean(P);
%Comp_Rand_BS
[c,p]=blsprice(50,52,0.02,1,0.2);
price= RCdfEP (50,55,0.02,1,0.2,10000);

% HaltonCdfEP
function price = HaltonCdfEurPut(s0,x,r,t,sig,n,B)
S = sig*sqrt(t);
k = (r-0.5*sig^2)*t;
h = GetHalton(ceil(n),B);
N = norminv(h);
P = exp(-r*t)*max(0,x-s0*exp(k-S*N));
price=mean(P);

作業 15
function [price,ci,n_Cr]=UOPutMC(s0,x,r,t,sig,b,N,M)
P=zeros(M,1);
n_Cr=0;
for i=1:M
path=AssetPath1(s0,r,sig,t,N,1);
Cr=any(path>=b);
if Cr==0
P(i)=max(0,x-path(N+1));
else
P(i)=0;
n_Cr+=1;
end
end
[price,aux,ci]=normfit(P*exp(-t*t));

You might also like