LAMPSPUC / StateSpaceModels.jl

StateSpaceModels.jl is a Julia package for time-series analysis using state-space models.
https://lampspuc.github.io/StateSpaceModels.jl/latest/
MIT License
271 stars 25 forks source link

Fix all parameters and calculate just the likelihood for a filter #328

Open FelixNoessler opened 1 month ago

FelixNoessler commented 1 month ago

Hello,

is it possible to calculate the marginal log likelihood given the observations and the model parameters? I want to do thinks similar to this tutorial from PINTS with statsmodesl in python: https://github.com/pints-team/pints/blob/main/examples/interfaces/statsmodels-state-space.ipynb

A bit simplified, they do:

model = UnobservedComponents(endog = [...], level='llevel')
model.loglike(parameters)

I tried it with StateSpaceModels.jl, but I get just NaN from the loglike function:

model = LocalLevel(rand(100))
fix_hyperparameters!(model, Dict("sigma2_ε" => 5.0, "sigma2_η" => 2.0))
loglike(model)

Then, I manually set the unconstrained parameter and it worked:

model = LocalLevel(rand(100))
fix_hyperparameters!(model, Dict("sigma2_ε" => 5.0, "sigma2_η" => 2.0))
model.hyperparameters.unconstrained_values = [sqrt(5.0), sqrt(2.0)]
loglike(model)

Is this the right marginal likelihood that I can use to run a MCMC algorithm to infer the variance (and some model) parameters?

Best, Felix

raphaelsaavedra commented 1 month ago

Hey @FelixNoessler,

It's been a while since I looked at this code, but I think the reason the first try didn't work and the second did is that loglike looks for the unconstrainted values of the parameters in order to compute the loglikelihood. These unconstrained values should be populated when fitting a model.

Basically, when we implemented this, I think we didn't envision a use case where a user would want the loglikelihood without fitting a model. But yes, based on what you did, loglike will take those set values and run the Kalman iterations in order to compute the loglikelihood using the specified parameters.

Hope this helps!