Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Categorical cannot take abstract array p #1084

Open
cossio opened this issue Feb 27, 2020 · 3 comments · May be fixed by #1908
Open

Categorical cannot take abstract array p #1084

cossio opened this issue Feb 27, 2020 · 3 comments · May be fixed by #1908

Comments

@cossio
Copy link
Contributor

cossio commented Feb 27, 2020

For example:

P = abs.(randn(5,4,2));
p = view(P,:,1,1);
p ./=sum(p);

Then:

julia> Categorical(p)
ERROR: MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type SubArray{Float64,1,Array{Float64,3},Tuple{Base.Slice{Base.OneTo{Int64}},Int64,Int64},true}
Closest candidates are:
  convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:14
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/LinearAlgebra/src/factorization.jl:53
  convert(::Type{T}, ::T) where T at essentials.jl:168
  ...
Stacktrace:
 [1] (::Distributions.var"#_#23#24")(::Bool, ::Type{DiscreteNonParametric{Int64,Float64,Base.OneTo{Int64},SubArray{Float64,1,Array{Float64,3},Tuple{Base.Slice{Base.OneTo{Int64}},Int64,Int64},true}}}, ::Base.OneTo{Int64}, ::SubArray{Float64,1,Array{Float64,3},Tuple{Base.Slice{Base.OneTo{Int64}},Int64,Int64},true}) at /home/cossio/.julia/packages/Distributions/kPXE9/src/univariate/discrete/discretenonparametric.jl:31
 [2] Type at ./none:0 [inlined]
 [3] #_#26 at /home/cossio/.julia/packages/Distributions/kPXE9/src/univariate/discrete/categorical.jl:31 [inlined]
 [4] Type at ./none:0 [inlined]
 [5] #Categorical#27 at /home/cossio/.julia/packages/Distributions/kPXE9/src/univariate/discrete/categorical.jl:34 [inlined]
 [6] DiscreteNonParametric{Int64,P,Base.OneTo{Int64},Ps} where Ps where P(::SubArray{Float64,1,Array{Float64,3},Tuple{Base.Slice{Base.OneTo{Int64}},Int64,Int64},true}) at /home/cossio/.julia/packages/Distributions/kPXE9/src/univariate/discrete/categorical.jl:34
 [7] top-level scope at REPL[16]:1

Cannot Categorical(p) admit generic AbstractVector as argument p?

@findmyway
Copy link
Contributor

You can bypass the check with Categorical(p;check_args=false)

@baedan
Copy link

baedan commented Jul 19, 2022

+1 to this. the error is hard to make sense of also

@vandenman
Copy link

Just got bitten by this... The issue is that in the constructor for DiscreteNonParametric there is

sort_order = sortperm(xs)
new{T,P,Ts,Ps}(xs[sort_order], ps[sort_order])

The return type of ps[sort_order] is not a view/ subarray but rather a vector. However, the type parameter Ps is inferred from the originally passed ps, which leads to the convert error.

I don't think it's a good idea to make Ps depend on the result of ps[sort_order], because then the constructor becomes type unstable (the branch that does not check arguments would return a different type for views).

IMO a better approach here is to ensure that the constructor for Categorical never does sort_order = sortperm(xs) because

const Categorical{P<:Real,Ps<:AbstractVector{P}} = DiscreteNonParametric{Int,P,Base.OneTo{Int},Ps}

implies that xs is always sorted. So perhaps it makes sense to add a specialized constructor for DiscreteNonParametric for T<:Integer and Ts<:Base.OneTo{<:Integer} that skips the sorting step. Perhaps something along the lines of

 function DiscreteNonParametric{T,P,Ts,Ps}(xs::Ts, ps::Ps; check_args::Bool=true) where {
            T<:Integer,P<:Real,Ts<:Base.OneTo{T},Ps<:AbstractVector{P}}
        check_args || return DiscreteNonParametric{T,P,Ts,Ps}(xs, ps, check_args = false)
        @check_args(
            DiscreteNonParametric,
            (length(xs) == length(ps), "length of support and probability vector must be equal"),
            (ps, isprobvec(ps), "vector is not a probability vector")
        )
        return DiscreteNonParametric{T,P,Ts,Ps}(xs, ps, check_args = false)
    end

I'd be happy to open a PR!

@devmotion devmotion linked a pull request Oct 2, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants