Quantitative finance collector
C++ Matlab VBA/Excel Java Mathematica R/Splus Net Code Site Other
Apr 28

Kalman Filter Finance Revisited

Posted by abiao at 22:21 | Code » Matlab | Comments(27) | Reads(25862)
Inspired by @MichaelRW at Twitter, I decide to continue the topic on Kalman Filter following posts Kalman Filter Example and Kalman Filter Finance.

Specifically, Kalman Filter is applied to estimate the parameters of a Cox Ingersoll Ross (CIR) one factor interest rate model, (Vasicek model is simplier than CIR, so the latter is chosen as an example), it is a widely used mean-reverting process with SDE
Open in new window
A three-factor CIR model has a measurement equation
Open in new window
and a transition equation
Open in new window
source from the paper "affine term structure models: theory and implementation" downloaded at www.bankofcanada.ca/en/res/wp/2001/wp01-15a.pdf

I skip the derivation part and recommend the following two papers: "estimating and testing exponential-affine term structure models by kalman filter " and "affine term structure models: theory and implementation" to understand the transition and measurement equations. Below are the sample Matlab implementation:
function [para, sumll] = TreasuryYieldKF()
% author: biao from www.mathfinance.cn

%%CIR parameter estimation using Kalman Filter for given treasury bonds yields
% check paper ""estimating and testing exponential-affine term structure
% models by kalman filter " and "affine term structure models: theory and
% implementation" for detail
% S(t+1) = mu + F S(t) + noise(Q)
% Y(t) = A + H S(t) + noise(R)

% read data Y
Y = xlsread('ir.xls');
[nrow, ncol] = size(Y);
tau = [1/4 1/2 1 5]; % stand for 3M, 6M, 1Y, 5Y yield

para0 = [0.05, 0.1, 0.1, -0.1, 0.01*rand(1,ncol).*ones(1,ncol)];
[x, fval] = fmincon(@loglik, para0,[],[],[],[],[0.0001,0.0001,0.0001, -1, 0.00001*ones(1,ncol)],[ones(1,length(para0))],[],[],Y, tau, nrow, ncol);
para = x;
sumll = fval;

flip over to next page...
Pages: [1] [2]

Hi, abiao,  hwo do you decide the initial input is 0.05, 0.1, 0.1, -0.1 and also what's the data you attached ? From which year ? Thanks
random starting values, you are right, the results are unstable with the starting values. Partly due to local minimum optimizer. I willtry to compare the performance of different optimizer and write a post later.
Thanks ! Looking forward for it !
hi,i don't understand what it is "Y" as loglik function's parameter...please help me
Y is the interest rates passed to the log-likelihood function.
thanks for answer.now i have other questions:1) what's the last parameter of "para0".why we use a random matrix multiplied for 0.01.This value┬áis used,after, to compute R(i,i)= sigmai(i)^2 2)what does outputs represent?we have a vector of 8 values and the log-likelihood function3)in the  "Affine term-structure models:Theory and implementation" paper's table 1 there is a simulation of CIR model.How can i have mean estimate and standard deviation?
1) the last few parameters are for measurement noice, they are small numbers and I just used random numbers for arbitrarily starting values for optimization; 2) output are parameters estimated;
hi, i must implement  bank of canada paper's  simulated examples (pg. 36). how can i do  that ? where can i download data?thanks in advance
the authors simulate the data as describedin section 3.4, so you can't "download" the data.
ok. What does data (in "ir.xls")stand for?Have you simulate or download them?
the data is from the author of "estimating and testing exponential-affine term structure models by kalman filter" @ http://www.rmi.nus.edu.sg/DuanJC/
what does it means error: " Matrix is singular, close to singular or badly scaled.Results may be inaccurate. RCOND = NaN.?" What can i do? Any solution?
abiao, it returns 8 "column" numbers - but what are they? I have been fiddling with the program but it returns the same numbers...
the parameters are for theta = para(1); kappa = para(2); sigma = para(3); lambda = para(4);
%volatility of measurement error
sigmai = para(5:end);
It also returns
??? Input argument "para" is undefined.

Error in ==> loglik
when I run the program...
Pages: 1/2 First page 1 2 Next page Final page
Add a comment
Enable HTML
Enable UBB
Enable Emots
Nickname   Password   Optional
Site URI   Email   [Register]