Professional Documents
Culture Documents
HW 9
HW 9
第六組
一、Halton
Halton_loop
% 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
end
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
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
二、隨機變數
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
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日幣美元兌換權
由於兌換價值會逐年變動,故不適合使用使用Black-Scholes,因此使用二項樹來評價. 若選擇權在T
期時仍然沒有執行,依然會有9.25還本.接下來利用 for loop 從後往前推算,同時比較若履約價值是否
有比不履約價值還要大,若有,則以履約價值來取代.
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
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
Outcome