Code covered by the BSD License

### Highlights from Kalman Filter Application two factor CIR

5.0
5.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 39.8 KB File ID: #27705 Version: 1.0

# Kalman Filter Application two factor CIR

### Nils Delava (view profile)

Estimates the parameters of the two factor CIR model on the UK German, and US term structures.

File Information
Description

2 .m files, 3 xls files with data from German, US, UK zero coupon bonds.
Files estimate the parameters on these bonds, the optimizer doesn't really work that well for this problem, so if some-one has a solution, please let me know.

For details of methodology see;

and/or
Ren-Raw Chen and Louis Scott, “Multi-Factor Cox-Ingersoll-Ross Models of the Term Structure: Estimates and Tests from a Kalman Filter Model,” The Journal of Real Estate Finance and Economics 27, no. 2 (2003): 143-172.
etc.

thanks Bill, 27493

Acknowledgements

Kalman Filter Application inspired this file.

This file inspired Similarity Solution.

Required Products xPC Target Embedded Option
MATLAB release MATLAB 7.7 (R2008b)
14 Aug 2011 ben salah

### ben salah (view profile)

how can i run this code?

Comment only
01 Aug 2011 Steve G

### Steve G (view profile)

I believe there is another problem in the code, LLtwoCIR, in the calculation of the Q matrix, the (1,1) element of which is,
I think the correct code is,
The AdjS only multiplies the second term, not both the first and second, see Chen and Scott (2003), page 147, second formula.
Thanks

Comment only
29 Jul 2011 Steve G

### Steve G (view profile)

I like the simple code, however I donot understand the following code segment:
VarS=VarS*(1-KalmanGain*H);
Given the KalmanGain definition:
KalmanGain=VarS*H'*InvVarY;
The above VarS is not symmetric, and (1-KalmanGain*H) is not correct either. I believe the corrrect calculation should be:
VarS=(eye(2)-KalmanGain*H)*VarS;

Comment? Thanks.
Steve

Comment only
24 Aug 2010 luisa cutillo

### luisa cutillo (view profile)

I am trying to replicate your results but the code as it is produces errors in the optimization part and it stops around the 50th iteration. The final mean values are very far from the true one. Moreover the numbers that I am getting are very far from the ones in CIR200.mat. Can you please help? I am just running your code as it is with matlab 2008. Thanks in advance. Luisa.

Comment only
17 Aug 2010 Christian

### Christian (view profile)

Dear Nils, dear Yan,

How do these optimizer tricks look like? I currently work on a Kalman Filter Application for a (correlated) multifactor Vasicek Model, which has got even more parameters to optimize and I am afraid the optimizer will stuck.

Christian

Comment only
15 Aug 2010 Qinyu Yu

### Qinyu Yu (view profile)

I have another question, what does the "sigmai" stand for? Is that represent to the variances of measurement errors? But why does it can be negative? could you tell me what is the definition of this paremeter? Thank you very much!

The location of this parameter as follows:
% Initialize the parameters for CIR model
theta1=para(1);
kappa1=para(2);
sigma1=para(3);
lambda1=para(4);
theta2=para(5);
kappa2=para(6);
sigma2=para(7);
lambda2=para(8);
sigmai=para(9:end);

R=eye(ncol);
for i=1:ncol
R(i,i)=sigmai(i)^2;
end

Comment only
08 Aug 2010 Nils Delava

### Nils Delava (view profile)

the initial parameters are decided with a little trial and error and some common sense. In the files I've set them as the answer because then the optimizer works better, but you are right it does not run smoothly. There is some common sense aswell, if you "surf()" the historic term structure you can see that it is always upward sloping so the risk aversion coeff. will be negative, etc.

Also the papers I've cited in the description gives some indication.

I also played around a bit with the other optimizers such as patternsearch and fmin.

When I have some time i will re-do them, as I don't think that the measurement errors (the other sigma's) should be in the optimizer. I think they should be 0.005*randn() and then resample (run the optimiser) 100 times or so to get 100 answers (slightly different) and then take the mean of that.

Comment only
03 Aug 2010 Salim

### Salim (view profile)

Hi Nils, Yan,

I am working on the CIR calibration using a Kalman filter. How do you make the optimization work? Sometimes, the fmincon function generates a warning of the form: "Matrix is close to singular or badly scaled. Results may be inaccurate". Have you found a way around that? Yan, how have you solved the optimization problem in general?

Thanks guys

Comment only
30 Jul 2010 Qinyu Yu

### Qinyu Yu (view profile)

Hi Hils, could you tell me how did you decide the initial parameter value as input ? Thank you very much!

06 Jul 2010 Nils Delava

### Nils Delava (view profile)

sent you email

Comment only
29 Jun 2010 Yan Du

### Yan Du (view profile)

Hi Nils, i was able to make the optimizer work by using some tricks. However, I would like to know how would you decide the initial parameter value as input ?

Comment only