Skip to content

ArgumentError: Union{} does not have elements when trying to run a state space model #2151

@sjwild

Description

@sjwild

I am trying to run a state space model, and I keep encountering the error in the subject line when using ReverseDiff. I found a previous issue raising it, but it was with Julia 1.4. (EDIT: link)

Here is a minimum reproducible example with Julia 1.10.0 and Turing 0.30.0

#Julia 1.10

using Plots, StatsPlots
using Turing, ReverseDiff, Memoization 
using LinearAlgebra


# build data
t = [1:.05:8;]
σ = .3
y = @. rand(sin(t) + Normal(0, σ))

scatter(t, y)

@model function state_space(y, 
    TT,
    ::Type{T}=Float64) where {T}

    # Priors 
    α ~ Normal(y[1], 0.001)
    τ ~ Exponential(1)
    η ~ filldist(Normal(0, 1), TT-1)
    σ ~ Exponential(1)

    # create latent variable
    x = Vector{T}(undef, TT)

    x[1] = α

    for t in 2:TT
        x[t] = x[t-1] + η[t-1] * τ
    end

    # measurement model
    y ~ MvNormal(x, σ * I)

    return x

end

n_iter = 1500
model = state_space(y, length(t))
chns = sample(model, NUTS(n_iter * 2, 0.8; adtype = AutoReverseDiff(true)), n_iter);

And here is the stacktrace

