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

金融商品設計與評價 HW9

第六組

一、Halton

Halton_loop

定義 ​Halton_loop​ 函數,使用迴圈重複呼叫 ​Halton_loop ​函數,讓它的 ouput 可以跟


GetHalton​ 一樣。

% Haltontest.m
function​ Haltontest()
% use Halton
seq=zeros(10,1);
for​ i=1:10
seq(i)=Halton(i,2);
end
seq
% use Halton_loop
lseq=Halton_loop(10,2)
% use GetHalton
hseq=GetHalton(10,2)
end

function​ h=Halton(n,b)
Code
n0 = n;
h = 0;
f = 1/b;
while​ (n0 > 0)
n1 = floor(n0/b);
r = n0 - n1*b;
h = h+f*r;
f = f/b;
n0=n1;
end
end

function​ seq = Halton_loop(n,b)


% to make Halton work like GelHalton
seq=zeros(n,1);
for​ i=1:10
seq(i)=Halton(i,b);
end

end

function​ Seq = GetHalton(HowMany, Base)

Seq = zeros(HowMany,1);
NumBits = 1+ceil(log(HowMany)/log(Base));
VetBase = Base.^(-(1:NumBits));
WorkVet = zeros(1,NumBits);

for​ i=1:HowMany
​% increment last bit and carry over if necessary
j=1;
ok = 0;
​while​ ok == 0
WorkVet(j) = WorkVet(j)+1;
​if​ WorkVet(j) < Base
ok = 1;
​else
WorkVet(j) = 0;
j = j+1;
​end
​end
Seq(i) = dot(WorkVet,VetBase);
end
end
Outcome
CompBlsHalton

呼叫 ​Halton​ 函數,除了價格之外,同時顯示出信賴區間。由圖可知,Halton27 時的價格最接近


Bls 價格,但信賴區間的大小都差不多,

% CompBlsHalton.m Compare blsprice and BlsHalton


function​ CompBlsHalton()
S0=50; X=52; r=0.1; T=5/12; sigma=0.4; NRepl=5000;
Base11=2; Base12=7;
Base21=11; Base22=7;
Base31=2; Base32=4;

Bls=blsprice(S0,X,r,T,sigma);
[Halton27,CI27] =
BlsHalton(S0,X,r,T,sigma,NRepl,Base11,Base12);
[Halton117,CI117] =
Code BlsHalton(S0,X,r,T,sigma,NRepl,Base21,Base22);
[Halton24,CI24] =
BlsHalton(S0,X,r,T,sigma,NRepl,Base31,Base32);

Bls
Halton27
CI27
Halton117
CI117
Halton24
CI24
end
Outcome

二、隨機變數

Box Muller Generator by rand() or Halton

建立一function, 利用 Box Muller生成 Standard Normal 序列。參數調整 :


(1) sampsize : Uniform 亂數大小
(2) HowMany : 生成序列大小
(3) option : 0 for rand() & others for Halton
(4) Base : Halton Method基數

function​ Seq = Box_Muller(samp_size,HowMany, option, Base )


% sample size from uniform(0,1)
% generating uniform through : option=0 for rand() &
option=1 for Halton
% Halton Base

if option == 0
Seq = zeros(HowMany,1);
for i = 1:HowMany
u1 = datasample(rand(samp_size,1),1); ​% U[0,1]
u2 = datasample(rand(samp_size,1),1); ​% U[0,1]
r2 = -2*log(u1); ​% R
z1 = sqrt(r2).*cos(2*pi*u2); ​% N[0,1]
z2 = sqrt(r2).*cos(2*pi*u2); ​% N[0,1]
Seq(i) = z1;
Code
end
else
Seq = zeros(HowMany,1);
for i = 1:HowMany
u1 = datasample(GetHalton(samp_size,Base),1); ​%
U[0,1]
u2 = datasample(GetHalton(samp_size,Base),1); ​%
U[0,1]
r2 = -2*log(u1);
z1 = sqrt(r2).*cos(2*pi*u2); ​% N[0,1]
z2 = sqrt(r2).*cos(2*pi*u2); ​% N[0,1]
Seq(i) = z1;
end
end
Outcome

Halton&CLT

U1,U2是用Halton模擬出來的數列,一個數列各模擬出來12個亂數,再用CLT將U1,U2轉成標準常
態分配,接著利用兩個標準常態分配的亂數算出選擇權到期時的報酬折現後的價格。

%BlsHalton.m
function​ Price=CLT_Halton(S0,X,r,T,sigma,Base1,Base2)
nuT=(r-0.5*sigma^2)*T;
siT=sigma*sqrt(T);
%Use CLT to generate standard normals
U1=GetHalton(12,Base1);
Norm1=12*(mean(U1)-0.5);
U2=GetHalton(12,Base2);
Norm2=12*(mean(U2)-0.5);
Code Norm=[Norm1,Norm2];
DiscPayoff=exp(-r*T)*max(0,-(S0*exp(nuT+siT*Norm)-X));
Price=mean(DiscPayoff);
S0=50;
X=52;
r=0.1;
T=5/12;
sigma=0.4;
Base1=2;
Base2=7;
Outcome

Halton + Box Muller: European Put

