# Kalman Filter + DLM Package in R

8 messages
Open this post in threaded view
|

## Kalman Filter + DLM Package in R

 Hi I plan to estimate time varying model with DLM package in R, but I can not modified DLM package for Kalman Filter Mean Reverting  Model. For example: R(it)= Alpha(it)+ Beta(it)R(mt)+ V(it)   KF Mean Reverting Alpha(it)= Alphabar(i)+ phi* (Alpha(it-1)-Alphabar(i))+W(i1t) Beta(it)= Betabar(i)+ phi* (Beta(it-1)-Betahabar(i))+W(i2t) Please let me know how to do that. Regards, Serdar
Open this post in threaded view
|

## Re: Kalman Filter + DLM Package in R

 This is Kalman Filter random walk process code : library(dlm) buildCAPM<-function(u){         dlmModReg(rm,dV=exp(u[1]),dW=exp(u[2:3]))  # rm market } outMLE<-dlmMLE(rt,parm=rep(0,3),buildCAPM)  # rt return of indusrty mod<-buildCAPM(outMLE\$par) outFilter<-dlmFilter(rt,mod) mae<-mean(abs(outFilter\$f)-rt)   # MAE mse<-mean(((outFilter\$f)-rt)^2) #MSE What I am asking how to modify this code as Kalman Filter Mean Reverting For example: R(it)= Alpha(it)+ Beta(it)R(mt)+ V(it) KF Mean Reverting Alpha(it)= Alphabar(i)+ phi* (Alpha(it-1)-Alphabar(i))+W(i1t) Beta(it)= Betabar(i)+ phi* (Beta(it-1)-Betahabar(i))+W(i2t) Regards, Serdar
Open this post in threaded view
|

## Re: Kalman Filter + DLM Package in R

 Seder, Your question is a little unclear, so it's not possible to give you a precise answer. Could you clarify your question? It's also helpful if you briefly describe your application -- for example, tell us what time series are you modeling. First, is phi a given? Or do you intend phi to be a parameter (and, hence, estimated by dlmMLE)? Second, are alphabar and betabar given? Or are they parameters which must be estimated? If alphabar, and betabar are given, I suggest recentering alpha and beta around alphabar and betabar: subtract alphabar from alpha, and subtract betabar from beta. That will greatly simplify the recursion equations for alpha and beta:    alpha[i,t] = phi*alpha[i,t] + W[i,1,t]    beta[i,t] = phi*beta[i,t] + W[i,2,t] These equations fit easily into the state-space paradigm of DLM, and phi becomes just another parameter to estimate using buildCAPM and dlmMLE. If alphabar and betabar are parameters, not givens, then the situation is a little more complicated: you will need to incorporate them into the G matrix of the DLM model and also incorporate them into buildCAPM for estimation by dlmMLE. By the way, are you aware that you must have abs(phi) < 1.0? Otherwise these equations are not mean-reverting. Finally, are you familiar with the Triantafyllopoulos-Montana model of mean-reverting spreads? Your state-space equations are very similar to theirs, so perhaps you could build on their work.   Paul Teetor, Elgin, IL  USA http://quanttrader.info/public________________________________  From: nserdar <[hidden email]> To: [hidden email] Sent: Thursday, October 25, 2012 6:22 PM Subject: Re: [R-SIG-Finance] Kalman Filter + DLM Package in R This is Kalman Filter random walk process code : library(dlm) buildCAPM<-function(u){     dlmModReg(rm,dV=exp(u[1]),dW=exp(u[2:3]))  # rm market } outMLE<-dlmMLE(rt,parm=rep(0,3),buildCAPM)  # rt return of indusrty mod<-buildCAPM(outMLE\$par) outFilter<-dlmFilter(rt,mod) mae<-mean(abs(outFilter\$f)-rt)   # MAE mse<-mean(((outFilter\$f)-rt)^2) #MSE What I am asking how to modify this code as Kalman Filter Mean Reverting For example: R(it)= Alpha(it)+ Beta(it)R(mt)+ V(it) KF Mean Reverting Alpha(it)= Alphabar(i)+ phi* (Alpha(it-1)-Alphabar(i))+W(i1t) Beta(it)= Betabar(i)+ phi* (Beta(it-1)-Betahabar(i))+W(i2t) Regards, Serdar -- View this message in context: http://r.789695.n4.nabble.com/Kalman-Filter-DLM-Package-in-R-tp4647470p4647474.htmlSent from the Rmetrics mailing  list archive at Nabble.com. _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Open this post in threaded view
|

## Re: Kalman Filter + DLM Package in R

 Hi Paul: I like your idea of subtraction but, even if alphabar and betabar are not time varying, rather than centering around the grand means, I think he needs to center around the respective mean at that point in time ( since he's got it indexed by i, I think that's what OP means ). But, once OP does that, as you pointed out,  then it can easily be recast in the state space form. So, nice idea. Mark P.S: It might be better to use phi_1 and phi_2 rather than assume that both parameters have the same AR coefficient. Then, if the estimates are not significantly different, you can change them to phi later. Forcing both parameters to have the same AR coefficient seems restrictive to me. On Tue, Oct 30, 2012 at 9:37 PM, Paul Teetor <[hidden email]> wrote: > > > Seder, > > Your question is a little unclear, so it's not possible to give you a > precise answer. Could you clarify your question? It's also helpful if you > briefly describe your application -- for example, tell us what time series > are you modeling. > > First, is phi a given? Or do you intend phi to be a parameter (and, hence, > estimated by dlmMLE)? > > Second, are alphabar and betabar given? Or are they parameters which must > be estimated? > > If alphabar, and betabar are given, I suggest recentering alpha and beta > around alphabar and betabar: subtract alphabar from alpha, and subtract > betabar from beta. That will greatly simplify the recursion equations for > alpha and beta: > >    alpha[i,t] = phi*alpha[i,t] + W[i,1,t] > >    beta[i,t] = phi*beta[i,t] + W[i,2,t] > > These equations fit easily into the state-space paradigm of DLM, and phi > becomes just another parameter to estimate using buildCAPM and dlmMLE. > > If alphabar and betabar are parameters, not givens, then the situation is > a little more complicated: you will need to incorporate them into the G > matrix of the DLM model and also incorporate them into buildCAPM for > estimation by dlmMLE. > > By the way, are you aware that you must have abs(phi) < 1.0? Otherwise > these equations are not mean-reverting. > > > Finally, are you familiar with the Triantafyllopoulos-Montana model of > mean-reverting spreads? Your state-space equations are very similar to > theirs, so perhaps you could build on their work. > > Paul Teetor, Elgin, IL  USA > http://quanttrader.info/public> > > ________________________________ >  From: nserdar <[hidden email]> > To: [hidden email] > Sent: Thursday, October 25, 2012 6:22 PM > Subject: Re: [R-SIG-Finance] Kalman Filter + DLM Package in R > > This is Kalman Filter random walk process code : > > library(dlm) > > buildCAPM<-function(u){ > >     dlmModReg(rm,dV=exp(u[1]),dW=exp(u[2:3]))  # rm market > > } > > outMLE<-dlmMLE(rt,parm=rep(0,3),buildCAPM)  # rt return of indusrty > > mod<-buildCAPM(outMLE\$par) > > outFilter<-dlmFilter(rt,mod) > > mae<-mean(abs(outFilter\$f)-rt)   # MAE > > mse<-mean(((outFilter\$f)-rt)^2) #MSE > > What I am asking how to modify this code as Kalman Filter Mean Reverting > > For example: > > R(it)= Alpha(it)+ Beta(it)R(mt)+ V(it) > > KF Mean Reverting > > Alpha(it)= Alphabar(i)+ phi* (Alpha(it-1)-Alphabar(i))+W(i1t) > Beta(it)= Betabar(i)+ phi* (Beta(it-1)-Betahabar(i))+W(i2t) > > Regards, > Serdar > > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Kalman-Filter-DLM-Package-in-R-tp4647470p4647474.html> Sent from the Rmetrics mailing >  list archive at Nabble.com. > > _______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions > should go. >         [[alternative HTML version deleted]] > > > _______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions > should go. >         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Open this post in threaded view
|

## Re: Kalman Filter + DLM Package in R

 In reply to this post by Paul Teetor Thanks for your response Model: R(it)= Alpha(it)+ Beta(it)R(mt)+ V(it) KF Mean Reverting Alpha(it)= Alphabar(i)+ phi* (Alpha(it-1)-Alphabar(i))+W(i1t) Beta(it)= Betabar(i)+ phi* (Beta(it-1)-Betahabar(i))+W(i2t) Parameters are  estimated in  MLE function, these are Phi_1, Phi_2 ,  Alpha_bar, Beta_bar, Variance(Wi1t), Variance(Wi2t) and  Variance (Vit) Some restrictions I need in this process: 0 <=  Phi_1  and Phi_2  < =1 Variances >0 Alpha_bar and Beta_bar  are close to least square results. But some resources suggest Alpha is constant, otherwise multicollinearity problem can be raised. Regards, Serdar
Open this post in threaded view
|

## Re: Kalman Filter + DLM Package in R

 Thanks for the clarification, Serdar. Three more questions. Mark Leeds reminded us that your variables are indexed by "i". I assumed that referred to the "i-th" security in your CAPM model. Is that correct? Do you really want phi_1 and phi_2 to be identical for all securities (that is, for all "i")? Also, I believe that four parameters are missing in your list: the initial values for alpha, beta, alphabar, and betabar (at time t = 0). Or do you have known, initial values? Paul   Paul Teetor, Elgin, IL  USA http://quanttrader.info/public________________________________  From: nserdar <[hidden email]> To: [hidden email] Sent: Wednesday, October 31, 2012 3:36 AM Subject: Re: [R-SIG-Finance] Kalman Filter + DLM Package in R Thanks for your response Model: R(it)= Alpha(it)+ Beta(it)R(mt)+ V(it) KF Mean Reverting Alpha(it)= Alphabar(i)+ phi* (Alpha(it-1)-Alphabar(i))+W(i1t) Beta(it)= Betabar(i)+ phi* (Beta(it-1)-Betahabar(i))+W(i2t) Parameters are  estimated in  MLE function, these are Phi_1, Phi_2 ,  Alpha_bar, Beta_bar, Variance(Wi1t), Variance(Wi2t) and Variance (Vit) Some restrictions I need in this process: 0 <=  Phi_1  and Phi_2  < =1 Variances >0 Alpha_bar and Beta_bar  are close to least square results. But some resources suggest Alpha is constant, otherwise multicollinearity problem can be raised. Regards, Serdar -- View this message in context: http://r.789695.n4.nabble.com/Kalman-Filter-DLM-Package-in-R-tp4647470p4647970.htmlSent from the Rmetrics mailing list archive at Nabble.com. _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Open this post in threaded view
|

## Re: Kalman Filter + DLM Package in R

 This post was updated on . Thanks, i is defined as each industries, sectors etc. All parameters should define for each sectors. I defined initial values for those parameters, phi_1 = 0.5 Phi_2 = 0.5 Alpha_bar=  OLS  regression results  (intercept) from first 10 observation Beta_bar= OLS    regression results  (beta) from first 10 observation Var(Wi1t) = 1.001 Var(Wi2t) = 1.001 Var(Vit) = 1.001 DLM and other packages just only focus on KF Random Walk process, none of those does not provide any ideas about KF mean reverting. Regards, Ser
Open this post in threaded view
|

## Re: Kalman Filter + DLM Package in R

 In reply to this post by Paul Teetor good point paul. so the bar variables must be the grand means of each stock ? definitely not mean up to that point in time. my bad there. On Wed, Oct 31, 2012 at 7:04 AM, Paul Teetor <[hidden email]> wrote: > Thanks for the clarification, Serdar. Three more questions. > > Mark Leeds reminded us that your variables are indexed by "i". I assumed > that referred to the "i-th" security in your CAPM model. Is that correct? > > Do you really want phi_1 and phi_2 to be identical for all securities > (that is, for all "i")? > > Also, I believe that four parameters are missing in your list: the initial > values for alpha, beta, alphabar, and betabar (at time t = 0). Or do you > have known, initial values? > > Paul > > > Paul Teetor, Elgin, IL  USA > http://quanttrader.info/public> > > ________________________________ >  From: nserdar <[hidden email]> > To: [hidden email] > Sent: Wednesday, October 31, 2012 3:36 AM > Subject: Re: [R-SIG-Finance] Kalman Filter + DLM Package in R > > Thanks for your response > > Model: > > R(it)= Alpha(it)+ Beta(it)R(mt)+ V(it) > > KF Mean Reverting > > Alpha(it)= Alphabar(i)+ phi* (Alpha(it-1)-Alphabar(i))+W(i1t) > Beta(it)= Betabar(i)+ phi* (Beta(it-1)-Betahabar(i))+W(i2t) > > Parameters are  estimated in  MLE function, these are > > Phi_1, Phi_2 ,  Alpha_bar, Beta_bar, Variance(Wi1t), Variance(Wi2t) and > Variance (Vit) > > Some restrictions I need in this process: > > 0 <=  Phi_1  and Phi_2  < =1 > > Variances >0 > > Alpha_bar and Beta_bar  are close to least square results. > > But some resources suggest Alpha is constant, otherwise multicollinearity > problem can be raised. > > Regards, > Serdar > > > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Kalman-Filter-DLM-Package-in-R-tp4647470p4647970.html> Sent from the Rmetrics mailing list archive at Nabble.com. > > _______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions > should go. >         [[alternative HTML version deleted]] > > > _______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance> -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions > should go. >         [[alternative HTML version deleted]] _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance-- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.