## Calibration of Vasicek through "lsqnonlin"

### Fulvio Rizzardini (view profile)

on 10 Jan 2016
Latest activity Commented on by Brendan Hamm

### Brendan Hamm (view profile)

on 11 Jan 2016
Hi, I have to calibrate the parameters of the Vasicek model. I want to use "lsqnonlin", but it doesn't work. This is my code:
x=[0.01 0.3 0.1];
epsilon=randn;
t=[0.25, 0.5, 0.75, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 25, 30];
r=spotrate'; %they are short rate with the maturities t.
r0=spotrate(1);
rate = r0+x(1)*(x(2)-r0)*t+x(3)*sqrt(t)*epsilon; %thanks to Vasicek model
sigma = std(rate);
price = 100*exp(-t.*rate); % ZCB price
K=100./(1+t.*r); %strike price K
put= blsprice(price, K, rate+1, t, sigma) %rate+1 because, otherwise, I can have negative rate
fun=@(x) blsprice(100*exp(-t.*(r0+x(1)*(x(2)-r0)*t+x(3)*sqrt(t)*epsilon)), K, 1+(r0+x(1)*(x(2)-r0)*t+x(3)*sqrt(t)*epsilon), t, sigma)-CAP %cap is an array with the prices of some caps.
x0=[0.1, 0.3, 0.2]
z = lsqnonlin(fun, x0)
I have these errors:
Error in
@(x)blsprice(100*exp(-t.*(r0+x(1)*(x(2)-r0)*t+x(3)*sqrt(t)*epsilon)),K,1+(r0+x(1)*(x(2)-r0)*t+x(3)*sqrt(t)*epsilon),t,sigma)-CAP
Error in snls (line 329)
newfvec = feval(funfcn{3},xcurr,varargin{:});
Error in lsqncommon (line 156)
snls(funfcn,xC,lb,ub,flags.verbosity,options,defaultopt,initVals.F,initVals.J,caller,
...
Error in lsqnonlin (line 238)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,caller,...
Thank you

Walter Roberson

### Walter Roberson (view profile)

on 11 Jan 2016
Please show the complete error message. For example did it complain about matrix dimensions not being compatible? Did it complain about subscript out of range?
Brendan Hamm

### Brendan Hamm (view profile)

on 11 Jan 2016
Hard to help without more information. Quite frankly it is difficult to follow what you are trying to do.
Why have you generated one sample from the standard normal distribution, rather than a new sample for each time step? Why are you using this value in the objective? Why are there no bounds on the values of your parameters?
Also, I should point out this is NOT the solution of the Vasicek model:
rate = r0+x(1)*(x(2)-r0)*t+x(3)*sqrt(t)*epsilon; %thanks to Vasicek model
This may make you rethink your parameters in the model.
Furthermore, you are using the same sample from the normal distribution for all time steps. The link here has some information about calibrating the Vasicek model using Least-Squares and also using MLE and includes code.