I’m making 2d state space model by using Turing.jl
Here is my code. x[t] is state equation and y[t] is observation equation.
#Multivariate SSM
#sample model
# model
using Turing
using Random, Distributions
using Plots
using StaticArrays,LinearAlgebra
using Libtask
B = [0.9 0.1; 0.0 0.9]
Σ_X = [0.2 0.1; 0.1 0.2]
Σ_Y = [0.1 0.05; 0.05 0.1]
X = zeros(T, 2)
X[1, :] = [0, 0]
Y = zeros(T, 2)
for t in 2:T
X[t, :] = B * X[t-1, :] + rand(MvNormal(Σ_X))
Y[t, :] = X[t, :] + rand(MvNormal(Σ_Y))
end
#plot data
plot(Y[:, 1], label="Y1", title="Multivariate State Space Model", xlabel="t", ylabel="Y", legend=:bottomright)
plot!(Y[:, 2], label="Y2")
plot!(X[:, 1], label="X1")
plot!(X[:, 2], label="X2")
@model function SSM_2D(y)
T = size(y, 1)
k = size(y, 2)
#prior
Σ_x ~ InverseWishart(2, [1.0 0.0; 0.0 1.0])
Σ_y ~ InverseWishart(2, [1.0 0.0; 0.0 1.0])
B = zeros(k, k )
for i in 1:k
for j in 1:k
B[i,j] ~ Normal(0, 1)
end
end
x = tzeros(T, k)
for t in 2:T
μ_x = B * x[t-1, :]
x[t, :] ~ MvNormal(μ_x, Σ_x)
x_t = x[t, :]
y[t, :] ~ MvNormal(x_t, Σ_y)
end
end
#MCMC sampling
model = SSM_2D(Y)
chain = sample(model, NUTS(0.65), 1000, progress=true)
but I got this error.
MethodError: no method matching MvNormal(::Vector{Any}, ::Matrix{Float64})
Closest candidates are:
MvNormal(::Tracker.TrackedVector{<:Real}, ::Matrix{<:Real})
@ DistributionsADTrackerExt ~/.julia/packages/DistributionsAD/rvsL2/ext/DistributionsADTrackerExt.jl:441
MvNormal(::AbstractVector{<:Real}, ::AbstractMatrix{<:Real})
@ Distributions ~/.julia/packages/Distributions/fYgbJ/src/multivariate/mvnormal.jl:201
I suppose it happened in for loop. Could you teach me how to fix my error?