Hi All,
I’m trying to do a multivariable linear regression y = \Phi(A, x) using Turing, where y \in \mathbb R^N, x \in \mathbb R^N, and the function \Phi is linear in the unknown parameters A \in \mathbb R^{N \times N}. For my particular problem, the function \Phi = (\phi_1, \cdots, \phi_n)^T needs to be constructed row-by-row.
It’s straightforward to build a Turing model to infer the unknown matrix A from a dataset containing the inputs x and outputs y. However, when I run the sampler, I find the following error:
ERROR: TypeError: in typeassert, expected Float64, got ForwardDiff.Dual{Nothing,Float64,9}
Below is a minimal example to reproduce this error. I also found that the error does not occur if all the rows of \Phi are constructed in a single step.
I checked this related previous post, but the solutions there do not seem to solve the problem.
Does anybody have a suggestion about how to fix this issue?
Thanks!
using Distributions
using LinearAlgebra
using StatsBase
using Turing
function Φ(A, x)
N = size(x,1)
temp = zeros(N)
for i = 1:N
temp[i] = dot(A[i, :], x)
end
return temp
end
N = 5
number_of_samples = 100
# Generate data
A = rand(Normal(), N,N) + I(N)
xdata = rand(LogNormal(0,1), (N, number_of_samples) )
ydata = hcat([Φ(A, x) for x in eachcol(xdata)]...)
# Build model
@model function regression(x, y)
N = size(x, 1)
number_of_samples = size(x, 2)
# Priors
σ ~ truncated(Normal(0, 100), 0, Inf)
A ~ filldist(Normal(0,1), N, N)
# Equations
for k = 1: number_of_samples
# mu = A*x[:, k]
mu = Φ(A, x[:, k])
y[:, k] ~ MvNormal(mu, sqrt(σ))
end
end
# Inference
model = regression(xdata, ydata)
chain = sample(model, NUTS(0.65), 300)