H1和H2為利用Base1和Base2產生的Halton’s數列,再利用Box Muller的方法來產生標準常態分配
亂數的過程,其中Norm1及Norm2分別代表利用Box-Muller方法產生的兩個標準常態分配亂數。接
著將每次模擬得到的選擇權到到期時報酬折現得到DisPayoff。再將報酬取平均最後就可以得到歐
式賣權目前的價格。

%BLsHaltonPut.m
function
Price=BlsHaltonPut(S0,X,r,T,sigma,NPoints,Base1,Base2)
S0=50;
X=52;
r=0.1;
T=5/12;
sigma=0.4;
NPoints=5000;
Base1=2;
Base2=7;
nuT=(r-0.5*sigma^2)*T;
siT=sigma*sqrt(T);
%Use Box Muller to generate standard normals
H1=GetHalton(ceil(NPoints/2),Base1);
H2=GetHalton(ceil(NPoints/2),Base2);
VLog=sqrt(-2*log(H1));
Norm1=VLog.*cos(2*pi*H2);
Norm2=VLog.*sin(2*pi*H2);
Norm=[Norm1;Norm2];
DisPayoff=exp(-r*T)*max(X-S0*exp(nuT+siT*Norm),0);
Price=mean(DisPayoff);
end
%GetHalton
function​ Seq = GetHalton(HowMany,Base)
Seq = zeros(HowMany,1);
NumBits = 1+ceil(log(HowMany)/log(Base));
VetBase = Base.^(-(1:NumBits));
WorkVet = zeros(1,NumBits);
for​ i=1:HowMany
​% increment last bit and carry over if necessary
j=1;
ok = 0;
​while​ ok == 0
WorkVet(j) = WorkVet(j)+1;
​if​ WorkVet(j) < Base
ok = 1;
​else
WorkVet(j) = 0;
j = j+1;
​end
​end
Seq(i) = dot(WorkVet,VetBase);
end
end

Outcome

三、SalliMei日幣美元兌換權

利用 Binomial Tree 計算 SallieMae 日幣美元 Option

由於兌換價值會逐年變動,故不適合使用使用Black-Scholes,因此使用二項樹來評價. 若選擇權在T
期時仍然沒有執行,依然會有9.25還本.接下來利用 for loop 從後往前推算,同時比較若履約價值是否
有比不履約價值還要大,若有,則以履約價值來取代.

function [price, lattice] = SallieMae(S0,X,rjpy,rusd,T,sigma,N)


deltaT=T/N;
u=exp(sigma * sqrt(deltaT));
Code
d=1/u;
p=(exp((rjpy-rusd)*deltaT) - d)/(u-d);
lattice = zeros(N+1,N+1);
for j=0:N
lattice(N+1,j+1)=9.25;
end

for i=N-1:-1:0
if i ~= 0
for j=0:i
lattice(i+1,j+1) = max( 50*
((S0*u^j*d^(i-j))-X(i))/(S0*u^j*d^(i-j)) , ...
exp(-rusd*deltaT) *(p * lattice(i+2,j+2) + (1-p) *
lattice(i+2,j+1)));
end
else
for j=0:i
lattice(i+1,j+1) = exp(-rusd*deltaT) *(p *
lattice(i+2,j+2) + (1-p) * lattice(i+2,j+1));
end
end
end
price = lattice(1,1);

end

% Original inputs
% S0=128; X=[131.75, 129.50, 127.00, 124.50]; rjpy=0.02; rusd=0.04;
T=5; sigma=0.2; N=5;
% Inputs to issue at par
% S0=128; X=[131.75, 129.50, 127.00, 127.65]; rjpy=0.02; rusd=0.04;
T=5; sigma=0.2; N=5;

Outcome

利用 Binomial Tree 計算 SallieMae 日幣美元 Option (改成多期)

將上一題推廣,把一年切成多期,並假設每一年都可以決定要不要執行. 同樣的利用for loop 從後往前


推算,將 i *delta T ,轉成時間. 而 mod函數則是用來判斷x是否能夠整除,要是不行的話,代表不適在能
夠執行合約的時間點,因此不需要去考慮是否要履約,只需折現即可.
function [price, lattice] =
SallieMae_multiplePeriod(S0,X,rjpy,rusd,T,sigma,N)
deltaT=T/N;
u=exp(sigma * sqrt(deltaT));
d=1/u;
p=(exp((rjpy-rusd)*deltaT) - d)/(u-d);
lattice = zeros(N+1,N+1);

for j=0:N
lattice(N+1,j+1)=9.25;
end

for i=N-1:-1:0
t = i * deltaT;
if (i == 0 || mod(t,1) ~= 0 )
for j=0:i
lattice(i+1,j+1) = exp(-rusd*deltaT) *(p *
Code lattice(i+2,j+2) + (1-p) * lattice(i+2,j+1));
end
else
for j=0:i
lattice(i+1,j+1) = max( 50*
((S0*u^j*d^(i-j))-X(t))/(S0*u^j*d^(i-j)) , ...
exp(-rusd*deltaT) *(p * lattice(i+2,j+2) + (1-p) *
lattice(i+2,j+1)));
end
end
end
price = lattice(1,1);

end

% S0=128; X=[131.75, 129.50, 127.00, 124.50]; rjpy=0.02; rusd=0.04;


T=5; sigma=0.2; N=15;

Outcome

You might also like