Closed
Description
I would like to use DiscreteNonParametric
to wrap existing arrays of symbols and probabilities in a type-safe manner, e.g., to pass it to a function taking ::DiscreteUnivariateDistribution
. This does not compile, though. The code
using Distributions
a = [1, 2, 3]
p = [0.5, 0.5]
DiscreteNonParametric((@view a[2:end]), p)
yields
ERROR: MethodError: Cannot `convert` an object of type Vector{Int64} to an object of type SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}
The function `convert` exists, but no method is defined for this combination of argument types.
Closest candidates are:
SubArray{T, N, P, I, L}(::Any, ::Any, ::Any, ::Any) where {T, N, P, I, L}
@ Base subarray.jl:19
convert(::Type{T}, ::T) where T
@ Base Base.jl:126
convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray
@ LinearAlgebra ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/factorization.jl:104
...
Stacktrace:
[1] DiscreteNonParametric{Int64, Float64, SubArray{Int64, 1, Vector{…}, Tuple{…}, true}, Vector{Float64}}(xs::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{…}}, true}, ps::Vector{Float64}; check_args::Bool)
@ Distributions ~/.julia/packages/Distributions/cWeit/src/univariate/discrete/discretenonparametric.jl:34
[2] DiscreteNonParametric
@ ~/.julia/packages/Distributions/cWeit/src/univariate/discrete/discretenonparametric.jl:24 [inlined]
[3] DiscreteNonParametric(vs::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, ps::Vector{Float64})
@ Distributions ~/.julia/packages/Distributions/cWeit/src/univariate/discrete/discretenonparametric.jl:38
[4] top-level scope
@ REPL[4]:1
Some type information was truncated. Use `show(err)` to see complete types.
If the probability distribution is stored and processed, I can easily use DiscreteNonParametric(a[2:end], p)
. However, the extra allocation is inefficient if the type is only used as a wrapper.
Metadata
Metadata
Assignees
Labels
No labels