Kalman Filters example translation from python

I want to implement this Kalman Filter from pykalman in julia


kf = KalmanFilter(
    transition_matrices = [1],
    observation_matrices = [1],
    initial_state_mean = 0,
    initial_state_covariance = 1,
    observation_covariance=1,
    transition_covariance=0.01
)

state_means, _ = kf.filter(df['Close'].values)
state_means = pd.Series(state_means.flatten(), index=df.index)
df['kma']=state_means

This is my attempt but I am getting errors when running.

y = filtered.last

# State transition matrix (F)
F = [1.0]

# Observation matrix (H)
H = [1.0]

# Process noise covariance (Q)
Q = 1e-5

# Observation noise covariance (R)
R = 1e-2

# Initial state estimate (x0)
x0 = y[1]

# Initial state covariance (P0)
P0 = 1.0


mu = measurement_update(x0, P0, y[1], H, R)
for i = 1:100
    measurement = 2.0 + randn()
    tu = time_update(get_state(mu), get_covariance(mu), F, Q)
    mu = measurement_update(get_state(tu), get_covariance(tu), measurement, H, R)
end

Which error message do you get?

Did you consider to use: Home · LowLevelParticleFilters Documentation This package includes different Kalman filters, well documented.

I would like to use LowLevelParticleFilters but I don’t know how to setup the state space to match the python example.

Could you show me an example of using it using stated parameters?

This is the error I get with KalmanFilters.jl

MethodError: no method matching -(::Float64, ::Vector{Float64})
For element-wise subtraction, use broadcasting with dot syntax: scalar .- array

Closest candidates are:
  -(::ChainRulesCore.NotImplemented, ::Any)
   @ ChainRulesCore ~/.julia/packages/ChainRulesCore/I1EbV/src/tangent_arithmetic.jl:49
  -(::Any, ::PyCall.PyObject)
   @ PyCall ~/.julia/packages/PyCall/1gn3u/src/pyoperators.jl:14
  -(::PyCall.PyObject, ::Any)
   @ PyCall ~/.julia/packages/PyCall/1gn3u/src/pyoperators.jl:13
  ...


Stacktrace:
 [1] calc_innovation(H::Vector{Float64}, x::Float64, y::Float64)
   @ KalmanFilters ~/.julia/packages/KalmanFilters/TFByI/src/kf.jl:34
 [2] measurement_update(x::Float64, P::Float64, y::Float64, H::Vector{Float64}, R::Float64; consider::Nothing)
   @ KalmanFilters ~/.julia/packages/KalmanFilters/TFByI/src/kf.jl:21
 [3] measurement_update(x::Float64, P::Float64, y::Float64, H::Vector{Float64}, R::Float64)
   @ KalmanFilters ~/.julia/packages/KalmanFilters/TFByI/src/kf.jl:20
 [4] top-level scope
   @ In[40]:24

The issue could be related to inconsistent use of scalars vs vectors. I notice your F, H are matrices for a state vector of one. But Q and R and P0 are scalars, not 1x1 matrices. And x0 looks like a scalar. My guess is you need to use 1-vectors for state and measurement, and 1x1 matrices for the covariances.

This is a common mismatch everyone can make, where I feel the error messages are hard to understand, since it’s not obvious that it’s from multiple dispatch. Matlab would probably not even notice since it treats scalars as matrices, and even if it did, it often gives a helpful message about size mismatches.

I wonder if better errors could be produced by intentionally defining mismatched scalar-vector methods that only produce semi-helpful error messages. It may also be helpful to define some pure scalar systems since they are so common. (Maybe they already are?) But still people will mismatch.

Edit: Forgot to mention the clue is method missing error. Subtracting a float and a vector suggests the mismatch.