# problem with lsqcurvefit

#### mouna

C++:
%--------------------------------------------------------------------------
% @description:    Attempt to estimate the parameter of the market price
%           of risk, lamda of the Vasicek model 1977 of the term structure
%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.051356;
beta = 0.1374;
sigma = 0.0118;
r = 0.012;
lamda0 = - 0.031; % Starting guess%
Function F = myfun(lamda,xdata);
F =  Ralpha +((1-exp(-beta*xdata))/(beta*xdata))*(r-Ralpha)+...
((sigma.^2)/(4*(beta.^3)*xdata)*((1-exp(-xdata)).^2));

Ralpha =(beta -(lamda*sigma)/alpha)-(sigma.^2)/(2*(alpha.^2));
[x,resnorm] = lsqcurvefit(@myfun,lamda0,xdata,ydata);
plot (xdata,ydata,lsqcurvefit);
but i get the following answer in the workspace

??? Attempt to execute SCRIPT function as a function:
C:\Program Files\MATLAB\R2009a\toolbox\matlab\lang\function.m

Error in ==> fit at 33
Function F = myfun(lamda,xdata);

please can you help me the resolve the problem

#### alain

##### Older and Wiser
I don't know anything about Matlab but... where is lamda ?

#### Mr Doe

Function F = myfun(lamda,xdata);
F = Ralpha +((1-exp(-beta*xdata))/(beta*xdata))*(r-Ralpha)+...
((sigma.^2)/(4*(beta.^3)*xdata)*((1-exp(-xdata)).^2));

write;

F = @(lambda,xdata,Ralpha,r,beta,sigma) Ralpha +((1-exp(-beta*xdata))/(beta*xdata))*(r-Ralpha)+...
((sigma.^2)/(4*(beta.^3)*xdata)*((1-exp(-xdata)).^2));

This way you are saying, F is a function of lambda, xdata, Ralpha, etc. I tend to write a new m-file specifically for this kind of things. Also, matlab is probably going to ***** here
plot (xdata,ydata,lsqcurvefit);
cause he will not know what lsqcurvefit is. I assume you want plot your data, then fit it to some curve, and then plot the fitted curve. First plot your data, then write hold on; in the prompt, and then plot the fitted curve.

@Alain: Lol, didn't even notice lambda is nowhere in the code.

#### mouna

write;

F = @(lambda,xdata,Ralpha,r,beta,sigma) Ralpha +((1-exp(-beta*xdata))/(beta*xdata))*(r-Ralpha)+...
((sigma.^2)/(4*(beta.^3)*xdata)*((1-exp(-xdata)).^2));

This way you are saying, F is a function of lambda, xdata, Ralpha, etc.

i have do it and i have run the program but i get the following message in the workspace:

Optimization terminated: first-order optimality less than OPTIONS.TolFun,
and no negative/zero curvature detected in trust region model.

I assume you want plot your data, then fit it to some curve, and then plot the fitted curve.
yes exactly what i want to do.

First plot your data, then write hold on; in the prompt, and then plot the fitted curve.
i haven't understand. please can you explain me more.

@Alain: Lol, didn't even notice lambda is nowhere in the code.

lamda is in Ralpha =@(lamda) (beta -(lamda*sigma)/alpha)-(sigma.^2)/(2*(alpha.^2));
and F is function of Ralpha so of lamda as lamda is the only unknown in Ralpha

thank you very much for helping me .

#### alain

##### Older and Wiser
(again, I don't know anything about matlab) but from the original post:

C++:
Ralpha =(beta -(lamda*sigma)/alpha)-(sigma.^2)/(2*(alpha.^2));

and

C++:
Ralpha [B]=@(lamda)[/B] (beta -([COLOR=red]lamda[/COLOR]*sigma)/alpha)-(sigma.^2)/(2*(alpha.^2));

seemed different to me.

#### mouna

(again, I don't know anything about matlab) but from the original post:

C++:
Ralpha =(beta -(lamda*sigma)/alpha)-(sigma.^2)/(2*(alpha.^2));
and

C++:
Ralpha [B]=@(lamda)[/B] (beta -([COLOR=red]lamda[/COLOR]*sigma)/alpha)-(sigma.^2)/(2*(alpha.^2));
seemed different to me.
Ralpha =@(lamda) this is to specify that Ralpha is function of lamda. and
(beta -(lamda*sigma)/alpha)-(sigma.^2)/(2*(alpha.^2)); is the function form.

#### alain

##### Older and Wiser
did you make that change in the original code?

#### alain

##### Older and Wiser
Look at the code in the original post.

#### mouna

Look at the code in the original post.
ah! yes i have put it in the original code.

Replies
2
Views
1K
Replies
0
Views
1K
Replies
7
Views
2K
Replies
0
Views
1K
Replies
6
Views
3K