Skip to content

Instantly share code, notes, and snippets.

@mschauer
Created August 23, 2023 17:58
Show Gist options
  • Save mschauer/2845fcf62570fac67a03f3a9b7fc7365 to your computer and use it in GitHub Desktop.
Save mschauer/2845fcf62570fac67a03f3a9b7fc7365 to your computer and use it in GitHub Desktop.
Fancy mean with Kalman filter
using Kalman
using Kalman.GaussianDistributions
using Statistics, LinearAlgebra
# prior for time 0
x0 = 0.0
P0 = floatmax(x0)
# observation operator
H = 1.0
# (mock) data
ys = [2, 3, 2, 2, 1, 5, 4, 4, 4, 4]
# Kalman filter
R = var(ys)
p = Gaussian(x0, P0)
for i in 1:length(ys)
global p
p, yres, _ = Kalman.correct(Kalman.JosephForm(), p, (Gaussian(ys[i], R), H))
end
@show p
@mschauer
Copy link
Author

julia> include("test/mean.jl")
p = Gaussian{Float64, Float64}(3.1, 0.16555555555555562)
Gaussian{Float64, Float64}(3.1, 0.16555555555555562)

julia> mean_and_var(ys) ./ (1, (length(ys)))
(3.1, 0.1655555555555556)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment