Skip to content

Unexpected broadcasting behavior of pdf function #46

@OkonSamuel

Description

@OkonSamuel

@bkamins recently pointed out attention to an unexpected behavior that occurs when broadcasting with the pdf function. This issue tracks it.

julia> using CategoricalDistributions

julia> import CategoricalDistributions: classes

julia> u = UnivariateFinite(['x', 'z'], rand(2, 3, 2), pool=missing, ordered=true)
2×3 UnivariateFiniteArray{OrderedFactor{2}, Char, UInt8, Float64, 2}:
 UnivariateFinite{OrderedFactor{2}}(x=>0.215, z=>0.742)    UnivariateFinite{OrderedFactor{2}}(x=>0.903, z=>0.735)
 UnivariateFinite{OrderedFactor{2}}(x=>0.575, z=>0.907)     UnivariateFinite{OrderedFactor{2}}(x=>0.911, z=>0.904)

julia> l = u[1:2, 1:1]
2×1 UnivariateFiniteArray{OrderedFactor{2}, Char, UInt8, Float64, 2}:
 UnivariateFinite{OrderedFactor{2}}(x=>0.215, z=>0.742)
 UnivariateFinite{OrderedFactor{2}}(x=>0.575, z=>0.907)

julia> c = permutedims(classes(l))
1×2 CategoricalArrays.CategoricalArray{Char,2,UInt8}:
 'x'  'z'

julia> pdf.(l, c) # Expected a (2,2) matrix
2×1 Matrix{Float64}:
 0.21520308715626935
 0.9066550037643591

As shown in the above code block, the output of pdf(l,c) was expected 2 x 2 matrix, but instead gave a 2 x 1 matrix.
The relevant section of our codebase is https://github.com/JuliaAI/CategoricalDistributions.jl/blob/dev/src/arrays.jl#L184-L204.

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis doesn't seem right

    Type

    No type

    Projects

    Status

    priority high / straightforward

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions