Matlab Black Scholes formula how to get volatility from B&S price - matlab

I'm quite beginning with matlab and have a question maybe simple ?
i got Black&Scholes formula to get a call option price with the following input parameters :
S = stock price, K = strike , r = rate, T = time to expiration , sigma = volatility
Call option price is given by :
C(S,K,r,t,sigma) = S *N(d1) - K e^-rT *N(d2)
d1 = (1/sqrt(T))*(ln(S/K)+(r+0.5*sigma^2)*T)
d2 = d1 - sigma*sqrt(T)
I have no probleme with this function but i need to get the black scholes volatility.
I have as inputs : S, K , r , T , BlackScholesPrice and i want to return sigma.
I tried with "syms sig; solve ("blackscholesequation"==blacksholesprice, sig);" but there's nothing to do, i always get an error
"Error using symfun>validateArgNames (line 211)
Second input must be a scalar or vector of unique symbolic variables.
Error in symfun (line 45)
y.vars = validateArgNames(inputs);
Error in sym/subsasgn (line 762)
C = symfun(B,[inds{:}]);
Error in normcdf>localnormcdf (line 100)
p(sigma==0 & x<mu) = 0;
Error in normcdf (line 46)
[varargout{1:max(1,nargout)}] = localnormcdf(uflag,x,varargin{:});
Error in BlackScholesInverse (line 3)
solve(S0*normcdf((log(S0/K)+(r+0.5*sig*sig)*T)*(1/(sig*sqrt(T))))-K*exp(-r*T)*normcdf((log(S0/K)+(r+0.5*sig*sig)*T)*(1/(sig*sqrt(T)))-sig*sqrt(T))==
prixBS,sig)"
If anybody got an idea it would be awesome. Thx all'

You are trying to find implied volatility. For that you can use blsimpv. It would be easier to use this function, just that you need to give the inputs according.

Related

"Unable to prove literally" error when trying to create a symbolic function based on an existing function

I need to find inverse of virtual value function of lognormal random variables. This is what I tried to do:
syms flogn(x,p1,p2)
% assume(p2<=0); %Adding this doesn't change the error
flogn(x,p1,p2) = x - logncdf(x,p1,p2,'upper')/lognpdf(x,p1,p2);
glogn = finverse(flogn,x);
But I got the error:
Error using symengine
Unable to prove 'p2 <= 0' literally. Use 'isAlways' to test the statement mathematically.
Error in sym/subsindex (line 810)
X = find(mupadmex('symobj::logical',A.s,9)) - 1;
Error in sym/privsubsasgn (line 1085)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 922)
C = privsubsasgn(L,R,inds{:});
Error in logncdf>locallogncdf (line 73)
sigma(sigma <= 0) = NaN;
Error in logncdf (line 47)
[varargout{1:max(1,nargout)}] = locallogncdf(uflag,x,varargin{:});
Error in Untitled5 (line 3)
flogn(x,p1,p2) = x - logncdf(x,p1,p2,'upper')/lognpdf(x,p1,p2);
I also tried with beta distribution, and got a similar error. How can I use logncdf with symbolic variables?
Note that you could have minimalize your sample code even more, like this:
syms flogn(x,p1,p2)
flogn(x,p1,p2) = logncdf(x,p1,p2);
It's shorter, produces the same error and helps us to focus on the source of the error message.
So, the error doesn't come from trying to inverse a function, but from trying to use an existing function for numerical calculations with symbolic variables.
The error comes from the fact that you want to create a symbolic function flogn based on existing function logncdf, and logncdf has a multiple comparisons.
With the command edit logncdf, you can read the source code of the function and see comparison at lines 73 and 76.
% Return NaN for out of range parameters.
sigma(sigma <= 0) = NaN;
% Negative data would create complex values, which erfc cannot handle.
x(x < 0) = 0;
Matlab cannot compare symbols so it throws errors.
Depending on what you really need, you can have different solutions.
Do you really need to symbolize the function flogn? Couldn't you just write it as a function then calculate the inverse of it (if it can be inversed...)?
If you really want to keep the symbolization, you can also rewrite your own function logncdf (with another name) so it does not have the comparisons. But it's still not guaranteed that you will find an inverse.

Receiving MATLAB Error Regarding Function Arguments

