Trying to input a range of numbers and generate a matrix...

16 Ansichten (letzte 30 Tage)
Joe Bannen
Joe Bannen am 15 Mai 2015
Kommentiert: Thorsten am 20 Mai 2015
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

Akzeptierte Antwort

Thorsten
Thorsten am 18 Mai 2015
Bearbeitet: Thorsten am 18 Mai 2015
You write "C seems to be a 100 x 100 matrix with repeated rows", but it is not. The differences between the rows are just quite small compared to the difference between the first and last column.
Try
plot(C(1,:) - C(2,:))
or
mesh(S,t,C)
view(-pi/2, 0)
There are some small differences.
You can see them also if you zoom in and use a different colormap
colormap hsv
surf(S(:,20:end-40),t(:,20:end-40),C(:,20:end-40)), shading interp
  4 Kommentare
Joe Bannen
Joe Bannen am 20 Mai 2015
Thorsten
Still having the same problem!
>> S=[1:1:10];
>> t=[1:2:20];
>> [S,t]=meshgrid(S,t);
>> C=bsf3(S,t);
>> mesh(S,t,C)
This still has C having reach row the same. This is driving me crazy! How do I plug the individual elements of the mesh and evaluate in position via bsf3?
Thanks
Joe
Thorsten
Thorsten am 20 Mai 2015
Are you sure they are 100% the same? Are you sure that you have choosen your value such that the function should return rows that are visually different in a mesh plot?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Joseph Cheng
Joseph Cheng am 15 Mai 2015
Bearbeitet: Joseph Cheng am 15 Mai 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 Kommentar
Joe Bannen
Joe Bannen am 18 Mai 2015
Bearbeitet: Joe Bannen am 18 Mai 2015
Joseph
Many, many thanks for your reply. As you can probably guess, I am a beginner with MATLAB and am just beginning to come to terms with the incredible power of the product.
Just to clarify in my own mind what is going on here:
If we take a 3x3 matrix:
A=[1 1.2 1.4;1 1.3 1.5;1 1.6 2]
I am looking for the code to take each element of this matrix and perform the function on it element wise.
Is this something which cannot be done directly but which your code adjustment enables?
Many thanks
Joe

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by