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

Matlab exercise

101071041

Bond pricing
function [price, lattice] = LatticePricing(S0,r,T,sigma,N)
deltaT = T/N;
u=exp(sigma * sqrt(deltaT));
d=1/u;
p=(exp(r*deltaT) - d)/(u-d);
lattice = zeros(N+1,N+1);
for j=0:N
lattice(N+1,j+1)=S0*(u^j)*(d^(N-j));
if lattice(N+1,j+1)>=40
lattice(N+1,j+1)=3550;
end
if 40>lattice(N+1,j+1)>=25
lattice(N+1,j+1)=1000+(lattice(N+1,j+1)-25)*170;
end
if lattice(N+1,j+1)<25
lattice(N+1,j+1)=1000;
end
end

for i=N-1:-1:0
for j=0:i
lattice(i+1,j+1) = exp(r*deltaT) * ...
(p * lattice(i+2,j+2) + (1-p) *
lattice(i+2,j+1));
end
end
price = lattice(1,1);
for i=1:1:500
price(i)=LatticePricing(30,0.02,1,0
.2,i);
end
plot (1:1:500, price)
xlabel N
ylabel price

Bond pricing
function [price, lattice] = LatticePricing(S0,r,T,sigma,N)
deltaT = T/N;
u=exp(sigma * sqrt(deltaT));
d=1/u;
p=(exp(r*deltaT) - d)/(u-d);
lattice = zeros(N+1,N+1);
x=zeros(1,N);
for j=0:N
x(j+1)=S0*(u^j)*(d^(N-j));
if x(j+1)>=169
lattice(N+1,j+1)=1000;
end
if 169>x(j+1)>=84.5
lattice(N+1,j+1)=1000-1000*((169/x(j+1))-1);
end
if x(j+1)<84.5
lattice(N+1,j+1)=0;
end
end

for i=N-1:-1:0
for j=0:i
lattice(i+1,j+1) = exp(-r*deltaT)
* ...
(p * lattice(i+2,j+2) + (1-p) *
lattice(i+2,j+1));
end
end
price = lattice(1,1);
for i=1:1:500
price(i)=LatticePricing(100,0.02,1,0.5,i)
;
end
plot (1:1:500, price)
xlabel N
ylabel price

Monte carlo--Antithetic variates


% BlsMCAV.m
function [Price, CI] = BlsMCAV(S0,X,
r,T,sigma,NRepl)
nuT = (r - 0.5*sigma^2)*T;
siT = sigma * sqrt(T);
Veps = randn(NRepl,1);
Payoff1 = max( 0 , X-S0*exp(nuT+si
T*Veps) );
Payoff2 = max( 0 , X-S0*exp(nuT+si
T*(-Veps)) );
DiscPayoff = exp(-r*T) * 0.5 * (Payo
ff1+Payoff2);
[Price,~, CI] = normfit(DiscPayoff);

S0=50;
X=52;
r=0.1;
T=5/12;
sigma=0.4;
NRepl1=100000;
NRepl2=200000;
[c,Bls]=blsprice(S0,X,r,T,sigma)
randn('seed',0);
[MC200000, CI1] =
BlsMC(S0,X,r,T,sigma,NRepl2)
randn('seed',0);
[MCAV100000, CI2] =
BlsMCAV(S0,X,r,T,sigma,NRepl1)

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

exp =6.3891
exp10 =5.9957
exp100
=6.3768
exp1000
=6.3803


x=rand(1,1000000);
y=rand(1,1000000);
count=0;
for i=1:1000000
if x(i)*x(i)+y(i)*y(i) <=1
count=count+1;
end
end
pia=4*(count/1000000)

http://upload.wikimedia.org/wikipedia/commons/8/84/Pi_30K.gif

You might also like