When running my MATLAB script below, I keep getting an error that states:
Error using spa (Line 147)
The value of the window
size must be an integer greater than 2.
Error in "projectName" G = spa(xFunction2, x)
I've tried putting in multiple types of arguments into "spa" (data, windowsize, frequency) but it still yields the same error(s). Help?
n = 1:1024;
%Signal Function
xFunction = sqrt(10)*exp(j*2*pi*0.10*n)+ sqrt(20)*exp(j*2*pi*0.20*n) + sqrt(625);
%Complex Noise Function
zFunction = 0.707*randn(size(n)) + j*0.707*randn(size(n));
%Computing the value of x(n) + sqrt(625)*z
xFunction2 = xFunction + sqrt(625)*zFunction;
G = spa(xFunction2,51);
figure(1);
plot(w, 10*log10(G));
Acording the documentation of spa the first argument is iddata type. In addition, the time serie has to be a column vector.
So, to make it works change G = spa(xFunction2,51); for G = spa(xFunction2(:),51);. To do it the correct way, convert your time serie to iddata:
Ts = 0.1; % what ever is your sampling time.
myiddata = iddata(xFunction2(:),[],Ts);
G = spa(myiddata,51);
In addition, you should use spectrum(G) or bode(G)to plot the result.

MATLAB fminsearch optimization error

I want to optimize x value in the following equation
formula_1=((x(1)+x(2).*exp(-TR./x(3)))./(1+cos(time_stamps).* exp(-TR./x(3))));
diff =#(x)sum((formula_1-img_vol).^2);
[pa,fval,exfl]= fminsearch(diff,startingvals,opts);
startingvals=[1,1,0.1];
opts = optimset('Display','off','TolFun',1e-9,'TolX',1e-9);
img_vol = 32x32x11 vector.
These all I am doing in the main script file. But the error comes out is
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
Error in biexp_main (line 65)
[pa,fval,exfl]=fminsearch(diff,startingvals,opts);
Kindly help me what I suppose to do??
It is rather difficult to reproduce your error since there is many information missing. (What is the exact error? What are the sizes of TR and time_stamps?).
What should help is:
change your function diff so that it returns a scalar if this is currently not the case (check with diff(startginvals)). Maybe change it to sum(sum(sum(.))).
As indicated in the comments: diff is independent from x in the current form. The 'indirect contribution' you mention does not work: it considers formula_1 and hence x as constant.
Putting this together, try:
TR = rand; %?
time_stamps = rand; %?
img_vol = rand(32,32,11);
formula_1 = #(x) (x(1)+x(2).*exp(-TR./x(3))) ./ (1+cos(time_stamps).*exp(-TR./x(3)));
diff = #(x) sum(sum(sum((formula_1(x)-img_vol).^2)));
opts = optimset('Display','off','TolFun',1e-9,'TolX',1e-9);
startingvals=[1,1,0.1];
[xOpt,diffOpt] = fminsearch(diff,startingvals,opts);

"Unable to prove `expr` literally..." error when trying to compare a symbol inside a function

