Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

 ∫02[e^x]dx = 6.

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

 Rand + Box Muller

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)

function Price = 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

 Halton + Box Muller: European Put

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

function Price = BlsHaltonBoxMuller(S0,X,r,T,sigma,NPoints,Base1,Base2);

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

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

You might also like