Professional Documents
Culture Documents
HW 9
HW 9
3891
clear;
rand('seed',0);
N=1000;
meanexp=zeros(1,N);
for i=(1:N)
meanexp(i)=2*mean(exp(2*rand(1,i)));
end
exp=exp(2)-1
exp10=meanexp(10)
exp100=meanexp(100)
exp1000=meanexp(1000)
plot(1:N,meanexp);
clear;
S0=50;
X=52;
r=0.1;
T=5/12;
sigma=0.4;
NPoints=5000;
Bls_Rand_BoxMuller(S0,X,r,T,sigma,NPoints)
nuT=(r-0.5*sigma^2)*T;
siT=sigma*sqrt(T);
U1=transpose(rand(1,NPoints/2));
U2=transpose(rand(1,NPoints/2));
VLog=sqrt(-2*log(U1));
Norm1=VLog.*cos(2*pi*U2);
Norm2=VLog.*sin(2*pi*U2);
Norm=[Norm1;Norm2];
DiscPayoff=exp(-r*T)*max(0,S0*exp(nuT+siT*Norm)-X);
Price=mean(DiscPayoff)
end
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=BlsHaltonBoxMuller(S0,X,r,T,sigma,NRepl,Base11,Base12);
Halton117=BlsHaltonBoxMuller(S0,X,r,T,sigma,NRepl,Base21,Base22);
Halton24=BlsHaltonBoxMuller(S0,X,r,T,sigma,NRepl,Base31,Base32);
Bls
Halton27
Halton117
Halton24
nuT=(r-0.5*sigma^2)*T;
siT=sigma*sqrt(T);
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];
DiscPayoff=exp(-r*T)*max(0,X-S0*exp(nuT+siT*Norm));
Price=mean(DiscPayoff);
end
Seq = zeros(HowMany,1);
NumBits = 1+ceil(log(HowMany)/log(Base));
VetBase = Base.^(-(1:NumBits));
WorkVet = zeros(1,NumBits);
for i=1:HowMany
j=1;
ok = 0;
while ok == 0
WorkVet(j) = WorkVet(j)+1;
ok = 1;
else
WorkVet(j) = 0;
j = j+1;
end
end
Seq(i) = dot(WorkVet,VetBase);
end
end