What's wrong in this function

2 visualizaciones (últimos 30 días)
형현
형현 el 26 de Sept. de 2023
Editada: Voss el 26 de Sept. de 2023
function ELS_Price=StepDown_ELS_F(ns, face, val_date, mid_date, strike, c_rate, dummy, ki, ki_YesNo, ref_S, S, r, vol, rho, q)
temp_ch=datenum(mid_date);-datenum(val_date);
mid_ch=temp_ch(find(temp_ch>=0));
c_rate=c_rate(find(temp_ch>=0));
strike=strike(find(temp_ch>=0));
payment=zeros(ns,mid_size);
for i=1:ns
for j=1:mid_size
payment(i,j)=face*(1+c_rate(j));
end
end
corr=[1 rho;rho 1];
M=chol(corr);
SP1=zeros(ns,N+1);
SP1(:,1)=S(1);
SP2=zeros(ns,N+1);
SP2(:,1)=S(2);
strike_ch=WP(:,mid_ch+1);
payoff=zeros(ns,mid_size);
for i=1:ns
for j=1:mid_size
if strike_ch(i,j)>=strike(j)
payoff(i,j)=payment(i,j);
break
end
end
continue
end
for i=1:ns
if payoff(i,:)==0
switch ki_YesNo
case 'No'
ki_event=any(WP(i,:)<ki);
if ki_event==1
payoff(i,end)=face*WP(i,end);
else
payoff(i,end)=face*(1+dummy);
end
case 'Yes'
payoff(i,end)=face*WP(i,end);
otherwise
error('ki_YesNo = Yes or No');
end
end
end
exp_payoff=mean(payoff);
disc_payoff=zeros(1,mid_size);
for j=1:mid_size
disc_payoff(j)=exp_payoff(j)*exp(-r*mid_ch(j)/365);
end
ELS_Price=sum(disc_payoff);
-----------------------------------------
I build this function and put the data in below
-----------------------------------------
ns=10;
face=10000;
val_date='04-02-2007';
mid_date= ['09-05-2007';'03-05-2008';'09-05-2008';'03-05-2009'];
strike=[0.9 0.85 0.8 0.75];
c_rate=[0.055 0.11 0.165 0.22];
dummy=0.16;
ki=0.6;
ki_YesNo='No';
ref_S= [48300 86800];
S=[45800 84600];
r=0.05;
vol=[0.25 0.3];
rho=0.5;
q=[0.01 0.01];
----------------------------------------------
But there was an error
>> StepDown_ELS_F(ns, face, val_date, mid_date, strike, c_rate, dummy, ki, ki_YesNo, ref_S, SS, r, vol, rho, q)
'SS' is an unrecognizable function or variable.
But i do not type the variable 'SS' what's wrong?
  2 comentarios
Walter Roberson
Walter Roberson el 26 de Sept. de 2023
In that command line you posted, where did the SS between ref_S and r come from if you did not type it?
Dyuman Joshi
Dyuman Joshi el 26 de Sept. de 2023
Editada: Dyuman Joshi el 26 de Sept. de 2023
It could be a typo, that you typed SS instead of S.
Note that there are undefined parameters (variables/functions) in your code such as mid_size, N etc. in the function. I suggest you to check your code again, thoroughly.

Iniciar sesión para comentar.

Respuestas (2)

Voss
Voss el 26 de Sept. de 2023
Editada: Voss el 26 de Sept. de 2023
"i do not type the variable 'SS' "
You run this command:
StepDown_ELS_F(ns, face, val_date, mid_date, strike, c_rate, dummy, ki, ki_YesNo, ref_S, SS, r, vol, rho, q)
% ^^
% SS is here! ^^
The variable SS is listed in the location I indicated above.
  4 comentarios
