ReactiveBayes / RxInfer.jl

Julia package for automated Bayesian inference on a factor graph with reactive message passing
MIT License
242 stars 24 forks source link

Kalman filtering example with system inputs #166

Closed caxelrud closed 8 months ago

caxelrud commented 8 months ago

Hi, do you have a Kalman filtering example that includes a State-Space system with inputs (u)? Generally described as:

x(n+1)=A.x(n)+B.u(n) y(n)=C.x(n)+D.u(n)

Thanks,

bartvanerp commented 8 months ago

Hi @caxelrud!

Yes we do, you can check out the mountain car example in our docs. The code there is a bit complicated, so for clarity I would recommend checking out the Kalman filtering example. The model specification in this notebook can easily be adapted to include controls, as shown below

@model function rotate_ssm(nr_samples, x0, A, B, C, Q, P)

    x = randomvar(nr_samples)                       # hidden states
    u = randomvar(nr_samples)                       # latent controls
    y = datavar(Vector{Float64}, nr_samples)        # observations

    # prior on latent state
    x_prior ~ MvNormalMeanCovariance(mean(x0), cov(x0))
    x_prev = x_prior

    for n in 1 : nr_samples

        # prior on controls
        u[n] ~ MvNormalMeanCovariance(zeros(2), I)

        # state transition with inputs
        x[n] ~ MvNormalMeanCovariance(A * x_prev + B * u[n], Q)

        # observation model
        y[n] ~ MvNormalMeanCovariance(C * x[i], P)

        x_prev = x[n]

    end

end

I hope this helps :)