- Joined
- 11/3/08
- Messages
- 18
- Points
- 11
I have make an optimization program using the lsqnonlin function but I have some problem with it.
When I run it I get the following error message. That I haven’t understand the sense of it.
??? Error using ==> blsprice at 98
Volatilities cannot be negative.
Error in ==> myfun at 2
z = blsprice(36.12,35,0.07,7/52,x) - 2.15;
Error in ==> lsqnonlin at 203
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in ==> fit2 at 44
[lamda,resnorm] = lsqnonlin(@myfun,lamda0);
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue.
Please can you help me.
C++:
function F = myfun(lamda)
%--------------------------------------------------------------------------
% @description: Attempt to estimate the parameter of the market price
%model of yields. We do this by using curve fitting to the observed data.
%
% Xdata: - vector of times to maturity .
% ydata - vector of observed yields-to-maturity that correspond to
% the matching value by the yields-to-maturity correspinf to
% the vasicek model wich has the following form:
% R(T) = R(alpha) +(r0 - (beta - R(alpha)) *
% (1- exp (- beta*T))/ (beta *T) +
% ((sigma^2)*(1- exp (- beta*T))^2)/(4*(beta^3)*T).
%
%
%where R(alpha)= (beta - ((lamda * sigma)/ alpha)- ((sigma^2)/ (2*(alpha^2)))
% and T is time to maturity.
%
% xdata = [0.5 1 2 3 5 7 10];
% ydata =[0.079 0.0782 0.0791 0.0793 0.0791 0.0802 0.0798];
%--------------------------------------------------------------------------
xdata = [0.5 1 2 3 5 7 10];
ydata = [0.079 0.0782 0.0791 0.0793 0.0791 0.0802 0.0798];
alpha = 0.035364;
beta = 0.0713;
sigma = 0.0110;
r = 0.012;
Ralpha =@(lamda) (beta -(lamda*sigma)/alpha)-(sigma.^2)/(2*(alpha.^2));
F =@(lamda,xdata,alpha,beta,sigma,r) Ralpha(lamda) +...
((1-exp(-beta*xdata))./(beta*xdata))*(r - Ralpha(lamda))+...
((sigma.^2)./(4*(beta.^3)*xdata).*((1-exp(-xdata)).^2));
lamda0 = - 0.03; % initialisation of the parameter lamda%
options = optimset('TolFun',1e-15);
[lamda,resnorm] = lsqnonlin(@myfun,lamda0,[],[],options);
When I run it I get the following error message. That I haven’t understand the sense of it.
??? Error using ==> blsprice at 98
Volatilities cannot be negative.
Error in ==> myfun at 2
z = blsprice(36.12,35,0.07,7/52,x) - 2.15;
Error in ==> lsqnonlin at 203
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in ==> fit2 at 44
[lamda,resnorm] = lsqnonlin(@myfun,lamda0);
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue.
Please can you help me.