Trying to input a range of numbers and generate a matrix...
Mostrar comentarios más antiguos
Hi
I am trying to input a mesh into a slight variant of the standard Black-Scholes function. I wish to vary the 'S' and the 't'. I am using the linspace for this so that I generate 100 equally spaced points in equally spaced intervals.
function C=bsf3(S,t)
% Here our function is C=bsf3(S,t,--all the rest given ---K,r,sigma,T)
% We will construct the Black-Scholes formula for
% a European call option
% We set up our variables:
T=1; % Time to Expiry
K=1; % Strike Price
r=0.05; % Riskless Interest Rate
sigma=0.25; % Market Volatility
tau=T-t;
if tau>0
d1=(log(S/K)+(r+0.5*sigma^2)*tau)/(sigma*sqrt(tau));
d2=d1-sigma*sqrt(tau);
% From standard Black-Scholes materials
N1=0.5*(1+erf(d1/sqrt(2)));
N2=0.5*(1+erf(d2/sqrt(2)));
C=S*N1-K*exp(-r*tau)*N2;
else
C=max(S-K,0);
end
I try to call bsf3 using the following:
>> [S,t]=meshgrid(linspace(0,2),linspace(0,1));
>> C=bsf3(S,t);
>> mesh(S,t,C)
C seems to be a 100 x 100 matrix with repeated rows, which is not what I am looking for as each row should be different. In particular the mesh has no 'skew' or tilt.
How do I generate C by varying the input along S and along t so that I have 100x100 different entries?
Should I be using different inputs?
Thanks
Joe
Respuesta aceptada
Más respuestas (1)
Joseph Cheng
el 15 de Mayo de 2015
Editada: Joseph Cheng
el 15 de Mayo de 2015
Okay so i'm not familiar with the Black-Scholes method or the name doesn't ring a bell but looking at the code is see a few issues that is causing your repeated rows. The first is that the if statement does not work the way you are using it. it is not performing the tau>0 for each instance of tau>0. if you use the debugger and put a breakpoint at that spot you'll see what is going on. The second thing i notice is that you are not performing an element by element multiplication or division such that you're still doing the matrix multiplication of sum(row*column) which i do not think you are going for.
I quickly went through and adapted the code (hopefully i kept the purpose intact) but it should give you an idea of what needs to be done.
function C=bsf3(S,t)
% Here our function is C=bsf3(S,t,--all the rest given ---K,r,sigma,T)
% We will construct the Black-Scholes formula for
% a European call option
% We set up our variables:
T=1; % Time to Expiry
K=1; % Strike Price
r=0.05; % Riskless Interest Rate
sigma=0.25; % Market Volatility
tau=T-t;
C=zeros(size(S));
d1 = C;
d2 = C;
iftau=tau>0;
d1(iftau)=(log(S(iftau)/K)+(r+0.5*sigma^2)*tau(iftau))./(sigma*sqrt(tau(iftau)));
d2(iftau)=d1(iftau)-sigma*sqrt(tau(iftau));
% From standard Black-Scholes materials
N1=0.5*(1+erf(d1/sqrt(2)));
N2=0.5*(1+erf(d2/sqrt(2)));
C(iftau)=S(iftau).*N1(iftau)-K*exp(-r*tau(iftau)).*N2(iftau);
C(~iftau)=max(S(~iftau)-K,0);
So by defining the components as 100x100 matrixes i can use the logical matrix iftau to perform the calculations for each point of when tau>0 is true. then in the last line the inverse of iftau is used perform last for each position of tau<0 in other words when tau>0 is false.
1 comentario
Joe Bannen
el 18 de Mayo de 2015
Editada: Joe Bannen
el 18 de Mayo de 2015
Categorías
Más información sobre Time Series Events en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!