Why I can't get my output? What's wrong with line 13?

function [call,put,calldif,putdif]=bs(S,r,T,sigma,K,q)
% S is current stock price
% r is annualized risk free rate
% T is time to expiration (in years)
% sigma is annualized stock return standard deviation/volatility
% K is strike price
% q is annualized dividend rate
temp1=(log(S/K)+(r-q+sigma^2/2)*T)/(sigma*T^0.5);
temp2=temp1-sigma*T^0.5;
call=S*exp(-q*T)*normcdf(temp1)-K*exp(-r*T)*normcdf(temp2);
put=-S*exp(-q*T)*normcdf(-temp1)+K*exp(-r*T)*normcdf(-temp2);
[a,b]=blsprice(S,K,r,T,sigma,q);
[calldif,putdif]=[call,put]-[a,b]
end

댓글 수: 5

For future reference, it's better to post code, rather than an image of code, so that we can paste it into a file to test.
Sorry for this, the code is as follows,
function [call,put,calldif,putdif]=bs(S,r,T,sigma,K,q)
% S is current stock price
% r is annualized risk free rate
% T is time to expiration (in years)
% sigma is annualized stock return standard deviation/volatility
% K is strike price
% q is annualized dividend rate
temp1=(log(S/K)+(r-q+sigma^2/2)*T)/(sigma*T^0.5);
temp2=temp1-sigma*T^0.5;
call=S*exp(-q*T)*normcdf(temp1)-K*exp(-r*T)*normcdf(temp2);
put=-S*exp(-q*T)*normcdf(-temp1)+K*exp(-r*T)*normcdf(-temp2);
[a,b]=blsprice(S,K,r,T,sigma,q);
[calldif,putdif]=[call,put]-[a,b]
end
Is your code throwing an error? If so, please copy and paste the entire red text from your Command Window and paste it to a Comment here.
What are the sizes of ‘call’, ‘put’, ‘a’ and ‘b’?
>> bs(100, 0.1, 0.25, 0.5, 95, 0)
Error using -
Too many output arguments.
Error in bs (line 17)
[calldif,putdif]=[call,put]-[a,b]
call, put, a, b are all scalers. call and put are what I attempt to compute using my defined formula, and a and b are the values computed using blsprice function in MATLAB. Basically, what I am doing here is to compare the two results.
Joseph Cheng
Joseph Cheng 2015년 10월 6일
편집: Joseph Cheng 2015년 10월 6일
shawn read the cyclist's answer to remedy your comment above.

댓글을 달려면 로그인하십시오.

 채택된 답변

the cyclist
the cyclist 2015년 10월 5일
편집: the cyclist 2015년 10월 6일
[calldif, putdif] = [call,put] - [a,b]
is not valid MATLAB syntax. MATLAB can't distinguish how the right-hand variables should be sorted into the left-hand variables. The right-hand side is a 1x2 vector, and MATLAB doesn't "know" how you want that parceled out between the two output variables. It is similar to the (invalid) syntax
[x,y] = [[1 2 3],[4 5]] - [[5 6 7],[8 9]]
In that case, the right-hand side is a vector of length 5, and MATLAB would not know how to split it up between x and y.
Instead, you could do
callputdiff = [call,put] - [a,b]
calldiff = callputdiff(1);
putdiff = callputdiff(2);
Alternatively, you might not need to separate them at all, just keeping them in the vector while you operate on it. Similarly, perhaps call and put could just have been in a vector themselves.

댓글 수: 1

Many thanks, I think I'll just change my output to a vector called "callputdiff".

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Financial Toolbox에 대해 자세히 알아보기

태그

질문:

2015년 10월 5일

댓글:

2015년 10월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by