HW 14

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

金融商品設計與評價 HW14

第六組

一、FINITE DIFFERENCE METHOD - IMPLICIT METHOD

European Call

使用 ​EuPutImpl.m ​原本的程式碼修改,在 boundary conditions 的部分將到期日的報酬改為


S-X ,且 ​matval(1,:)​與 ​matval(M+1,:)​的報酬互換,就能計算 European Call 的價格。

% EuPutImpl.m
function​ price = EuCallImpl(S0,X,r,T,sigma,Smax,dS,dt)
% set up grid and adjust increments if necessary
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;

% set up boundary conditions


matval(:,N+1) = max(vetS-X,0);
matval(1,:) = 0;
matval(M+1,:) = X*exp(-r*dt*(N-vetj));
Code
% set up the tridiagonal coefficients matrix
a = 0.5*(r*dt*veti-sigma^2*dt*(veti.^2));
b = 1+sigma^2*dt*(veti.^2)+r*dt;
c = -0.5*(r*dt*veti+sigma^2*dt*(veti.^2));
coeff = diag(a(3:M),-1) + diag(b(2:M)) +
diag(c(2:M-1),1);
%[L,U] = lu(coeff); % factorization

% solve the sequence of linear systems


aux = zeros(M-1,1);
for​ j=N:-1:1
aux(1) = - a(2) * matval(1,j);
​%matval(2:M,j) = U \ (L \ (matval(2:M,j+1) + aux));
matval(2:M,j) = coeff \ (matval(2:M,j+1) + aux);
end
% find closest point to S0 on the grid and return price
% possibly with a linear interpolation
idown = floor(S0/dS);
iup = ceil(S0/dS);
if​ idown == iup
price = matval(idown+1,1);
else
price = matval(idown+1,1) + ​...
(S0 - idown*dS)*(matval(idown+2,1) -
matval(idown+1,1))/dS;
end

end

% S0=50; X=50; r=0.1; T=5/12; sigma=0.4; Smax=100;


dS=0.5; dT=5/2400;

Outcome

Comopare BS Formula and Implicit Method

比較內建的 ​blsprice ​函數與有限差分法所計算的 European Call 之價格。從結果看來,兩


種方法所得的價格有微小的差異。

%CompBlsExpl.m Compare blsprice and BlsHalton


function​ CompBlsImpl_Call()
S0=50; X=50; r=0.1; T=5/12; sigma=0.4; Smax=100; dS=0.5;
dT=5/2400;
%or dS=2;

Code [c,p]=blsprice(S0,X,r,T,sigma);
Impl=EuCallImpl(S0,X,r,T,sigma,Smax,dS,dT);

c
Impl
end
Outcome

3D Diagram of Implicit Price

將歐式賣權的價格與標的資產價格和時間的關係,用3D圖呈現。當時間越長時,選擇權價格
會有小幅度的增加。而股價越高時,選擇權的價格則有較明顯的增加。但是當股價很接近上限
Smax=100 ​時,價格又會遞減至0。

% CompImpl3D.m Compare blsprice and BlsHalton


function​ CompImpl3D_Call()
S0=50; X=50; r=0.1; T=5/12; sigma=0.3; Smax=100; dS=1.5;
dT=5/1200;

[ExpdS1,matval,vetS,vetT]=EuCallImplall(S0,X,r,T,sigma,Sm
ax,dS,dT);