형현
형현 el 26 de Sept. de 2023
And, the How can i solve it... it just run with sam error....
Voss
Voss el 26 de Sept. de 2023
Anyway, the names of the variables you use to call the function don't matter. What matters is the order you pass them to the function.
The error is because "SS" is undefined in the workspace you are calling the function from. "S" is defined there, so maybe you meant to use that. You could call it anything, say "bababooey"; it doesn't matter.
% define the inputs to StepDown_ELS_F():
ns=10;
face=10000;
val_date='04-02-2007';
mid_date= ['09-05-2007';'03-05-2008';'09-05-2008';'03-05-2009'];
strike=[0.9 0.85 0.8 0.75];
c_rate=[0.055 0.11 0.165 0.22];
dummy=0.16;
ki=0.6;
ki_YesNo='No';
ref_S= [48300 86800];
S=[45800 84600]; % if you want this to be used as the 11th input to StepDown_ELS_F, then you should use it down there vvv
r=0.05;
vol=[0.25 0.3];
rho=0.5;
q=[0.01 0.01];
% call StepDown_ELS_F():
StepDown_ELS_F(ns, face, val_date, mid_date, strike, c_rate, dummy, ki, ki_YesNo, ref_S, SS, r, vol, rho, q)
% ^^ but you are using SS, which is undefined
Make sure the order you pass things in matches how they are defined in the function defintion, because the order is how the function knows what's what.
a = 1;
b = 2;
c = 3;
test(a,b,c)
a=1 b=2 c=3
test(a,c,b)
a=1 b=3 c=2
some = 1;
other = 2;
names = 3;
test(some,other,names)
a=1 b=2 c=3
function test(a,b,c)
disp("a="+string(a));
disp("b="+string(b));
disp("c="+string(c));
end

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 26 de Sept. de 2023
You define your function as follows (with a break added so we can see all the input arguments on one line.)
function ELS_Price=StepDown_ELS_F(ns, face, val_date, mid_date, strike, c_rate, dummy, ...
ki, ki_YesNo, ref_S, S, r, vol, rho, q)
You define a number of variables in your script file before (I assume) calling the StepDown_ELS_F function. Each of these match a variable name in the function definition.
ns=10;
face=10000;
val_date='04-02-2007';
mid_date= ['09-05-2007';'03-05-2008';'09-05-2008';'03-05-2009'];
strike=[0.9 0.85 0.8 0.75];
c_rate=[0.055 0.11 0.165 0.22];
dummy=0.16;
ki=0.6;
ki_YesNo='No';
ref_S= [48300 86800];
S=[45800 84600];
r=0.05;
vol=[0.25 0.3];
rho=0.5;
q=[0.01 0.01];
However, the command you executed to try to run StepDown_ELS_F is the following (with the same break added so we can see all the input arguments.)
StepDown_ELS_F(ns, face, val_date, mid_date, strike, c_rate, dummy, ...
ki, ki_YesNo, ref_S, SS, r, vol, rho, q)
Note that the input argument fifth from the end of the list is SS. That variable does not exist in the workspace when you run this code. There is a variable named S in the workspace that matches the name in that position in the definition, but that does not matter to MATLAB. MATLAB doesn't say "I don't recognize the name that you specified when you called the function, let's see if there's a variable with the name I 'expect' in my caller's workspace and use that instead" or anything like that. MATLAB does a limited amount of that type of error correction, but usually it is limited to suggesting that you may have misspelled a function name.
There does not need to be any sort of connection between the name of the variable in the workspace from which call StepDown_ELS_F and the name of the variable in the definition of StepDown_ELS_F. You could define variables x1, x2, x3, x4, ... x15 and call StepDown_ELS_F with those fifteen variables as input. I wouldn't recommend it since there's nothing to indicate what each of those variables means, but it would be a legal call. You could even pass fifteen literals (not named variables) as the inputs. [I know some of your inputs can't be numbers by the way you use them, but this is just for illustration.]
StepDown_ELS_F(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) % No named variables
To resolve this problem, change the way you call StepDown_ELS_F to pass the variable S (which exists) instead of SS (which doesn't exist) as that input argument.

Categorías

Más información sobre Google en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by