フィルターのクリア

Matlab function gives 'ans=0' answer problem

6 ビュー (過去 30 日間)
형현
형현 2023 年 10 月 2 日
回答済み: Image Analyst 2023 年 10 月 2 日
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));
N=mid_ch(end);
dt=1/365;
mid_size=length(mid_ch)
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);
for i=1:ns
w0=randn(N,2);
w=w0*M;
for j=2:N+1
SP1(i,j-1)*exp((r-q(1)-vol(1)^2/2)*dt+vol(1)*sqrt(dt)*w(j-1,1));
SP2(i,j-1)*exp((r-q(2)-vol(2)^2/2)*dt+vol(2)*sqrt(dt)*w(j-1,1));
end
end
R1=SP1/ref_S(1);
R2=SP2/ref_S(2);
WP=min(R1,R2);
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);
----------------------------------------------------------------------------------
ns=1000;
face=10000;
val_date='06-14-2023';
mid_date= ['12-11-2023';'06-11-2024';'12-10-2024';'06-10-2025';'12-09-2025';'06-09-2026'];
strike=[0.9 0.85 0.8 0.8 0.8 0.75];
c_rate=[0.0375 0.075 0.1125 0.15 0.1875 0.225];
dummy=0.225;
ki=0.5;
ki_YesNo='No';
ref_S= [6576.8 4372];
S=[6148 4288];
r=0.075;
vol=[0.388 0.256];
rho=0.0395;
q=[0.01 0.01];
-----------------------------------------------------------------------
The upper part is the function definition and the second is data set. When I put the data set first and execute the function, Matlab program gives
ans =
0
answer. But I want is to receive the answer "ELS_Price=?"... Whats wrong?

回答 (2 件)

John D'Errico
John D'Errico 2023 年 10 月 2 日
編集済み: John D'Errico 2023 年 10 月 2 日
First, you need to understand a fundamental feature of MATLAB.
Suppose we call ANY function? It need not be one you wrote yourself. But we can write one below.
Now, I'll call that function, just as you did, like this:
a = 1;
b = 2;
addvars(a,b)
ans = 3
whos
Name Size Bytes Class Attributes a 1x1 8 double ans 1x1 8 double b 1x1 8 double cmdout 1x33 66 char
Was the variable c created in the base workspace? NO. Instead, it stuffed the answer into a dummy variable called ans.
The point is, if you want a function to return a named variabe, you need to supply the place where the result will be stored. But you can call the result anything you want. It need not be called c. c was the name we used INSIDE the function. Try it again now.
aplusb = addvars(a,b)
aplusb = 3
whos
Name Size Bytes Class Attributes a 1x1 8 double ans 1x1 8 double aplusb 1x1 8 double b 1x1 8 double cmdout 1x33 66 char
Do you see that now the result was stuffed into the variable named aplusb?
function c = addvars(a,b)
% forms the sum of a and b.
c = a+b;
end

Image Analyst
Image Analyst 2023 年 10 月 2 日
Evidently you never took the training onramp course. John gave an excellent explanation. To learn other fundamental yet essential concepts, invest 2 hours of your time here:

カテゴリ

Help Center および File ExchangeTime Series についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by