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

Handle Real and Any better #96

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/multivariate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ end
MvNormal(d::Int, σ::TrackedReal{<:Real}) = TuringMvNormal(d, σ)

TuringMvNormal(d::Int, σ::Real) = TuringMvNormal(zeros(d), σ)
TuringMvNormal(m::AbstractVector{<:Real}, σ::Real) = TuringScalMvNormal(m, σ)
TuringMvNormal(m::AbstractVector, σ::Real) = TuringScalMvNormal(m, σ)
TuringMvNormal(σ::AbstractVector) = TuringMvNormal(zeros(length(σ)), σ)
TuringMvNormal(A::AbstractMatrix) = TuringMvNormal(zeros(size(A, 1)), A)
function TuringMvNormal(m::AbstractVector{<:Real}, σ::AbstractVector{<:Real})
Expand Down
4 changes: 2 additions & 2 deletions src/mvcategorical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function Base.rand(rng::AbstractRNG, d::MvDiscreteNonParametric{T,P}) where {T,P
p = probs(d)
n, k = size(p)
map(1:k) do j
draw = rand(rng, P)
draw = rand(rng, (P === Real ? Float64 : P))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a quite crude heuristic. Ideally we would want P to be

mapreduce(promote_type, 1:n) do i
    typeof(p[i, j])
end

assuming that all provided probabilities can be promoted to a common non-abstract type in each column (but possibly not the same type for all columns) - I guess that should usually be satisfied?

BTW copying the logic from Distributions.DiscreteNonParametric here unfortunately introduces also the bugs present in the implementation in Distributions (see e.g. JuliaStats/Distributions.jl#1128), which might not be fixed when the upstream bugs are fixed if people don't keep track of Distributions closely.

cp = zero(P)
i = 0
while cp < draw && i < n
Expand All @@ -87,7 +87,7 @@ function Base.rand(rng::AbstractRNG, d::MvDiscreteNonParametric{T,P}) where {T,P
end
end

Base.rand(d::MvDiscreteNonParametric) = rand(GLOBAL_RNG, d)
Base.rand(d::MvDiscreteNonParametric) = rand(Random.GLOBAL_RNG, d)

# Override the method in testutils.jl since it assumes
# an evenly-spaced integer support
Expand Down