You are on page 1of 14

Homework7

101071010

CompIntegration.m

rand('seed',0);
k=2;
N=1000;
meanexp=zeros(1,N);
for i=1:N
meanexp(i)=k*mean(exp(k*rand(1,i)));
end

exp=exp(2)-1
exp10=meanexp(10)
exp100=meanexp(100)
exp1000=meanexp(1000)
plot(1:N,meanexp);

100

200

300

400

500

600

700

800

900

6.3891

1000

GetHalton.m

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
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

IrwinHall.m

function d=IrwinHall(n)
x=rand(1,n);
c=0;
for i=0:n-12
for j=1:12
c=c+x(i+j);
end
d(i+1)=c-6;
c=0;
end

CompIrwinHall.m

d=IrwinHall (100000);
a=mean(d)
b=std(d)
hist(d)
3

x 10

2.5

1.5

0.5

0
-4

-3

-2

-1

Untonorm by Box Muller.m

function [ N1, N2 ] = untonorm_BM( H1, H2 )


Vlog=sqrt(-2*log(H1));
N1=Vlog.*cos(2*pi*H2);
N2=Vlog.*sin(2*pi*H2);

Comp BoxMuller by Halton&Rand

[ H1, H2] = untonorm_BM(GetHalton(10000,5), GetHalton(10000,7) );


[ R1, R2 ] = untonorm_BM(rand(1,10000), rand(1,10000) );
d=IrwinHall(10000);
a=randn(1,10000);

%Draw histogram
subplot(3,2,1);
hist(H1);
title('BM Halton by cos');
subplot(3,2,2);
hist(H2);
title('BM Halton by sin');

Comp BoxMuller by Halton&Rand

subplot(3,2,3);
hist(R1);
title('BM Rand by cos');
subplot(3,2,4);
hist(R2);
title('BM Rand by sin');
subplot(3,2,5);
hist(d);
title('IrwinHall');
subplot(3,2,6);
hist(a);
title('Normal Distribution');

10000
BM Halton by cos

BM Halton by sin

4000

3000

3000

2000

2000
1000

1000
0
-5

-4

-3

-2

-1

0
-4

-3

-2

-1

BM Rand by cos

BM Rand by sin

3000

4000
3000

2000

2000
1000

0
-4

1000

-3

-2

-1

0
-5

-4

-3

-2

IrwinHall
3000

2000

2000

1000

1000

-3

-2

-1

Normal Distribution

3000

0
-4

-1

0
-4

-3

-2

-1

HaltonEurPut.m
function Price = HaltonEurPut(S0,X,r,T,sigma,Npoints,Base1,Base2)
nuT=(r-0.5*sigma^2)*T;
siT=sigma*sqrt(T);
%Halton
H1=GetHalton(ceil(Npoints/2),Base1);
H2=GetHalton(ceil(Npoints/2),Base2);
%Box Muller
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);

CompHaltonEurPut.m

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;
[BlsC,BlsP]=blsprice(S0,X,r,T,sigma);
Halton27 = HaltonEurPut(S0,X,r,T,sigma,NRepl,Base11,Base12);
Halton117 = HaltonEurPut(S0,X,r,T,sigma,NRepl,Base21,Base22);
Halton24 = HaltonEurPut(S0,X,r,T,sigma,NRepl,Base31,Base32);

BlsP
Halton27
Halton117
Halton24

You might also like