I just started learning MATLAB and I'm trying to normalize a bump function given by
function b = bump(x)
region1 = abs(x) < 1
b(region1) = (exp(-1./(1 - x(region1).^2)))
region2 = abs(x) >= 1
b(region2) = 0
end
To do this, I need to divide by the definite integral from -1 to 1. However, when I input
syms x;
int(bump(x), -1, 1)
I get a long error message, which says
Error using symengine (line 58)
Unable to prove 'abs(x) < 1' literally. To test the statement mathematically, use isAlways.
Error in sym/subsindex (line 1554)
X = find(mupadmex('symobj::logical',A.s,9)) - 1;
Error in sym>privformat (line 2357)
x = subsindex(x)+1;
Error in sym/subsref (line 1578)
[inds{k},refs{k}] = privformat(inds{k});
Error in bump (line 3)
b(region1) = (exp(-1./(1 - x(region1).^2)))
I tried replacing abs(x)<1 with what I think is the suggested isAlways(abs(x)<1), and that removes the error, although it gives the wrong answer (it says the integral is zero).
I don't understand what does the error message means.
syms x defines x as a symbolic variable, invoking symbolic computation on x. This probably isn't what you want.
Instead, define x as some kind of input matrix, e.g. x = zeros(3);. Or, to do numeric integration, use the integral function:
integral(#bump, -1, 1)

correct function handle for integral2 in matlab

I created a function in matlab that returns a vector like
function w = W_1D(x,pos,h)
w=zeros(1,length(x));
if (h~=0)
xmpos = x-pos;
inds1 = (-h <= xmpos) & (xmpos < 0);
w(inds1) = xmpos(inds1)./h + 1;
inds2 = (0 <= xmpos) & (xmpos <= h);
w(inds2) = -xmpos(inds2)./h + 1;
else
error('h shouldn't be 0')
end
end
Thus, in the end, there is a vector w of size length(x).
Now i created a second function like
function f = W_2D(x,y,pos_1,pos_2,h)
w_x = W_1D(x,pos_1,h);
w_y = W_1D(y,pos_2,h);
f = w_x'*w_y;
end
where length(x)=length(y). Thus, the function W_2D obviously returns a matrix.
But when I now try to evaluate the integral over a rectangular domain like e.g.
V = integral2(#(x,y) W_2D(x,y,2,3,h),0,10,0,10);
matlab returns some errors:
Error using integral2Calc>integral2t/tensor (line 242)
Integrand output size does not match the input size.
Error in integral2Calc>integral2t (line 56)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 10)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 107)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
I also tried to vary something in the W_2D-function: instead of f = w_x'*w_y;
I tried f = w_x.'*w_y;
or w_y = transpose(w_y); f = kron(w_x,w_y);, but there is always this error with the Integrand output size-stuff.
Can anyone explain, where my fault is?
EDIT: After Werner's hint with the keyboard debugging method, I can tell you the following.
The first step returns w_x of type <1x154 double>, w_y is <1x192 double>, x and y are both <14x14 double>. In the next step, f appears with a value of <154x192 double>. Then everything disappears, except x and y and the matlab-function integral2Calc.m appears in the editor and it jumps to the Function Call Stack integral2t/tensor and after some more steps, the error occurs here
Z = FUN(X,Y); NFE = NFE + 1;
if FIRSTFUNEVAL
if ~isfloat(Z)
error(message('MATLAB:integral2:UnsupportedClass',class(Z)));
end
% Check that FUN is properly vectorized. This is important here
% because we (otherwise) always pass in square matrices, which
% reduces the probability of the user generating an error by
% using matrix functions instead of elementwise functions.
Z1 = FUN(X(VTSTIDX),Y(VTSTIDX)); NFE = NFE + 1;
if ~isequal(size(Z),size(X)) || ~isequal(size(Z1),size(VTSTIDX))
% Example:
% integral2(#(x,y)1,0,1,0,1)
error(message('MATLAB:integral2:funSizeMismatch'));
end
Hope that information is detailed enough...I have no idea what happenes, because my example is exact as it is given on the mathworks site about integral2, isn't it?
Maybe I should precise a bit more, what I wanna do: since W_2D gives me a surface w(x,y) of a compactly supported 2-dimensional hat-function, stored in a matrix w, I want to calculate the volume between the (x,y)-plane and the surface z=w(x,y)...
EDIT2: I still do not understand how to handle the problem, that integral2 creates matrices as inputs for my W_1D-functions, which are called in W_2D and intended to have a <1xn double>-valued input and return a <1xn double> output, but at least I can simply use the following to solve the integration over the tensor product by using two one-dimensional integral-calls, that is
V = integral(#(x)integral(#(y)W_1D(y,3,h),0,10).*W_1D(x,2,h),0,10);
This first function is quite wrong. You are not indexing the array positions while you are doing w = x inside for.
Besides, if that would work, you are returning a line vector, that is, size 1xlength(x) and when you do w_x'*w_y you are doing length(x)x1 times 1xlength(y), which would give you a matrix length(x)*length(y).
Consider correcting your function:
function w = W_1D(x,pos)
w = zeros(length(x),1); % Allocate w as column vector, so that the product gives a scalar (as I suppose that it is what you want.
for ii=1:length(x) % Here, so that is indexes w and x elements as you need
w(ii)=x(ii) - pos; % I changed your code to something that makes sense, but I don't know if that is what you want to do, you have to adapt it to work correctly.
end
end
You may also want to debug your functions, consider adding keyboard before your operations and check what they are returning using dbstep. I.e:
function f = W_2D(x,y,pos_1,pos_2)
w_x = W_1D(x,pos_1);
w_y = W_1D(y,pos_2);
keyboard
f = w_x'*w_y;
end
Execution will stop at keyboard, then you can check w_x size, w_y size, and do dbstep to go after f = w_x'*w_y and see what it returned. After you finish debug, you can do dbcont so that it will continue execution.
This answer is a draft as it is quite difficult to help you with the information you have provided. But I think you can start working the things out with this. If you have more doubts feel free to ask.