helske / KFAS

KFAS: R Package for Exponential Family State Space Models
64 stars 17 forks source link

Observation updates #41

Closed mallerhand closed 3 years ago

mallerhand commented 5 years ago

Suppose I fit a model to a univariate time series, maybe like this:

y = Seatbelts[,"drivers"] model = SSModel(y ~ SSMtrend(degree=2, Q=list(matrix(NA), matrix(NA))), H=matrix(NA)) fit = fitSSM(model, inits=c(0,0,0), method="BFGS") KFS(fit$model)

and then one new consecutive observation becomes available. What code can I use to update my model and get a new prediction?

helske commented 5 years ago

There isn't really super simple solution for this as KFAS is not really meant for online filtering. Do you want to have model parameters updated as well or just new state predictions? If you wan't to update model parameters, you could just build your model, fit and filter again using the previous estimates as initial values, which should make it pretty fast. But if you just want to get new state predictions given the same hyperparameter values, then it is obviously bit of waste to run the KFS function again. You could use the standard Kalman filter formulas manually, or make a dummy model with one time point (new observation), using the state estimates and covariances of the previous model as initial values a1 and P1 for this dummy model, and so on. I should add this functionality to KFAS at some point, it should be simple given that KFAS already contains Fortran subroutine for one-step predictions...

mallerhand commented 5 years ago

thanks Jouni. I want new state predictions as each new observation arrives, based on the model as it is without having to re-run optim. (For that, thanks for the idea of using the previous estimated pars as new inits). I guess I was hoping for Kalman filtering functions. I'll try to write this but may get stuck.