ERROR: ArgumentError: Union{} does not have elements
Stacktrace:
  [1] eltype(::Type{Union{}})
    @ Base ./abstractarray.jl:237
  [2] eltype(vi::DynamicPPL.ThreadSafeVarInfo{DynamicPPL.TypedVarInfo{…}, Vector{…}}, spl::DynamicPPL.Sampler{NUTS{…}})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/YWihz/src/abstract_varinfo.jl:366
  [3] get_matching_type(spl::DynamicPPL.Sampler{…}, vi::DynamicPPL.ThreadSafeVarInfo{…}, ::Type{…})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/YWihz/src/compiler.jl:722
  [4] matchingvalue(sampler::DynamicPPL.Sampler{…}, vi::DynamicPPL.ThreadSafeVarInfo{…}, value::Type{…})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/YWihz/src/compiler.jl:693
  [5] matchingvalue(context::DynamicPPL.SamplingContext{…}, vi::DynamicPPL.ThreadSafeVarInfo{…}, value::Type)
    @ DynamicPPL ~/.julia/packages/DynamicPPL/YWihz/src/compiler.jl:706
  [6] macro expansion
    @ ~/.julia/packages/DynamicPPL/YWihz/src/model.jl:993 [inlined]
  [7] make_evaluate_args_and_kwargs
    @ ~/.julia/packages/DynamicPPL/YWihz/src/model.jl:971 [inlined]
  [8] _evaluate!!(model::DynamicPPL.Model{…}, varinfo::DynamicPPL.ThreadSafeVarInfo{…}, context::DynamicPPL.SamplingContext{…})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/YWihz/src/model.jl:962
  [9] evaluate_threadsafe!!
    @ ~/.julia/packages/DynamicPPL/YWihz/src/model.jl:952 [inlined]
 [10] evaluate!!
    @ ~/.julia/packages/DynamicPPL/YWihz/src/model.jl:887 [inlined]
 [11] logdensity(f::LogDensityFunction{…}, θ::ReverseDiff.TrackedArray{…})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/YWihz/src/logdensityfunction.jl:94
 [12] Fix1
    @ Base ./operators.jl:1118 [inlined]
 [13] ReverseDiff.GradientTape(f::Base.Fix1{…}, input::Vector{…}, cfg::ReverseDiff.GradientConfig{…})
    @ ReverseDiff ~/.julia/packages/ReverseDiff/UJhiD/src/api/tape.jl:199
 [14] ReverseDiff.GradientTape(f::Function, input::Vector{Float64})
    @ ReverseDiff ~/.julia/packages/ReverseDiff/UJhiD/src/api/tape.jl:198
 [15] _compiledtape
    @ LogDensityProblemsADReverseDiffExt ~/.julia/packages/LogDensityProblemsAD/OQ0BL/ext/LogDensityProblemsADReverseDiffExt.jl:56 [inlined]
 [16] #ADgradient#1
    @ LogDensityProblemsADReverseDiffExt ~/.julia/packages/LogDensityProblemsAD/OQ0BL/ext/LogDensityProblemsADReverseDiffExt.jl:50 [inlined]
 [17] ADgradient(ad::AutoReverseDiff, ℓ::LogDensityFunction{…})
    @ Turing.Essential ~/.julia/packages/Turing/tdsVz/src/essential/ad.jl:44
 [18] ADgradient(ad::AutoReverseDiff, ℓ::LogDensityFunction)
    @ Turing.Essential ~/.julia/packages/Turing/tdsVz/src/essential/ad.jl:20 [inlined]
 [19] initialstep(rng::Random.TaskLocalRNG, model::DynamicPPL.Model{…}, spl::DynamicPPL.Sampler{…}, vi::DynamicPPL.TypedVarInfo{…}; init_params::Nothing, nadapts::Int64, kwargs::@Kwargs{…})
    @ Turing.Inference ~/.julia/packages/Turing/tdsVz/src/mcmc/hmc.jl:148
 [20] step(rng::Random.TaskLocalRNG, model::DynamicPPL.Model{…}, spl::DynamicPPL.Sampler{…}; initial_params::Nothing, kwargs::@Kwargs{…})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/YWihz/src/sampler.jl:116
 [21] step
    @ ~/.julia/packages/DynamicPPL/YWihz/src/sampler.jl:99 [inlined]
 [22] macro expansion
    @ ~/.julia/packages/AbstractMCMC/Es490/src/sample.jl:127 [inlined]
 [23] macro expansion
    @ ~/.julia/packages/ProgressLogging/6KXlp/src/ProgressLogging.jl:328 [inlined]
 [24] macro expansion
    @ ~/.julia/packages/AbstractMCMC/Es490/src/logging.jl:9 [inlined]
 [25] mcmcsample(rng::Random.TaskLocalRNG, model::DynamicPPL.Model{…}, sampler::DynamicPPL.Sampler{…}, N::Int64; progress::Bool, progressname::String, callback::Nothing, discard_initial::Int64, thinning::Int64, chain_type::Type, initial_state::Nothing, kwargs::@Kwargs{…})
    @ AbstractMCMC ~/.julia/packages/AbstractMCMC/Es490/src/sample.jl:117
 [26] sample(rng::Random.TaskLocalRNG, model::DynamicPPL.Model{…}, sampler::DynamicPPL.Sampler{…}, N::Int64; chain_type::Type, resume_from::Nothing, initial_state::Nothing, progress::Bool, nadapts::Int64, discard_adapt::Bool, discard_initial::Int64, kwargs::@Kwargs{})
    @ Turing.Inference ~/.julia/packages/Turing/tdsVz/src/mcmc/hmc.jl:117
 [27] sample
    @ Turing.Inference ~/.julia/packages/Turing/tdsVz/src/mcmc/hmc.jl:86 [inlined]
 [28] #sample#2
    @ Turing.Inference ~/.julia/packages/Turing/tdsVz/src/mcmc/Inference.jl:190 [inlined]
 [29] sample
    @ Turing.Inference ~/.julia/packages/Turing/tdsVz/src/mcmc/Inference.jl:183 [inlined]
 [30] #sample#1
    @ Turing.Inference ~/.julia/packages/Turing/tdsVz/src/mcmc/Inference.jl:180 [inlined]
 [31] sample(model::DynamicPPL.Model{…}, alg::NUTS{…}, N::Int64)
    @ Turing.Inference ~/.julia/packages/Turing/tdsVz/src/mcmc/Inference.jl:174
 [32] top-level scope
    @ REPL[11]:1
Some type information was truncated. Use `show(err)` to see complete types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions