Where's the bug of my code as follows?
3 views (last 30 days)
Show older comments
FYI: The blsprice function returns price for call and put, for instance, [Call, Put] = blsprice(100, 95, 0.1, 0.25, 0.5) solves for: European stock options that expire in three months with an exercise price of $95. Assume that the underlying stock pays no dividend, trades at $100, and has a volatility of 50% per annum. The risk-free rate is 10% per annum.
function [SigmaCall,SigmaPut]=FindVolatility(S,r,T,K,q,c0)
% 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
% c0 is options price
temp=@(x)blsprice(S,K,r,T,x,q)-c0;
[SigmaCall,SigmaPut]=fzero(temp,10)
end
However, I came across with this error:
FindVolatility(100, 0.1, 0.25, 95, 0, 13.7)
Error using blscheck (line 103)
Volatilities cannot be negative.
Error in blsprice (line 96)
blscheck(S, X, r, T, sig, q);
Error in FindVolatility>@(x)blsprice(S,K,r,T,x,q)-c0 (line 9)
temp=@(x)blsprice(S,K,r,T,x,q)-c0;
Error in fzero (line 343)
a = x - dx; fa = FunFcn(a,varargin{:});
Error in FindVolatility (line 10)
[SigmaCall,SigmaPut]=fzero(temp,10)
0 Comments
Answers (1)
Walter Roberson
on 6 Oct 2015
fzero only restricts the range of values if you pass in a vector with two elements for x0. You only pass in a single value (10) so fzero is permitted to take x negative, which your lower level routines do not like.
2 Comments
Walter Roberson
on 6 Oct 2015
[SigmaCall,SigmaPut] = fzero(temp,[0 MaximumAllowedVolitility])
for some numeric MaximumAllowedVolitility.
Note: the two-output form of fzero returns the first output as either the x that made the function value 0 or else the x at one of the boundaries (if it was not detectably 0 over the range). The second output is returned as the value of the function ("test" in this case) at that x value; if all went well then the value of the function would be 0.
It seems unlikely to me that those two values are directly the SigmaCall and SigmaPut that you are hoping to return. It seems more likely to me that you want to take the returned volatility and compute SigmaCall and SigmaPut from it.
See Also
Categories
Find more on Financial Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!