matval
ExpdS1
mesh(vetT,vetS,matval);
ylabel(​'Stock price'​); xlabel(​'Time'​); title(​'European
Call Option, Implicit Method'​);

Code
end

% EuPutImplall.m
function​ [price,matval,vetS,vetT] =
EuCallImplall(S0,X,r,T,sigma,Smax,dS,dt)

% set up grid and adjust increments if necessary


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;
vetT= linspace(0,T,N+1)';

% set up boundary conditions


matval(:,N+1) = max(vetS-X,0);
matval(1,:) = 0;
matval(M+1,:) = X*exp(-r*dt*(N-vetj));

% set up the tridiagonal coefficients matrix


a = 0.5*(r*dt*veti-sigma^2*dt*(veti.^2));
b = 1+sigma^2*dt*(veti.^2)+r*dt;
c = -0.5*(r*dt*veti+sigma^2*dt*(veti.^2));
coeff = diag(a(3:M),-1) + diag(b(2:M)) +
diag(c(2:M-1),1);
[L,U] = lu(coeff);

% solve the sequence of linear systems


aux = zeros(M-1,1);
for​ j=N:-1:1
aux(1) = - a(2) * matval(1,j);
matval(2:M,j) = U \ (L \ (matval(2:M,j+1) + aux));
end

% find closest point to S0 on the grid and return price


% possibly with a linear interpolation
idown = floor(S0/dS);
iup = ceil(S0/dS);
if​ idown == iup
price = matval(idown+1,1);
else
price = matval(idown+1,1) + ​...
(S0 - idown*dS)*(matval(idown+2,1) -
matval(idown+1,1))/dS;
end

end
Outcome

二、CRANK-NICOLSON METHOD

Down-and-Out Call

用 ​DOPutCK.m ​的程式碼,改變 boundary condition,到期時的報酬改為 S-X,就能計算


Down-and-Out Call 的價格。

% DOPutCK.m
function​ price = DOCallCK(S0,X,r,T,sigma,Sb,Smax,dS,dt)
% set up grid and adjust increments if necessary
M = round((Smax-Sb)/dS);
dS = (Smax-Sb)/M;
N = round(T/dt);
dt = T/N;
matval = zeros(M+1,N+1);
vetS = linspace(Sb,Smax,M+1)';
Code
vetj = 0:N;
veti = vetS / dS;

% set up boundary conditions


matval(:,N+1) = max(vetS-X,0);
matval(1,:) = 0;
matval(M+1,:) = 0;

% set up the coefficients matrix


alpha = 0.25*dt*( sigma^2*(veti.^2) - r*veti );
beta = -dt*0.5*( sigma^2*(veti.^2) + r );
gamma = 0.25*dt*( sigma^2*(veti.^2) + r*veti );
M1 = -diag(alpha(3:M),-1) + diag(1-beta(2:M)) -
diag(gamma(2:M-1),1);
[L,U] = lu(M1);
M2 = diag(alpha(3:M),-1) + diag(1+beta(2:M)) +
diag(gamma(2:M-1),1);

% solve the sequence of linear systems


for​ j=N:-1:1
matval(2:M,j) = U \ (L \ (M2*matval(2:M,j+1)));
end

% find closest point to S0 on the grid and return price


% possibly with a linear interpolation
idown = floor((S0-Sb)/dS);
iup = ceil((S0-Sb)/dS);
if​ idown == iup
price = matval(idown+1,1);
else
price = matval(idown+1,1) + ​...
(S0 - Sb-idown*dS)*(matval(idown+2,1) -
matval(idown+1,1))/dS;
end

end

% S0=50; X=50; r=0.1; T=5/12; sigma=0.4; Sb=40; Smax=100;


dS=0.5; dt=5/2400;

Outcome

Down-and-In Call

已知 Down-and-In Call 和 Down-and-Out Call 的價格相加後,會相等於同條件下一般的 Call


。根據此關係,我們可以用內建的 ​blsprice ​和 前一題的 ​DOCallCK ​函數,計算出
Down-and-In Call 的價格。
% CompDOPutCK.m Compare blsprice and BlsHalton
function​ CompDOCallCK()
S0=50; X=50; r=0.1; T=5/12; sigma=0.4; Sb=40; Smax=100;
dS=0.5; dT=5/2400;

%DownOutPut=DOPut(S0,X,r,T,sigma,Sb);
DownOutCallCK=DOCallCK(S0,X,r,T,sigma,Sb,Smax,dS,dT);

Code [c,p]=blsprice(S0,X,r,T,sigma);
DownInCallCK = c - DownOutCallCK;

%DownOutPut
DownOutCallCK
DownInCallCK

end

Outcome

三、AMERICAN PUT

Explicit Method

由 ​EuPutExpl1.m ​的程式碼修改 boundary conditions。到期日報酬 ​matval (:,N+1)和股


價最高時的 matval(M+1,:) 都​與原本的 European Put 相同,但因 American Put 可以提
早執行,在股價最低的狀況下 ​matval(1,:) ​報酬都會是執行價格 ​X ,不需要將 X 折現​。
另外,在逆向推導(Backward Induction)時,要檢查是否會提早執行。

% EuPutExpl1.m
function​ price = AmPutExpl1(S0,X,r,T,sigma,Smax,dS,dt)
% set up grid and adjust increments if necessary
Code 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;

% set up boundary conditions


matval(:,N+1) = max(X-vetS,0);
matval(1,:) = X; ​% no need to discount
matval(M+1,:) = 0;
% set up coefficients
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;

% solve backward in time


for​ j=N:-1:1
​for​ i=2:M
matval(i,j) = max(X-vetS(i), a(i)*matval(i-1,j+1)
+ b(i)*matval(i,j+1)+ ​...
c(i)*matval(i+1,j+1)); ​% check early exercise

​end
end

% find closest point to S0 on the grid and return price


% possibly with a linear interpolation
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

end

% S0=50; X=50; r=0.1; T=5/12; sigma=0.3; Smax=100; dS=2;


dt=5/1200;
Outcome

Implicit Method

由 ​EuPutImpl.m ​的程式碼修改 boundary conditions。到期日報酬 ​matval (:,N+1)和股


價最高時的 matval(M+1,:) 都​與原本的 European Put 相同,但因 American Put 可以提
早執行,在股價最低的狀況下 ​matval(1,:) ​報酬都會是執行價格 ​X ,不需要將 X 折現​。
另外,在逆向推導(Backward Induction)時,要檢查是否會提早執行。

% EuPutImpl.m
function​ price = AmPutImpl(S0,X,r,T,sigma,Smax,dS,dt)
% set up grid and adjust increments if necessary
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;

% set up boundary conditions


matval(:,N+1) = max(X-vetS,0);
Code matval(1,:) = X;
matval(M+1,:) = 0;

% set up the tridiagonal coefficients matrix


a = 0.5*(r*dt*veti-sigma^2*dt*(veti.^2));
b = 1+sigma^2*dt*(veti.^2)+r*dt;
c = -0.5*(r*dt*veti+sigma^2*dt*(veti.^2));
coeff = diag(a(3:M),-1) + diag(b(2:M)) +
diag(c(2:M-1),1);
%[L,U] = lu(coeff); % factorization

% solve the sequence of linear systems


aux = zeros(M-1,1);
for​ j=N:-1:1
aux(1) = - a(2) * matval(1,j);
​ matval(2:M,j) = U \ (L \ (matval(2:M,j+1) + aux));
%
matval(2:M,j) = coeff \ (matval(2:M,j+1) + aux);
​% check early exercise
​for​ i=2:M
matval(i,j) = max(X-vetS(i), matval(i,j));
​end
end

% find closest point to S0 on the grid and return price


% possibly with a linear interpolation
idown = floor(S0/dS);
iup = ceil(S0/dS);
if​ idown == iup
price = matval(idown+1,1);
else
price = matval(idown+1,1) + ​...
(S0 - idown*dS)*(matval(idown+2,1) -
matval(idown+1,1))/dS;
end

end

% S0=50; X=50; r=0.1; T=5/12; sigma=0.3; Smax=100; dS=2;


dt=5/1200;

Outcome

Crank-Nicolson Method

由 ​DOPutCK.m ​的程式碼修改 boundary conditions。到期日報酬 ​matval (:,N+1)為​ X-S


,​股價最高時的​ ​matval(M+1,:)為 ​0,且因 American Put 可以提早執行,在股價最低的狀
況下 ​matval(1,:) ​報酬都會是執行價格 ​X ,不需要將 X 折現​。另外,在逆向推導(
Backward Induction)時,要檢查是否會提早執行。

% DOPutCK.m
function​ price = AmPutCK(S0,X,r,T,sigma,Sb,Smax,dS,dt)
Code
% set up grid and adjust increments if necessary
M = round((Smax-Sb)/dS);
dS = (Smax-Sb)/M;
N = round(T/dt);
dt = T/N;
matval = zeros(M+1,N+1);
vetS = linspace(Sb,Smax,M+1)';
vetj = 0:N;
veti = vetS / dS;

% set up boundary conditions


matval(:,N+1) = max(X-vetS,0);
matval(1,:) = X;
matval(M+1,:) = 0;

% set up the coefficients matrix


alpha = 0.25*dt*( sigma^2*(veti.^2) - r*veti );
beta = -dt*0.5*( sigma^2*(veti.^2) + r );
gamma = 0.25*dt*( sigma^2*(veti.^2) + r*veti );
M1 = -diag(alpha(3:M),-1) + diag(1-beta(2:M)) -
diag(gamma(2:M-1),1);
[L,U] = lu(M1);
M2 = diag(alpha(3:M),-1) + diag(1+beta(2:M)) +
diag(gamma(2:M-1),1);

% solve the sequence of linear systems


for​ j=N:-1:1
matval(2:M,j) = U \ (L \ (M2*matval(2:M,j+1)));
​% check early exercise
​for​ i=2:M
matval(i,j) = max(X-vetS(i), matval(i,j));
​end
end

% check early exercise


for​ j=N:-1:1
​for​ i=2:M
matval(i,j) = max(X-vetS(i), matval(i,j));
​end
end

% find closest point to S0 on the grid and return price


% possibly with a linear interpolation
idown = floor((S0-Sb)/dS);
iup = ceil((S0-Sb)/dS);
if​ idown == iup
price = matval(idown+1,1);
else
price = matval(idown+1,1) + ​...
(S0 - Sb-idown*dS)*(matval(idown+2,1) -
matval(idown+1,1))/dS;
end

end

% S0=50; X=50; r=0.1; T=5/12; sigma=0.3; Smax=100; dS=2;


dt=5/1200;

Outcome

You might also like