
|
Table des matières
1 A parabolic problemThe following problem :
have
sigma = sqrt(2)/%pi; b = 1/%pi; L=1; T=1; hmax= sigma^2/abs(b); // state step must be lower than hmax pmin=ceil((2*L)/hmax)+1; p=maxi(pmin,20); // number of discretization points x = linspace(-L,L,p) ; // discretization points x = x(2:$-1) ; // remove boundary points. h=x(2)-x(1); // state discretization step.
sigma = sqrt(2)/%pi; b = 1/%pi; L=1; T=1;
hmax= sigma^2/abs(b); // state step must be lower than hmax
pmin= ...
p=maxi(pmin,20); // number of discretization points
x = linspace(-L,L,p) ; // discretization points
x = x(2:$-1) ; // remove boundary points.
h=x(2)-x(1); // state discretization step.
alpha = ..
beta = ..
gama = ..
n= p -2 ; // Approximate the diffusion operator
Ah= diag(beta*ones(1,n))+diag(gama*ones(1,n-1),1)+...
diag(alpha*ones(1,n-1),-1) ;
theta = ...
dtmax = ... // max time step
delta = dtmax/2;
q= ... ;
delta = ...
t = linspace(0,T,q+1); // discrete time
Bmh= ...
Ch= ...
// final condition.
deff('[y]=f(x)','y=...')
fh = feval( x' , f);
// instantaneous cost.
deff('[y]=g(x,t)','...') ;
c=feval(x,t,g); // la fonction \(c(t,x)\) }
v=zeros(n,q+1);
v(:,q+1)=fh ; // initialisation de \(v(x,T) \)}
for i=(q:-1:1) ,
v(:,i) = ...
end
plot3d(x/maxi(x),t,v/maxi(abs(v)),35,45,"X@T@V");
// compare with explicit solution.
deff('[y]=Vref(x,t)','y=exp(t-T)*sin(%pi*x)');
vref=feval(x,t,Vref);
Er=maxi(abs(vref-v))
2 finite differences for Black and Scholes equationWe will here solve by finite differences the problem :
where
which is obtained when solving the black and scholes equation in the
r = 0.05; sigma = 0.3; // constants hmax=... // max value for state step size h=.. // selected step size L=5; // x in [-L,L] // we discretize x in [-L,L] pmin= .. // min number of points p=maxi(p,100); h = L/p; x = ... // discrete points alpha = ... beta = ... gama = ... n= 2*p+1; Ah= diag(beta*ones(1,n))+diag(gama*ones(1,n-1),1)+ diag(alpha*ones(1,n-1),-1) ;
T=1; // horizon theta = 1/2; // theta used in the theta schema dtmax = ... // statbility condition delta = dtmax/2; q= ceil((T/delta)); q=maxi(q,50); delta = T/q; t = T*(0:q)/q; // discrete times S= ... R= ...
K= 40;
deff('[y]=f(x)','...') // a call or a put
fh = feval( x' , f); // the value of v at time T
v=zeros(n,q+1);
v(:,q+1)=... // final condition
// iteration
for i=(q:-1:1) ,
v(:,i) =...
end
plot3d(x/maxi(x),t,v/maxi(abs(v)))
xbasc(); i1=... i2=... xn=x(i1:i2)'; for i=1:q+1; plot2d(xn,v(i1:i2,i));end
S0=K;
d1=(1/(sigma*sqrt(T)))*(log(S0/K)+(r+sigma**2/2)*T);
d2=d1-sigma*sqrt(T);
call_price=S0*cdfnor("PQ",d1,0,1)-K*exp(-r*T)*cdfnor("PQ",d2,0,1);
put_price=-(S0-K*exp(-r*T))+(S0*cdfnor("PQ",d1,0,1)-K*exp(-r*T)*cdfnor("PQ",d2,0,1));
p = ...
// compare p with the call price or the put_price.
p -call_price ...
|